From 46c924b9d2edd84bc6ecb5367ba52fcff82804fa Mon Sep 17 00:00:00 2001 From: Mauro Sardara Date: Tue, 18 Feb 2020 16:21:07 +0100 Subject: [HICN-528] Add progress bar to higet. Change-Id: I645ef2b8834f4310933793fb1f59e8f37e3d6aef Signed-off-by: Mauro Sardara --- libtransport/src/hicn/transport/http/response.cc | 60 ++++++++++-------------- 1 file changed, 25 insertions(+), 35 deletions(-) (limited to 'libtransport/src/hicn/transport/http/response.cc') 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 #include -#include +#include +#include #include @@ -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 &&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 &&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 &&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 &&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 *>(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 -- cgit 1.2.3-korg