Async 1.8.0
AsyncHttpServerConnection.h
Go to the documentation of this file.
1
31#ifndef ASYNC_HTTP_SERVER_CONNECTION_INCLUDED
32#define ASYNC_HTTP_SERVER_CONNECTION_INCLUDED
33
34
35/****************************************************************************
36 *
37 * System Includes
38 *
39 ****************************************************************************/
40
41#include <stdint.h>
42#include <vector>
43#include <deque>
44#include <cstring>
45#include <map>
46#include <string>
47#include <sstream>
48
49
50/****************************************************************************
51 *
52 * Project Includes
53 *
54 ****************************************************************************/
55
56#include <AsyncTcpConnection.h>
57
58
59/****************************************************************************
60 *
61 * Local Includes
62 *
63 ****************************************************************************/
64
65
66
67/****************************************************************************
68 *
69 * Forward declarations
70 *
71 ****************************************************************************/
72
73
74
75/****************************************************************************
76 *
77 * Namespace
78 *
79 ****************************************************************************/
80
81namespace Async
82{
83
84
85/****************************************************************************
86 *
87 * Forward declarations of classes inside of the declared namespace
88 *
89 ****************************************************************************/
90
91
92
93/****************************************************************************
94 *
95 * Defines & typedefs
96 *
97 ****************************************************************************/
98
99
100
101/****************************************************************************
102 *
103 * Exported Global Variables
104 *
105 ****************************************************************************/
106
107
108
109/****************************************************************************
110 *
111 * Class definitions
112 *
113 ****************************************************************************/
114
130{
131 public:
132 typedef std::map<std::string, std::string> Headers;
133 struct Request
134 {
135 std::string method;
136 std::string target;
137 unsigned ver_major;
138 unsigned ver_minor;
140
142 {
143 clear();
144 }
145
146 void clear(void)
147 {
148 method.clear();
149 target.clear();
150 headers.clear();
151 ver_major = 0;
152 ver_minor = 0;
153 headers.clear();
154 }
155
157 {
158 method = std::move(other.method);
159 target = std::move(other.target);
160 ver_major = other.ver_major;
161 ver_minor = other.ver_minor;
162 headers = std::move(other.headers);
163 other.clear();
164 return *this;
165 }
166 };
167
169 {
170 public:
171 Response(void) : m_code(0), m_send_content(false) {}
172
173 unsigned code(void) const { return m_code; }
174 void setCode(unsigned code) { m_code = code; }
175
176 const Headers& headers(void) const { return m_headers; }
177 template <typename T>
178 void setHeader(const std::string& key, T value)
179 {
180 std::ostringstream os;
181 os << value;
182 m_headers[key] = os.str();
183 }
184
185 const std::string& content(void) const { return m_content; }
186 void setContent(const std::string& content_type,
187 const std::string& content)
188 {
189 m_content = content;
190 setHeader("Content-type", content_type);
191 setHeader("Content-length", m_content.size());
192 setSendContent(true);
193 }
194 bool sendContent(void) const { return m_send_content; }
195 void setSendContent(bool send_content)
196 {
197 m_send_content = send_content;
198 }
199
200 void clear(void)
201 {
202 m_code = 0;
203 m_headers.clear();
204 m_content.clear();
205 }
206
207 private:
208 unsigned m_code;
209 Headers m_headers;
210 std::string m_content;
211 bool m_send_content;
212 };
213
218 explicit HttpServerConnection(size_t recv_buf_len = DEFAULT_RECV_BUF_LEN);
219
227 HttpServerConnection(int sock, const IpAddress& remote_addr,
228 uint16_t remote_port,
229 size_t recv_buf_len = DEFAULT_RECV_BUF_LEN);
230
235
245 virtual TcpConnection& operator=(TcpConnection&& other_base) override;
246
254 void setChunked(void) { m_chunked = true; }
255
261 virtual bool write(const Response& res);
262
273 virtual bool write(const char* buf, int len);
274
280 sigc::signal<void, HttpServerConnection *, DisconnectReason> disconnected;
281
290 sigc::signal<void, HttpServerConnection *, Request&> requestReceived;
291
292 protected:
293 sigc::signal<void, bool> sendBufferFull;
294
301 virtual void closeConnection(void) override;
302
309 virtual void onDisconnected(DisconnectReason reason) override;
310
324 virtual int onDataReceived(void *buf, int count) override;
325
330 virtual void emitDisconnected(DisconnectReason reason) override
331 {
332 disconnected(this, reason);
334 }
335
336 private:
337 enum State {
338 STATE_DISCONNECTED, STATE_EXPECT_START_LINE, STATE_EXPECT_HEADER,
339 STATE_EXPECT_PAYLOAD, STATE_REQ_COMPLETE
340 };
341
342 State m_state;
343 std::string m_row;
344 Request m_req;
345 bool m_chunked;
346
350 void handleStartLine(void);
351 void handleHeader(void);
352 //void onSendBufferFull(bool is_full);
353 void disconnectCleanup(void);
354 const char* codeToString(unsigned code);
355
356}; /* class HttpServerConnection */
357
358
359} /* namespace */
360
361#endif /* ASYNC_HTTP_SERVER_CONNECTION_INCLUDED */
362
363
364
365/*
366 * This file has not been truncated
367 */
Contains a class for handling exiting TCP connections.
void setContent(const std::string &content_type, const std::string &content)
void setHeader(const std::string &key, T value)
A class representing a HTTP server side connection.
HttpServerConnection(int sock, const IpAddress &remote_addr, uint16_t remote_port, size_t recv_buf_len=DEFAULT_RECV_BUF_LEN)
Constructor.
virtual TcpConnection & operator=(TcpConnection &&other_base) override
Move assignmnt operator.
virtual bool write(const char *buf, int len)
Write data to the socket.
virtual void closeConnection(void) override
Disconnect from the remote peer.
sigc::signal< void, HttpServerConnection *, Request & > requestReceived
A signal that is emitted when a HTTP request has been received on the connection.
HttpServerConnection(size_t recv_buf_len=DEFAULT_RECV_BUF_LEN)
Constructor.
virtual ~HttpServerConnection(void)
Destructor.
void setChunked(void)
Send data with chunked transfer encoding.
virtual void emitDisconnected(DisconnectReason reason) override
Emit the disconnected signal.
virtual bool write(const Response &res)
Send a HTTP response.
virtual int onDataReceived(void *buf, int count) override
Called when data has been received on the connection.
std::map< std::string, std::string > Headers
virtual void onDisconnected(DisconnectReason reason) override
Called when a connection has been terminated.
sigc::signal< void, bool > sendBufferFull
sigc::signal< void, HttpServerConnection *, DisconnectReason > disconnected
A signal that is emitted when a connection has been terminated.
A class for representing an IP address in an OS independent way.
A class for handling exiting TCP connections.
static const int DEFAULT_RECV_BUF_LEN
The default size of the reception buffer.
DisconnectReason
Reason code for disconnects.
virtual void emitDisconnected(DisconnectReason reason)
Emit the disconnected signal.
virtual int write(const void *buf, int count)
Write data to the TCP connection.
Namespace for the asynchronous programming classes.