-
Notifications
You must be signed in to change notification settings - Fork 0
Feature HTTP parser #54
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| third_party/** |
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
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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| #include "http/http_parser.hpp" | ||
|
|
||
| #include "util/string.hpp" | ||
|
|
||
| #include <cstring> | ||
|
|
||
| HttpParser::HttpParser() | ||
| : status_(HttpParser::kIncomplete) | ||
| { | ||
| } | ||
|
|
||
| void HttpParser::feed_data(const char* buf, size_t n) | ||
| { | ||
| if (status_ == kError) | ||
| return; | ||
|
|
||
| buffer_.append(buf, n); | ||
|
|
||
| parse_request_line(); | ||
| parse_headers(); | ||
| parse_body(); | ||
| } | ||
|
|
||
| static size_t min3(size_t a, size_t b, size_t c) | ||
| { | ||
| return std::min(std::min(a, b), c); | ||
| } | ||
|
|
||
| size_t HttpParser::slurp_data(char* buf, size_t n) | ||
| { | ||
| if (status_ != kHeadersDone) | ||
| return 0; | ||
|
|
||
| size_t bytes_left = req_.content_length - bytes_slurped_; | ||
| size_t to_copy = min3(buffer_.size(), bytes_left, n); | ||
|
|
||
| std::memcpy(buf, buffer_.data(), to_copy); | ||
| bytes_slurped_ += to_copy; | ||
| buffer_.erase(0, to_copy); | ||
|
|
||
| if (bytes_slurped_ == req_.content_length) | ||
| status_ = kBodyDone; | ||
|
|
||
| return to_copy; | ||
| } | ||
|
|
||
| static bool is_valid_request_line(const std::vector<std::string>& v) | ||
iboukhss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| if (v.size() != 3) | ||
| return false; | ||
|
|
||
| if (v[0] != "GET" && v[0] != "POST" && v[0] != "DELETE") | ||
| return false; | ||
|
|
||
| if (v[2] != "HTTP/1.0" && v[2] != "HTTP/1.1") | ||
| return false; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| // Format: METHOD SP REQUEST-URI SP HTTP-VERSION CRLF | ||
| void HttpParser::parse_request_line() | ||
| { | ||
| if (status_ != kIncomplete) | ||
| return; | ||
|
|
||
| size_t crlf = buffer_.find("\r\n"); | ||
| if (crlf == std::string::npos) | ||
| return; | ||
|
|
||
| std::string line = buffer_.substr(0, crlf); | ||
| std::vector<std::string> v = str_split(line, " "); | ||
|
|
||
| if (!is_valid_request_line(v)) { | ||
| status_ = kError; | ||
| return; | ||
| } | ||
|
|
||
| req_.method = v[0]; | ||
| req_.path = v[1]; | ||
| req_.http_version = v[2]; | ||
|
|
||
| buffer_.erase(0, crlf + 2); | ||
|
|
||
| status_ = kRequestLineDone; | ||
| } | ||
|
|
||
| // Need to parse: content_length, is_chunked?, keep_alive? | ||
| void HttpParser::parse_headers() | ||
| { | ||
| if (status_ != kRequestLineDone) | ||
| return; | ||
|
|
||
| size_t pos = buffer_.find("\r\n\r\n"); | ||
| if (pos == std::string::npos) | ||
| return; | ||
|
|
||
| std::vector<std::string> v = str_split(buffer_, "\r\n"); | ||
|
|
||
| for (size_t i = 0; i < v.size() && !v[i].empty(); i++) { | ||
|
|
||
| size_t colon = v[i].find(':'); | ||
| if (colon == std::string::npos) { | ||
| status_ = kError; | ||
| return; | ||
| } | ||
|
|
||
| std::string name = v[i].substr(0, colon); | ||
| std::string value = str_trim(v[i].substr(colon + 1)); | ||
| req_.headers[name] = value; | ||
| } | ||
|
|
||
| buffer_.erase(0, pos + 4); | ||
| status_ = kHeadersDone; | ||
| } | ||
|
|
||
| void HttpParser::parse_body() | ||
| { | ||
| if (status_ != kHeadersDone) | ||
| return; | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #ifndef HTTP_HTTP_PARSER_HPP_ | ||
| #define HTTP_HTTP_PARSER_HPP_ | ||
|
|
||
| #include "http/http_request.hpp" | ||
|
|
||
| #include <string> | ||
|
|
||
| class HttpParser { | ||
| public: | ||
| HttpParser(); | ||
|
|
||
| enum Status { kError = 0, kIncomplete, kRequestLineDone, kHeadersDone, kBodyDone }; | ||
|
|
||
| void feed_data(const char* buf, size_t n); | ||
| size_t slurp_data(char* buf, size_t n); | ||
|
|
||
| Status status() { return status_; } | ||
| const HttpRequest& request() { return req_; } | ||
|
|
||
| private: | ||
| void parse_request_line(); | ||
| void parse_headers(); | ||
| void parse_body(); | ||
|
|
||
| Status status_; | ||
| std::string buffer_; | ||
| size_t bytes_slurped_; | ||
| HttpRequest req_; | ||
| }; | ||
|
|
||
| #endif // HTTP_HTTP_PARSER_HPP_ |
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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #include "http/http_request.hpp" | ||
|
|
||
| HttpRequest::HttpRequest() | ||
| : content_length(0), | ||
| is_chunked(false), | ||
| keep_alive(false), | ||
| headers_done(false) | ||
| { | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.