diff options
author | Angelo Mantellini <angelo.mantellini@cisco.com> | 2020-02-18 17:27:27 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@fd.io> | 2020-02-18 17:27:27 +0000 |
commit | 0710f1ff754ebf01ae5befabb055349fe472b0c2 (patch) | |
tree | 648f6b622437353da87af9fcd3019de5fd5b24a3 /libtransport/src/hicn/transport/http/response.cc | |
parent | e43298508d6e8afe67cebb53f97499dd06f4d822 (diff) | |
parent | 46c924b9d2edd84bc6ecb5367ba52fcff82804fa (diff) |
Merge "[HICN-528] Add progress bar to higet."
Diffstat (limited to 'libtransport/src/hicn/transport/http/response.cc')
-rw-r--r-- | libtransport/src/hicn/transport/http/response.cc | 60 |
1 files changed, 25 insertions, 35 deletions
diff --git a/libtransport/src/hicn/transport/http/response.cc b/libtransport/src/hicn/transport/http/response.cc index db7306cca..a2bc47e6b 100644 --- a/libtransport/src/hicn/transport/http/response.cc +++ b/libtransport/src/hicn/transport/http/response.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2017-2020 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -16,7 +16,8 @@ #include <hicn/transport/errors/errors.h> #include <hicn/transport/http/response.h> -#include <algorithm> +#include <experimental/algorithm> +#include <experimental/functional> #include <cstring> @@ -26,30 +27,32 @@ namespace http { HTTPResponse::HTTPResponse() {} -HTTPResponse::HTTPResponse(const HTTPHeaders &headers, - const HTTPPayload &payload) { - headers_ = headers; - payload_ = payload; +HTTPResponse::HTTPResponse(std::unique_ptr<utils::MemBuf> &&response) { + parse(std::move(response)); } -const HTTPHeaders &HTTPResponse::getHeaders() { - parse(); - return headers_; -} - -const HTTPPayload &HTTPResponse::getPayload() { - parse(); - return payload_; +void HTTPResponse::appendResponseChunk( + std::unique_ptr<utils::MemBuf> &&response_chunk) { + if (headers_.empty()) { + parse(std::move(response_chunk)); + } else { + payload_->prependChain(std::move(response_chunk)); + } } -bool HTTPResponse::parseHeaders() { +bool HTTPResponse::parseHeaders(std::unique_ptr<utils::MemBuf> &&buffer) { const char *crlf2 = "\r\n\r\n"; + const char *begin = (const char *)buffer->data(); + const char *end = begin + buffer->length(); auto it = - std::search(this->begin(), this->end(), crlf2, crlf2 + strlen(crlf2)); + std::experimental::search(begin, end, + std::experimental::make_boyer_moore_searcher( + crlf2, crlf2 + strlen(crlf2))); - if (it != end()) { + if (it != end) { + buffer->trimStart(it + strlen(crlf2) - begin); std::stringstream ss; - ss.str(std::string(begin(), it + 1)); + ss.str(std::string(begin, it)); std::string line; getline(ss, line); @@ -99,24 +102,15 @@ bool HTTPResponse::parseHeaders() { } } + payload_ = std::move(buffer); + return true; } -void HTTPResponse::parse() { - if (!parseHeaders()) { +void HTTPResponse::parse(std::unique_ptr<utils::MemBuf> &&response) { + if (!parseHeaders(std::move(response))) { throw errors::RuntimeException("Malformed HTTP response"); } - - if (payload_.empty()) { - const char *crlf2 = "\r\n\r\n"; - auto it = - std::search(this->begin(), this->end(), crlf2, crlf2 + strlen(crlf2)); - - if (it != this->end()) { - erase(begin(), it + strlen(crlf2)); - payload_ = std::move(*dynamic_cast<std::vector<uint8_t> *>(this)); - } - } } const std::string &HTTPResponse::getStatusCode() const { return status_code_; } @@ -125,10 +119,6 @@ const std::string &HTTPResponse::getStatusString() const { return status_string_; } -const std::string &HTTPResponse::getHttpVersion() const { - return http_version_; -} - } // namespace http } // namespace transport
\ No newline at end of file |