Exemple #1
0
    // 处理 text/plain 数据体
    bool do_plain(http_response& res, const char* req_charset, string& err)
    {
        string body;
        if (res.get_body(body, local_charset_) == false)
        {
            err += "get_body error";
            return false;
        }
        printf("body:\r\n(%s)\r\n", body.c_str());

        // 设置响应头字段
        http_header& header = res.response_header();
        string ctype("text/plain");
        if (req_charset)
        {
            ctype << "; charset=" << req_charset;
            header.set_content_type(ctype);
        }
        else
            header.set_content_type(ctype);

        printf(">>ctype: %s\r\n", ctype.c_str());

        // 发送响应体数据
        return response_body(res, body, req_charset);
    }
Exemple #2
0
void handler(http_request const& req, http_response& res)
{
    res.setStatus(Poco::Net::HTTPResponse::HTTP_OK);
    res.add("Content-Length", "14");
    res.add("Content-Type", "text/plain");

    res.stream() << "hello, world!\n";
}
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();
}
Exemple #4
0
        static void send(http_response & response, const unsigned int httpCode, const std::string & caption, const std::string & description)
        {
            std::ostringstream pageContent;

            pageContent << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" \
                "<html xmlns=\"http://www.w3.org/1999/xhtml\">" \
                "<head>" \
                "  <title></title>" \
                "  <style>" \
                "  *{margin:0;padding:0;color: #424242;}" \
                "  .errblock {padding: 25px; overflow: hidden;}" \
                "  .errlogo {width: 45px; height: 55px;line-height:0px;border: 1px solid #424242; float: left;}" \
                "  .errmsg {margin-left: 80px;margin:top: 20px; min-height: 30px;}" \
                "  .wpix{background-color: white;width: 5px;height: 5px; display:inline-block;}" \
                "  .bpix{background-color: #424242;width: 5px;height: 5px; display:inline-block;}" \
                "  .productmsg {padding:10px;}" \
                "  </style>" \
                "</head>" \
                "<body>" \
                "<div class='errblock'>" \
                "  <div class='errlogo'>" \
                "    <p class='bpix'></p><p class='bpix'></p><p class='bpix'></p><p class='bpix'></p><p class='bpix'></p><p class='bpix'></p><p class='bpix'></p><p class='wpix'></p><p class='wpix'></p>" \
                "    <p class='bpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='bpix'></p><p class='wpix'></p><p class='bpix'></p><p class='wpix'></p>" \
                "    <p class='bpix'></p><p class='wpix'></p><p class='bpix'></p><p class='wpix'></p><p class='wpix'></p><p class='bpix'></p><p class='wpix'></p><p class='wpix'></p><p class='bpix'></p>" \
                "    <p class='bpix'></p><p class='wpix'></p><p class='bpix'></p><p class='wpix'></p><p class='wpix'></p><p class='bpix'></p><p class='bpix'></p><p class='bpix'></p><p class='bpix'></p>" \
                "    <p class='bpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='bpix'></p>" \
                "    <p class='bpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='bpix'></p>" \
                "    <p class='bpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='bpix'></p>" \
                "    <p class='bpix'></p><p class='wpix'></p><p class='wpix'></p><p class='bpix'></p><p class='bpix'></p><p class='bpix'></p><p class='wpix'></p><p class='wpix'></p><p class='bpix'></p>" \
                "    <p class='bpix'></p><p class='wpix'></p><p class='bpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='bpix'></p><p class='wpix'></p><p class='bpix'></p>" \
                "    <p class='bpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='wpix'></p><p class='bpix'></p>" \
                "    <p class='bpix'></p><p class='bpix'></p><p class='bpix'></p><p class='bpix'></p><p class='bpix'></p><p class='bpix'></p><p class='bpix'></p><p class='bpix'></p><p class='bpix'></p>" \
                "  </div>" \
                "  <div class='errmsg'>";
            pageContent << "    <h2>" << httpCode << " " << caption << "</h2>";
            pageContent << "    <h4>" + description + "</h4>";
            pageContent << "  </div>" \
                "</div>" \
                "<hr width='300px;' />" \
                "<div class='productmsg'>" \
                "<h5>WebMVC++ Open Source Web Application Engine</h5>" \
                "<h5>Version: " << WEBMVCPP_MAJOR_VERSION << "." << WEBMVCPP_MINOR_VERSION << "." << WEBMVCPP_BUILD_NUMBER << "</h5>" \
                "</div>" \
                "</body>" \
                "</html>";

            std::ostringstream httpStatus;
            httpStatus << httpCode << " " << caption;
            response.status = httpStatus.str();

            response.contentType = "text/html";
            response.send_header();
            response.send_content(pageContent.str());
            response.end();
        }
