forked from glynos/cpp-netlib
-
Notifications
You must be signed in to change notification settings - Fork 426
Here's my attempt at properly handling the content length header #164
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d00836d
Take content length into account when reading body
joelreymont 78afee4
Qualify stoi and only catch exceptions thrown by it.
joelreymont a941fb1
Use optional member variable to keep track of content length.
joelreymont 3079bda
Use size_t to store content length.
joelreymont 4054936
Print a debug message if content length conversion to size_t fails.
joelreymont File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Take content length into account when reading body
- Loading branch information
commit d00836dfe281a59af8dacbb6864fe0bcae2d39a0
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -430,20 +430,53 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c | |
placeholders::bytes_transferred))); | ||
} else { | ||
NETWORK_MESSAGE("no callback provided, appending to body..."); | ||
bool get_more = true; | ||
buffer_type::const_iterator begin = this->part.begin(); | ||
buffer_type::const_iterator end = begin; | ||
std::advance(end, bytes_transferred); | ||
// check the content length header | ||
//auto headers_future = headers_promise.get_future(); | ||
auto it = headers_.find("Content-Length"); | ||
if (it != headers_.end()) { | ||
try { | ||
unsigned content_length = stoi(it->second); | ||
get_more = (end - begin) < content_length; | ||
NETWORK_MESSAGE("Content-Length: " << content_length | ||
<< ", disconnect: " << !get_more); | ||
} catch(...) { | ||
} | ||
} | ||
// Here we don't have a body callback. Let's | ||
// make sure that we deal with the remainder | ||
// from the headers part in case we do have data | ||
// that's still in the buffer. | ||
this->parse_body(request_strand_.wrap( | ||
boost::bind( | ||
&this_type::handle_received_data, | ||
this_type::shared_from_this(), | ||
body, | ||
get_body, | ||
callback, | ||
placeholders::error, | ||
placeholders::bytes_transferred)), | ||
bytes_transferred); | ||
if (get_more) { | ||
this->parse_body(request_strand_.wrap( | ||
boost::bind( | ||
&this_type::handle_received_data, | ||
this_type::shared_from_this(), | ||
body, | ||
get_body, | ||
callback, | ||
placeholders::error, | ||
placeholders::bytes_transferred)), | ||
bytes_transferred); | ||
} else { | ||
std::string body_string; | ||
std::swap(body_string, this->partial_parsed); | ||
body_string.append( | ||
this->part.begin() | ||
, bytes_transferred | ||
); | ||
this->body_promise.set_value(body_string); | ||
// TODO set the destination value somewhere! | ||
this->destination_promise.set_value(""); | ||
this->source_promise.set_value(""); | ||
this->part.assign('\0'); | ||
this->response_parser_.reset(); | ||
//NETWORK_MESSAGE("forcing socket disconnect on content length"); | ||
//connection_delegate_->disconnect(); | ||
} | ||
} | ||
} | ||
return; | ||
|
@@ -709,6 +742,7 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c | |
boost::trim(header_pair.second); | ||
headers.insert(header_pair); | ||
} | ||
this->headers_ = headers; | ||
headers_promise.set_value(headers); | ||
} | ||
|
||
|
@@ -790,6 +824,7 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c | |
boost::promise<boost::uint16_t> status_promise; | ||
boost::promise<std::string> status_message_promise; | ||
boost::promise<std::multimap<std::string, std::string> > headers_promise; | ||
std::multimap<std::string, std::string> headers_; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest doing this another way:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, please review. |
||
boost::promise<std::string> source_promise; | ||
boost::promise<std::string> destination_promise; | ||
boost::promise<std::string> body_promise; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You really don't need to force the disconnection. The connection can die a natural death.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about the block below // TODO? Is it really needed? I don't understand the meaning of source and destination here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The source of the message is technically the server from which the message originated from. I used to populate this (I think) with an IP address of the remote host. The destination I leave blank because technically the destination of the response is the current connection handling the request. In the future I may put something useful there, but in the meantime the destination is kept empty.
The assigning of an empty string is unnecessary, as default-constructed strings usually use small-string optimization and will hold 16 bytes (or some similar amount) of strings. Remove that as you don't really need to do it anyway.