Commit eaefce37 authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

nghttp: Record request method to output it in har correctly

parent 558934ce
......@@ -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));
}
auto method = http2::get_header(build_headers, ":method");
assert(method);
req->method = method->value;
std::string trailer_names;
if (!config.trailer.empty()) {
trailer_names = config.trailer[0].name;
......@@ -808,6 +813,7 @@ int HttpClient::on_upgrade_connect() {
base64::encode(std::begin(settings_payload),
std::begin(settings_payload) + settings_payloadlen);
util::to_token68(token68);
std::string req;
if (reqvec[0]->data_prd) {
// 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)) {
req = "GET ";
reqvec[0]->method = "GET";
} else {
req = (*meth).value;
req += " ";
reqvec[0]->method = (*meth).value;
}
req += reqvec[0]->make_reqpath();
}
auto headers = Headers{{"Host", hostport},
{"Connection", "Upgrade, HTTP2-Settings"},
{"Upgrade", NGHTTP2_CLEARTEXT_PROTO_VERSION_ID},
{"HTTP2-Settings", token68},
{"Accept", "*/*"},
{"User-Agent", "nghttp2/" NGHTTP2_VERSION}};
auto headers = Headers{{"host", hostport},
{"connection", "Upgrade, HTTP2-Settings"},
{"upgrade", NGHTTP2_CLEARTEXT_PROTO_VERSION_ID},
{"http2-settings", token68},
{"accept", "*/*"},
{"user-agent", "nghttp2/" NGHTTP2_VERSION}};
auto initial_headerslen = headers.size();
for (auto &kv : config.headers) {
size_t i;
if (kv.name.empty() || kv.name[0] == ':') {
continue;
}
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;
break;
}
......@@ -845,9 +856,7 @@ int HttpClient::on_upgrade_connect() {
if (i < initial_headerslen) {
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";
......@@ -867,9 +876,10 @@ int HttpClient::on_upgrade_connect() {
std::cout << " HTTP Upgrade request\n" << req << std::endl;
}
// record request time if this is GET request
if (!reqvec[0]->data_prd) {
// record request time if this is a part of real request.
reqvec[0]->record_request_start_time();
reqvec[0]->req_nva = std::move(headers);
}
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
// option.
auto head_request =
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);
auto head_request = stream_user_data && stream_user_data->method == "HEAD";
rv = nghttp2_session_upgrade2(session, settings_payload.data(),
settings_payloadlen, head_request,
stream_user_data);
......@@ -1397,13 +1401,6 @@ void HttpClient::output_har(FILE *outfile) {
auto request = json_object();
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();
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(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, "httpVersion", json_string("HTTP/2.0"));
json_object_set_new(request, "cookies", json_array());
......
......@@ -137,6 +137,7 @@ struct Request {
Headers res_nva;
Headers req_nva;
std::string method;
// URI without fragment
std::string uri;
http_parser_url u;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment