/src/wt/src/http/Connection.h
Line | Count | Source |
1 | | // This may look like C code, but it's really -*- C++ -*- |
2 | | /* |
3 | | * Copyright (C) 2008 Emweb bv, Herent, Belgium. |
4 | | * |
5 | | * All rights reserved. |
6 | | */ |
7 | | // |
8 | | // connection.hpp |
9 | | // ~~~~~~~~~~~~~~ |
10 | | // |
11 | | // Copyright (c) 2003-2006 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
12 | | // |
13 | | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
14 | | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
15 | | // |
16 | | |
17 | | #ifndef HTTP_CONNECTION_HPP |
18 | | #define HTTP_CONNECTION_HPP |
19 | | |
20 | | #include <Wt/AsioWrapper/asio.hpp> |
21 | | #ifdef HTTP_WITH_SSL |
22 | | #include <Wt/AsioWrapper/ssl.hpp> |
23 | | #endif |
24 | | #include <Wt/AsioWrapper/strand.hpp> |
25 | | #include <Wt/AsioWrapper/steady_timer.hpp> |
26 | | |
27 | | #include "Buffer.h" |
28 | | #include "Reply.h" |
29 | | #include "Request.h" |
30 | | #include "RequestHandler.h" |
31 | | #include "RequestParser.h" |
32 | | |
33 | | #include "Wt/WFlags.h" |
34 | | |
35 | | #include <atomic> |
36 | | |
37 | | namespace http { |
38 | | namespace server { |
39 | | |
40 | | namespace asio = Wt::AsioWrapper::asio; |
41 | | |
42 | | class ConnectionManager; |
43 | | class Server; |
44 | | |
45 | | /// Represents a single connection from a client. |
46 | | class Connection : public std::enable_shared_from_this<Connection> |
47 | | { |
48 | | public: |
49 | | /// Construct a connection with the given io_service. |
50 | | Connection(asio::io_service& io_service, Server *server, |
51 | | ConnectionManager& manager, RequestHandler& handler); |
52 | | |
53 | | Connection(const Connection& other) = delete; |
54 | | |
55 | | /// Get the socket associated with the connection. |
56 | | virtual asio::ip::tcp::socket& socket() = 0; |
57 | | |
58 | | /// Start the first asynchronous operation for the connection. |
59 | | virtual void start(); |
60 | | |
61 | | void close(); |
62 | | bool closed() const; |
63 | | |
64 | | /// Like CGI's Url scheme: http or https |
65 | | virtual const char *urlScheme() = 0; |
66 | | |
67 | | virtual ~Connection(); |
68 | | |
69 | 0 | Server *server() const { return server_; } |
70 | 0 | Wt::AsioWrapper::strand& strand() { return strand_; } |
71 | | |
72 | | /// Marks the TCP socket as transferrable. |
73 | | /// Once that handleWriteResponse() is executed, and this method has |
74 | | /// been called before. The callback will be performed, and the socket |
75 | | /// will be moved. |
76 | | void requestTcpSocketTransfer(const std::function<void(std::unique_ptr<asio::ip::tcp::socket>)>& callback); |
77 | | |
78 | | #ifdef HTTP_WITH_SSL |
79 | | /// Marks the SSL socket as transferrable. |
80 | | /// Once that handleWriteResponse() is executed, and this method has |
81 | | /// been called before. The callback will be performed, and the socket |
82 | | /// will be moved. |
83 | | void requestSslSocketTransfer(const std::function<void(std::unique_ptr<asio::ssl::stream<asio::ip::tcp::socket>>)>& callback); |
84 | | #endif |
85 | | |
86 | | /// Stop all asynchronous operations associated with the connection. |
87 | | void scheduleStop(); |
88 | | |
89 | | #ifdef HTTP_WITH_SSL |
90 | | void registerSslHandle(SSL *ssl) { request_.ssl = ssl; } |
91 | | #endif |
92 | | |
93 | 0 | bool waitingResponse() const { return waitingResponse_; } |
94 | 0 | void setHaveResponse() { haveResponse_ = true; } |
95 | 0 | void setResponseDone() { responseDone_ = true; } |
96 | | void startWriteResponse(ReplyPtr reply); |
97 | | |
98 | | void handleReadBody(ReplyPtr reply); |
99 | | void readMore(ReplyPtr reply, int timeout); |
100 | | bool readAvailable(); |
101 | | |
102 | | // NOTE: detectDisconnect will only register one callback at a time, |
103 | | // further calls to detectDisconnect are ignored |
104 | | void detectDisconnect(ReplyPtr reply, |
105 | | const std::function<void()>& callback); |
106 | | void asyncDetectDisconnect(ReplyPtr reply, |
107 | | const std::function<void()>& callback); |
108 | | |
109 | | protected: |
110 | | /// Get the native handle of the socket |
111 | | asio::ip::tcp::socket::native_handle_type native(); |
112 | | |
113 | | void handleWriteResponse0(ReplyPtr reply, |
114 | | const Wt::AsioWrapper::error_code& e, |
115 | | std::size_t bytes_transferred); |
116 | | void handleWriteResponse(ReplyPtr reply); |
117 | | void handleReadRequest(const Wt::AsioWrapper::error_code& e, |
118 | | std::size_t bytes_transferred); |
119 | | /// Process read buffer, reading request. |
120 | | void handleReadRequest0(); |
121 | | void handleReadBody0(ReplyPtr reply, |
122 | | const Wt::AsioWrapper::error_code& e, |
123 | | std::size_t bytes_transferred); |
124 | | |
125 | | void setReadTimeout(int seconds); |
126 | | void setWriteTimeout(int seconds); |
127 | | |
128 | | /// The manager for this connection. |
129 | | ConnectionManager& ConnectionManager_; |
130 | | |
131 | | Wt::AsioWrapper::strand strand_; |
132 | | |
133 | | void finishReply(); |
134 | | |
135 | | enum State { |
136 | | Idle = 0x0, |
137 | | Reading = 0x1, |
138 | | Writing = 0x2 |
139 | | }; |
140 | | |
141 | | Wt::WFlags<State> state_; |
142 | | |
143 | | virtual void stop(); |
144 | | |
145 | | /// Indicates that the socket of this connection is to be moved away |
146 | | /// from the connection. If set to true, the socket's ownership will be |
147 | | /// transferred using a callback, when the connection finished writing |
148 | | /// the HTTP response. |
149 | | bool socketTransferRequested_; |
150 | | |
151 | | /// Performs the callback that was set when requestTcpSocketTrasfer() |
152 | | /// or requestSslSocketTransfer() was executed. |
153 | | virtual void doSocketTransferCallback() = 0; |
154 | | |
155 | | std::function<void(std::unique_ptr<asio::ip::tcp::socket>)> tcpSocketTransferCallback_; |
156 | | #ifdef HTTP_WITH_SSL |
157 | | std::function<void(std::unique_ptr<asio::ssl::stream<asio::ip::tcp::socket>>)> sslSocketTransferCallback_; |
158 | | #endif |
159 | | |
160 | | private: |
161 | | /* |
162 | | * Asynchronoulsy reading a request |
163 | | */ |
164 | | virtual void startAsyncReadRequest(Buffer& buffer, int timeout) = 0; |
165 | | |
166 | | /* |
167 | | * Asynchronoulsy reading a request body |
168 | | */ |
169 | | virtual void startAsyncReadBody(ReplyPtr reply, Buffer& buffer, |
170 | | int timeout) = 0; |
171 | | |
172 | | /* |
173 | | * Asynchronoulsy writing a response |
174 | | */ |
175 | | virtual void startAsyncWriteResponse(ReplyPtr reply, |
176 | | const std::vector<asio::const_buffer>& buffers, |
177 | | int timeout) = 0; |
178 | | |
179 | | /// Generic I/O error handling: closes the connection and cancels timers |
180 | | void handleError(const Wt::AsioWrapper::error_code& e); |
181 | | |
182 | | void sendStockReply(Reply::status_type code); |
183 | | |
184 | | /// The handler used to process the incoming request. |
185 | | RequestHandler& request_handler_; |
186 | | |
187 | | void cancelReadTimer(); |
188 | | void cancelWriteTimer(); |
189 | | |
190 | | void timeout(const Wt::AsioWrapper::error_code& e); |
191 | | void doTimeout(); |
192 | | |
193 | | /// Timer for reading data. |
194 | | asio::steady_timer readTimer_, writeTimer_; |
195 | | |
196 | | /// Current request buffer data |
197 | | std::list<Buffer> rcv_buffers_; |
198 | | |
199 | | /// Size of last buffer and iterator for next request in last buffer |
200 | | std::size_t rcv_buffer_size_; |
201 | | char *rcv_remaining_; |
202 | | bool rcv_body_buffer_; |
203 | | |
204 | | /// The incoming request. |
205 | | Request request_; |
206 | | |
207 | | /// The parser for the incoming request. |
208 | | RequestParser request_parser_; |
209 | | |
210 | | /// Recycled reply pointers |
211 | | ReplyPtr lastWtReply_, lastProxyReply_, lastStaticReply_; |
212 | | |
213 | | /// The server that owns this connection |
214 | | Server *server_; |
215 | | |
216 | | /// Indicates that we're waiting for a response while invoking a |
217 | | /// Reply function and thus that Reply function should not start a |
218 | | /// write response but simply indicate haveResponse_ |
219 | | std::atomic_bool waitingResponse_; |
220 | | |
221 | | /// Indicates that we can send a response |
222 | | bool haveResponse_; |
223 | | |
224 | | /// Indicates that the current response is finished (after the |
225 | | /// current write operation) |
226 | | bool responseDone_; |
227 | | |
228 | | std::function<void()> disconnectCallback_; |
229 | | }; |
230 | | |
231 | | typedef std::shared_ptr<Connection> ConnectionPtr; |
232 | | |
233 | | } // namespace server |
234 | | } // namespace http |
235 | | |
236 | | #endif // HTTP_CONNECTION_HPP |