Hybrid ICN (hICN) plugin  v21.06-rc0-4-g18fa668
http_session.h
1 /*
2  * Copyright (c) 2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <hicn/transport/core/packet.h>
19 
20 #include "http_1x_message_fast_parser.h"
21 
22 #define ASIO_STANDALONE
23 #include <asio.hpp>
24 #include <deque>
25 #include <functional>
26 
27 namespace transport {
28 
29 using asio::ip::tcp;
30 
31 struct Metadata;
32 
33 typedef std::function<void(const uint8_t *data, std::size_t size, bool is_last,
34  bool headers, Metadata *metadata)>
35  ContentReceivedCallback;
36 typedef std::function<bool(asio::ip::tcp::socket &socket)> OnConnectionClosed;
37 typedef std::function<void()> ContentSentCallback;
38 typedef std::deque<
39  std::pair<std::unique_ptr<utils::MemBuf>, ContentSentCallback>>
40  BufferQueue;
41 
42 struct Metadata {
43  std::string http_version;
44  HTTPHeaders headers;
45 };
46 
48  std::string method;
49  std::string path;
50 };
51 
53  std::string status_code;
54  std::string status_string;
55 };
56 
57 class HTTPClientConnectionCallback;
58 
59 class HTTPSession {
60  friend class HTTPClientConnectionCallback;
61  static constexpr uint32_t buffer_size = 1024 * 512;
62 
63  enum class ConnectorState {
64  CLOSED,
65  CONNECTING,
66  CONNECTED,
67  };
68 
69  public:
70  HTTPSession(asio::io_service &io_service, std::string &ip_address,
71  std::string &port, ContentReceivedCallback receive_callback,
72  OnConnectionClosed on_reconnect_callback, bool client = false);
73 
74  HTTPSession(asio::ip::tcp::socket socket,
75  ContentReceivedCallback receive_callback,
76  OnConnectionClosed on_reconnect_callback, bool client = true);
77 
78  ~HTTPSession();
79 
80  void send(const uint8_t *buffer, std::size_t len,
81  ContentSentCallback &&content_sent = 0);
82 
83  void send(utils::MemBuf *buffer, ContentSentCallback &&content_sent);
84 
85  void close();
86 
87  private:
88  void doConnect();
89 
90  void doReadHeader();
91 
92  void doReadBody(std::size_t body_size, std::size_t additional_bytes);
93 
94  void doReadChunkedHeader();
95 
96  void doWrite();
97 
98  bool checkConnected();
99 
100  private:
101  void handleRead(std::error_code ec, std::size_t length);
102  void tryReconnection();
103  void startConnectionTimer();
104  void handleDeadline(const std::error_code &ec);
105 
106  asio::io_service &io_service_;
107  asio::ip::tcp::socket socket_;
108  asio::ip::tcp::resolver resolver_;
109  asio::ip::tcp::resolver::iterator endpoint_iterator_;
110  asio::steady_timer timer_;
111 
112  BufferQueue write_msgs_;
113 
114  asio::streambuf input_buffer_;
115 
116  bool reverse_;
117  bool is_reconnection_;
118  bool data_available_;
119 
120  std::size_t content_length_;
121 
122  // Chunked encoding
123  bool is_last_chunk_;
124  bool chunked_;
125 
126  ContentReceivedCallback receive_callback_;
127  OnConnectionClosed on_connection_closed_callback_;
128 
129  // HTTP headers
130  std::unique_ptr<Metadata> header_info_;
131 
132  // Connector state
133  ConnectorState state_;
134 };
135 
136 } // namespace transport
transport::Metadata
Definition: http_session.h:42
transport
Definition: forwarder_config.h:32
transport::HTTPSession
Definition: http_session.h:59
transport::RequestMetadata
Definition: http_session.h:47
transport::ResponseMetadata
Definition: http_session.h:52
utils::MemBuf
Definition: membuf.h:45