diff options
Diffstat (limited to 'icnet/http')
-rw-r--r-- | icnet/http/icnet_http_client_connection.cc | 31 | ||||
-rw-r--r-- | icnet/http/icnet_http_client_connection.h | 13 | ||||
-rw-r--r-- | icnet/http/icnet_http_message.h | 64 | ||||
-rw-r--r-- | icnet/http/icnet_http_request.cc | 20 | ||||
-rw-r--r-- | icnet/http/icnet_http_request.h | 25 | ||||
-rw-r--r-- | icnet/http/icnet_http_response.cc | 131 | ||||
-rw-r--r-- | icnet/http/icnet_http_response.h | 59 | ||||
-rw-r--r-- | icnet/http/icnet_http_server_acceptor.cc | 76 | ||||
-rw-r--r-- | icnet/http/icnet_http_server_acceptor.h | 10 | ||||
-rw-r--r-- | icnet/http/icnet_http_server_publisher.cc | 24 | ||||
-rw-r--r-- | icnet/http/icnet_http_server_publisher.h | 7 |
11 files changed, 350 insertions, 110 deletions
diff --git a/icnet/http/icnet_http_client_connection.cc b/icnet/http/icnet_http_client_connection.cc index f07edcc2..d3ccd812 100644 --- a/icnet/http/icnet_http_client_connection.cc +++ b/icnet/http/icnet_http_client_connection.cc @@ -28,7 +28,8 @@ namespace http { using namespace transport; HTTPClientConnection::HTTPClientConnection() - : consumer_(Name("ccnx:"), transport::TransportProtocolAlgorithms::RAAQM) { + : consumer_(Name("ccnx:"), transport::TransportProtocolAlgorithms::RAAQM), timer_(nullptr), + response_(std::make_shared<HTTPResponse>()) { consumer_.setSocketOption(ConsumerCallbacksOptions::CONTENT_OBJECT_TO_VERIFY, (ConsumerContentObjectVerificationCallback) std::bind(&HTTPClientConnection::verifyData, @@ -41,9 +42,15 @@ HTTPClientConnection::HTTPClientConnection() this, std::placeholders::_1, std::placeholders::_2)); + + std::shared_ptr<ccnx::Portal> portal; + consumer_.getSocketOption(GeneralTransportOptions::PORTAL, portal); + timer_.reset(new boost::asio::steady_timer(portal->getIoService())); } -HTTPClientConnection &HTTPClientConnection::get(std::string &url, HTTPHeaders headers, HTTPPayload payload) { +HTTPClientConnection &HTTPClientConnection::get(const std::string &url, + HTTPHeaders headers, + HTTPPayload payload) { HTTPRequest request(GET, url, headers, payload); @@ -60,7 +67,7 @@ HTTPClientConnection &HTTPClientConnection::get(std::string &url, HTTPHeaders he // Send content to producer piggybacking it through first interest (to fix) - response_.clear(); + response_->clear(); // Factor icn name @@ -78,12 +85,12 @@ HTTPClientConnection &HTTPClientConnection::get(std::string &url, HTTPHeaders he } HTTPResponse &&HTTPClientConnection::response() { - return std::move(response_); + return std::move(*response_); } void HTTPClientConnection::processPayload(transport::ConsumerSocket &c, std::vector<uint8_t> &&payload) { - response_ = std::move(payload); + *std::static_pointer_cast<std::vector<uint8_t>>(response_) = std::move(payload); } bool HTTPClientConnection::verifyData(transport::ConsumerSocket &c, const ContentObject &contentObject) { @@ -99,10 +106,10 @@ bool HTTPClientConnection::verifyData(transport::ConsumerSocket &c, const Conten void HTTPClientConnection::processLeavingInterest(transport::ConsumerSocket &c, const Interest &interest, std::string &payload) { - if (interest.getName().get(-1).toSegment() == 0) { + // if (interest.getName().get(-1).toSegment() == 0) { Interest &int2 = const_cast<Interest &>(interest); int2.setPayload((const uint8_t *) payload.data(), payload.size()); - } + // } } HTTPClientConnection& HTTPClientConnection::stop() { @@ -116,6 +123,16 @@ transport::ConsumerSocket& HTTPClientConnection::getConsumer() { return consumer_; } +void HTTPClientConnection::setTimeout(const std::chrono::seconds &timeout) { + timer_->cancel(); + timer_->expires_from_now(timeout); + timer_->async_wait([this](boost::system::error_code ec) { + if (!ec) { + consumer_.stop(); + } + }); +} + } } diff --git a/icnet/http/icnet_http_client_connection.h b/icnet/http/icnet_http_client_connection.h index 41a2a4f9..ae52aa3e 100644 --- a/icnet/http/icnet_http_client_connection.h +++ b/icnet/http/icnet_http_client_connection.h @@ -19,9 +19,11 @@ #include "icnet_transport_socket_producer.h" #include "icnet_utils_uri.h" #include "icnet_http_request.h" +#include "icnet_http_response.h" #include "icnet_http_default_values.h" #include <vector> +#include <boost/asio/steady_timer.hpp> #define HTTP_VERSION "1.0" @@ -33,7 +35,9 @@ class HTTPClientConnection { public: HTTPClientConnection(); - HTTPClientConnection &get(std::string &url, HTTPHeaders headers = {}, HTTPPayload payload = {}); + HTTPClientConnection &get(const std::string &url, + HTTPHeaders headers = {}, + HTTPPayload payload = {}); HTTPResponse &&response(); @@ -41,16 +45,19 @@ class HTTPClientConnection { transport::ConsumerSocket &getConsumer(); + void setTimeout(const std::chrono::seconds &timeout); + private: - void processPayload(transport::ConsumerSocket &c, std::vector<uint8_t> &&payload); + void processPayload(transport::ConsumerSocket &c, std::vector<uint8_t> &&response); bool verifyData(transport::ConsumerSocket &c, const transport::ContentObject &contentObject); void processLeavingInterest(transport::ConsumerSocket &c, const transport::Interest &interest, std::string &payload); - HTTPResponse response_; + std::shared_ptr<HTTPResponse> response_; transport::ConsumerSocket consumer_; + std::unique_ptr<boost::asio::steady_timer> timer_; }; } // end namespace http diff --git a/icnet/http/icnet_http_message.h b/icnet/http/icnet_http_message.h new file mode 100644 index 00000000..3d6f9827 --- /dev/null +++ b/icnet/http/icnet_http_message.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2017 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 "icnet_utils_sharable_vector.h" + +#include <sstream> +#include <vector> +#include <map> + +#define HTTP_VERSION "1.0" + +namespace icnet { + +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::vector<uint8_t> HTTPPayload; + +class HTTPMessage { + public: + + virtual ~HTTPMessage() = default; + + virtual HTTPHeaders &getHeaders() = 0; + + virtual HTTPPayload &getPayload() = 0; + + virtual std::string &getHttpVersion() = 0; + + protected: + HTTPHeaders headers_; + HTTPPayload payload_; + std::string http_version_; +}; + +} // end namespace http + +} // end namespace hicnet
\ No newline at end of file diff --git a/icnet/http/icnet_http_request.cc b/icnet/http/icnet_http_request.cc index f793e893..7710739b 100644 --- a/icnet/http/icnet_http_request.cc +++ b/icnet/http/icnet_http_request.cc @@ -20,17 +20,10 @@ namespace icnet { namespace http { -static std::map<HTTPMethod, std::string> method_map = { - {GET, "GET"}, - {POST, "POST"}, - {PUT, "PUT"}, - {PATCH, "PATCH"}, - {DELETE, "DELETE"}, -}; - -//std::map<HTTPMethod, std::string> method_map - -HTTPRequest::HTTPRequest(HTTPMethod method, std::string &url, HTTPHeaders &headers, HTTPPayload &payload) { +HTTPRequest::HTTPRequest(HTTPMethod method, + const std::string &url, + const HTTPHeaders &headers, + const HTTPPayload &payload) { utils::Uri uri; uri.parse(url); @@ -39,6 +32,7 @@ HTTPRequest::HTTPRequest(HTTPMethod method, std::string &url, HTTPHeaders &heade protocol_ = uri.getProtocol(); locator_ = uri.getLocator(); port_ = uri.getPort(); + http_version_ = HTTP_VERSION; headers_ = headers; payload_ = payload; @@ -99,6 +93,10 @@ std::string &HTTPRequest::getRequestString() { return request_string_; } +std::string &HTTPRequest::getHttpVersion() { + return http_version_; +} + } }
\ No newline at end of file diff --git a/icnet/http/icnet_http_request.h b/icnet/http/icnet_http_request.h index 985d7628..ec4a3d6f 100644 --- a/icnet/http/icnet_http_request.h +++ b/icnet/http/icnet_http_request.h @@ -15,6 +15,9 @@ #pragma once +#include "icnet_utils_sharable_vector.h" +#include "icnet_http_message.h" + #include <sstream> #include <vector> #include <map> @@ -25,23 +28,11 @@ namespace icnet { namespace http { -typedef enum { - GET, - POST, - PUT, - PATCH, - DELETE -} HTTPMethod; - -typedef std::map<std::string, std::string> HTTPHeaders; -typedef std::vector<uint8_t> HTTPPayload; -typedef std::vector<uint8_t> HTTPResponse; - -class HTTPRequest { +class HTTPRequest : public HTTPMessage { public: HTTPRequest(HTTPMethod method, - std::string &url, HTTPHeaders &headers, HTTPPayload &payload); + const std::string &url, const HTTPHeaders &headers, const HTTPPayload &payload); std::string &getQueryString(); @@ -55,9 +46,11 @@ class HTTPRequest { std::string &getRequestString(); - HTTPHeaders &getHeaders(); + HTTPHeaders &getHeaders() override; + + HTTPPayload &getPayload() override; - HTTPPayload &getPayload(); + std::string &getHttpVersion() override; private: std::string query_string_, path_, protocol_, locator_, port_; diff --git a/icnet/http/icnet_http_response.cc b/icnet/http/icnet_http_response.cc new file mode 100644 index 00000000..c9d31738 --- /dev/null +++ b/icnet/http/icnet_http_response.cc @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2017 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 "icnet_http_response.h" +#include "icnet_errors.h" + +#include <algorithm> + +#include <cstring> + +namespace icnet { + +namespace http { + +HTTPResponse::HTTPResponse() { +} + +HTTPResponse::HTTPResponse(const HTTPHeaders &headers, const HTTPPayload &payload) { + headers_ = headers; + payload_ = payload; +} + +HTTPHeaders &HTTPResponse::getHeaders() { + parse(); + return headers_; +} + +HTTPPayload &HTTPResponse::getPayload() { + parse(); + return payload_; +} + +bool HTTPResponse::parseHeaders() { + const char *crlf2 = "\r\n\r\n"; + auto it = std::search(this->begin(), this->end(), 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; + 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 false; + } + http_version_ = line.substr(separator + 1, _http_version.length() - separator - 1); + } else { + return false; + } + + std::string status_code, 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 false; + } + } + } + + return true; +} + +void HTTPResponse::parse() { + if (!parseHeaders()) { + 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)); + } + } +} + +std::string &HTTPResponse::getStatusCode() { + return status_code_; +} + +std::string &HTTPResponse::getStatusString() { + return status_string_; +} + +std::string &HTTPResponse::getHttpVersion() { + return http_version_; +} + +} + +}
\ No newline at end of file diff --git a/icnet/http/icnet_http_response.h b/icnet/http/icnet_http_response.h new file mode 100644 index 00000000..b925e1e3 --- /dev/null +++ b/icnet/http/icnet_http_response.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2017 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 "icnet_utils_sharable_vector.h" +#include "icnet_http_message.h" +#include "icnet_utils_array.h" + +#include <sstream> +#include <vector> +#include <map> + +namespace icnet { + +namespace http { + +class HTTPResponse : public HTTPMessage, public utils::SharableVector<uint8_t> { + public: + + HTTPResponse(const HTTPHeaders &headers, const HTTPPayload &payload); + + HTTPResponse(); + + HTTPHeaders &getHeaders() override; + + HTTPPayload &getPayload() override; + + std::string &getStatusCode(); + + std::string &getStatusString(); + + std::string &getHttpVersion() override; + + void parse(); + + private: + bool parseHeaders(); + + private: + std::string status_code_; + std::string status_string_; +}; + +} // end namespace http + +} // end namespace hicnet
\ No newline at end of file diff --git a/icnet/http/icnet_http_server_acceptor.cc b/icnet/http/icnet_http_server_acceptor.cc index 9406955d..424d380f 100644 --- a/icnet/http/icnet_http_server_acceptor.cc +++ b/icnet/http/icnet_http_server_acceptor.cc @@ -106,65 +106,29 @@ void HTTPServerAcceptor::processIncomingInterest(transport::ProducerSocket &p, c if (method == "GET") { HTTPRequest request(GET, url, headers, payload); - auto publisher = std::make_shared<HTTPServerPublisher>(request_name); - callback_(publisher, (uint8_t *) request.getRequestString().data(), request.getRequestString().size()); + + int request_id = utils::Hash::hash32(request.getRequestString().data(), request.getRequestString().size()); + + if (publishers_.find(request_id) != publishers_.end()) { + if (publishers_[request_id]) { + publishers_[request_id]->getProducer().onInterest(interest.getName(), interest); + return; + } + } else { + std::cout << "Request ID" << request_id << std::endl; + } + + publishers_[request_id] = std::make_shared<HTTPServerPublisher>(request_name); + callback_(publishers_[request_id], + (uint8_t *) request.getRequestString().data(), + request.getRequestString().size(), + request_id); } } -//void HTTPServerConnection::sendResponse() { -// -// std::thread t([]() { -// -// // Get the name of the HTTP method to compute -// std::string method = request_name.get(1).toString(); -// std::transform(method.begin(), method.end(), method.begin(), ::toupper); -// std::string path; -// -// // This is done for getting rid of useless name components such as ccnx: or ndn: -// if (request_name.getSegmentCount() > 2) { -// std::string rawPath = request_name.getSubName(2).toString(); -// std::size_t pos = rawPath.find("/"); -// path = rawPath.substr(pos); -// } -// -// // Create new producer -// -// // Create timer for response availability -// std::shared_ptr<core::Portal> portal; -// po->getSocketOption(icnet::GeneralTransportOptions::PORTAL, portal); -// boost::asio::io_service &ioService = portal->getIoService(); -// -// boost::asio::deadline_timer t(ioService, boost::posix_time::seconds(5)); -// -// std::function<void(const boost::system::error_code e)> -// wait_callback = [&ioService](const boost::system::error_code e) { -// if (!e) { -// // Be sure to delete the timer before the io_service, otherwise we'll get some strange behavior! -// ioService.stop(); -// } -// }; -// -// std::function<void(transport::ProducerSocket, const core::Interest &interest)> -// interest_enter_callback = [this, &wait_callback, &t] -// (transport::ProducerSocket &p, const core::Interest &interest) { -// t.cancel(); -// t.expires_from_now(boost::posix_time::seconds(5)); -// t.async_wait(wait_callback); -// }; -// -// p->setSocketOption(transport::ProducerCallbacksOptions::INTEREST_INPUT, -// (transport::ProducerInterestCallback) interest_enter_callback); -// -// t.async_wait(wait_callback); -// -// p->serveForever(); -// -// std::unique_lock<std::mutex> lock(thread_list_mtx_); -// icn_producers_.erase(request_name); -// }); -// -// t.detach(); -//} +std::map<int, std::shared_ptr<HTTPServerPublisher>>& HTTPServerAcceptor::getPublishers() { + return publishers_; +} } diff --git a/icnet/http/icnet_http_server_acceptor.h b/icnet/http/icnet_http_server_acceptor.h index 2d0b7f25..f5af47e8 100644 --- a/icnet/http/icnet_http_server_acceptor.h +++ b/icnet/http/icnet_http_server_acceptor.h @@ -31,7 +31,7 @@ namespace http { //typedef std::vector<uint8_t> HTTPResponse; typedef std::vector<uint8_t> HttpRequest; -typedef std::function<void(std::shared_ptr<HTTPServerPublisher> &, const uint8_t *, std::size_t)> OnHttpRequest; +typedef std::function<void(std::shared_ptr<HTTPServerPublisher> &, const uint8_t *, std::size_t, int request_id)> OnHttpRequest; class HTTPServerAcceptor { public: @@ -42,11 +42,7 @@ class HTTPServerAcceptor { HttpRequest &&request(); -// void asyncSendResponse(); - -// HTTPClientConnection& get(std::string &url, HTTPHeaders headers = {}, HTTPPayload payload = {}); -// -// HTTPResponse&& response(); + std::map<int, std::shared_ptr<HTTPServerPublisher>>& getPublishers(); private: @@ -55,6 +51,8 @@ class HTTPServerAcceptor { OnHttpRequest callback_; HttpRequest request_; std::shared_ptr<transport::ProducerSocket> acceptor_producer_; + + std::map<int, std::shared_ptr<HTTPServerPublisher>> publishers_; }; } // end namespace http diff --git a/icnet/http/icnet_http_server_publisher.cc b/icnet/http/icnet_http_server_publisher.cc index 82c90051..716eaa7b 100644 --- a/icnet/http/icnet_http_server_publisher.cc +++ b/icnet/http/icnet_http_server_publisher.cc @@ -36,10 +36,14 @@ HTTPServerPublisher& HTTPServerPublisher::attachPublisher() { return *this; } -HTTPServerPublisher &HTTPServerPublisher::setTimeout(uint32_t timeout) { +transport::ProducerSocket& HTTPServerPublisher::getProducer() { + return *producer_; +} + +HTTPServerPublisher &HTTPServerPublisher::setTimeout(const std::chrono::milliseconds &timeout, bool timeout_renewal) { std::shared_ptr<transport::Portal> portal; producer_->getSocketOption(transport::GeneralTransportOptions::PORTAL, portal); - timer_ = std::unique_ptr<boost::asio::deadline_timer>(new boost::asio::deadline_timer(portal->getIoService(), boost::posix_time::seconds(timeout))); + timer_ = std::unique_ptr<boost::asio::steady_timer>(new boost::asio::steady_timer(portal->getIoService(), timeout)); wait_callback_ = [this](const boost::system::error_code e) { if (!e) { @@ -47,14 +51,16 @@ HTTPServerPublisher &HTTPServerPublisher::setTimeout(uint32_t timeout) { } }; - interest_enter_callback_ = [this, timeout](transport::ProducerSocket &p, const transport::Interest &interest) { - this->timer_->cancel(); - this->timer_->expires_from_now(boost::posix_time::seconds(timeout)); - this->timer_->async_wait(wait_callback_); - }; + if (timeout_renewal) { + interest_enter_callback_ = [this, timeout](transport::ProducerSocket &p, const transport::Interest &interest) { + this->timer_->cancel(); + this->timer_->expires_from_now(timeout); + this->timer_->async_wait(wait_callback_); + }; - producer_->setSocketOption(transport::ProducerCallbacksOptions::INTEREST_INPUT, - (transport::ProducerInterestCallback) interest_enter_callback_); + producer_->setSocketOption(transport::ProducerCallbacksOptions::INTEREST_INPUT, + (transport::ProducerInterestCallback) interest_enter_callback_); + } timer_->async_wait(wait_callback_); diff --git a/icnet/http/icnet_http_server_publisher.h b/icnet/http/icnet_http_server_publisher.h index 7162f4d1..7c96ce15 100644 --- a/icnet/http/icnet_http_server_publisher.h +++ b/icnet/http/icnet_http_server_publisher.h @@ -20,6 +20,7 @@ #include "icnet_http_default_values.h" #include <vector> +#include <boost/asio/steady_timer.hpp> #include <functional> #ifdef __ANDROID__ @@ -51,16 +52,18 @@ class HTTPServerPublisher { void stop(); - HTTPServerPublisher &setTimeout(uint32_t timeout); + HTTPServerPublisher &setTimeout(const std::chrono::milliseconds &timeout, bool timeout_renewal); HTTPServerPublisher &attachPublisher(); + transport::ProducerSocket& getProducer(); + private: void processIncomingInterest(transport::ProducerSocket &p, const transport::Interest &interest); transport::Name content_name_; - std::unique_ptr<boost::asio::deadline_timer> timer_; + std::unique_ptr<boost::asio::steady_timer> timer_; std::unique_ptr<transport::ProducerSocket> producer_; transport::ProducerInterestCallback interest_enter_callback_; DeadlineTimerCallback wait_callback_; |