diff options
Diffstat (limited to 'libtransport/src/hicn/transport/http')
-rw-r--r-- | libtransport/src/hicn/transport/http/CMakeLists.txt | 35 | ||||
-rw-r--r-- | libtransport/src/hicn/transport/http/client_connection.cc | 220 | ||||
-rw-r--r-- | libtransport/src/hicn/transport/http/client_connection.h | 121 | ||||
-rw-r--r-- | libtransport/src/hicn/transport/http/default_values.h | 32 | ||||
-rw-r--r-- | libtransport/src/hicn/transport/http/facade.h | 22 | ||||
-rw-r--r-- | libtransport/src/hicn/transport/http/message.h | 70 | ||||
-rw-r--r-- | libtransport/src/hicn/transport/http/request.cc | 74 | ||||
-rw-r--r-- | libtransport/src/hicn/transport/http/request.h | 56 | ||||
-rw-r--r-- | libtransport/src/hicn/transport/http/response.cc | 138 | ||||
-rw-r--r-- | libtransport/src/hicn/transport/http/response.h | 58 | ||||
-rw-r--r-- | libtransport/src/hicn/transport/http/server_acceptor.cc | 110 | ||||
-rw-r--r-- | libtransport/src/hicn/transport/http/server_acceptor.h | 63 | ||||
-rw-r--r-- | libtransport/src/hicn/transport/http/server_publisher.cc | 173 | ||||
-rw-r--r-- | libtransport/src/hicn/transport/http/server_publisher.h | 69 |
14 files changed, 0 insertions, 1241 deletions
diff --git a/libtransport/src/hicn/transport/http/CMakeLists.txt b/libtransport/src/hicn/transport/http/CMakeLists.txt deleted file mode 100644 index b24c80195..000000000 --- a/libtransport/src/hicn/transport/http/CMakeLists.txt +++ /dev/null @@ -1,35 +0,0 @@ -# Copyright (c) 2017-2019 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: -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -cmake_minimum_required(VERSION 3.5 FATAL_ERROR) - -list(APPEND SOURCE_FILES - ${CMAKE_CURRENT_SOURCE_DIR}/client_connection.cc - ${CMAKE_CURRENT_SOURCE_DIR}/request.cc - ${CMAKE_CURRENT_SOURCE_DIR}/server_publisher.cc - ${CMAKE_CURRENT_SOURCE_DIR}/server_acceptor.cc - ${CMAKE_CURRENT_SOURCE_DIR}/response.cc) - -list(APPEND HEADER_FILES - ${CMAKE_CURRENT_SOURCE_DIR}/client_connection.h - ${CMAKE_CURRENT_SOURCE_DIR}/request.h - ${CMAKE_CURRENT_SOURCE_DIR}/server_publisher.h - ${CMAKE_CURRENT_SOURCE_DIR}/server_acceptor.h - ${CMAKE_CURRENT_SOURCE_DIR}/default_values.h - ${CMAKE_CURRENT_SOURCE_DIR}/facade.h - ${CMAKE_CURRENT_SOURCE_DIR}/response.h - ${CMAKE_CURRENT_SOURCE_DIR}/message.h -) - -set(SOURCE_FILES ${SOURCE_FILES} PARENT_SCOPE) -set(HEADER_FILES ${HEADER_FILES} PARENT_SCOPE)
\ No newline at end of file diff --git a/libtransport/src/hicn/transport/http/client_connection.cc b/libtransport/src/hicn/transport/http/client_connection.cc deleted file mode 100644 index bd21bc448..000000000 --- a/libtransport/src/hicn/transport/http/client_connection.cc +++ /dev/null @@ -1,220 +0,0 @@ -/* - * 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: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <hicn/transport/http/client_connection.h> -#include <hicn/transport/utils/hash.h> - -#define DEFAULT_BETA 0.99 -#define DEFAULT_GAMMA 0.07 - -namespace transport { - -namespace http { - -using namespace transport; - -HTTPClientConnection::HTTPClientConnection() - : consumer_(TransportProtocolAlgorithms::RAAQM), - read_bytes_callback_(nullptr), - read_buffer_(nullptr), - response_(std::make_shared<HTTPResponse>()), - timer_(nullptr) { - consumer_.setSocketOption( - ConsumerCallbacksOptions::CONTENT_OBJECT_TO_VERIFY, - (ConsumerContentObjectVerificationCallback)std::bind( - &HTTPClientConnection::verifyData, this, std::placeholders::_1, - std::placeholders::_2)); - - consumer_.setSocketOption(ConsumerCallbacksOptions::READ_CALLBACK, this); - - consumer_.connect(); - std::shared_ptr<typename ConsumerSocket::Portal> portal; - consumer_.getSocketOption(GeneralTransportOptions::PORTAL, portal); - timer_ = std::make_unique<asio::steady_timer>(portal->getIoService()); -} - -HTTPClientConnection::RC HTTPClientConnection::get( - const std::string &url, HTTPHeaders headers, HTTPPayload &&payload, - std::shared_ptr<HTTPResponse> response, ReadBytesCallback *callback, - std::string ipv6_first_word) { - return sendRequest(url, HTTPMethod::GET, headers, std::move(payload), - response, callback, ipv6_first_word); -} - -HTTPClientConnection::RC HTTPClientConnection::sendRequest( - const std::string &url, HTTPMethod method, HTTPHeaders headers, - HTTPPayload &&payload, std::shared_ptr<HTTPResponse> response, - ReadBytesCallback *callback, std::string ipv6_first_word) { - current_url_ = url; - read_bytes_callback_ = callback; - if (!response) { - response_ = std::make_shared<HTTPResponse>(); - } else { - response_ = response; - } - - auto start = std::chrono::steady_clock::now(); - request_.init(method, url, headers, std::move(payload)); - - success_callback_ = [this, method = std::move(method), url = std::move(url), - start = std::move(start)](std::size_t size) -> void { - auto end = std::chrono::steady_clock::now(); - TRANSPORT_LOGI( - "%s %s [%s] duration: %llu [usec] %zu [bytes]\n", - method_map[method].c_str(), url.c_str(), name_.str().c_str(), - (unsigned long long) - std::chrono::duration_cast<std::chrono::microseconds>(end - start) - .count(), - size); - }; - - sendRequestGetReply(ipv6_first_word); - return return_code_; -} - -void HTTPClientConnection::sendRequestGetReply(std::string &ipv6_first_word) { - const std::string &request_string = request_.getRequestString(); - const std::string &locator = request_.getLocator(); - - // Hash it - - uint32_t locator_hash = - utils::hash::fnv32_buf(locator.c_str(), locator.size()); - uint64_t request_hash = - utils::hash::fnv64_buf(request_string.c_str(), request_string.size()); - - consumer_.setSocketOption( - ConsumerCallbacksOptions::INTEREST_OUTPUT, - (ConsumerInterestCallback)std::bind( - &HTTPClientConnection::processLeavingInterest, this, - std::placeholders::_1, std::placeholders::_2)); - - // Factor hicn name using hash - name_.str(""); - - name_ << ipv6_first_word << ":"; - - for (uint16_t *word = (uint16_t *)&locator_hash; - std::size_t(word) < (std::size_t(&locator_hash) + sizeof(locator_hash)); - word++) { - name_ << ":" << std::hex << *word; - } - - for (uint16_t *word = (uint16_t *)&request_hash; - std::size_t(word) < (std::size_t(&request_hash) + sizeof(request_hash)); - word++) { - name_ << ":" << std::hex << *word; - } - - name_ << "|0"; - - consumer_.consume(Name(name_.str())); - - consumer_.stop(); -} - -std::shared_ptr<HTTPResponse> HTTPClientConnection::response() { - response_->coalescePayloadBuffer(); - return response_; -} - -bool HTTPClientConnection::verifyData( - ConsumerSocket &c, const core::ContentObject &contentObject) { - if (contentObject.getPayloadType() == PayloadType::CONTENT_OBJECT) { - TRANSPORT_LOGI("VERIFY CONTENT\n"); - } else if (contentObject.getPayloadType() == PayloadType::MANIFEST) { - TRANSPORT_LOGI("VERIFY MANIFEST\n"); - } - - return true; -} - -void HTTPClientConnection::processLeavingInterest( - ConsumerSocket &c, const core::Interest &interest) { - if (interest.payloadSize() == 0) { - Interest &int2 = const_cast<Interest &>(interest); - auto payload = request_.getRequestString(); - auto payload2 = request_.getPayload(); - int2.appendPayload((uint8_t *)payload.data(), payload.size()); - if (payload2) - int2.appendPayload((uint8_t *)payload2->data(), payload2->length()); - } -} - -ConsumerSocket &HTTPClientConnection::getConsumer() { return consumer_; } - -HTTPClientConnection &HTTPClientConnection::stop() { - // This is thread safe and can be called from another thread - consumer_.stop(); - - return *this; -} - -HTTPClientConnection &HTTPClientConnection::setTimeout( - const std::chrono::seconds &timeout) { - timer_->cancel(); - timer_->expires_from_now(timeout); - timer_->async_wait([this](std::error_code ec) { - if (!ec) { - consumer_.stop(); - } - }); - - return *this; -} - -HTTPClientConnection &HTTPClientConnection::setCertificate( - const std::string &cert_path) { - if (consumer_.setSocketOption(GeneralTransportOptions::CERTIFICATE, - cert_path) == SOCKET_OPTION_NOT_SET) { - throw errors::RuntimeException("Error setting the certificate."); - } - - return *this; -} - -// Read buffer management -void HTTPClientConnection::readBufferAvailable( - std::unique_ptr<utils::MemBuf> &&buffer) noexcept { - if (!read_bytes_callback_) { - response_->appendResponseChunk(std::move(buffer)); - } else { - read_bytes_callback_->onBytesReceived(std::move(buffer)); - } -} - -// Read buffer management -void HTTPClientConnection::readError(const std::error_code ec) noexcept { - TRANSPORT_LOGE("Error %s during download of %s", ec.message().c_str(), - current_url_.c_str()); - if (read_bytes_callback_) { - read_bytes_callback_->onError(ec); - } - - return_code_ = HTTPClientConnection::RC::DOWNLOAD_FAILED; -} - -void HTTPClientConnection::readSuccess(std::size_t total_size) noexcept { - success_callback_(total_size); - if (read_bytes_callback_) { - read_bytes_callback_->onSuccess(total_size); - } - - return_code_ = HTTPClientConnection::RC::DOWNLOAD_SUCCESS; -} - -} // namespace http - -} // namespace transport diff --git a/libtransport/src/hicn/transport/http/client_connection.h b/libtransport/src/hicn/transport/http/client_connection.h deleted file mode 100644 index e93a37111..000000000 --- a/libtransport/src/hicn/transport/http/client_connection.h +++ /dev/null @@ -1,121 +0,0 @@ -/* - * 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: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include <hicn/transport/http/default_values.h> -#include <hicn/transport/http/request.h> -#include <hicn/transport/http/response.h> -#include <hicn/transport/interfaces/socket_consumer.h> -#include <hicn/transport/interfaces/socket_producer.h> -#include <hicn/transport/utils/uri.h> - -#include <vector> - -namespace transport { - -namespace http { - -using namespace interface; -using namespace core; - -class HTTPClientConnection : public ConsumerSocket::ReadCallback { - static constexpr uint32_t max_buffer_capacity = 64 * 1024; - - public: - class ReadBytesCallback { - public: - virtual void onBytesReceived(std::unique_ptr<utils::MemBuf> &&buffer) = 0; - virtual void onSuccess(std::size_t bytes) = 0; - virtual void onError(const std::error_code ec) = 0; - }; - - enum class RC : uint32_t { DOWNLOAD_FAILED, DOWNLOAD_SUCCESS }; - - HTTPClientConnection(); - - RC get(const std::string &url, HTTPHeaders headers = {}, - HTTPPayload &&payload = nullptr, - std::shared_ptr<HTTPResponse> response = nullptr, - ReadBytesCallback *callback = nullptr, - std::string ipv6_first_word = "b001"); - - RC sendRequest(const std::string &url, HTTPMethod method, - HTTPHeaders headers = {}, HTTPPayload &&payload = nullptr, - std::shared_ptr<HTTPResponse> response = nullptr, - ReadBytesCallback *callback = nullptr, - std::string ipv6_first_word = "b001"); - - std::shared_ptr<HTTPResponse> response(); - - HTTPClientConnection &stop(); - - interface::ConsumerSocket &getConsumer(); - - HTTPClientConnection &setTimeout(const std::chrono::seconds &timeout); - - HTTPClientConnection &setCertificate(const std::string &cert_path); - - private: - void sendRequestGetReply(std::string &ipv6_first_word); - - bool verifyData(interface::ConsumerSocket &c, - const core::ContentObject &contentObject); - - void processLeavingInterest(interface::ConsumerSocket &c, - const core::Interest &interest); - - // Read callback - bool isBufferMovable() noexcept override { return true; } - void getReadBuffer(uint8_t **application_buffer, - size_t *max_length) override {} - void readDataAvailable(size_t length) noexcept override {} - size_t maxBufferSize() const override { return max_buffer_capacity; } - void readBufferAvailable( - std::unique_ptr<utils::MemBuf> &&buffer) noexcept override; - void readError(const std::error_code ec) noexcept override; - void readSuccess(std::size_t total_size) noexcept override; - - // The consumer socket - ConsumerSocket consumer_; - - // The current url provided by the application - std::string current_url_; - // The current hICN name used for downloading - std::stringstream name_; - // Function to be called when the read is successful - std::function<void(std::size_t)> success_callback_; - // Return code for current download - RC return_code_; - - // Application provided callback for saving the received content during - // the download. If this callback is used, the HTTPClient will NOT save - // any byte internally. - ReadBytesCallback *read_bytes_callback_; - - HTTPRequest request_; - - // Internal read buffer and HTTP response, to be used if the application does - // not provide any read_bytes_callback - std::unique_ptr<utils::MemBuf> read_buffer_; - std::shared_ptr<HTTPResponse> response_; - - // Timer - std::unique_ptr<asio::steady_timer> timer_; -}; - -} // end namespace http - -} // end namespace transport
\ No newline at end of file diff --git a/libtransport/src/hicn/transport/http/default_values.h b/libtransport/src/hicn/transport/http/default_values.h deleted file mode 100644 index 2d5a6b821..000000000 --- a/libtransport/src/hicn/transport/http/default_values.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2017-2019 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: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include <cstdint> - -namespace transport { - -namespace http { - -namespace default_values { - -const uint16_t ipv6_first_word = 0xb001; // Network byte order - -} // namespace default_values - -} // namespace http - -} // end namespace transport diff --git a/libtransport/src/hicn/transport/http/facade.h b/libtransport/src/hicn/transport/http/facade.h deleted file mode 100644 index 1551ede3a..000000000 --- a/libtransport/src/hicn/transport/http/facade.h +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2017-2019 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: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include <hicn/transport/http/client_connection.h> -#include <hicn/transport/http/server_acceptor.h> -#include <hicn/transport/http/server_publisher.h> - -namespace libl4 = transport;
\ No newline at end of file diff --git a/libtransport/src/hicn/transport/http/message.h b/libtransport/src/hicn/transport/http/message.h deleted file mode 100644 index b8756224f..000000000 --- a/libtransport/src/hicn/transport/http/message.h +++ /dev/null @@ -1,70 +0,0 @@ -/* - * 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: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#ifdef _WIN32 -#include <hicn/transport/portability/win_portability.h> -#endif - -#include <hicn/transport/utils/membuf.h> - -#include <map> -#include <sstream> -#include <vector> - -#define HTTP_VERSION "1.1" - -namespace transport { - -namespace http { - -typedef enum { GET, POST, PUT, PATCH, DELETE } HTTPMethod; - -static std::map<HTTPMethod, std::string> method_map = { - {GET, "GET"}, {POST, "POST"}, {PUT, "PUT"}, - {PATCH, "PATCH"}, {DELETE, "DELETE"}, -}; - -typedef std::map<std::string, std::string> HTTPHeaders; -typedef std::unique_ptr<utils::MemBuf> HTTPPayload; - -class HTTPMessage { - public: - virtual ~HTTPMessage() = default; - - const HTTPHeaders getHeaders() { return headers_; }; - - void coalescePayloadBuffer() { - auto it = headers_.find("Content-Length"); - if (it != headers_.end()) { - auto content_length = std::stoul(it->second); - payload_->gather(content_length); - } - } - - HTTPPayload &&getPayload() { return std::move(payload_); } - - std::string getHttpVersion() const { return http_version_; }; - - protected: - HTTPHeaders headers_; - HTTPPayload payload_; - std::string http_version_; -}; - -} // end namespace http - -} // end namespace transport
\ No newline at end of file diff --git a/libtransport/src/hicn/transport/http/request.cc b/libtransport/src/hicn/transport/http/request.cc deleted file mode 100644 index 09f709642..000000000 --- a/libtransport/src/hicn/transport/http/request.cc +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <hicn/transport/http/request.h> -#include <hicn/transport/utils/uri.h> - -namespace transport { - -namespace http { - -HTTPRequest::HTTPRequest() {} - -HTTPRequest::HTTPRequest(HTTPMethod method, const std::string &url, - const HTTPHeaders &headers, HTTPPayload &&payload) { - init(method, url, headers, std::move(payload)); -} - -void HTTPRequest::init(HTTPMethod method, const std::string &url, - const HTTPHeaders &headers, HTTPPayload &&payload) { - utils::Uri uri; - uri.parse(url); - - path_ = uri.getPath(); - query_string_ = uri.getQueryString(); - protocol_ = uri.getProtocol(); - locator_ = uri.getLocator(); - port_ = uri.getPort(); - http_version_ = HTTP_VERSION; - - headers_ = headers; - payload_ = std::move(payload); - - std::transform(locator_.begin(), locator_.end(), locator_.begin(), ::tolower); - - std::transform(protocol_.begin(), protocol_.end(), protocol_.begin(), - ::tolower); - - std::stringstream stream; - stream << method_map[method] << " " << uri.getPath() << " HTTP/" - << HTTP_VERSION << "\r\n"; - for (auto &item : headers) { - stream << item.first << ": " << item.second << "\r\n"; - } - stream << "\r\n"; - request_string_ = stream.str(); -} - -std::string HTTPRequest::getPort() const { return port_; } - -std::string HTTPRequest::getLocator() const { return locator_; } - -std::string HTTPRequest::getProtocol() const { return protocol_; } - -std::string HTTPRequest::getPath() const { return path_; } - -std::string HTTPRequest::getQueryString() const { return query_string_; } - -std::string HTTPRequest::getRequestString() const { return request_string_; } - -} // namespace http - -} // namespace transport
\ No newline at end of file diff --git a/libtransport/src/hicn/transport/http/request.h b/libtransport/src/hicn/transport/http/request.h deleted file mode 100644 index 54904d696..000000000 --- a/libtransport/src/hicn/transport/http/request.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include <hicn/transport/http/message.h> - -#include <map> -#include <sstream> -#include <vector> - -namespace transport { - -namespace http { - -class HTTPRequest : public HTTPMessage { - public: - HTTPRequest(); - HTTPRequest(HTTPMethod method, const std::string &url, - const HTTPHeaders &headers, HTTPPayload &&payload); - - void init(HTTPMethod method, const std::string &url, - const HTTPHeaders &headers, HTTPPayload &&payload); - - std::string getQueryString() const; - - std::string getPath() const; - - std::string getProtocol() const; - - std::string getLocator() const; - - std::string getPort() const; - - std::string getRequestString() const; - - private: - std::string query_string_, path_, protocol_, locator_, port_; - std::string request_string_; -}; - -} // end namespace http - -} // end namespace transport
\ No newline at end of file diff --git a/libtransport/src/hicn/transport/http/response.cc b/libtransport/src/hicn/transport/http/response.cc deleted file mode 100644 index ba0acd1ac..000000000 --- a/libtransport/src/hicn/transport/http/response.cc +++ /dev/null @@ -1,138 +0,0 @@ -/* - * 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: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <hicn/transport/errors/errors.h> -#include <hicn/transport/http/response.h> - -#include <experimental/algorithm> -#include <experimental/functional> - -#include <cstring> - -namespace transport { - -namespace http { - -HTTPResponse::HTTPResponse() {} - -HTTPResponse::HTTPResponse(std::unique_ptr<utils::MemBuf> &&response) { - parse(std::move(response)); -} - -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(std::unique_ptr<utils::MemBuf> &&buffer) { - auto ret = - HTTPResponse::parseHeaders(buffer->data(), buffer->length(), headers_, - http_version_, status_code_, status_string_); - - if (ret) { - buffer->trimStart(ret); - payload_ = std::move(buffer); - return true; - } - - return false; -} - -std::size_t HTTPResponse::parseHeaders(const uint8_t *buffer, std::size_t size, - HTTPHeaders &headers, - std::string &http_version, - std::string &status_code, - std::string &status_string) { - const char *crlf2 = "\r\n\r\n"; - const char *begin = (const char *)buffer; - const char *end = begin + size; - auto it = - std::experimental::search(begin, end, - std::experimental::make_boyer_moore_searcher( - crlf2, crlf2 + strlen(crlf2))); - - if (it != end) { - std::stringstream ss; - ss.str(std::string(begin, it)); - - std::string line; - getline(ss, line); - std::istringstream line_s(line); - std::string _http_version; - - line_s >> _http_version; - std::size_t separator; - if ((separator = _http_version.find('/')) != std::string::npos) { - if (_http_version.substr(0, separator) != "HTTP") { - return 0; - } - http_version = - line.substr(separator + 1, _http_version.length() - separator - 1); - } else { - return 0; - } - - std::string _status_string; - - line_s >> status_code; - line_s >> _status_string; - - auto _it = std::search(line.begin(), line.end(), status_string.begin(), - status_string.end()); - - status_string = std::string(_it, line.end() - 1); - - std::size_t param_end; - std::size_t value_start; - while (getline(ss, line)) { - if ((param_end = line.find(':')) != std::string::npos) { - value_start = param_end + 1; - if ((value_start) < line.size()) { - if (line[value_start] == ' ') { - value_start++; - } - if (value_start < line.size()) { - headers[line.substr(0, param_end)] = - line.substr(value_start, line.size() - value_start - 1); - } - } - } else { - return 0; - } - } - } - - return it + strlen(crlf2) - begin; -} - -void HTTPResponse::parse(std::unique_ptr<utils::MemBuf> &&response) { - if (!parseHeaders(std::move(response))) { - throw errors::RuntimeException("Malformed HTTP response"); - } -} - -const std::string &HTTPResponse::getStatusCode() const { return status_code_; } - -const std::string &HTTPResponse::getStatusString() const { - return status_string_; -} - -} // namespace http - -} // namespace transport
\ No newline at end of file diff --git a/libtransport/src/hicn/transport/http/response.h b/libtransport/src/hicn/transport/http/response.h deleted file mode 100644 index bab41acb8..000000000 --- a/libtransport/src/hicn/transport/http/response.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include <hicn/transport/http/message.h> -#include <hicn/transport/utils/array.h> - -#include <map> -#include <sstream> -#include <vector> - -namespace transport { - -namespace http { - -class HTTPResponse : public HTTPMessage { - public: - HTTPResponse(); - - HTTPResponse(std::unique_ptr<utils::MemBuf> &&response); - - void appendResponseChunk(std::unique_ptr<utils::MemBuf> &&response_chunk); - - const std::string &getStatusCode() const; - - const std::string &getStatusString() const; - - void parse(std::unique_ptr<utils::MemBuf> &&response); - - bool parseHeaders(std::unique_ptr<utils::MemBuf> &&buffer); - - static std::size_t parseHeaders(const uint8_t *buffer, std::size_t size, - HTTPHeaders &headers, - std::string &http_version, - std::string &status_code, - std::string &status_string); - - private: - std::string status_code_; - std::string status_string_; -}; - -} // end namespace http - -} // end namespace transport
\ No newline at end of file diff --git a/libtransport/src/hicn/transport/http/server_acceptor.cc b/libtransport/src/hicn/transport/http/server_acceptor.cc deleted file mode 100644 index e478dfcd4..000000000 --- a/libtransport/src/hicn/transport/http/server_acceptor.cc +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (c) 2017-2019 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: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <hicn/transport/http/server_acceptor.h> -#include <hicn/transport/utils/hash.h> -#include <hicn/transport/utils/uri.h> - -namespace transport { - -namespace http { - -HTTPServerAcceptor::HTTPServerAcceptor(std::string &&server_locator, - OnHttpRequest callback) - : HTTPServerAcceptor(server_locator, callback) {} - -HTTPServerAcceptor::HTTPServerAcceptor(std::string &server_locator, - OnHttpRequest callback) - : callback_(callback) { - utils::Uri uri; - - uri.parseProtocolAndLocator(server_locator); - std::string protocol = uri.getProtocol(); - std::string locator = uri.getLocator(); - - std::transform(locator.begin(), locator.end(), locator.begin(), ::tolower); - - std::transform(protocol.begin(), protocol.end(), protocol.begin(), ::tolower); - - if (protocol != "http") { - throw errors::RuntimeException( - "Malformed server_locator. The locator format should be in the form " - "http://locator"); - } - - uint32_t locator_hash = - utils::hash::fnv32_buf(locator.c_str(), locator.size()); - - std::stringstream stream; - stream << std::hex << http::default_values::ipv6_first_word << ":0000"; - - for (uint16_t *word = (uint16_t *)&locator_hash; - std::size_t(word) < (std::size_t(&locator_hash) + sizeof(locator_hash)); - word++) { - stream << ":" << std::hex << *word; - } - - stream << "::0"; - - std::string network = stream.str(); - - core::Prefix acceptor_namespace(network, 64); - - std::string producer_identity = "acceptor_producer"; - acceptor_producer_ = std::make_shared<ProducerSocket>(); - acceptor_producer_->registerPrefix(acceptor_namespace); -} - -void HTTPServerAcceptor::listen(bool async) { - acceptor_producer_->setSocketOption( - ProducerCallbacksOptions::INTEREST_INPUT, - (ProducerInterestCallback)bind( - &HTTPServerAcceptor::processIncomingInterest, this, - std::placeholders::_1, std::placeholders::_2)); - acceptor_producer_->connect(); - - if (!async) { - acceptor_producer_->serveForever(); - } -} - -void HTTPServerAcceptor::processIncomingInterest(ProducerSocket &p, - Interest &interest) { - // Temporary solution. With - auto payload = interest.getPayload(); - - int request_id = utils::hash::fnv32_buf(payload->data(), payload->length()); - - if (publishers_.find(request_id) != publishers_.end()) { - if (publishers_[request_id]) { - publishers_[request_id]->getProducer().onInterest(interest); - return; - } - } - - publishers_[request_id] = - std::make_shared<HTTPServerPublisher>(interest.getName()); - callback_(publishers_[request_id], (uint8_t *)payload->data(), - payload->length(), request_id); -} - -std::map<int, std::shared_ptr<HTTPServerPublisher>> - &HTTPServerAcceptor::getPublishers() { - return publishers_; -} - -} // namespace http - -} // namespace transport diff --git a/libtransport/src/hicn/transport/http/server_acceptor.h b/libtransport/src/hicn/transport/http/server_acceptor.h deleted file mode 100644 index 6ed58f70e..000000000 --- a/libtransport/src/hicn/transport/http/server_acceptor.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2017-2019 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: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include <hicn/transport/http/default_values.h> -#include <hicn/transport/http/request.h> -#include <hicn/transport/http/server_publisher.h> -#include <hicn/transport/interfaces/socket_consumer.h> -#include <hicn/transport/interfaces/socket_producer.h> - -#include <functional> -#include <vector> - -namespace transport { - -namespace http { - -class HTTPServerAcceptor { - friend class HTTPServerPublisher; - using OnHttpRequest = - std::function<void(std::shared_ptr<HTTPServerPublisher> &, - const uint8_t *, std::size_t, int request_id)>; - - public: - HTTPServerAcceptor(std::string &&server_locator, OnHttpRequest callback); - HTTPServerAcceptor(std::string &server_locator, OnHttpRequest callback); - - void listen(bool async); - - std::map<int, std::shared_ptr<HTTPServerPublisher>> &getPublishers(); - - // void asyncSendResponse(); - - // HTTPClientConnection& get(std::string &url, HTTPHeaders headers = {}, - // HTTPPayload payload = {}); - // - // HTTPResponse&& response(); - - private: - void processIncomingInterest(ProducerSocket &p, Interest &interest); - - OnHttpRequest callback_; - std::shared_ptr<ProducerSocket> acceptor_producer_; - - std::map<int, std::shared_ptr<HTTPServerPublisher>> publishers_; -}; - -} // end namespace http - -} // end namespace transport diff --git a/libtransport/src/hicn/transport/http/server_publisher.cc b/libtransport/src/hicn/transport/http/server_publisher.cc deleted file mode 100644 index b4deb5333..000000000 --- a/libtransport/src/hicn/transport/http/server_publisher.cc +++ /dev/null @@ -1,173 +0,0 @@ -/* - * Copyright (c) 2017-2019 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: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <hicn/transport/http/server_publisher.h> -#include <hicn/transport/utils/literals.h> - -namespace transport { - -namespace http { - -HTTPServerPublisher::HTTPServerPublisher(const core::Name &content_name) - : content_name_(content_name) { - std::string identity = "acceptor_producer"; - producer_ = std::make_unique<ProducerSocket>(); - // utils::Identity::generateIdentity(identity)); - core::Prefix publisher_prefix(content_name_, 128); - producer_->registerPrefix(publisher_prefix); -} - -HTTPServerPublisher::~HTTPServerPublisher() { - if (timer_) { - this->timer_->cancel(); - } -} - -HTTPServerPublisher &HTTPServerPublisher::attachPublisher() { - // Create a new publisher - producer_->setSocketOption(GeneralTransportOptions::DATA_PACKET_SIZE, - 1410_U32); - producer_->connect(); - return *this; -} - -HTTPServerPublisher &HTTPServerPublisher::setTimeout( - const std::chrono::milliseconds &timeout, bool timeout_renewal) { - std::shared_ptr<typename ProducerSocket::Portal> portal; - producer_->getSocketOption(GeneralTransportOptions::PORTAL, portal); - timer_ = - std::make_unique<asio::steady_timer>(portal->getIoService(), timeout); - - wait_callback_ = [this](const std::error_code &e) { - if (!e) { - producer_->stop(); - } - }; - - if (timeout_renewal) { - interest_enter_callback_ = [this, timeout](ProducerSocket &p, - const Interest &interest) { - this->timer_->cancel(); - this->timer_->expires_from_now(timeout); - this->timer_->async_wait(wait_callback_); - }; - - producer_->setSocketOption( - ProducerCallbacksOptions::CACHE_HIT, - (ProducerInterestCallback)interest_enter_callback_); - } - - timer_->async_wait(wait_callback_); - - return *this; -} - -void HTTPServerPublisher::publishContent( - const uint8_t *buf, size_t buffer_size, - std::chrono::milliseconds content_lifetime, bool is_last) { - if (producer_) { - producer_->setSocketOption( - GeneralTransportOptions::CONTENT_OBJECT_EXPIRY_TIME, - static_cast<uint32_t>(content_lifetime.count())); - producer_->produce(content_name_, buf, buffer_size, is_last); - // producer_->setSocketOption(ProducerCallbacksOptions::CACHE_MISS, - // [this](ProducerSocket &p, const - // core::Interest &interest){ - // producer_->stop(); - // }); - } -} - -template <typename Handler> -void HTTPServerPublisher::asyncPublishContent( - const uint8_t *buf, size_t buffer_size, - std::chrono::milliseconds content_lifetime, Handler &&handler, - bool is_last) { - if (producer_) { - producer_->setSocketOption( - GeneralTransportOptions::CONTENT_OBJECT_EXPIRY_TIME, - static_cast<uint32_t>(content_lifetime.count())); - producer_->asyncProduce(content_name_, buf, buffer_size, - std::forward<Handler>(handler), is_last); - } -} - -void HTTPServerPublisher::serveClients() { producer_->serveForever(); } - -void HTTPServerPublisher::stop() { - std::shared_ptr<typename ProducerSocket::Portal> portal_ptr; - producer_->getSocketOption(GeneralTransportOptions::PORTAL, portal_ptr); - portal_ptr->getIoService().stop(); -} - -ProducerSocket &HTTPServerPublisher::getProducer() { return *producer_; } - -void HTTPServerPublisher::setPublisherName(std::string &name, - std::string &mask) { - // Name represents the last 64 bits of the ipv6 address. - // It is an ipv6 address with the first 64 bits set to 0 - uint16_t i; - std::string s = content_name_.toString(); - std::shared_ptr<core::Sockaddr> sockaddr = content_name_.getAddress(); - in6_addr name_ipv6 = ((core::Sockaddr6 *)sockaddr.get())->sin6_addr; - - in6_addr bitmask, new_address, _name; - - if (inet_pton(AF_INET6, mask.c_str(), &bitmask) != 1) { - throw errors::RuntimeException("Error during conversion to ipv6 address."); - } - - if (inet_pton(AF_INET6, name.c_str(), &_name) != 1) { - throw errors::RuntimeException("Error during conversion to ipv6 address."); - } - - for (i = 0; i < sizeof(new_address.s6_addr); i++) { - new_address.s6_addr[i] = name_ipv6.s6_addr[i] & bitmask.s6_addr[i]; - } - - for (i = 0; i < sizeof(new_address.s6_addr); i++) { - new_address.s6_addr[i] |= _name.s6_addr[i] & ~bitmask.s6_addr[i]; - } - - // Effectively change the name - char str[INET6_ADDRSTRLEN]; - inet_ntop(AF_INET6, &new_address, str, INET6_ADDRSTRLEN); - std::string str2(str); - - core::Name new_name(str2, 0); - - // If the new name differs from the one required by the consumer part, send a - // manifest - if (!new_name.equals(content_name_, false)) { - // Publish manifest pointing to the new name - - auto manifest = - std::make_shared<ContentObjectManifest>(content_name_.setSuffix(0)); - - content_name_ = core::Name(str2, 0); - - // manifest->setNameList(content_name_); - manifest->setLifetime(4000 * 1000); - manifest->encode(); - producer_->produce(*manifest); - - core::Prefix ns(content_name_, 128); - producer_->registerPrefix(ns); - } -} - -} // namespace http - -} // namespace transport diff --git a/libtransport/src/hicn/transport/http/server_publisher.h b/libtransport/src/hicn/transport/http/server_publisher.h deleted file mode 100644 index 33d596f63..000000000 --- a/libtransport/src/hicn/transport/http/server_publisher.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2017-2019 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: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include <hicn/transport/http/default_values.h> -#include <hicn/transport/interfaces/socket_consumer.h> -#include <hicn/transport/interfaces/socket_producer.h> - -#include <functional> -#include <vector> - -namespace transport { - -namespace http { - -using namespace interface; -using namespace core; - -class HTTPServerPublisher { - public: - HTTPServerPublisher(const core::Name &content_name); - - ~HTTPServerPublisher(); - - void publishContent(const uint8_t *buf, size_t buffer_size, - std::chrono::milliseconds content_lifetime, bool is_last); - - template <typename Handler> - void asyncPublishContent(const uint8_t *buf, size_t buffer_size, - std::chrono::milliseconds content_lifetime, - Handler &&handler, bool is_last); - - void serveClients(); - - void stop(); - - ProducerSocket &getProducer(); - - HTTPServerPublisher &setTimeout(const std::chrono::milliseconds &timeout, - bool timeout_renewal); - - HTTPServerPublisher &attachPublisher(); - - void setPublisherName(std::string &name, std::string &mask); - - private: - Name content_name_; - std::unique_ptr<asio::steady_timer> timer_; - std::unique_ptr<ProducerSocket> producer_; - ProducerInterestCallback interest_enter_callback_; - utils::UserCallback wait_callback_; -}; - -} // end namespace http - -} // end namespace transport |