-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttpd.c
516 lines (439 loc) · 14.6 KB
/
httpd.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/* httpd.c
*
* Copyright (C) 2018 < Daniel Finneran, [email protected] >
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the GPL license. See the LICENSE file for details.
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include <signal.h>
#include <time.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stddef.h>
#include "httpd.h"
#include "utils.h"
#include "log.h"
// Function Prototypes
int receive(int socket);
size_t tcpPort;
char *contentType;
struct sockaddr_in address;
struct sockaddr_storage connector;
int current_socket;
int connecting_socket;
socklen_t addr_size;
httpResponse *response;
char *postData;
char *(*postCallback)(httpRequest *);
char *(*getCallback)(httpRequest *);
/*****************************************************************/
/* Functions Start */
/*****************************************************************/
/* Call back functions, allow the http server to send the raw text back to other libraries or code to process
*
*/
void SetPostFunction( char *(*postCallbackFunction)(httpRequest *))
{
postCallback = postCallbackFunction;
}
/* Call back functions, allow the http server to send the raw text back to other libraries or code to process
*
*/
void SetGetFunction( char *(*getCallbackFunction)(httpRequest *))
{
getCallback = getCallbackFunction;
}
void setPort(size_t port)
{
tcpPort = port;
}
void setContentType(char *type)
{
if (type) {
contentType = type;
}
}
void createINETSocket()
{
current_socket = socket(AF_INET, SOCK_STREAM, 0);
if ( current_socket == -1 )
{
perror("Create socket");
exit(-1);
}
}
void bindToINETSocketWithPort()
{
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(tcpPort);
if ( bind(current_socket, (struct sockaddr *)&address, sizeof(address)) < 0 )
{
perror("Bind to port");
exit(-1);
}
}
void startListener()
{
if ( listen(current_socket, SOMAXCONN) < 0 )
{
perror("Listen on port");
exit(-1);
}
}
void handle(int socket)
{
if (receive((int)socket) < 0)
{
perror("Receive");
exit(-1);
}
}
void acceptConnection()
{
addr_size = sizeof(connector);
connecting_socket = accept(current_socket, NULL,NULL);
if ( connecting_socket < 0 )
{
perror("Accepting sockets");
exit(-1);
}
handle(connecting_socket);
close(connecting_socket);
}
/* These functions will handle the steps of creating the sockets, binding to them
* and then listening to any traffic that is sent through them.
*
*
*/
void parseRequestLine(httpRequest *request)
{
if (request && request->requestLine) {
char *parse = strdup(request->requestLine);
request->method = strtok(parse, " ");
request->URI = strtok(NULL , " ");
request->HTTPVersion = strtok(NULL, " ");
}
}
char *returnNewRequestLine(char *rawData)
{
char *headerPointer = strstr(rawData, "\r\n");
ptrdiff_t requestSize = (void *)headerPointer - (void *)rawData;
char *requestLine = malloc(requestSize);
strncpy(requestLine, rawData, requestSize);
return requestLine;
}
char *returnNewMessageBody(char *rawData)
{
char *headerDelimiter = "\r\n\r\n";
return strdup((strstr(rawData,headerDelimiter)+strlen(headerDelimiter)));
}
char *returnNewHeaders(char *rawData, httpRequest *response)
{
// This returns a new copy of the headers, will need freeing seperately
char *headerStart = strstr(rawData, "\r\n");
char *headersEnd = strstr(rawData,"\r\n\r\n");
ptrdiff_t requestSize = (void *)headerStart - (void *)rawData;
ptrdiff_t headersSize = (void *)headersEnd - (void *)headerStart;
char *newHeaders = malloc(headersSize);
// Pointer arithmatict to move from start of raw data + request line + CRLF
strncpy(newHeaders, rawData+requestSize+2, headersSize-2);
return newHeaders;
}
httpRequest *processHttpRequest(char *rawData)
{
httpRequest *request = malloc(sizeof(httpRequest));
request->messageBody = returnNewMessageBody(rawData);
request->headers = returnNewHeaders(rawData, request);
request->requestLine = returnNewRequestLine(rawData);
parseRequestLine(request);
return request;
}
int freeRequest(httpRequest *request)
{
//TODO
return EXIT_FAILURE;
}
char *dataForHeader(char *headerKey, httpRequest *request)
{
char *headers = strdup(request->headers);
char *headers_search = headers;
headers_search = strtok(headers, "\r\n");
while (headers_search != NULL) {
headers_search = strtok(NULL, "\r\n");
if (headers_search) {
if (strstr(headers_search, headerKey)) {
const char seperator = ' ';
char *found = strchr(headers_search, seperator);
return strdup(found);
}
}
}
return 0;
}
/* These functions will deal with the HTTPD responses
*
*
*/
int setHTTPResponse(char *messageBody, int responseCode)
{
if (messageBody) {
response->messageBody = messageBody;
response->messageLength = strlen(messageBody);
} else {
response->messageLength = 0;
}
response->responseCode = responseCode;
return EXIT_SUCCESS;
}
size_t sendString(char *message, int socket)
{
size_t length, bytes_sent;
length = strlen(message);
bytes_sent = send(socket, message, length, 0);
return bytes_sent;
}
size_t sendBinary(int *byte, int length)
{
size_t bytes_sent;
bytes_sent = send(connecting_socket, byte, length, 0);
return bytes_sent;
}
void sendHeader(char *Status_code, char *Content_Type, size_t TotalSize, int socket)
{
// If no content type is set then just do a catch all
if (!Content_Type) {
Content_Type = "*/*";
}
char *head = "HTTP/1.1 ";
char *content_head = "\r\nContent-Type: ";
char *server_head = "\r\nServer: urchin";
char *length_head = "\r\nContent-Length: ";
char *date_head = "\r\nDate: ";
char *newline = "\r\n";
char contentLength[100];
time_t rawtime;
time ( &rawtime );
// int contentLength = strlen(HTML);
sprintf(contentLength, "%zu", TotalSize);
char *message = malloc((
strlen(head) +
strlen(content_head) +
strlen(server_head) +
strlen(length_head) +
strlen(date_head) +
strlen(newline) +
strlen(Status_code) +
strlen(Content_Type) +
strlen(contentLength) +
28 +
sizeof(char)) * 2);
if ( message != NULL )
{
strcpy(message, head);
strcat(message, Status_code);
strcat(message, content_head);
strcat(message, Content_Type);
strcat(message, server_head);
strcat(message, length_head);
strcat(message, contentLength);
strcat(message, date_head);
strcat(message, (char*)ctime(&rawtime));
strcat(message, newline);
sendString(message, socket);
free(message);
}
}
void sendHTML(char *statusCode, char *contentType, char *content, int size, int socket)
{
sendHeader(statusCode, contentType, size, socket);
sendString(content, socket);
}
int handleHttpGET(char *input)
{
if (postData) {
} else {
sendHeader("200 OK", contentType,0, connecting_socket);
sendString("<html>Not Storing Data</html>\n", connecting_socket);
}
return -1;
}
int respond()
{
/* Check reponse is allocated then
* work through the response that the callback should have populated
* free up the allocated memory before finishing
*
* This isn't "FULLY" JSON-RPC compliant yet -> https://www.simple-is-better.org/json-rpc/transport_http.html
*/
if (response) {
switch (response->responseCode) {
case 200:
sendHeader("200 OK", contentType, response->messageLength, connecting_socket);
break;
case 202:
sendHeader("202 Accepted", contentType, response->messageLength, connecting_socket);
break;
case 204:
sendHeader("204 No Response", contentType, response->messageLength, connecting_socket);
break;
case 405:
sendHeader("405 Method Not Allowed", contentType, response->messageLength, connecting_socket);
break;
case 415:
sendHeader("415 Unsupported Media Type", contentType, response->messageLength, connecting_socket);
break;
default:
sendString("HTTP/1.1 500 Error\r\n\r\n", connecting_socket);
}
/* If the message length is more that 0 and that the messageBody isn't NULL
* then send it back to the client and de-allocate the messagebody memory
* finally de-allocate the reponse memory
*/
if (( response->messageLength > 0) && response->messageBody) {
sendString(response->messageBody, connecting_socket);
free(response->messageBody);
free(response);
}
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
int receive(int socket)
{
size_t msgLen = 0;
char buffer[BUFFER_SIZE];
memset (buffer,'\0', BUFFER_SIZE);
if ((msgLen = recv(socket, buffer, BUFFER_SIZE, 0)) <= 0)
{
printf("Error handling incoming request");
return -1;
}
/* Read through the first data from the client,
* identify the HTTPD request and the method
* check headers for more data and process accordingly
* hand off to correct function
*/
httpRequest *request = processHttpRequest(buffer);
if ( stringMatch("GET", request->method) ) // GET
{
if (getCallback != NULL) {
char *getData = getCallback(request);
if (getData == NULL) {
sendString("HTTP/1.0 500 Error\r\n\r\n", connecting_socket);
logFatal("Error parsing GET\n");
}
if (strlen(getData) == 0) {
logError("Response was zero bytes");
sendString("HTTP/1.0 500 Error\r\n\r\n", connecting_socket);
} else {
sendHeader("200 OK", contentType, strlen(getData), connecting_socket);
sendString(getData,connecting_socket);
free(getData);
}
}
//
// if (postData) {
// sendHeader("200 OK", contentType, strlen(postData), connecting_socket);
// sendString(postData, connecting_socket);
// }
return 0;
}
else if ( stringMatch("HEAD", request->method) ) // HEAD
{
// SendHeader();
}
else if ( stringMatch("POST", request->method) ) // POST
{
/* Process the data, if the headers reveal that their is a Expect: 100-continue
* header then additional processing is required in order to handle the recv
* We will need to get the numerical value from the header contentLength, which
* should exist as part of the RFC https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4
* more details here -> http://stackoverflow.com/questions/2773396/whats-the-content-length-field-in-http-header
* If it doesnt, exit. If it does then we recv the correct amount of bytes and process them accordingly
*/
char *expectContinue = dataForHeader("Expect:", request);
if (expectContinue) {
char *contentLength = dataForHeader("Content-Length:", request);
if (contentLength) {
char *endPointer;
long messageSize = strtol(contentLength, &endPointer, 10);
if (messageSize > BUFFER_SIZE) {
sendString("HTTP/1.0 500 Error\r\n\r\n", connecting_socket);
return 1;
}
sendString("HTTP/1.0 100 Continue\r\n\r\n", connecting_socket);
memset (buffer,'\0', BUFFER_SIZE);
long messageBodySizeCounter = 0;
char *buffer_location = buffer;
while (messageBodySizeCounter < messageSize) {
if ((msgLen = recv(socket, buffer_location, BUFFER_SIZE, 0)) == -1)
{
sendString("HTTP/1.0 500 Error\r\n\r\n", connecting_socket);
return 1;
}
buffer_location += msgLen;
messageBodySizeCounter += msgLen;
if (messageBodySizeCounter > messageSize) {
/* Something has gone very wrong we have more data than expected
* Return an error to the client
*/
sendString("HTTP/1.0 500 Error\r\n\r\n", connecting_socket);
return 1;
}
}
}
// Only wipe over the messageBody if we've had the expect header
request->messageBody = buffer;
}
/* Allocate required memory for reponse
* post the data as a callback to a handling function
* respond accordingly to the client
*/
if (postData) {
free(postData);
}
response = malloc(sizeof(httpResponse));
postData = strdup(request->messageBody);
setHTTPResponse("", 200);
respond();
return 1;
}
else // Not a handled HTTPD request (GET/POST)
{
sendString("HTTP/1.0 400 Bad Request\r\n\r\n", connecting_socket);
}
return 1;
}
/*****************************************************************************/
void handleInterrupt(int s){
//printf("Caught signal %d\n",(int)s);
printf("\nExiting Urchin\n");
close(current_socket);
}
void setHTTPDInterruptHandler()
{
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = handleInterrupt;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
}
void startListenLoop()
{
// Enable the interupt handler, so that a ctrl-c will tidy up the socket
setHTTPDInterruptHandler();
while (1) {
acceptConnection();
}
}