nghttp, nghttpd: Use :authority and host instead of :host

This commit is contained in:
Tatsuhiro Tsujikawa 2013-10-24 23:04:28 +09:00
parent 2c37341a25
commit c4ae19e2a0
2 changed files with 37 additions and 16 deletions

View File

@ -681,7 +681,7 @@ void append_nv(Request *req, nghttp2_nv *nva, size_t nvlen)
namespace { namespace {
const char *REQUIRED_HEADERS[] = { const char *REQUIRED_HEADERS[] = {
":host", ":method", ":path", ":scheme", nullptr ":method", ":path", ":scheme", nullptr
}; };
} // namespace } // namespace
@ -714,6 +714,19 @@ int hd_on_frame_recv_callback
return 0; return 0;
} }
} }
// intermediary translating from HTTP/1 request to HTTP/2 may
// not produce :authority header field. In this case, it should
// provide host HTTP/1.1 header field.
if(!http2::get_unique_header(frame->headers.nva,
frame->headers.nvlen,
":authority") &&
!http2::get_unique_header(frame->headers.nva,
frame->headers.nvlen,
"host")) {
nghttp2_submit_rst_stream(session, stream_id,
NGHTTP2_PROTOCOL_ERROR);
return 0;
}
auto req = util::make_unique<Request>(stream_id); auto req = util::make_unique<Request>(stream_id);
append_nv(req.get(), frame->headers.nva, frame->headers.nvlen); append_nv(req.get(), frame->headers.nva, frame->headers.nvlen);
hd->add_stream(stream_id, std::move(req)); hd->add_stream(stream_id, std::move(req));

View File

@ -391,9 +391,9 @@ struct HttpClient;
} // namespace } // namespace
namespace { namespace {
void submit_request(HttpClient *client, int submit_request(HttpClient *client,
const std::map<std::string, std::string>& headers, const std::map<std::string, std::string>& headers,
Request *req); Request *req);
} // namespace } // namespace
namespace { namespace {
@ -703,7 +703,9 @@ struct HttpClient {
// data // data
for(auto i = std::begin(reqvec)+(need_upgrade() && !reqvec[0]->data_prd); for(auto i = std::begin(reqvec)+(need_upgrade() && !reqvec[0]->data_prd);
i != std::end(reqvec); ++i) { i != std::end(reqvec); ++i) {
submit_request(this, config.headers, (*i).get()); if(submit_request(this, config.headers, (*i).get()) != 0) {
return -1;
}
} }
return on_write(); return on_write();
} }
@ -874,16 +876,16 @@ http_parser_settings htp_hooks = {
} // namespace } // namespace
namespace { namespace {
void submit_request(HttpClient *client, int submit_request(HttpClient *client,
const std::map<std::string, std::string>& headers, const std::map<std::string, std::string>& headers,
Request *req) Request *req)
{ {
enum eStaticHeaderPosition enum eStaticHeaderPosition
{ {
POS_METHOD = 0, POS_METHOD = 0,
POS_PATH, POS_PATH,
POS_SCHEME, POS_SCHEME,
POS_HOST, POS_AUTHORITY,
POS_ACCEPT, POS_ACCEPT,
POS_ACCEPT_ENCODING, POS_ACCEPT_ENCODING,
POS_USERAGENT POS_USERAGENT
@ -894,7 +896,7 @@ void submit_request(HttpClient *client,
":method", req->data_prd ? "POST" : "GET", ":method", req->data_prd ? "POST" : "GET",
":path", path.c_str(), ":path", path.c_str(),
":scheme", scheme.c_str(), ":scheme", scheme.c_str(),
":host", client->hostport.c_str(), ":authority", client->hostport.c_str(),
"accept", "*/*", "accept", "*/*",
"accept-encoding", "gzip, deflate", "accept-encoding", "gzip, deflate",
"user-agent", "nghttp2/" NGHTTP2_VERSION "user-agent", "nghttp2/" NGHTTP2_VERSION
@ -931,8 +933,8 @@ void submit_request(HttpClient *client,
else if ( util::strieq( key, "user-agent" ) ) { else if ( util::strieq( key, "user-agent" ) ) {
nv[POS_USERAGENT*2+1] = value; nv[POS_USERAGENT*2+1] = value;
} }
else if ( util::strieq( key, "host" ) ) { else if ( util::strieq( key, ":authority" ) ) {
nv[POS_HOST*2+1] = value; nv[POS_AUTHORITY*2+1] = value;
} }
else { else {
nv[pos] = key; nv[pos] = key;
@ -943,9 +945,14 @@ void submit_request(HttpClient *client,
} }
nv[pos] = nullptr; nv[pos] = nullptr;
int r = nghttp2_submit_request(client->session, req->pri, int rv = nghttp2_submit_request(client->session, req->pri,
nv.get(), req->data_prd, req); nv.get(), req->data_prd, req);
assert(r == 0); if(rv != 0) {
std::cerr << "nghttp2_submit_request() returned error: "
<< nghttp2_strerror(rv) << std::endl;
return -1;
}
return 0;
} }
} // namespace } // namespace
@ -1661,8 +1668,9 @@ int main(int argc, char **argv)
} }
case 'H': { case 'H': {
char *header = optarg; char *header = optarg;
char *value = strchr( optarg, ':' ); // Skip first possible ':' in the header name
if ( ! value || header == value) { char *value = strchr( optarg + 1, ':' );
if ( ! value || header + 1 == value) {
std::cerr << "-H: invalid header: " << optarg std::cerr << "-H: invalid header: " << optarg
<< std::endl; << std::endl;
exit(EXIT_FAILURE); exit(EXIT_FAILURE);