src: Add at most N bytes as padding if --padding option is used

This commit is contained in:
Tatsuhiro Tsujikawa 2014-02-15 16:40:32 +09:00
parent 3f3f258cd6
commit 7504d89f9b
6 changed files with 17 additions and 37 deletions

View File

@ -66,7 +66,7 @@ const std::string NGHTTPD_SERVER = "nghttpd nghttp2/" NGHTTP2_VERSION;
Config::Config()
: data_ptr(nullptr),
output_upper_thres(1024*1024),
padding_boundary(0),
padding(0),
header_table_size(-1),
port(0),
verbose(false),
@ -931,14 +931,7 @@ ssize_t select_padding_callback
void *user_data)
{
auto hd = static_cast<Http2Handler*>(user_data);
auto bd = hd->get_config()->padding_boundary;
if(bd == 0) {
return frame->hd.length;
}
if(frame->hd.length == 0) {
return std::min(max_payload, bd);
}
return std::min(max_payload, (frame->hd.length + bd - 1) / bd * bd);
return std::min(max_payload, frame->hd.length + hd->get_config()->padding);
}
} // namespace
@ -988,7 +981,7 @@ void fill_callback(nghttp2_session_callbacks& callbacks, const Config *config)
callbacks.on_data_chunk_recv_callback = on_data_chunk_recv_callback;
callbacks.on_header_callback = on_header_callback;
callbacks.on_begin_headers_callback = on_begin_headers_callback;
if(config->padding_boundary) {
if(config->padding) {
callbacks.select_padding_callback = select_padding_callback;
}
}

View File

@ -56,7 +56,7 @@ struct Config {
std::string cert_file;
void *data_ptr;
size_t output_upper_thres;
size_t padding_boundary;
size_t padding;
ssize_t header_table_size;
uint16_t port;
bool verbose;

View File

@ -82,7 +82,7 @@ struct Config {
std::string keyfile;
std::string datafile;
size_t output_upper_thres;
size_t padding_boundary;
size_t padding;
ssize_t peer_max_concurrent_streams;
ssize_t header_table_size;
int32_t pri;
@ -100,7 +100,7 @@ struct Config {
bool continuation;
Config()
: output_upper_thres(1024*1024),
padding_boundary(0),
padding(0),
peer_max_concurrent_streams(NGHTTP2_INITIAL_MAX_CONCURRENT_STREAMS),
header_table_size(-1),
pri(NGHTTP2_PRI_DEFAULT),
@ -1131,14 +1131,7 @@ ssize_t select_padding_callback
(nghttp2_session *session, const nghttp2_frame *frame, size_t max_payload,
void *user_data)
{
auto bd = config.padding_boundary;
if(bd == 0) {
return frame->hd.length;
}
if(frame->hd.length == 0) {
return std::min(max_payload, bd);
}
return std::min(max_payload, (frame->hd.length + bd - 1) / bd * bd);
return std::min(max_payload, frame->hd.length + config.padding);
}
} // namespace
@ -1598,7 +1591,7 @@ int run(char **uris, int n)
}
callbacks.on_data_chunk_recv_callback = on_data_chunk_recv_callback;
callbacks.on_header_callback = on_header_callback;
if(config.padding_boundary) {
if(config.padding) {
callbacks.select_padding_callback = select_padding_callback;
}
@ -1723,9 +1716,8 @@ void print_help(std::ostream& out)
<< " is large enough as it is seen as unlimited.\n"
<< " -c, --header-table-size=<N>\n"
<< " Specify decoder header table size.\n"
<< " -b, --padding=<BOUNDARY>\n"
<< " Padding boundary for frame payload. Specify\n"
<< " 0 to disable padding.\n"
<< " -b, --padding=<N> Add at most <N> bytes to a frame payload as\n"
<< " padding. Specify 0 to disable padding.\n"
<< " --color Force colored log output.\n"
<< " --continuation Send large header to test CONTINUATION.\n"
<< std::endl;
@ -1780,7 +1772,7 @@ int main(int argc, char **argv)
print_help(std::cout);
exit(EXIT_SUCCESS);
case 'b':
config.padding_boundary = strtol(optarg, nullptr, 10);
config.padding = strtol(optarg, nullptr, 10);
break;
case 'n':
config.null_out = true;

View File

@ -115,9 +115,8 @@ void print_help(std::ostream& out)
<< " -p/=/foo.png -p/doc=/bar.css\n"
<< " PATH and PUSH_PATHs are relative to document\n"
<< " root. See --htdocs option.\n"
<< " -b, --padding=<BOUNDARY>\n"
<< " Padding boundary for frame payload. Specify\n"
<< " 0 to disable padding.\n"
<< " -b, --padding=<N> Add at most <N> bytes to a frame payload as\n"
<< " padding. Specify 0 to disable padding.\n"
<< " -h, --help Print this help.\n"
<< std::endl;
}
@ -156,7 +155,7 @@ int main(int argc, char **argv)
config.verify_client = true;
break;
case 'b':
config.padding_boundary = strtol(optarg, nullptr, 10);
config.padding = strtol(optarg, nullptr, 10);
break;
case 'd':
config.htdocs = optarg;

View File

@ -676,8 +676,8 @@ void print_help(std::ostream& out)
<< " --backend-no-tls Disable SSL/TLS on backend connections.\n"
<< " --http2-no-cookie-crumbling\n"
<< " Don't crumble cookie header field.\n"
<< " --padding=<BOUNDARY>\n"
<< " Padding boundary for HTTP/2 frame payload.\n"
<< " --padding=<N> Add at most <N> bytes to a HTTP/2 frame payload\n"
<< " as padding.\n"
<< " Specify 0 to disable padding. This option is\n"
<< " meant for debugging purpose and not intended\n"
<< " to enhance protocol security.\n"

View File

@ -98,11 +98,7 @@ ssize_t select_padding_callback
(nghttp2_session *session, const nghttp2_frame *frame, size_t max_payload,
void *user_data)
{
auto bd = get_config()->padding;
if(frame->hd.length == 0) {
return std::min(max_payload, bd);
}
return std::min(max_payload, (frame->hd.length + bd - 1) / bd * bd);
return std::min(max_payload, frame->hd.length + get_config()->padding);
}
} // namespace http