Exemple #1
0
void handle_list(web::http::http_request& request) {
	std::map<jsonextend, std::string> list;
	json::value result;
	try {

		list = g_mainDB->getAllKV();
	} catch (std::exception const & ex) {
		LOG(error)<<"DB Error "<<ex.what();
		result["status"] = json::value::string("ERROR");
		result["error_msg"] = json::value::string(ex.what());
		request.reply(status_codes::InternalError, result).wait();
		return;
	}
	uint32_t counter = 0;
	json::value tmp;
	result[0] = tmp;
	for (auto iterator = list.begin(); iterator != list.end(); iterator++) {
		tmp["waveProperties"] = iterator->first;
		tmp["command"] = json::value::string(iterator->second);
		result[counter++] = tmp;
	}

	request.reply(status_codes::OK, result).wait();

}
      void CartController::handle_get(web::http::http_request request)
      {

        web::http::http_response response;

        granada::http::session::MapSession simple_session(request,response);
        business::Cart cart(&simple_session);

        auto paths = uri::split_path(uri::decode(request.relative_uri().path()));
        if (!paths.empty()){
          std::string name = utility::conversions::to_utf8string(paths[0]);

          if(name == "count"){

            // returns the number of products added to the cart.
            int count = cart.Count();
            response.set_body("{\"count\":\"" + std::to_string(count) + "\"}");

          }else if (name == "list"){

            // returns a list with the products added to the cart but only
            // if the user is logged in.
            if (simple_session.roles()->Is("USER")){
              response.set_body("{\"data\":" + cart.List() + "}");
            }else{
              response.set_body("{\"data\":[]}");
            }
          }
        }

        response.set_status_code(status_codes::OK);
        response.headers().add(U("Content-Type"), U("text/json; charset=utf-8"));

        request.reply(response);
      }
void HabiticaConnector::doRequest(web::http::http_request request, std::wstring taskID)
{
	// Headers for Habitica authentication
	request.headers().add(U("x-api-user"), API_USER);
	request.headers().add(U("x-api-key"), API_KEY);

	// Make HTTP request as asynchronous task
	pplx::task<web::http::status_code> requestTask = client.request(request).then([=](web::http::http_response response)
	{
		_MESSAGE("Received response status code:%u\n", response.status_code());
		// Log the status code of the finished request
		finishedRequestTasks[taskID] = response.status_code();
        return response.status_code();
	});
}
    void shared_key_authentication_handler::sign_request(web::http::http_request& request, operation_context context) const
    {
        web::http::http_headers& headers = request.headers();
        headers.add(ms_header_date, utility::datetime::utc_now().to_string());

        if (m_credentials.is_shared_key())
        {
            utility::string_t string_to_sign = m_canonicalizer->canonicalize(request, context);
            
            if (core::logger::instance().should_log(context, client_log_level::log_level_verbose))
            {
                utility::string_t with_dots(string_to_sign);
                std::replace(with_dots.begin(), with_dots.end(), _XPLATSTR('\n'), _XPLATSTR('.'));
                core::logger::instance().log(context, client_log_level::log_level_verbose, _XPLATSTR("StringToSign: ") + with_dots);
            }

            utility::string_t header_value;
            header_value.reserve(256);
            header_value.append(m_canonicalizer->authentication_scheme());
            header_value.append(_XPLATSTR(" "));
            header_value.append(m_credentials.account_name());
            header_value.append(_XPLATSTR(":"));
            header_value.append(calculate_hmac_sha256_hash(string_to_sign, m_credentials));

            headers.add(web::http::header_names::authorization, header_value);
        }
    }
pplx::task<web::http::http_response> HttpMessageRequest::ClientHandler(
    web::http::http_request request,
    std::shared_ptr<web::http::http_pipeline_stage> nextStage)
{
    auto iter = request.headers().find(header_names::user_agent);
    if (iter == cend(request.headers()))
    {
        request.headers().add(header_names::user_agent, GetUserAgent());
    }
    else
    {
        iter->second = GetUserAgent();
    }

    return nextStage->propagate(request);
}
Exemple #6
0
void handle_execute(web::http::http_request& request) {

	json::value response;
	concurrency::streams::istream body = request.body();
	uint64_t content_lenght = request.headers().content_length();
	LOG(debug)<<"Content lenght of request "<<content_lenght;
	if (content_lenght == 0) {
		LOG(error)<<"Bad request! Empty body";
		response["error_msg"] = json::value::string("Bad request.Empty body!");
		response["status"] = json::value::string("ERROR");
		request.reply(status_codes::BadRequest, response).wait();
		return;

	}
	uint8_t * waveData = new uint8_t[content_lenght];
	Concurrency::streams::rawptr_buffer<uint8_t> buffer(waveData,
			content_lenght);
	body.read(buffer, content_lenght).get();
	WaveFile wave(waveData, content_lenght);
	delete[] waveData;
	Samples waveSamples(wave);
	jsonextend wavePropertiesJSON = g_processAndAnlyze.getSummary(waveSamples);

	std::map<jsonextend, std::string> list;
	list = g_mainDB->getAllKV();
	auto tmpFun = g_policies[DEFAULT_POLICY];

	std::string command = tmpFun(list, wavePropertiesJSON);

	if (!command.empty()) {

		web::json::value cmdResult = Runner::run(command, " ");
		response["status"] = json::value::string("OK");
		response["command"] = json::value::string(command);
		response["command_ret"] = cmdResult;
		request.reply(status_codes::OK, response).wait();
		return;
	}

	response["status"] = json::value::string("NOT_FOUND");

	request.reply(status_codes::OK, response).wait();

}
 utility::string_t shared_key_table_canonicalizer::canonicalize(const web::http::http_request& request, operation_context context) const
 {
     canonicalizer_helper helper(request, m_account_name);
     helper.append(request.method());
     helper.append_header(web::http::header_names::content_md5);
     helper.append_header(web::http::header_names::content_type);
     helper.append_date_header(true);
     helper.append_resource(true);
     return helper.str();
 }
      void CartController::handle_put(web::http::http_request request)
      {
        web::http::http_response response;
        granada::http::session::MapSession simple_session(request,response);
        business::Cart cart(&simple_session);

        std::string product_id = "";
        int quantity = 0;
        std::string body = request.extract_utf8string(true).get();
        std::unordered_map<std::string,std::string> parsed_query = granada::http::parser::ParseQueryString(body);

        auto it = parsed_query.find("id");
        if (it != parsed_query.end()){
          product_id.assign(it->second);
        }

        auto it2 = parsed_query.find("quantity");
        if (it2 != parsed_query.end()){
          quantity = std::atoi(it2->second.c_str());
        }

        int count = 0;
        if (product_id.empty()){

          // invalid product id, do not add the product,
          // just return the number of products already added.
          count = cart.Count();

        }else{

          // adds a product to the cart.
          count = cart.Add(product_id,quantity);

        }

        response.set_status_code(status_codes::OK);
        response.headers().add(U("Content-Type"), U("text/json; charset=utf-8"));
        response.set_body("{\"count\":\"" + std::to_string(count) + "\"}");
        request.reply(response);
      }
    void add_metadata(web::http::http_request& request, const cloud_metadata& metadata)
    {
        web::http::http_headers& headers = request.headers();
        for (cloud_metadata::const_iterator it = metadata.cbegin(); it != metadata.cend(); ++it)
        {
            if (core::is_empty_or_whitespace(it->second))
            {
                throw std::invalid_argument(protocol::error_empty_metadata_value);
            }

            headers.add(ms_header_metadata_prefix + it->first, it->second);
        }
    }
 utility::string_t shared_key_blob_queue_canonicalizer::canonicalize(const web::http::http_request& request, operation_context context) const
 {
     canonicalizer_helper helper(request, m_account_name);
     helper.append(request.method());
     helper.append_header(web::http::header_names::content_encoding);
     helper.append_header(web::http::header_names::content_language);
     helper.append_header(web::http::header_names::content_length);
     helper.append_header(web::http::header_names::content_md5);
     helper.append_header(web::http::header_names::content_type);
     helper.append_date_header(false);
     helper.append_header(web::http::header_names::if_modified_since);
     helper.append_header(web::http::header_names::if_match);
     helper.append_header(web::http::header_names::if_none_match);
     helper.append_header(web::http::header_names::if_unmodified_since);
     helper.append_header(web::http::header_names::range);
     helper.append_x_ms_headers();
     helper.append_resource(false);
     return helper.str();
 }
 void sas_authentication_handler::sign_request(web::http::http_request& request, operation_context context) const
 {
     web::http::uri request_uri = request.request_uri();
     request_uri = m_credentials.transform_uri(request_uri);
     request.set_request_uri(request_uri);
 }
Exemple #12
0
void handle_add(web::http::http_request& request) {

	json::value response;
	std::map<std::string, std::string> querymap = uri::split_query(
			request.relative_uri().query());
	if (querymap.find("name") == querymap.end()
			|| querymap.find("command") == querymap.end()) {
		LOG(error)<<"Bad request";
		response["error_msg"] = json::value::string("Bad request. Read doc for info. Missing name or command field in request.");
		response["status"] = json::value::string("ERROR");
		request.reply(status_codes::BadRequest, response).wait();
		return;

	}
	std::string commandName = uri::decode(querymap["name"]);
	std::string commandString = uri::decode(querymap["command"]);
	LOG(debug)<<"Add command name "<<commandName<<" command string: "<<commandString;
	concurrency::streams::istream body = request.body();
	uint64_t content_lenght = request.headers().content_length();
	LOG(debug)<<"Content lenght of request "<<content_lenght;
	if (content_lenght == 0) {
		LOG(error)<<"Bad request! Empty body";
		response["error_msg"] = json::value::string("Bad request.Empty body!");
		response["status"] = json::value::string("ERROR");
		request.reply(status_codes::BadRequest, response).wait();
		return;

	}
	try {
		uint8_t * waveData = new uint8_t[content_lenght];
		Concurrency::streams::rawptr_buffer<uint8_t> buffer(waveData,
				content_lenght);
		//body.read_to_end(buffer).get();
		body.read(buffer, content_lenght).get();
		WaveFile wave(waveData, content_lenght);
		delete[] waveData;
		Samples waveSamples(wave);
		jsonextend wavepropertiesJSON = g_processAndAnlyze.getSummary(waveSamples);
		wavepropertiesJSON["name"] =  web::json::value::string(commandName);

		try {
			LOG(debug)<<"Saving "<<wavepropertiesJSON.to_string();
			g_mainDB->put(wavepropertiesJSON, commandString);
		} catch (std::exception const & ex) {
			LOG(error)<<"Error "<<ex.what();
			response["status"] = json::value::string("ERROR");
			response["error_msg"] = json::value::string(ex.what());
			request.reply(status_codes::InternalError, response).wait();
			return;
		}
		response["status"] = json::value::string("OK");
		response["command"] = json::value::string(commandString);
		request.reply(status_codes::OK, response).wait();
	} catch (std::exception &e) {

		LOG(error)<<"Error "<<e.what();
		response["status"] = json::value::string("ERROR");
		response["command"] = json::value::string(e.what());
		request.reply(status_codes::BadRequest, response).wait();
	}

}
Exemple #13
0
void handle_test(web::http::http_request& request) {
	json::value result;
	result["status"] = json::value::string("OK");
	request.reply(status_codes::OK, result).wait();
}