std::string HttpClient::readHtmlResponseBody(http_response response) { if (response.status_code() == status_codes::OK && contentTypeContains(response, "text/html")) { return readResponseBody(response); } else { return ""; } }
json::value GoogleSearchClient::extractJsonFromResponseBody(http_response response) { if (response.status_code() == web::http::status_codes::OK) { auto extractJsonTask = response.extract_json(); extractJsonTask.wait(); return extractJsonTask.get(); } return json::value(); }
// Actual initiates sending the response. pplx::task<void> details::_http_request::_reply_impl(http_response response) { // If the user didn't explicitly set a reason phrase then we should have it default // if they used one of the standard known status codes. if(response.reason_phrase().empty()) { static http_status_to_phrase idToPhraseMap[] = { #define _PHRASES #define DAT(a,b,c) {status_codes::a, c}, #include "cpprest/details/http_constants.dat" #undef _PHRASES #undef DAT }; for(const auto &iter : idToPhraseMap) { if( iter.id == response.status_code() ) { response.set_reason_phrase(iter.phrase); break; } } } pplx::task<void> response_completed; #if !defined(__cplusplus_winrt) && _WIN32_WINNT >= _WIN32_WINNT_VISTA auto server_api = experimental::details::http_server_api::server_api(); if (m_server_context && server_api) { // Add a task-based continuation so no exceptions thrown from the task go 'unobserved'. response._set_server_context(std::move(m_server_context)); response_completed = experimental::details::http_server_api::server_api()->respond(response); response_completed.then([](pplx::task<void> t) { try { t.wait(); } catch(...) {} }); } else #endif { // There's no server context. The message may be replied to locally, as in a HTTP client // pipeline stage. There's no sending required, so we can simply consider the reply // done and return an already filled-in task. response_completed = pplx::task_from_result(); } m_response.set(response); return response_completed; }
void application_update_controller::on_response(const http_response &response, bool forced) { if (!response.is_success_status()) { LOG(error) << "GitHub returned status " << response.status_code(); return; } picojson::value v; std::string err = picojson::parse(v, response.content()); if (!err.empty()) { LOG(error) << "Error when parsing release JSON: " << err; return; } picojson::object obj = v.get<picojson::object>(); std::string version = obj["tag_name"].get<std::string>(); if (version[0] == 'v') { version = version.substr(1); } semver::version parsedVersion(version); std::wstring wideVersion = to_wstring(version); configuration &cfg = configuration::instance(); if (cfg.ignored_update() == wideVersion && !forced) { // Just return if we have ignored this update. return; } if (parsedVersion > semver::version(version_info::current_version())) { TCHAR title[100]; StringCchPrintf(title, ARRAYSIZE(title), TR("picotorrent_v_available"), to_wstring(version).c_str()); uri uri(to_wstring(obj["html_url"].get<std::string>())); notify(title, uri, wideVersion); } else if (forced) { ui::task_dialog dlg; dlg.set_common_buttons(TDCBF_OK_BUTTON); dlg.set_main_icon(TD_INFORMATION_ICON); dlg.set_parent(wnd_->handle()); dlg.set_main_instruction(TR("no_update_available")); dlg.set_title(L"PicoTorrent"); dlg.show(); } }
// Actual initiates sending the response. pplx::task<void> details::_http_request::_reply_impl(http_response response) { // If the user didn't explicitly set a reason phrase then we should have it default // if they used one of the standard known status codes. if (response.reason_phrase().empty()) { response.set_reason_phrase(get_default_reason_phrase(response.status_code())); } pplx::task<void> response_completed; #if !defined(__cplusplus_winrt) && _WIN32_WINNT >= _WIN32_WINNT_VISTA auto server_api = experimental::details::http_server_api::server_api(); if (m_server_context && server_api) { // Add a task-based continuation so no exceptions thrown from the task go 'unobserved'. response._set_server_context(std::move(m_server_context)); response_completed = server_api->respond(response); response_completed.then([](pplx::task<void> t) { try { t.wait(); } catch(...) {} }); } else #endif { // There's no server context. The message may be replied to locally, as in a HTTP client // pipeline stage. There's no sending required, so we can simply consider the reply // done and return an already filled-in task. response_completed = pplx::task_from_result(); } m_response.set(response); return response_completed; }