h2load: Override user-agent with -H option

This commit allows user to override user-agent with -H option.  We
also enabled custom header support for http/1.1 requests.  We also did
minor optimizations (std::vector::reserve).
This commit is contained in:
Tatsuhiro Tsujikawa 2015-11-18 22:25:02 +09:00
parent 79ee999f1b
commit 8c94341c18
1 changed files with 35 additions and 9 deletions

View File

@ -1893,8 +1893,8 @@ int main(int argc, char **argv) {
shared_nva.emplace_back("user-agent", user_agent); shared_nva.emplace_back("user-agent", user_agent);
// list overridalbe headers // list overridalbe headers
auto override_hdrs = auto override_hdrs = make_array<std::string>(":authority", ":host", ":method",
make_array<std::string>(":authority", ":host", ":method", ":scheme"); ":scheme", "user-agent");
for (auto &kv : config.custom_headers) { for (auto &kv : config.custom_headers) {
if (std::find(std::begin(override_hdrs), std::end(override_hdrs), if (std::find(std::begin(override_hdrs), std::end(override_hdrs),
@ -1912,20 +1912,44 @@ int main(int argc, char **argv) {
} }
} }
auto method_it =
std::find_if(std::begin(shared_nva), std::end(shared_nva),
[](const Header &nv) { return nv.name == ":method"; });
assert(method_it != std::end(shared_nva));
config.h1reqs.reserve(reqlines.size());
config.nva.reserve(reqlines.size());
config.nv.reserve(reqlines.size());
for (auto &req : reqlines) { for (auto &req : reqlines) {
// For HTTP/1.1 // For HTTP/1.1
std::string h1req; auto h1req = (*method_it).value;
h1req = config.data_fd == -1 ? "GET" : "POST"; h1req += " ";
h1req += " " + req; h1req += req;
h1req += " HTTP/1.1\r\n"; h1req += " HTTP/1.1\r\n";
h1req += "Host: " + config.host + "\r\n"; for (auto &nv : shared_nva) {
h1req += "User-Agent: " + user_agent + "\r\n"; if (nv.name == ":authority") {
h1req += "Accept: */*\r\n"; h1req += "Host: ";
h1req += nv.value;
h1req += "\r\n";
continue;
}
if (nv.name[0] == ':') {
continue;
}
h1req += nv.name;
h1req += ": ";
h1req += nv.value;
h1req += "\r\n";
}
h1req += "\r\n"; h1req += "\r\n";
config.h1reqs.push_back(h1req);
config.h1reqs.push_back(std::move(h1req));
// For nghttp2 // For nghttp2
std::vector<nghttp2_nv> nva; std::vector<nghttp2_nv> nva;
// 1 for :path
nva.reserve(1 + shared_nva.size());
nva.push_back(http2::make_nv_ls(":path", req)); nva.push_back(http2::make_nv_ls(":path", req));
@ -1937,6 +1961,8 @@ int main(int argc, char **argv) {
// For spdylay // For spdylay
std::vector<const char *> cva; std::vector<const char *> cva;
// 2 for :path and :version, 1 for terminal nullptr
cva.reserve(2 * (2 + shared_nva.size()) + 1);
cva.push_back(":path"); cva.push_back(":path");
cva.push_back(req.c_str()); cva.push_back(req.c_str());