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); }
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); } }
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(); }
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(); } }