// 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();
    }
}
Exemple #7
0
std::string HttpClient::readHtmlResponseBody(http_response response) {
  if (response.status_code() == status_codes::OK && contentTypeContains(response, "text/html")) {
    return readResponseBody(response);
  } else {
    return "";
  }
}
Exemple #8
0
    // 处理 text/json 数据
    bool do_json(http_response& res, const char* req_charset, string& err)
    {
        json body;
        if (res.get_body(body, local_charset_) == false)
        {
            err += "get_body error";
            return false;
        }

        json_node* node = body.first_node();
        while (node)
        {
            if (node->tag_name())
            {
                printf("tag: %s", node->tag_name());
                if (node->get_text())
                    printf(", value: %s\r\n", node->get_text());
                else
                    printf("\r\n");
            }
            node = body.next_node();
        }

        // 设置响应头字段
        http_header& header = res.response_header();
        string ctype("text/json");
        if (req_charset)
        {
            ctype << "; charset=" << req_charset;
            header.set_content_type(ctype);
        }
        else
            header.set_content_type(ctype);

        printf(">>ctype: %s\r\n", ctype.c_str());

        // 构建响应体数据,并发送响应体数据
        string buf;
        body.build_json(buf);

        // 发送响应体数据
        return response_body(res, buf, req_charset);
    }
Exemple #9
0
static bool handle_one(http_response& response, bool output)
{
	tutorial::AddressBook addr_req;
	if (response.read_request(&addr_req) == false)
		return false;

	tutorial::AddressBook addr_res;

	for (int i = 0; i < addr_req.person_size(); i++)
	{
		const tutorial::Person& person = addr_req.person(i);
		if (output)
		{
			printf("person->name: %s\r\n", person.name().c_str());
			printf("person->id: %d\r\n", person.id());
			printf("person->email: %s\r\n", person.has_email() ?
				person.email().c_str() : "null");
		}

		tutorial::Person* person2 = addr_res.add_person();
		person2->set_name(person.name().c_str());
		person2->set_email(person.has_email() ?
			person.email().c_str() : "null");
		person2->set_id(i);

		// 列出该用户的所有电话
		for (int j = 0; j < person.phone_size(); j++)
		{
			const tutorial::Person::PhoneNumber& phone = person.phone(j);
			if (output)
				printf("\tphone number: %s\r\n", phone.number().c_str());

			tutorial::Person::PhoneNumber* phone2 = person2->add_phone();
			phone2->set_number(phone.number().c_str());
		}
		if (output)
			printf("------------------------------------------\r\n");
	}

	return response.send_response(addr_res);
}
Exemple #10
0
    // 处理 text/xml 数据体
    bool do_xml(http_response& res, const char* req_charset, string& err)
    {
        xml body;
        if (res.get_body(body, local_charset_) == false)
        {
            err += "get_body error";
            return false;
        }
        xml_node* node = body.first_node();
        while (node)
        {
            const char* tag = node->tag_name();
            const char* name = node->attr_value("name");
            const char* pass = node->attr_value("pass");
            printf(">>tag: %s, name: %s, pass: %s\r\n",
                   tag ? tag : "null",
                   name ? name : "null",
                   pass ? pass : "******");
            node = body.next_node();
        }

        // 设置响应头字段
        http_header& header = res.response_header();
        string ctype("text/xml");
        if (req_charset)
        {
            ctype << "; charset=" << req_charset;
            header.set_content_type(ctype);
        }
        else
            header.set_content_type(ctype);

        printf(">>ctype: %s\r\n", ctype.c_str());

        // 构建响应体数据,并发送响应体数据
        string buf;
        body.build_xml(buf);

        // 发送响应体数据
        return response_body(res, buf, req_charset);
    }
Exemple #11
0
    bool response_body(http_response& res, const string& body,
                       const char* req_charset)
    {
        // 如果本地字符集与请求字符集相同,则直接发送
        if (local_charset_ == req_charset)
            return res.response(body.c_str(), body.length());

        // 不一致时,则需要进行转码
        string buf;
        charset_conv conv;
        if (conv.convert(local_charset_, req_charset,
                         body.c_str(), body.length(), &buf) == false)
        {
            logger_error("charset convert from %s to %s error",
                         local_charset_.c_str(), req_charset);
            return false;
        }

        printf(">>response\r\n(%s)\r\n", buf.c_str());
        return res.response(buf.c_str(), buf.length());
    }
// 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;
}
void apache_application::send_response(local::apache_helper *apache,
  request_rec *apache_req, const http_response &response) const {
	// set all headers
	http_response::http_headers::const_iterator i = response.all_headers().begin();
	while (i != response.all_headers().end()) {
		if ((i->first != string("Content-Length")) &&
		    (i->first != string("Content-Type")))
			apr_table_set(apache_req->headers_out, i->first.c_str(), i->second.c_str());
		++i;
	}
	const http_cookies &c = response.get_cookies();
	for (http_cookies::const_iterator i = c.begin(); i != c.end(); ++i) {
		apr_table_set(apache_req->headers_out, "Set-Cookie", i->str().c_str());
	}
	// set other response variables
	apache_req->status = response.get_status_code();
	if (!response.get_content_type().empty())
		apache->set_content_type(apr_pstrdup(apache_req->pool,
		  response.get_content_type().c_str()), apache_req);
	// set response content
	if (response.get_content_size() > 0)
		apache->output(response.get_content_size(), response.get_content(),
		  apache_req);
}
Exemple #14
0
bool HttpClient::contentTypeContains(const http_response &response, std::string pattern) {
  utility::string_t contentType = response.headers().content_type();
  std::string convertedContentType = std::string(contentType.begin(), contentType.end());
  return convertedContentType.find(pattern) != std::string::npos;
}
Exemple #15
0
std::string HttpClient::readResponseBody(http_response response) {
  container_buffer<std::string> stringStream;
  response.body().read_to_end(stringStream).wait();
  return stringStream.collection();
}