-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_response.c
433 lines (404 loc) · 15.5 KB
/
http_response.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
/**MIT License
Copyright (c) 2018 Vysakh P Pillai
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**/
#include "http_response.h"
#include "http_common.h"
#include "http_config.h"
#include <stdio.h>
#include <string.h>
http_response_fileType_t http_response_getFileType(char *requestPath)
{
char *fileType;
fileType = strrchr(requestPath, '.');
if (0 == fileType)
{
PRINT_ERROR("File type unknown(%s)\r\n", requestPath);
return HTTP_fileType_unknown;
}
fileType += 1;
if (0 == strcmp(fileType, "shtml"))
return HTTP_fileType_SHTML;
else if (0 == strcmp(fileType, "shtm"))
return HTTP_fileType_SHTM;
else if (0 == strcmp(fileType, "ssi"))
return HTTP_fileType_SSI;
else if (0 == strcmp(fileType, "xml"))
return HTTP_fileType_XML;
else if (0 == strcmp(fileType, "cgi"))
return HTTP_fileType_CGI;
else if (0 == strcmp(fileType, "sh"))
return HTTP_fileType_SH;
else if (0 == strcmp(fileType, "exe"))
return HTTP_fileType_EXE;
else if (0 == strcmp(fileType, "html"))
return HTTP_fileType_HTML;
else if (0 == strcmp(fileType, "htm"))
return HTTP_fileType_HTM;
else if (0 == strcmp(fileType, "txt"))
return HTTP_fileType_TXT;
else if (0 == strcmp(fileType, "css"))
return HTTP_fileType_CSS;
else if (0 == strcmp(fileType, "js"))
return HTTP_fileType_JS;
else if (0 == strcmp(fileType, "json"))
return HTTP_fileType_JSON;
else if (0 == strcmp(fileType, "jpeg"))
return HTTP_fileType_JPEG;
else if (0 == strcmp(fileType, "jpg"))
return HTTP_fileType_JPEG;
else if (0 == strcmp(fileType, "png"))
return HTTP_fileType_PNG;
else if (0 == strcmp(fileType, "bin"))
return HTTP_fileType_BIN;
else if (0 == strcmp(fileType, "csv"))
return HTTP_fileType_CSV;
else if (0 == strcmp(fileType, "gif"))
return HTTP_fileType_GIF;
else if (0 == strcmp(fileType, "ico"))
return HTTP_fileType_ICO;
else if (0 == strcmp(fileType, "zip"))
return HTTP_fileType_ZIP;
else
return HTTP_fileType_unknown;
PRINT_ERROR("unhandled condition(%s)\r\n", requestPath);
return HTTP_FAILURE;
}
http_response_contenttype_t http_response_get_contentType_string(http_response_fileType_t fileType, char *buffer, unsigned int bufferLength)
{
if ((NULL == buffer) || (bufferLength <= 0))
{
PRINT_ERROR("NULL buffer or zero length(%d)\r\n", bufferLength);
return HTTP_FAILURE;
}
else
{
if ((fileType == HTTP_fileType_SHTML) || (fileType == HTTP_fileType_SHTM) ||
(fileType == HTTP_fileType_SSI) || (fileType == HTTP_fileType_XML) ||
(fileType == HTTP_fileType_SH) || (fileType == HTTP_fileType_EXE) ||
(fileType == HTTP_fileType_HTML) || (fileType == HTTP_fileType_HTM) ||
(fileType == HTTP_fileType_SHTM))
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_HTML, bufferLength);
buffer[bufferLength] = 0;
return HTTP_contentType_html;
}
else if (fileType == HTTP_fileType_TXT)
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_PLAINTEXT, bufferLength);
buffer[bufferLength] = 0;
return HTTP_contentType_plaintext;
}
else if (fileType == HTTP_fileType_CSS)
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_CSS, bufferLength);
buffer[bufferLength] = 0;
return HTTP_contentType_css;
}
else if (fileType == HTTP_fileType_JS)
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_JS, bufferLength);
buffer[bufferLength] = 0;
return HTTP_contentType_js;
}
else if (fileType == HTTP_fileType_JSON)
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_JSON, bufferLength);
buffer[bufferLength] = 0;
return HTTP_contentType_json;
}
else if (fileType == HTTP_fileType_JPEG)
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_JPEG, bufferLength);
buffer[bufferLength] = 0;
return HTTP_contentType_jpeg;
}
else if (fileType == HTTP_fileType_PNG)
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_PNG, bufferLength);
buffer[bufferLength] = 0;
return HTTP_contentType_png;
}
else if (fileType == HTTP_fileType_BIN)
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_BIN, bufferLength);
buffer[bufferLength] = 0;
return HTTP_contentType_bin;
}
else if (fileType == HTTP_fileType_CSV)
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_CSV, bufferLength);
buffer[bufferLength] = 0;
return HTTP_contentType_csv;
}
else if (fileType == HTTP_fileType_GIF)
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_GIF, bufferLength);
buffer[bufferLength] = 0;
return HTTP_contentType_gif;
}
else if (fileType == HTTP_fileType_ICO)
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_ICO, bufferLength);
buffer[bufferLength] = 0;
return HTTP_contentType_ico;
}
else if (fileType == HTTP_fileType_ZIP)
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_ZIP, bufferLength);
buffer[bufferLength] = 0;
return HTTP_contentType_zip;
}
else if (fileType == HTTP_fileType_unknown)
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_PLAINTEXT, bufferLength);
buffer[bufferLength] = 0;
return HTTP_contentType_plaintext;
}
}
PRINT_ERROR("unhandled condition(%d)\r\n", fileType);
return HTTP_FAILURE;
}
int http_response_contentTypeToString(http_response_contenttype_t contentType, char *buffer, unsigned int length)
{
if ((NULL == buffer) || (0 == length))
{
PRINT_ERROR("NULL buffer or 0 length(%d)\r\n", length);
return HTTP_FAILURE;
}
switch (contentType)
{
case HTTP_contentType_unknown:
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_PLAINTEXT, length);
break;
}
case HTTP_contentType_plaintext:
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_PLAINTEXT, length);
break;
}
case HTTP_contentType_html:
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_HTML, length);
break;
}
case HTTP_contentType_css:
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_CSS, length);
break;
}
case HTTP_contentType_csv:
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_CSV, length);
break;
}
case HTTP_contentType_jpeg:
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_JPEG, length);
break;
}
case HTTP_contentType_png:
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_PNG, length);
break;
}
case HTTP_contentType_gif:
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_GIF, length);
break;
}
case HTTP_contentType_json:
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_JSON, length);
break;
}
case HTTP_contentType_js:
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_JS, length);
break;
}
case HTTP_contentType_zip:
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_ZIP, length);
break;
}
case HTTP_contentType_bin:
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_BIN, length);
break;
}
case HTTP_contentType_ico:
{
strncpy(buffer, HTTP_RES_CONTENT_TYPE_ICO, length);
break;
}
default:
{
PRINT_ERROR("unhandled case. passing default(%d)\r\n", contentType);
strncpy(buffer, HTTP_RES_CONTENT_TYPE_PLAINTEXT, length);
}
}
PRINT_ERROR("unhandled case(%d)\r\n", contentType);
return HTTP_SUCCESS;
}
/*- responseBody can be NULL for responses that does not have a body
- if transferEncoding is set to transferEnc_chunked , contentLength will be omitted from response.
- so, budy need not be passed.
- either path or response contents type can be provided (both are optional , both can be null)
- if path is provided, response content type will be computed from the extension and responseContentType argument is ignored
- if path is null, response content type will be used.
- if response content type is also null, then no content type will be included in the response.
*/
int http_response_response_header(HTTP_response_headerRequest_t headerRequest)
{
if ((NULL == headerRequest.headerBuffer) || (headerRequest.bufferLength <= 0))
{
PRINT_ERROR(" null buffer or 0 length(%d)\r\n", (int)headerRequest.bufferLength);
return HTTP_FAILURE;
}
char responseLine[HTTP_RESPONSE_LINE1_LENGTH];
char contentTypeLine[HTTP_RESPONSE_CONTTYPE_LENGTH];
char contentLengthLine[HTTP_RESPONSE_CONLEN_LENGTH];
char contentTypeLineDone = 0;
char contentLengthLineDone = 0;
responseLine[0] = 0;
contentTypeLine[0] = 0;
contentLengthLine[0] = 0;
headerRequest.headerBuffer[0] = 0;
//sprint response line 1
//TODO: handle all responses
switch (headerRequest.responseCode)
{
case HTTP_RESCODE_successSuccess: //200 OK
{
snprintf(responseLine, HTTP_RESPONSE_LINE1_LENGTH, HTTP_RES_HTTP_VERSION " " HTTP_RESSTRING_SUCCESS_SUCCESS);
break;
}
case HTTP_RESCODE_successNocontent: //204
{
snprintf(responseLine, HTTP_RESPONSE_LINE1_LENGTH, HTTP_RES_HTTP_VERSION " " HTTP_RESSTRING_SUCCESS_NOCONTENT);
break;
}
case HTTP_RESCODE_redirectNotmodified: //304
{
snprintf(responseLine, HTTP_RESPONSE_LINE1_LENGTH, HTTP_RES_HTTP_VERSION " " HTTP_RESSTRING_REDIRECT_NOTMODIFIED);
break;
}
case HTTP_RESCODE_cerrorForbidden: // 403
{
snprintf(responseLine, HTTP_RESPONSE_LINE1_LENGTH, HTTP_RES_HTTP_VERSION " " HTTP_RESSTRING_CERROR_FORBIDDEN);
break;
}
case HTTP_RESCODE_cerrorNotfound: // 401
{
snprintf(responseLine, HTTP_RESPONSE_LINE1_LENGTH, HTTP_RES_HTTP_VERSION " " HTTP_RESSTRING_CERROR_NOTFOUND);
break;
}
case HTTP_RESCODE_cerrorPayloadlarge: //413
{
snprintf(responseLine, HTTP_RESPONSE_LINE1_LENGTH, HTTP_RES_HTTP_VERSION " " HTTP_RESSTRING_CERROR_PAYLOADLARGE);
break;
}
case HTTP_RESCODE_cerrorUritoolong: //414
{
snprintf(responseLine, HTTP_RESPONSE_LINE1_LENGTH, HTTP_RES_HTTP_VERSION " " HTTP_RESSTRING_CERROR_URITOOLONG);
break;
}
case HTTP_RESCODE_cerrorHeadertoolarge: ///431
{
snprintf(responseLine, HTTP_RESPONSE_LINE1_LENGTH, HTTP_RES_HTTP_VERSION " " HTTP_RESSTRING_CERROR_HEADERTOOLARGE);
break;
}
case HTTP_RESCODE_serrorInternalerror: //500
{
snprintf(responseLine, HTTP_RESPONSE_LINE1_LENGTH, HTTP_RES_HTTP_VERSION " " HTTP_RESSTRING_SERROR_INTERNALERROR);
break;
}
default:
{
PRINT_ERROR("default case. passing default\r\n(%d)", (int)headerRequest.responseCode);
snprintf(responseLine, HTTP_RESPONSE_LINE1_LENGTH, HTTP_RES_HTTP_VERSION " " HTTP_RESSTRING_SERROR_NOTIMPLEMENTED);
break;
}
}
//handle content type here
if (0 != headerRequest.contentType)
{
char lcontentTypeLine[HTTP_RESPONSE_CONTTYPE_LENGTH];
int retVal;
retVal = http_response_contentTypeToString(headerRequest.contentType, lcontentTypeLine, HTTP_RESPONSE_CONTTYPE_LENGTH);
if (retVal < 0)
{
PRINT_ERROR("error converting contentType to string(%d)\r\n", (int)headerRequest.contentType);
}
snprintf(contentTypeLine, HTTP_RESPONSE_CONTTYPE_LENGTH, HTTP_RESHEADER_CONTENT_TYPE ": %s", lcontentTypeLine);
contentTypeLineDone = 1;
}
else if (NULL != headerRequest.filePath)
{
http_response_fileType_t fileType;
char lcontentTypeLine[HTTP_RESPONSE_CONTTYPE_LENGTH];
fileType = http_response_getFileType(headerRequest.filePath);
int retval;
retval = http_response_get_contentType_string(fileType, lcontentTypeLine, HTTP_RESPONSE_CONTTYPE_LENGTH);
if (retval < 0)
{
PRINT_ERROR("error converting contentType to string(%d)\r\n", (int)fileType);
}
snprintf(contentTypeLine, HTTP_RESPONSE_CONTTYPE_LENGTH, HTTP_RESHEADER_CONTENT_TYPE ": %s", lcontentTypeLine);
contentTypeLineDone = 1;
}
//handle content length and transfer encoding.
if (transferEnc_chunked == headerRequest.transferEncoding)
{
snprintf(contentLengthLine, HTTP_RESPONSE_CONLEN_LENGTH, HTTP_RESHEADER_TRANSFER_ENCODING ": chunked");
contentLengthLineDone = 1;
}
else if (0 != headerRequest.bodyLength) //content length is applicable only when there is content
{
snprintf(contentLengthLine, HTTP_RESPONSE_CONLEN_LENGTH, HTTP_RESHEADER_CONTENT_LENGTH ": %d", headerRequest.bodyLength);
contentLengthLineDone = 1;
}
//final Assembly
if (headerRequest.bufferLength < (strlen(responseLine) + strlen(contentTypeLine) + strlen(contentLengthLine)))
{
PRINT_ERROR("total header length > provided buffer(%d)\r\n", (int)(strlen(responseLine) + strlen(contentTypeLine) + strlen(contentLengthLine)));
return HTTP_FAILURE;
}
int printedChar;
printedChar = snprintf(headerRequest.headerBuffer, headerRequest.bufferLength, "%s\r\n", responseLine);
if (0 != contentTypeLineDone)
{
headerRequest.bufferLength -= printedChar + 1;
printedChar += snprintf((headerRequest.headerBuffer + printedChar), headerRequest.bufferLength, "%s\r\n", contentTypeLine);
}
if (0 != contentLengthLineDone)
{
headerRequest.bufferLength -= printedChar + 1;
printedChar += snprintf((headerRequest.headerBuffer + printedChar), headerRequest.bufferLength, "%s\r\n", contentLengthLine);
}
printedChar += snprintf((headerRequest.headerBuffer + printedChar), headerRequest.bufferLength, "\r\n");
return printedChar; //to return actual buffer length
}
void http_response_initReponseStruct(HTTP_response_headerRequest_t *responseHeader)
{
if (NULL != responseHeader)
{
memset(responseHeader, 0, sizeof(HTTP_response_headerRequest_t));
}
}