-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_server.c
403 lines (340 loc) · 14.6 KB
/
http_server.c
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include "http_server.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <logger.h>
#include <base64.h>
#include "user_manager.h"
#include "picohttpparser.h"
#include "vty.h"
#include "cli_commands.h"
#include "cli_rest.h"
#include "event.h"
#include "vty_io.h"
#include "socket.h"
#define VERSION 23
#define ERROR 42
#define LOG 44
#define OK 200
#define FORBIDDEN 403
#define NOTFOUND 404
#define DENIED 401
#define OPTIONS 402
DECLARE_LOGGER(HTTPServer)
char* request_get_data(const char* request);
int request_create_command(const char* req_url, byte_buffer_t* buffer);
int header_find_value(const char* name, struct phr_header* headers, int header_size);
typedef struct http_event_context_t
{
void (*private_event_handler)(event_pump_t* pump,int, void*);
} http_event_context_t;
void http_server_create(int port, void (*on_read)(event_pump_t*,int, void*))
{
socket_create_server(port, &http_request_handle_connect_event, http_server_create_context(on_read));
}
void* http_server_create_context(void (*http_event_read_handler)(event_pump_t* pump,int, void*))
{
http_event_context_t* new_context = malloc(sizeof(http_event_context_t));
new_context->private_event_handler = http_event_read_handler;
return (void*)new_context;
}
void http_request_handle_connect_event(event_pump_t* pump, int fd, void* context)
{
http_event_context_t* context_data = (http_event_context_t*)context;
struct sockaddr remote_addr;
socklen_t addr_len;
memset(&remote_addr, 0, sizeof(struct sockaddr));
memset(&addr_len, 0, sizeof(socklen_t));
int session_sock = accept(fd, &remote_addr, &addr_len);
struct sockaddr_in* addr_in = (struct sockaddr_in*)&remote_addr;
struct in_addr ip_addr = addr_in->sin_addr;
char ip_str[INET_ADDRSTRLEN];
inet_ntop( AF_INET, &ip_addr, ip_str, INET_ADDRSTRLEN );
LOG_INFO(HTTPServer, "HTTP connection from %s", ip_str);
vty_io_data_t* vty_data = malloc(sizeof(vty_io_data_t));
vty_data->desc.socket = session_sock;
vty_t* vty_sock = vty_io_create(VTY_HTTP, vty_data);
vty_set_pump(vty_sock, pump);
vty_set_echo(vty_sock, false);
event_dispatcher_register_handler(pump, session_sock, context_data->private_event_handler, &vty_nonblock_write_event, (void*)vty_sock);
}
int http_server_read_request(http_vty_priv_t* http_priv, byte_buffer_t* buffer)
{
int j, file_fd;
long i, ret, len;
char * fstr;
http_set_response(http_priv, HTTP_RESP_OK);
int pret, minor_version;
size_t buflen = 0;
ssize_t rret;
buflen = byte_buffer_read_len(http_priv->request->buffer);
LOG_DEBUG(HTTPServer, "Request length: %d", buflen);
/* parse the request */
http_priv->request->num_headers = sizeof(http_priv->request->headers) / sizeof(http_priv->request->headers[0]);
pret = phr_parse_request(byte_buffer_get_read_ptr(http_priv->request->buffer), buflen, &http_priv->request->method, &http_priv->request->method_len, &http_priv->request->path, &http_priv->request->path_len,
&minor_version, http_priv->request->headers, &http_priv->request->num_headers, 0);
if (pret > 0)
{
// The request has been parsed, now make sure we got the entire body...
char* content_len_buf;
if(http_request_find_header_value(http_priv, "Content-Length", &content_len_buf))
{
http_priv->content_length = atoi(content_len_buf);
free(content_len_buf);
if(http_priv->content_length > 0)
{
if(buflen - pret < http_priv->content_length)
{
return 0;
}
char* request = request_get_data(byte_buffer_get_read_ptr(http_priv->request->buffer));
if(request == NULL || strlen(request) < http_priv->content_length)
{
// Read some more data...
return 0;
}
}
}
}
else if(pret == -1)
{
LOG_ERROR(HTTPServer, "Parse error");
http_set_response(http_priv, FORBIDDEN);
//http_server_prepare_response_headers(http_priv);
return 0;
}
else if(pret < 0)
{
return 0;
}
LOG_DEBUG(HTTPServer, "Request %s", byte_buffer_get_read_ptr(http_priv->request->buffer));
if(user_manager_get_count() > 0)
{
char* auth_start;
if(!http_request_find_header_value(http_priv, "Authorization", &auth_start))
{
LOG_ERROR(HTTPServer, "Access denied for request: %s", byte_buffer_get_read_ptr(http_priv->request->buffer));
http_set_response(http_priv, DENIED);
//http_server_prepare_response_headers(http_priv);
return 0;
}
else
{
char user_pass[256] = {0};
sscanf(auth_start, "Basic %s", user_pass);
char user_pass_decoded[256] = {0};
Base64decode(user_pass_decoded, user_pass);
free(auth_start);
char* user_tok = strtok(user_pass_decoded, ":");
char* pass_tok = strtok(NULL, ":");
if(!user_manager_authenticate(user_tok, pass_tok))
{
LOG_ERROR(HTTPServer, "Access denied, invalid username/password");
http_set_response(http_priv, DENIED);
//http_server_prepare_response_headers(http_priv);
return 0;
}
else
{
LOG_INFO(HTTPServer, "Authentication success for %s", user_tok);
}
}
}
if(strncmp(http_priv->request->method, "OPTIONS", http_priv->request->method_len) == 0)
{
LOG_DEBUG(HTTPServer, "Received OPTIONS request");
http_set_response(http_priv, HTTP_RESP_OK);
http_set_content_type(http_priv, "application/json");
http_set_header(http_priv, "Access-Control-Allow-Headers", "x-requested-with,content-type");
//http_server_prepare_response_headers(http_priv);
return 0;
}
// Handle POST request
if(strncmp(http_priv->request->method, "POST", http_priv->request->method_len) == 0)
{
char* content = NULL;
if(http_priv->content_length > 0)
{
content = calloc(1, http_priv->content_length+1);
strncpy(content, request_get_data(byte_buffer_get_read_ptr(http_priv->request->buffer)), http_priv->content_length);
http_priv->post_data = content;
}
}
// Browsers really like to get favicon.ico - tell them to fuck off
if(strstr(http_priv->request->path, "favicon.ico") != 0)
{
http_set_response(http_priv, NOTFOUND);
//http_server_prepare_response_headers(http_priv);
return 0;
}
char* request_body = calloc(http_priv->request->method_len + http_priv->request->path_len + 2, sizeof(char));
strncat(request_body, http_priv->request->method, http_priv->request->method_len);
strcat(request_body, " ");
strncat(request_body, http_priv->request->path, http_priv->request->path_len);
int command_len = request_create_command(request_body, buffer);
free(request_body);
return command_len;
}
void http_server_prepare_response_headers(http_vty_priv_t* http_priv, int content_length)
{
switch(http_priv->resp_code)
{
case HTTP_RESP_OK:
http_set_header(http_priv, "Access-Control-Allow-Origin", "*");
http_set_header(http_priv, "Access-Control-Allow-Methods", "GET,POST,PUT");
byte_buffer_adjust_write_pos(http_priv->response_header,
sprintf(byte_buffer_get_write_ptr(http_priv->response_header), "HTTP/1.1 200 OK\r\nServer: zae/0.1\r\n%s",
(http_priv->headers_size == 0)? "" : http_priv->headers)); /* Header + a blank line */
if(http_priv->can_cache)
{
byte_buffer_adjust_write_pos(http_priv->response_header, sprintf(byte_buffer_get_write_ptr(http_priv->response_header), "Cache-Control: public, max-age=%d\r\n", http_priv->cache_age));
}
else
{
byte_buffer_adjust_write_pos(http_priv->response_header, sprintf(byte_buffer_get_write_ptr(http_priv->response_header), "Cache-Control: no-cache\r\n"));
}
if(NULL != http_priv->content_type)
{
byte_buffer_adjust_write_pos(http_priv->response_header, sprintf(byte_buffer_get_write_ptr(http_priv->response_header), "Content-Type: %s\r\n", http_priv->content_type));
}
if(content_length > 0)
{
byte_buffer_adjust_write_pos(http_priv->response_header, sprintf(byte_buffer_get_write_ptr(http_priv->response_header), "Content-Length: %ld\r\n", content_length));
}
byte_buffer_adjust_write_pos(http_priv->response_header, sprintf(byte_buffer_get_write_ptr(http_priv->response_header), "\r\n"));
break;
case HTTP_RESP_SERVER_ERR:
byte_buffer_adjust_write_pos(http_priv->response_header, sprintf(byte_buffer_get_write_ptr(http_priv->response_header), "HTTP/1.1 500 SERVER ERROR\nServer: zae/0.1\nContent-Length: %ld\nConnection: close\nContent-Type: %s\n", content_length, http_priv->content_type)); /* Header + a blank line */
break;
case HTTP_RESP_NONE:
break;
case FORBIDDEN:
byte_buffer_adjust_write_pos(http_priv->response_header, sprintf(byte_buffer_get_write_ptr(http_priv->response_header), "HTTP/1.1 403 Forbidden\nContent-Length: 185\nConnection: close\nContent-Type: text/html\n\n<html><head>\n<title>403 Forbidden</title>\n</head><body>\n<h1>Forbidden</h1>\nThe requested URL, file type or operation is not allowed on this simple static file webserver.\n</body></html>\n"));
break;
case NOTFOUND:
byte_buffer_adjust_write_pos(http_priv->response_header, sprintf(byte_buffer_get_write_ptr(http_priv->response_header), "HTTP/1.1 404 Not Found\nContent-Length: 136\nConnection: close\nContent-Type: text/html\n\n<html><head>\n<title>404 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\nThe requested URL was not found on this server.\n</body></html>\n"));
break;
case DENIED:
byte_buffer_adjust_write_pos(http_priv->response_header, sprintf(byte_buffer_get_write_ptr(http_priv->response_header), "HTTP/1.1 401 Access denied\nWWW-Authenticate: Basic realm=\"ZWAVE Automation Server\"\nContent-Length: 0\n"));
break;
case HTTP_RESP_USER_ERR:
default:
byte_buffer_adjust_write_pos(http_priv->response_header, sprintf(byte_buffer_get_write_ptr(http_priv->response_header), "HTTP/1.1 400 BAD REQUEST\nServer: zae/0.1\nContent-Length: %ld\nConnection: close\nContent-Type: %s\n", content_length, http_priv->content_type)); /* Header + a blank line */
break;
}
LOG_DEBUG(HTTPServer, "Response header size %d: %s", byte_buffer_read_len(http_priv->response_header), byte_buffer_get_read_ptr(http_priv->response_header));
}
void http_set_response(http_vty_priv_t* http_priv, int http_resp)
{
http_priv->resp_code = http_resp;
}
void http_set_content_type(http_vty_priv_t* http_priv, const char* content_type)
{
if(NULL != http_priv->content_type)
{
free(http_priv->content_type);
http_priv->content_type = NULL;
}
if(NULL != content_type)
{
http_priv->content_type = strdup(content_type);
}
}
void http_set_cache_control(http_vty_priv_t* http_priv, bool is_set, int max_age)
{
http_priv->can_cache = is_set;
http_priv->cache_age = max_age;
}
void http_set_header(http_vty_priv_t* http_priv, const char* name, const char* value)
{
int header_size = strlen(name) + strlen(value) + 5;
http_priv->headers = realloc(http_priv->headers, http_priv->headers_size + header_size);
sprintf(http_priv->headers + http_priv->headers_size, "%s: %s\r\n", name, value);
http_priv->headers_size += header_size;
http_priv->headers[http_priv->headers_size-1] = 0;
http_priv->headers_size--;
}
int request_create_command(const char* req_url, byte_buffer_t* buffer)
{
//char* command = strdup(req_url);
char command_buffer[2048] = {0};
strcpy(command_buffer, req_url);
for(int i = 0; i < strlen(req_url); i++)
{
if(req_url[i] == '/')
{
command_buffer[i] = ' ';
}
}
// expression spaces will be encoded as %20 - we need to revert them back to ' '
// we will try to do in-place conversion...
int body_len = strlen(command_buffer);
int string_index = 0;
for(int i = 0; i < body_len; i++,string_index++)
{
if(i+2 < body_len)
{
if(command_buffer[i] == '%' && command_buffer[i+1] == '2' && command_buffer[i+2] == '0')
{
// forward i to past-%20 place
i += 2;
command_buffer[i] = ' ';
}
}
if(i != string_index)
{
command_buffer[string_index] = command_buffer[i];
}
}
command_buffer[string_index] = '\0';
byte_buffer_append(buffer, command_buffer, strlen(command_buffer));
return byte_buffer_read_len(buffer);
}
char* request_get_data(const char* request)
{
char* start_data = strstr(request, "\r\n\r\n");
if(NULL == start_data)
{
return NULL;
}
return start_data + 4;
}
int http_request_find_header_value_index(http_vty_priv_t* http_priv, const char* name)
{
for (int i = 0; i < http_priv->request->num_headers; i++)
{
if(strncasecmp(http_priv->request->headers[i].name, name, http_priv->request->headers[i].name_len) == 0)
{
return i;
}
}
return -1;
}
bool http_request_find_header_value_by_index(http_vty_priv_t* http_priv, int index, char** value)
{
if(index >= 0)
{
*value = calloc(http_priv->request->headers[index].value_len+1, sizeof(char));
strncpy(*value, http_priv->request->headers[index].value, http_priv->request->headers[index].value_len);
return true;
}
return false;
}
bool http_request_find_header_value(http_vty_priv_t* http_priv, const char* name, char** value)
{
return http_request_find_header_value_by_index(http_priv,
http_request_find_header_value_index(http_priv, name),
value);
}
char* http_response_get_post_data(vty_t* vty)
{
return ((http_vty_priv_t*)vty->priv)->post_data;
}