Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to implement gzip decompression of request body #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions stream_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ stream_parser::stream_parser(const pcre *url_filter_re, const pcre_extra *url_fi
url_filter_re(url_filter_re),
url_filter_extra(url_filter_extra),
output_path(output_path),
gzip_flag(false),
gzip_response_flag(false),
gzip_request_flag(false),
dump_flag(-1) {
std::memset(&next_seq, 0, sizeof next_seq);
std::memset(&ts_usc, 0, sizeof ts_usc);
Expand Down Expand Up @@ -109,7 +110,11 @@ int stream_parser::on_header_value(http_parser *parser, const char *at, size_t l
stream_parser *self = reinterpret_cast<stream_parser *>(parser->data);
if (parser->type == HTTP_RESPONSE) {
if (self->temp_header_field == "content-encoding" && std::strstr(at, "gzip")) {
self->gzip_flag = true;
self->gzip_response_flag = true;
}
} else if (parser->type == HTTP_REQUEST) {
if (self->temp_header_field == "content-encoding" && std::strstr(at, "gzip")) {
self->gzip_request_flag = true;
}
} else {
if (self->temp_header_field == "host") {
Expand Down Expand Up @@ -170,7 +175,7 @@ bool stream_parser::match_url(const std::string &url) {
void stream_parser::dump_http_request() {
if (dump_flag != 0) return;

if (gzip_flag && !body[HTTP_RESPONSE].empty()) {
if (gzip_response_flag && !body[HTTP_RESPONSE].empty()) {
std::string new_body;
if (gzip_decompress(body[HTTP_RESPONSE], new_body)) {
body[HTTP_RESPONSE].assign(new_body);
Expand Down Expand Up @@ -224,7 +229,8 @@ void stream_parser::dump_http_request() {
body_100_continue.clear();
host.clear();
std::memset(&ts_usc, 0, sizeof ts_usc);
gzip_flag = false;
gzip_response_flag = false;
gzip_request_flag = false;
dump_flag = 1;
}

Expand All @@ -250,7 +256,17 @@ std::ostream &operator<<(std::ostream &out, const stream_parser &parser) {
if (!parser.body_100_continue.empty()) {
out << parser.body_100_continue;
}
if (!is_atty || is_plain_text(parser.body[HTTP_REQUEST])) {
if(parser.gzip_request_flag) {
std::string non_const_request_body;
std::string new_body;
non_const_request_body.assign(parser.body[HTTP_REQUEST]);
if (gzip_decompress(non_const_request_body, new_body)) {
out << new_body;
} else {
out << ANSI_COLOR_RED << "[decompress error]" << ANSI_COLOR_RESET << std::endl;
}

} else if (!is_atty || is_plain_text(parser.body[HTTP_REQUEST])) {
out << parser.body[HTTP_REQUEST];
} else {
out << ANSI_COLOR_RED << "[binary request body] (size:" << parser.body[HTTP_REQUEST].size() << ")"
Expand Down
3 changes: 2 additions & 1 deletion stream_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class stream_parser {
std::string body_100_continue;

std::string temp_header_field;
bool gzip_flag;
bool gzip_response_flag;
bool gzip_request_flag;
int dump_flag;

uint32_t fin_nxtseq[HTTP_BOTH];
Expand Down