Libprocess: Avoided magic values in decoder.hpp. Review: https://reviews.apache.org/r/66638
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/35d1c550 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/35d1c550 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/35d1c550 Branch: refs/heads/1.3.x Commit: 35d1c55007ebb0aff14c19a155ec04231a80b294 Parents: b887783 Author: Alexander Rukletsov <[email protected]> Authored: Mon Apr 16 19:18:49 2018 +0200 Committer: Alexander Rukletsov <[email protected]> Committed: Sun Apr 22 22:31:50 2018 +0200 ---------------------------------------------------------------------- 3rdparty/libprocess/src/decoder.hpp | 118 +++++++++++++++++-------------- 1 file changed, 64 insertions(+), 54 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/35d1c550/3rdparty/libprocess/src/decoder.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/src/decoder.hpp b/3rdparty/libprocess/src/decoder.hpp index 2d54511..ecaf1fd 100644 --- a/3rdparty/libprocess/src/decoder.hpp +++ b/3rdparty/libprocess/src/decoder.hpp @@ -37,6 +37,16 @@ namespace process { +namespace http_parsing { + +// We expect callbacks to return 0 on success and 1 on failure. These constants +// are introduced solely to make decoders' code easier to read and are not meant +// to be used outside. +constexpr int SUCCESS = 0; +constexpr int FAILURE = 1; + +} // namespace http_parsing { + // TODO(benh): Make DataDecoder abstract and make RequestDecoder a // concrete subclass. class DataDecoder @@ -111,17 +121,17 @@ private: decoder->request = new http::Request(); - return 0; + return http_parsing::SUCCESS; } static int on_chunk_complete(http_parser* p) { - return 0; + return http_parsing::SUCCESS; } static int on_chunk_header(http_parser* p) { - return 0; + return http_parsing::SUCCESS; } static int on_url(http_parser* p, const char* data, size_t length) @@ -135,7 +145,7 @@ private: // `on_message_complete`. decoder->url.append(data, length); - return 0; + return http_parsing::SUCCESS; } static int on_header_field(http_parser* p, const char* data, size_t length) @@ -152,7 +162,7 @@ private: decoder->field.append(data, length); decoder->header = HEADER_FIELD; - return 0; + return http_parsing::SUCCESS; } static int on_header_value(http_parser* p, const char* data, size_t length) @@ -161,7 +171,7 @@ private: CHECK_NOTNULL(decoder->request); decoder->value.append(data, length); decoder->header = HEADER_VALUE; - return 0; + return http_parsing::SUCCESS; } static int on_headers_complete(http_parser* p) @@ -180,7 +190,7 @@ private: decoder->request->keepAlive = http_should_keep_alive(&decoder->parser) != 0; - return 0; + return http_parsing::SUCCESS; } static int on_body(http_parser* p, const char* data, size_t length) @@ -188,7 +198,7 @@ private: DataDecoder* decoder = (DataDecoder*) p->data; CHECK_NOTNULL(decoder->request); decoder->request->body.append(data, length); - return 0; + return http_parsing::SUCCESS; } static int on_message_complete(http_parser* p) @@ -233,7 +243,7 @@ private: if (decoded.isError()) { decoder->failure = true; - return 1; + return http_parsing::FAILURE; } decoder->request->url.query = decoded.get(); @@ -245,7 +255,7 @@ private: Try<std::string> decompressed = gzip::decompress(decoder->request->body); if (decompressed.isError()) { decoder->failure = true; - return 1; + return http_parsing::FAILURE; } decoder->request->body = decompressed.get(); @@ -258,7 +268,7 @@ private: decoder->requests.push_back(decoder->request); decoder->request = nullptr; - return 0; + return http_parsing::SUCCESS; } bool failure; @@ -359,22 +369,22 @@ private: decoder->response->body.clear(); decoder->response->path.clear(); - return 0; + return http_parsing::SUCCESS; } static int on_chunk_complete(http_parser* p) { - return 0; + return http_parsing::SUCCESS; } static int on_chunk_header(http_parser* p) { - return 0; + return http_parsing::SUCCESS; } static int on_url(http_parser* p, const char* data, size_t length) { - return 0; + return http_parsing::SUCCESS; } static int on_header_field(http_parser* p, const char* data, size_t length) @@ -391,7 +401,7 @@ private: decoder->field.append(data, length); decoder->header = HEADER_FIELD; - return 0; + return http_parsing::SUCCESS; } static int on_header_value(http_parser* p, const char* data, size_t length) @@ -400,7 +410,7 @@ private: CHECK_NOTNULL(decoder->response); decoder->value.append(data, length); decoder->header = HEADER_VALUE; - return 0; + return http_parsing::SUCCESS; } static int on_headers_complete(http_parser* p) @@ -414,7 +424,7 @@ private: decoder->field.clear(); decoder->value.clear(); - return 0; + return http_parsing::SUCCESS; } static int on_body(http_parser* p, const char* data, size_t length) @@ -422,7 +432,7 @@ private: ResponseDecoder* decoder = (ResponseDecoder*) p->data; CHECK_NOTNULL(decoder->response); decoder->response->body.append(data, length); - return 0; + return http_parsing::SUCCESS; } static int on_message_complete(http_parser* p) @@ -438,7 +448,7 @@ private: http::Status::string(decoder->parser.status_code); } else { decoder->failure = true; - return 1; + return http_parsing::FAILURE; } // We can only provide the gzip encoding. @@ -448,7 +458,7 @@ private: Try<std::string> decompressed = gzip::decompress(decoder->response->body); if (decompressed.isError()) { decoder->failure = true; - return 1; + return http_parsing::FAILURE; } decoder->response->body = decompressed.get(); @@ -461,12 +471,12 @@ private: decoder->responses.push_back(decoder->response); decoder->response = nullptr; - return 0; + return http_parsing::SUCCESS; } static int on_status(http_parser* p, const char* data, size_t length) { - return 0; + return http_parsing::SUCCESS; } bool failure; @@ -598,27 +608,27 @@ private: decoder->response->type = http::Response::PIPE; decoder->writer = None(); - return 0; + return http_parsing::SUCCESS; } static int on_chunk_complete(http_parser* p) { - return 0; + return http_parsing::SUCCESS; } static int on_chunk_header(http_parser* p) { - return 0; + return http_parsing::SUCCESS; } static int on_status(http_parser* p, const char* data, size_t length) { - return 0; + return http_parsing::SUCCESS; } static int on_url(http_parser* p, const char* data, size_t length) { - return 0; + return http_parsing::SUCCESS; } static int on_header_field(http_parser* p, const char* data, size_t length) @@ -628,7 +638,7 @@ private: // TODO(alexr): We currently do not support trailers, i.e., headers after // `on_headers_complete` has been called, and instead treat them as errors. if (decoder->response == nullptr) { - return 1; + return http_parsing::FAILURE; } if (decoder->header != HEADER_FIELD) { @@ -640,7 +650,7 @@ private: decoder->field.append(data, length); decoder->header = HEADER_FIELD; - return 0; + return http_parsing::SUCCESS; } static int on_header_value(http_parser* p, const char* data, size_t length) @@ -650,12 +660,12 @@ private: // TODO(alexr): We currently do not support trailers, i.e., headers after // `on_headers_complete` has been called, and instead treat them as errors. if (decoder->response == nullptr) { - return 1; + return http_parsing::FAILURE; } decoder->value.append(data, length); decoder->header = HEADER_VALUE; - return 0; + return http_parsing::SUCCESS; } static int on_headers_complete(http_parser* p) @@ -678,7 +688,7 @@ private: http::Status::string(decoder->parser.status_code); } else { decoder->failure = true; - return 1; + return http_parsing::FAILURE; } // We cannot provide streaming gzip decompression! @@ -686,7 +696,7 @@ private: decoder->response->headers.get("Content-Encoding"); if (encoding.isSome() && encoding.get() == "gzip") { decoder->failure = true; - return 1; + return http_parsing::FAILURE; } CHECK_NONE(decoder->writer); @@ -705,7 +715,7 @@ private: // auxiliary data structure. decoder->response = nullptr; - return 0; + return http_parsing::SUCCESS; } static int on_body(http_parser* p, const char* data, size_t length) @@ -717,7 +727,7 @@ private: http::Pipe::Writer writer = decoder->writer.get(); // Remove const. writer.write(std::string(data, length)); - return 0; + return http_parsing::SUCCESS; } static int on_message_complete(http_parser* p) @@ -728,7 +738,7 @@ private: // earlier (e.g., due to invalid status code). if (decoder->writer.isNone()) { CHECK(decoder->failure); - return 1; + return http_parsing::FAILURE; } http::Pipe::Writer writer = decoder->writer.get(); // Remove const. @@ -736,7 +746,7 @@ private: decoder->writer = None(); - return 0; + return http_parsing::SUCCESS; } bool failure; @@ -859,17 +869,17 @@ private: decoder->writer = None(); decoder->decompressor.reset(); - return 0; + return http_parsing::SUCCESS; } static int on_chunk_complete(http_parser* p) { - return 0; + return http_parsing::SUCCESS; } static int on_chunk_header(http_parser* p) { - return 0; + return http_parsing::SUCCESS; } static int on_url(http_parser* p, const char* data, size_t length) @@ -878,7 +888,7 @@ private: // URL should not be parsed after `on_headers_complete` has been called. if (decoder->request == nullptr) { - return 1; + return http_parsing::FAILURE; } // The current http_parser library (version 2.6.2 and below) @@ -887,7 +897,7 @@ private: // `on_header_complete`. decoder->url.append(data, length); - return 0; + return http_parsing::SUCCESS; } static int on_header_field(http_parser* p, const char* data, size_t length) @@ -897,7 +907,7 @@ private: // TODO(alexr): We currently do not support trailers, i.e., headers after // `on_headers_complete` has been called, and instead treat them as errors. if (decoder->request == nullptr) { - return 1; + return http_parsing::FAILURE; } if (decoder->header != HEADER_FIELD) { @@ -909,7 +919,7 @@ private: decoder->field.append(data, length); decoder->header = HEADER_FIELD; - return 0; + return http_parsing::SUCCESS; } static int on_header_value(http_parser* p, const char* data, size_t length) @@ -919,12 +929,12 @@ private: // TODO(alexr): We currently do not support trailers, i.e., headers after // `on_headers_complete` has been called, and instead treat them as errors. if (decoder->request == nullptr) { - return 1; + return http_parsing::FAILURE; } decoder->value.append(data, length); decoder->header = HEADER_VALUE; - return 0; + return http_parsing::SUCCESS; } static int on_headers_complete(http_parser* p) @@ -981,7 +991,7 @@ private: if (decoded.isError()) { decoder->failure = true; - return 1; + return http_parsing::FAILURE; } decoder->request->url.query = std::move(decoded.get()); @@ -1010,7 +1020,7 @@ private: // auxiliary data structure. decoder->request = nullptr; - return 0; + return http_parsing::SUCCESS; } static int on_body(http_parser* p, const char* data, size_t length) @@ -1028,7 +1038,7 @@ private: if (decompressed.isError()) { decoder->failure = true; - return 1; + return http_parsing::FAILURE; } body = std::move(decompressed.get()); @@ -1038,7 +1048,7 @@ private: writer.write(std::move(body)); - return 0; + return http_parsing::SUCCESS; } static int on_message_complete(http_parser* p) @@ -1049,7 +1059,7 @@ private: // earlier (e.g., due to invalid query parameters). if (decoder->writer.isNone()) { CHECK(decoder->failure); - return 1; + return http_parsing::FAILURE; } http::Pipe::Writer writer = decoder->writer.get(); // Remove const. @@ -1058,14 +1068,14 @@ private: !decoder->decompressor->finished()) { writer.fail("Failed to decompress body"); decoder->failure = true; - return 1; + return http_parsing::FAILURE; } writer.close(); decoder->writer = None(); - return 0; + return http_parsing::SUCCESS; } bool failure;
