-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathHttpHandler.h
186 lines (157 loc) · 4.78 KB
/
HttpHandler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#ifndef HV_HTTP_HANDLER_H_
#define HV_HTTP_HANDLER_H_
#include "HttpService.h"
#include "HttpParser.h"
#include "FileCache.h"
#include "WebSocketServer.h"
#include "WebSocketParser.h"
class HttpHandler {
public:
enum ProtocolType {
UNKNOWN,
HTTP_V1,
HTTP_V2,
WEBSOCKET,
} protocol;
enum State {
WANT_RECV,
HANDLE_BEGIN,
HANDLE_CONTINUE,
HANDLE_END,
WANT_SEND,
SEND_HEADER,
SEND_BODY,
SEND_DONE,
WANT_CLOSE,
} state;
// errno
int error;
// flags
unsigned ssl :1;
unsigned keepalive :1;
unsigned upgrade :1;
unsigned proxy :1;
unsigned proxy_connected :1;
unsigned forward_proxy :1;
unsigned reverse_proxy :1;
// peeraddr
char ip[64];
int port;
// for log
long pid;
long tid;
// for http
hio_t *io;
HttpService *service;
HttpRequestPtr req;
HttpResponsePtr resp;
HttpResponseWriterPtr writer;
HttpParserPtr parser;
HttpContextPtr ctx;
http_handler* api_handler;
// for GetSendData
std::string header;
// std::string body;
// for websocket
WebSocketService* ws_service;
WebSocketChannelPtr ws_channel;
WebSocketParserPtr ws_parser;
uint64_t last_send_ping_time;
uint64_t last_recv_pong_time;
// for sendfile
FileCache *files;
file_cache_ptr fc; // cache small file
struct LargeFile : public HFile {
HBuf buf;
uint64_t timer;
} *file; // for large file
// for proxy
std::string proxy_host;
int proxy_port;
HttpHandler(hio_t* io = NULL);
~HttpHandler();
bool Init(int http_version = 1);
void Reset();
void Close();
/* @workflow:
* HttpServer::on_recv -> HttpHandler::FeedRecvData -> Init -> HttpParser::InitRequest -> HttpRequest::http_cb ->
* onHeadersComplete -> proxy ? handleProxy -> connectProxy :
* onMessageComplete -> upgrade ? handleUpgrade : HandleHttpRequest -> HttpParser::SubmitResponse ->
* SendHttpResponse -> while(GetSendData) hio_write ->
* keepalive ? Reset : Close -> hio_close
*
* @return
* == len: ok
* == 0: WANT_CLOSE
* < 0: error
*/
int FeedRecvData(const char* data, size_t len);
/* @workflow:
* preprocessor -> middleware -> processor -> postprocessor
*
* @return status_code
* == 0: HANDLE_CONTINUE
* != 0: HANDLE_END
*/
int HandleHttpRequest();
int GetSendData(char** data, size_t* len);
int SendHttpResponse(bool submit = true);
int SendHttpStatusResponse(http_status status_code);
// HTTP2
bool SwitchHTTP2();
// websocket
bool SwitchWebSocket();
void WebSocketOnOpen() {
ws_channel->status = hv::SocketChannel::CONNECTED;
if (ws_service && ws_service->onopen) {
ws_service->onopen(ws_channel, req);
}
}
void WebSocketOnClose() {
ws_channel->status = hv::SocketChannel::DISCONNECTED;
if (ws_service && ws_service->onclose) {
ws_service->onclose(ws_channel);
}
}
int SetError(int error_code, http_status status_code = HTTP_STATUS_BAD_REQUEST) {
error = error_code;
if (resp) resp->status_code = status_code;
return error;
}
private:
const HttpContextPtr& context();
int handleRequestHeaders();
// Expect: 100-continue
void handleExpect100();
void addResponseHeaders();
// http_cb
void onHeadersComplete();
void onBody(const char* data, size_t size);
void onMessageComplete();
// default handlers
int defaultRequestHandler();
int defaultStaticHandler();
int defaultLargeFileHandler();
int defaultErrorHandler();
int customHttpHandler(const http_handler& handler);
int invokeHttpHandler(const http_handler* handler);
// sendfile
int openFile(const char* filepath);
int sendFile();
void closeFile();
bool isFileOpened();
// upgrade
int handleUpgrade(const char* upgrade_protocol);
int upgradeWebSocket();
int upgradeHTTP2();
// proxy
int handleProxy();
int handleForwardProxy();
int handleReverseProxy();
int connectProxy(const std::string& url);
int closeProxy();
int sendProxyRequest();
static void onProxyConnect(hio_t* upstream_io);
static void onProxyClose(hio_t* upstream_io);
};
#endif // HV_HTTP_HANDLER_H_