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() Config::Config()
: data_ptr(nullptr), : data_ptr(nullptr),
output_upper_thres(1024*1024), output_upper_thres(1024*1024),
padding_boundary(0), padding(0),
header_table_size(-1), header_table_size(-1),
port(0), port(0),
verbose(false), verbose(false),
@ -931,14 +931,7 @@ ssize_t select_padding_callback
void *user_data) void *user_data)
{ {
auto hd = static_cast<Http2Handler*>(user_data); auto hd = static_cast<Http2Handler*>(user_data);
auto bd = hd->get_config()->padding_boundary; return std::min(max_payload, frame->hd.length + hd->get_config()->padding);
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);
} }
} // namespace } // 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_data_chunk_recv_callback = on_data_chunk_recv_callback;
callbacks.on_header_callback = on_header_callback; callbacks.on_header_callback = on_header_callback;
callbacks.on_begin_headers_callback = on_begin_headers_callback; callbacks.on_begin_headers_callback = on_begin_headers_callback;
if(config->padding_boundary) { if(config->padding) {
callbacks.select_padding_callback = select_padding_callback; callbacks.select_padding_callback = select_padding_callback;
} }
} }

View File

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

View File

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

View File

@ -115,9 +115,8 @@ void print_help(std::ostream& out)
<< " -p/=/foo.png -p/doc=/bar.css\n" << " -p/=/foo.png -p/doc=/bar.css\n"
<< " PATH and PUSH_PATHs are relative to document\n" << " PATH and PUSH_PATHs are relative to document\n"
<< " root. See --htdocs option.\n" << " root. See --htdocs option.\n"
<< " -b, --padding=<BOUNDARY>\n" << " -b, --padding=<N> Add at most <N> bytes to a frame payload as\n"
<< " Padding boundary for frame payload. Specify\n" << " padding. Specify 0 to disable padding.\n"
<< " 0 to disable padding.\n"
<< " -h, --help Print this help.\n" << " -h, --help Print this help.\n"
<< std::endl; << std::endl;
} }
@ -156,7 +155,7 @@ int main(int argc, char **argv)
config.verify_client = true; config.verify_client = true;
break; break;
case 'b': case 'b':
config.padding_boundary = strtol(optarg, nullptr, 10); config.padding = strtol(optarg, nullptr, 10);
break; break;
case 'd': case 'd':
config.htdocs = optarg; 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" << " --backend-no-tls Disable SSL/TLS on backend connections.\n"
<< " --http2-no-cookie-crumbling\n" << " --http2-no-cookie-crumbling\n"
<< " Don't crumble cookie header field.\n" << " Don't crumble cookie header field.\n"
<< " --padding=<BOUNDARY>\n" << " --padding=<N> Add at most <N> bytes to a HTTP/2 frame payload\n"
<< " Padding boundary for HTTP/2 frame payload.\n" << " as padding.\n"
<< " Specify 0 to disable padding. This option is\n" << " Specify 0 to disable padding. This option is\n"
<< " meant for debugging purpose and not intended\n" << " meant for debugging purpose and not intended\n"
<< " to enhance protocol security.\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, (nghttp2_session *session, const nghttp2_frame *frame, size_t max_payload,
void *user_data) void *user_data)
{ {
auto bd = get_config()->padding; return std::min(max_payload, frame->hd.length + 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);
} }
} // namespace http } // namespace http