nghttp: Record request method to output it in har correctly

This commit is contained in:
Tatsuhiro Tsujikawa 2015-11-07 12:47:26 +09:00
parent 558934cee5
commit eaefce3792
2 changed files with 24 additions and 26 deletions

View File

@ -392,6 +392,11 @@ int submit_request(HttpClient *client, const Headers &headers, Request *req) {
nva.push_back(http2::make_nv(kv.name, kv.value, kv.no_index)); nva.push_back(http2::make_nv(kv.name, kv.value, kv.no_index));
} }
auto method = http2::get_header(build_headers, ":method");
assert(method);
req->method = method->value;
std::string trailer_names; std::string trailer_names;
if (!config.trailer.empty()) { if (!config.trailer.empty()) {
trailer_names = config.trailer[0].name; trailer_names = config.trailer[0].name;
@ -808,6 +813,7 @@ int HttpClient::on_upgrade_connect() {
base64::encode(std::begin(settings_payload), base64::encode(std::begin(settings_payload),
std::begin(settings_payload) + settings_payloadlen); std::begin(settings_payload) + settings_payloadlen);
util::to_token68(token68); util::to_token68(token68);
std::string req; std::string req;
if (reqvec[0]->data_prd) { if (reqvec[0]->data_prd) {
// If the request contains upload data, use OPTIONS * to upgrade // If the request contains upload data, use OPTIONS * to upgrade
@ -819,25 +825,30 @@ int HttpClient::on_upgrade_connect() {
if (meth == std::end(config.headers)) { if (meth == std::end(config.headers)) {
req = "GET "; req = "GET ";
reqvec[0]->method = "GET";
} else { } else {
req = (*meth).value; req = (*meth).value;
req += " "; req += " ";
reqvec[0]->method = (*meth).value;
} }
req += reqvec[0]->make_reqpath(); req += reqvec[0]->make_reqpath();
} }
auto headers = Headers{{"Host", hostport}, auto headers = Headers{{"host", hostport},
{"Connection", "Upgrade, HTTP2-Settings"}, {"connection", "Upgrade, HTTP2-Settings"},
{"Upgrade", NGHTTP2_CLEARTEXT_PROTO_VERSION_ID}, {"upgrade", NGHTTP2_CLEARTEXT_PROTO_VERSION_ID},
{"HTTP2-Settings", token68}, {"http2-settings", token68},
{"Accept", "*/*"}, {"accept", "*/*"},
{"User-Agent", "nghttp2/" NGHTTP2_VERSION}}; {"user-agent", "nghttp2/" NGHTTP2_VERSION}};
auto initial_headerslen = headers.size(); auto initial_headerslen = headers.size();
for (auto &kv : config.headers) { for (auto &kv : config.headers) {
size_t i; size_t i;
if (kv.name.empty() || kv.name[0] == ':') {
continue;
}
for (i = 0; i < initial_headerslen; ++i) { for (i = 0; i < initial_headerslen; ++i) {
if (util::strieq(kv.name, headers[i].name)) { if (kv.name == headers[i].name) {
headers[i].value = kv.value; headers[i].value = kv.value;
break; break;
} }
@ -845,9 +856,7 @@ int HttpClient::on_upgrade_connect() {
if (i < initial_headerslen) { if (i < initial_headerslen) {
continue; continue;
} }
if (kv.name.size() != 0 && kv.name[0] != ':') { headers.emplace_back(kv.name, kv.value, kv.no_index);
headers.emplace_back(kv.name, kv.value, kv.no_index);
}
} }
req += " HTTP/1.1\r\n"; req += " HTTP/1.1\r\n";
@ -867,9 +876,10 @@ int HttpClient::on_upgrade_connect() {
std::cout << " HTTP Upgrade request\n" << req << std::endl; std::cout << " HTTP Upgrade request\n" << req << std::endl;
} }
// record request time if this is GET request
if (!reqvec[0]->data_prd) { if (!reqvec[0]->data_prd) {
// record request time if this is a part of real request.
reqvec[0]->record_request_start_time(); reqvec[0]->record_request_start_time();
reqvec[0]->req_nva = std::move(headers);
} }
on_writefn = &HttpClient::noop; on_writefn = &HttpClient::noop;
@ -990,13 +1000,7 @@ int HttpClient::connection_made() {
} }
// If HEAD is used, that is only when user specified it with -H // If HEAD is used, that is only when user specified it with -H
// option. // option.
auto head_request = auto head_request = stream_user_data && stream_user_data->method == "HEAD";
stream_user_data &&
std::find_if(std::begin(config.headers), std::end(config.headers),
[](const Header &kv) {
return util::streq_l(":method", kv.name) &&
util::streq_l("HEAD", kv.value);
}) != std::end(config.headers);
rv = nghttp2_session_upgrade2(session, settings_payload.data(), rv = nghttp2_session_upgrade2(session, settings_payload.data(),
settings_payloadlen, head_request, settings_payloadlen, head_request,
stream_user_data); stream_user_data);
@ -1397,13 +1401,6 @@ void HttpClient::output_har(FILE *outfile) {
auto request = json_object(); auto request = json_object();
json_object_set_new(entry, "request", request); json_object_set_new(entry, "request", request);
auto method_ptr = http2::get_header(req->req_nva, ":method");
const char *method = "GET";
if (method_ptr) {
method = (*method_ptr).value.c_str();
}
auto req_headers = json_array(); auto req_headers = json_array();
json_object_set_new(request, "headers", req_headers); json_object_set_new(request, "headers", req_headers);
@ -1415,7 +1412,7 @@ void HttpClient::output_har(FILE *outfile) {
json_object_set_new(hd, "value", json_string(nv.value.c_str())); json_object_set_new(hd, "value", json_string(nv.value.c_str()));
} }
json_object_set_new(request, "method", json_string(method)); json_object_set_new(request, "method", json_string(req->method.c_str()));
json_object_set_new(request, "url", json_string(req->uri.c_str())); json_object_set_new(request, "url", json_string(req->uri.c_str()));
json_object_set_new(request, "httpVersion", json_string("HTTP/2.0")); json_object_set_new(request, "httpVersion", json_string("HTTP/2.0"));
json_object_set_new(request, "cookies", json_array()); json_object_set_new(request, "cookies", json_array());

View File

@ -137,6 +137,7 @@ struct Request {
Headers res_nva; Headers res_nva;
Headers req_nva; Headers req_nva;
std::string method;
// URI without fragment // URI without fragment
std::string uri; std::string uri;
http_parser_url u; http_parser_url u;