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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/net/httptypes.h"
#include "gate/results.h"
#include "gate/utilities.h"
#include "gate/uris.h"
gate_result_t gate_http_request_init_str(gate_http_request_t* request, char const* method, char const* path)
{
gate_result_t ret;
gate_string_t str_method, str_path;
if (NULL == gate_string_create(&str_method, method, gate_str_length(method)))
{
return GATE_RESULT_OUTOFMEMORY;
}
if (NULL == gate_string_create(&str_path, path, gate_str_length(path)))
{
gate_string_release(&str_method);
return GATE_RESULT_OUTOFMEMORY;
}
ret = gate_http_request_init(request, &str_method, &str_path);
gate_string_release(&str_path);
gate_string_release(&str_method);
return ret;
}
gate_result_t gate_http_request_init(gate_http_request_t* request, gate_string_t const* method, gate_string_t const* path)
{
gate_mem_clear(request, sizeof(gate_http_request_t));
if (method != NULL)
{
if (NULL == gate_string_clone(&request->method, method))
{
return GATE_RESULT_OUTOFMEMORY;
}
}
if (path != NULL)
{
if (NULL == gate_string_clone(&request->path, path))
{
gate_http_request_release(request);
return GATE_RESULT_OUTOFMEMORY;
}
}
if (NULL == gate_util_stringmap_create_ex(&request->headers, true, true))
{
gate_http_request_release(request);
return GATE_RESULT_OUTOFMEMORY;
}
if (NULL == gate_util_stringset_create(&request->accept_types))
{
gate_http_request_release(request);
return GATE_RESULT_OUTOFMEMORY;
}
return GATE_RESULT_OK;
}
gate_result_t gate_http_request_init_uri(gate_http_request_t* request, gate_string_t const* method, gate_uri_t* uri)
{
gate_result_t ret;
gate_string_t path = GATE_STRING_INIT_EMPTY;
do
{
ret = gate_uri_to_string(uri, &path, true);
GATE_BREAK_IF_FAILED(ret);
ret = gate_http_request_init(request, method, &path);
GATE_BREAK_IF_FAILED(ret);
if (!gate_string_is_empty(&uri->user_info))
{
ret = gate_uri_parse_user_info(&uri->user_info, &request->auth_user, &request->auth_pass);
GATE_BREAK_IF_FAILED(ret);
}
} while (0);
gate_string_release(&path);
return ret;
}
gate_result_t gate_http_request_copy(gate_http_request_t* request, gate_http_request_t const* src)
{
gate_result_t ret = GATE_RESULT_OUTOFMEMORY;
gate_mem_clear(request, sizeof(gate_http_request_t));
do
{
if (!gate_string_clone(&request->method, &src->method)) break;
if (!gate_string_clone(&request->path, &src->path)) break;
if (!gate_string_clone(&request->version, &src->version)) break;
if (!gate_string_clone(&request->auth_user, &src->auth_user)) break;
if (!gate_string_clone(&request->auth_pass, &src->auth_pass)) break;
if (!gate_map_copy(&request->headers, &src->headers)) break;
if (!gate_set_copy(&request->accept_types, &src->accept_types)) break;
if (!gate_string_clone(&request->proxy_server, &src->proxy_server)) break;
if (!gate_string_clone(&request->proxy_user, &src->proxy_user)) break;
if (!gate_string_clone(&request->proxy_pass, &src->proxy_pass)) break;
request->upload_stream = src->upload_stream;
if (request->upload_stream)
{
gate_object_retain(request->upload_stream);
}
request->connect_timeout_ms = src->connect_timeout_ms;
request->send_timeout_ms = src->send_timeout_ms;
request->receive_timeout_ms = src->receive_timeout_ms;
request->proxy_port = src->proxy_port;
ret = GATE_RESULT_OK;
} while (0);
if (GATE_FAILED(ret))
{
gate_http_request_release(request);
}
return ret;
}
gate_result_t gate_http_request_release(gate_http_request_t* request)
{
gate_string_release(&request->method);
gate_string_release(&request->path);
gate_string_release(&request->version);
gate_string_release(&request->auth_user);
gate_string_release(&request->auth_pass);
gate_map_destroy(&request->headers);
gate_set_destroy(&request->accept_types);
gate_string_release(&request->proxy_server);
gate_string_release(&request->proxy_user);
gate_string_release(&request->proxy_pass);
if (request->upload_stream != NULL)
{
gate_object_release(request->upload_stream);
}
gate_mem_clear(request, sizeof(gate_http_request_t));
return GATE_RESULT_OK;
}
gate_result_t gate_http_response_init(gate_http_response_t* response)
{
gate_result_t ret = GATE_RESULT_FAILED;
do
{
gate_mem_clear(response, sizeof(gate_http_response_t));
if (NULL == gate_util_stringmap_create_ex(&response->headers, true, true))
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
} while (0);
return GATE_RESULT_OK;
}
gate_result_t gate_http_response_copy(gate_http_response_t* response, gate_http_response_t const* src)
{
gate_result_t ret = GATE_RESULT_OK;
do
{
gate_mem_clear(response, sizeof(gate_http_response_t));
if (NULL == gate_map_copy(&response->headers, &src->headers))
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
response->response_stream = src->response_stream;
if (response->response_stream)
{
gate_object_retain(response->response_stream);
}
response->status_code = src->status_code;
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
gate_result_t gate_http_response_release(gate_http_response_t* response)
{
gate_map_destroy(&response->headers);
if (NULL != response->response_stream)
{
gate_object_release(response->response_stream);
}
gate_mem_clear(response, sizeof(gate_http_response_t));
return GATE_RESULT_OK;
}
char const* gate_http_response_status_text(gate_uint32_t status_code)
{
switch (status_code)
{
case 100: return "Continue";
case 101: return "Switching Protocols";
case 102: return "Processing";
case 103: return "Early Hints";
case 200: return "OK";
case 201: return "Created";
case 202: return "Accepted";
case 203: return "Non-Authoritative Information";
case 204: return "No Content";
case 205: return "Reset Content";
case 206: return "Partial Content";
case 207: return "Multi-Status";
case 208: return "Already Reported";
case 226: return "IM Used";
case 300: return "Multiple Choices";
case 301: return "Moved Permanently";
case 302: return "Found";
case 303: return "See Other";
case 304: return "Not Modified";
case 305: return "Use Proxy";
case 306: return "Switch Proxy";
case 307: return "Temporary Redirect";
case 308: return "Permanent Redirect";
case 400: return "Bad Request";
case 401: return "Unauthorized";
case 402: return "Payment Required";
case 403: return "Forbidden";
case 404: return "Not Found";
case 405: return "Method Not Allowed";
case 406: return "Not Acceptable";
case 407: return "Proxy Authentication Required";
case 408: return "Request Time-out";
case 409: return "Conflict";
case 410: return "Gone";
case 411: return "Length Required";
case 412: return "Precondition Failed";
case 413: return "Request Entity Too Large";
case 414: return "Request-URL Too Long";
case 415: return "Unsupported Media Type";
case 416: return "Requested range not satisfiable";
case 417: return "Expectation Failed";
case 418: return "I'm a teapot";
case 420: return "Policy Not Fulfilled";
case 421: return "Misdirected Request";
case 422: return "Unprocessable Entity";
case 423: return "Locked";
case 424: return "Failed Dependency";
case 425: return "Too Early";
case 426: return "Upgrade Required";
case 428: return "Precondition Required";
case 429: return "Too Many Requests";
case 431: return "Request Header Fields Too Large";
case 451: return "Unavailable For Legal Reasons";
case 500: return "Internal Server Error";
case 501: return "Not Implemented";
case 502: return "Bad Gateway";
case 503: return "Service Unavailable";
case 504: return "Gateway Timeout";
case 505: return "HTTP Version not supported";
case 506: return "Variant Also Negotiates";
case 507: return "Insufficient Storage";
case 508: return "Loop Detected";
case 509: return "Bandwidth Limit Exceeded";
case 510: return "Not Extended";
case 511: return "Network Authentication Required";
}
return NULL;
}
gate_result_t gate_http_parse_urlencoded_content(gate_string_t const* content,
gate_flatmap_t* append_to_stringmap)
{
gate_result_t ret = GATE_RESULT_OK;
gate_size_t start = 0;
gate_size_t pos = 0;
gate_string_t pair = GATE_STRING_INIT_EMPTY;
gate_string_t raw_key = GATE_STRING_INIT_EMPTY;
gate_string_t raw_value = GATE_STRING_INIT_EMPTY;
gate_string_t key = GATE_STRING_INIT_EMPTY;
gate_string_t value = GATE_STRING_INIT_EMPTY;
gate_size_t len = gate_string_length(content);
if (gate_flatmap_count(append_to_stringmap))
{
if (NULL == gate_flatmap_create(append_to_stringmap, &gate_compare_string,
sizeof(gate_string_t), &gate_string_copy_constructor, &gate_string_destructor,
sizeof(gate_string_t), &gate_string_copy_constructor, &gate_string_destructor))
{
return GATE_RESULT_OUTOFMEMORY;
}
}
while ((start < len) && GATE_SUCCEEDED(ret))
{
pos = gate_string_char_pos(content, '&', start);
if (GATE_STR_NPOS == pos)
{
gate_string_substr(&pair, content, start, GATE_STR_NPOS);
start = len;
}
else
{
gate_string_substr(&pair, content, start, pos - start);
start = pos + 1;
}
if (!gate_string_is_empty(&pair))
{
pos = gate_string_char_pos(&pair, '=', 0);
if (GATE_STR_NPOS == pos)
{
gate_uri_unescape(&pair, &key);
}
else
{
gate_string_substr(&raw_key, &pair, 0, pos);
gate_string_substr(&raw_value, &pair, pos + 1, GATE_STR_NPOS);
gate_uri_unescape(&raw_key, &key);
gate_uri_unescape(&raw_value, &value);
gate_string_release(&raw_value);
gate_string_release(&raw_key);
}
if (!gate_string_is_empty(&key))
{
if (NULL == gate_flatmap_add(append_to_stringmap, &key, &value))
{
ret = GATE_RESULT_OUTOFMEMORY;
}
}
gate_string_release(&value);
gate_string_release(&key);
}
gate_string_release(&pair);
}
return ret;
}
gate_result_t gate_http_build_urlencoded_content(gate_flatmap_t const* stringmap,
gate_strbuilder_t* append_to_builder)
{
gate_result_t ret = GATE_RESULT_OK;
gate_enumerator_t en;
gate_mapping_t const* m;<--- The scope of the variable 'm' can be reduced. [+]The scope of the variable 'm' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_string_t const* ptr_key;
gate_string_t const* ptr_value;
gate_string_t key = GATE_STRING_INIT_EMPTY;
gate_string_t value = GATE_STRING_INIT_EMPTY;
if (NULL == gate_flatmap_enumerate(stringmap, &en))
{
return GATE_RESULT_OUTOFMEMORY;
}
for (; gate_enumerator_valid(&en); gate_enumerator_next(&en))
{
m = (gate_mapping_t const*)gate_enumerator_get(&en);
if (NULL == m)
{
continue;
}
ptr_key = (gate_string_t const*)m->key;
ptr_value = (gate_string_t const*)m->value;
if (gate_string_is_empty(ptr_key))
{
continue;
}
gate_uri_escape(ptr_key, &key);
gate_uri_escape(ptr_value, &value);
gate_strbuilder_append(append_to_builder,
GATE_PRINT_CSTR, "&",
GATE_PRINT_STRING, &key,
GATE_PRINT_CSTR, "=",
GATE_PRINT_STRING, &value);
gate_string_release(&key);
gate_string_release(&value);
}
return ret;
}
typedef struct gate_http_mime_mapping_class
{
char const* file_ext;
gate_string_t const* mime_type;
} gate_http_mime_mapping_t;
static gate_string_t const mimetype_text_plain = GATE_STRING_INIT_STATIC("text/plain");
static gate_string_t const mimetype_text_html = GATE_STRING_INIT_STATIC("text/html");
static gate_string_t const mimetype_text_richtext = GATE_STRING_INIT_STATIC("text/richtext");
static gate_string_t const mimetype_text_css = GATE_STRING_INIT_STATIC("text/css");
static gate_string_t const mimetype_text_xml = GATE_STRING_INIT_STATIC("text/xml");
static gate_string_t const mimetype_text_js = GATE_STRING_INIT_STATIC("text/javascript");
static gate_string_t const mimetype_text_tsv = GATE_STRING_INIT_STATIC("text/tab-separated-values");
static gate_string_t const mimetype_text_setext = GATE_STRING_INIT_STATIC("text/x-setext");
static gate_string_t const mimetype_text_sgml = GATE_STRING_INIT_STATIC("text/x-sgml");
static gate_string_t const mimetype_text_jad = GATE_STRING_INIT_STATIC("text/vnd.sun.j2me.app-descriptor");
static gate_string_t const mimetype_text_wml = GATE_STRING_INIT_STATIC("text/vnd.wap.wml");
static gate_string_t const mimetype_image_gif = GATE_STRING_INIT_STATIC("image/gif");
static gate_string_t const mimetype_image_jpeg = GATE_STRING_INIT_STATIC("image/jpeg");
static gate_string_t const mimetype_image_tiff = GATE_STRING_INIT_STATIC("image/tiff");
static gate_string_t const mimetype_image_cmu_raster = GATE_STRING_INIT_STATIC("image/cmu-raster");
static gate_string_t const mimetype_image_freehand = GATE_STRING_INIT_STATIC("image/x-freehand");
static gate_string_t const mimetype_image_ief = GATE_STRING_INIT_STATIC("image/ief");
static gate_string_t const mimetype_image_anymap = GATE_STRING_INIT_STATIC("image/x-portable-anymap");
static gate_string_t const mimetype_image_graymap = GATE_STRING_INIT_STATIC("image/x-portable-graymap");
static gate_string_t const mimetype_image_pixmap = GATE_STRING_INIT_STATIC("image/x-portable-pixmap");
static gate_string_t const mimetype_image_rgb = GATE_STRING_INIT_STATIC("image/x-rgb");
static gate_string_t const mimetype_image_windowdump = GATE_STRING_INIT_STATIC("image/x-windowdump");
static gate_string_t const mimetype_image_wbmp = GATE_STRING_INIT_STATIC("image/vnd.wap.wbmp");
static gate_string_t const mimetype_app_octet_stream = GATE_STRING_INIT_STATIC("application/octet-stream");
static gate_string_t const mimetype_app_zip = GATE_STRING_INIT_STATIC("application/zip");
static gate_string_t const mimetype_app_pdf = GATE_STRING_INIT_STATIC("application/pdf");
static gate_string_t const mimetype_app_postscript = GATE_STRING_INIT_STATIC("application/postscript");
static gate_string_t const mimetype_app_autoconfig = GATE_STRING_INIT_STATIC("application/x-ns-proxy-autoconfig");
static gate_string_t const mimetype_app_acad = GATE_STRING_INIT_STATIC("application/acad");
static gate_string_t const mimetype_app_dxf = GATE_STRING_INIT_STATIC("application/dxf");
static gate_string_t const mimetype_app_mif = GATE_STRING_INIT_STATIC("application/mif");
static gate_string_t const mimetype_app_msword = GATE_STRING_INIT_STATIC("application/msword");
static gate_string_t const mimetype_app_mspowerpoint = GATE_STRING_INIT_STATIC("application/mspowerpoint");
static gate_string_t const mimetype_app_msexcel = GATE_STRING_INIT_STATIC("application/msexcel");
static gate_string_t const mimetype_app_mshelp = GATE_STRING_INIT_STATIC("application/mshelp");
static gate_string_t const mimetype_app_rtf = GATE_STRING_INIT_STATIC("application/rtf");
static gate_string_t const mimetype_app_sh = GATE_STRING_INIT_STATIC("application/x-sh");
static gate_string_t const mimetype_app_csh = GATE_STRING_INIT_STATIC("application/x-csh");
static gate_string_t const mimetype_app_latex = GATE_STRING_INIT_STATIC("application/x-latex");
static gate_string_t const mimetype_app_tar = GATE_STRING_INIT_STATIC("application/x-tar");
static gate_string_t const mimetype_app_bcpio = GATE_STRING_INIT_STATIC("application/x-bcpio");
static gate_string_t const mimetype_app_cpio = GATE_STRING_INIT_STATIC("application/x-cpio");
static gate_string_t const mimetype_app_sv4cpio = GATE_STRING_INIT_STATIC("application/x-sv4cpio");
static gate_string_t const mimetype_app_sv4crc = GATE_STRING_INIT_STATIC("application/x-sv4crc");
static gate_string_t const mimetype_app_hdf = GATE_STRING_INIT_STATIC("application/x-hdf");
static gate_string_t const mimetype_app_ustar = GATE_STRING_INIT_STATIC("application/x-ustar");
static gate_string_t const mimetype_app_shar = GATE_STRING_INIT_STATIC("application/x-shar");
static gate_string_t const mimetype_app_tcl = GATE_STRING_INIT_STATIC("application/x-tcl");
static gate_string_t const mimetype_app_dvi = GATE_STRING_INIT_STATIC("application/x-dvi");
static gate_string_t const mimetype_app_texinfo = GATE_STRING_INIT_STATIC("application/x-texinfo");
static gate_string_t const mimetype_app_troff = GATE_STRING_INIT_STATIC("application/x-troff");
static gate_string_t const mimetype_app_troff_man = GATE_STRING_INIT_STATIC("application/x-troff-man");
static gate_string_t const mimetype_app_troff_me = GATE_STRING_INIT_STATIC("application/x-troff-me");
static gate_string_t const mimetype_app_troff_ms = GATE_STRING_INIT_STATIC("application/x-troff-ms");
static gate_string_t const mimetype_app_netcdf = GATE_STRING_INIT_STATIC("application/x-netcdf");
static gate_string_t const mimetype_app_wais_source = GATE_STRING_INIT_STATIC("application/x-wais-source");
static gate_string_t const mimetype_app_jar = GATE_STRING_INIT_STATIC("application/x-jar");
static gate_string_t const mimetype_app_jnlp = GATE_STRING_INIT_STATIC("application/x-java-jnlp-file");
static gate_string_t const mimetype_app_wmlc = GATE_STRING_INIT_STATIC("application/vnd.wap.wmlc");
static gate_string_t const mimetype_app_wrl = GATE_STRING_INIT_STATIC("application/x-wrl+xml");
static gate_string_t const mimetype_audio_basic = GATE_STRING_INIT_STATIC("audio/basic");
static gate_string_t const mimetype_audio_aiff = GATE_STRING_INIT_STATIC("audio/x-aiff");
static gate_string_t const mimetype_audio_dspeeh = GATE_STRING_INIT_STATIC("audio/x-dspeeh");
static gate_string_t const mimetype_audio_midi = GATE_STRING_INIT_STATIC("audio/x-midi");
static gate_string_t const mimetype_audio_realaudio = GATE_STRING_INIT_STATIC("audio/x-pn-realaudio");
static gate_string_t const mimetype_audio_realaudio_plugin = GATE_STRING_INIT_STATIC("audio/x-pn-realaudio-plugin");
static gate_string_t const mimetype_video_mpeg = GATE_STRING_INIT_STATIC("video/mpeg");
static gate_string_t const mimetype_video_quicktime = GATE_STRING_INIT_STATIC("video/quicktime");
static gate_string_t const mimetype_video_msvideo = GATE_STRING_INIT_STATIC("video/x-msvideo");
static gate_string_t const mimetype_video_sgi_movie = GATE_STRING_INIT_STATIC("video/x-sgi-movie");
static gate_http_mime_mapping_t gate_http_default_mime_types[] =
{
{ ".txt", &mimetype_text_plain },
{ ".c", &mimetype_text_plain },
{ ".cc", &mimetype_text_plain },
{ ".g", &mimetype_text_plain },
{ ".h", &mimetype_text_plain },
{ ".hh", &mimetype_text_plain },
{ ".m", &mimetype_text_plain },
{ ".f90", &mimetype_text_plain },
{ ".rtx", &mimetype_text_richtext },
{ ".css", &mimetype_text_css },
{ ".xml", &mimetype_text_xml },
{ ".htm", &mimetype_text_html },
{ ".html", &mimetype_text_html },
{ ".shtm", &mimetype_text_html },
{ ".shtml", &mimetype_text_html },
{ ".js", &mimetype_text_js },
{ ".tsv", &mimetype_text_tsv },
{ ".etx", &mimetype_text_setext },
{ ".sgm", &mimetype_text_sgml },
{ ".sgml", &mimetype_text_sgml },
{ ".jad", &mimetype_text_jad },
{ ".wml", &mimetype_text_wml },
{ ".gif", &mimetype_image_gif },
{ ".jpeg", &mimetype_image_jpeg },
{ ".jpg", &mimetype_image_jpeg },
{ ".jpe", &mimetype_image_jpeg },
{ ".tiff", &mimetype_image_tiff },
{ ".tif", &mimetype_image_tiff },
{ ".ras", &mimetype_image_cmu_raster },
{ ".fh4", &mimetype_image_freehand },
{ ".fh5", &mimetype_image_freehand },
{ ".fhc", &mimetype_image_freehand },
{ ".ief", &mimetype_image_ief },
{ ".pnm", &mimetype_image_anymap },
{ ".PBM", &mimetype_image_anymap },
{ ".pgm", &mimetype_image_graymap },
{ ".ppm", &mimetype_image_pixmap },
{ ".rgb", &mimetype_image_rgb },
{ ".xwd", &mimetype_image_windowdump },
{ ".wbmp", &mimetype_image_wbmp },
{ ".exe", &mimetype_app_octet_stream },
{ ".com", &mimetype_app_octet_stream },
{ ".dll", &mimetype_app_octet_stream },
{ ".bin", &mimetype_app_octet_stream },
{ ".class", &mimetype_app_octet_stream },
{ ".zip", &mimetype_app_zip },
{ ".pdf", &mimetype_app_pdf },
{ ".ps", &mimetype_app_postscript },
{ ".ai", &mimetype_app_postscript },
{ ".eps", &mimetype_app_postscript },
{ ".pac", &mimetype_app_autoconfig },
{ ".dwg", &mimetype_app_acad },
{ ".dxf", &mimetype_app_dxf },
{ ".mif", &mimetype_app_mif },
{ ".doc", &mimetype_app_msword },
{ ".dot", &mimetype_app_msword },
{ ".ppt", &mimetype_app_mspowerpoint },
{ ".ppz", &mimetype_app_mspowerpoint },
{ ".pps", &mimetype_app_mspowerpoint },
{ ".pot", &mimetype_app_mspowerpoint },
{ ".xls", &mimetype_app_msexcel },
{ ".xla", &mimetype_app_msexcel },
{ ".hlp", &mimetype_app_mshelp },
{ ".chm", &mimetype_app_mshelp },
{ ".rtf", &mimetype_app_rtf },
{ ".sh", &mimetype_app_sh },
{ ".csh", &mimetype_app_csh },
{ ".latex", &mimetype_app_latex },
{ ".tar", &mimetype_app_tar },
{ ".bcpio", &mimetype_app_bcpio },
{ ".cpio", &mimetype_app_cpio },
{ ".sv4cpio", &mimetype_app_sv4cpio },
{ ".sv4crc", &mimetype_app_sv4crc },
{ ".hdf", &mimetype_app_hdf },
{ ".ustar", &mimetype_app_ustar },
{ ".shar", &mimetype_app_shar },
{ ".tcl", &mimetype_app_tcl },
{ ".dvi", &mimetype_app_dvi },
{ ".texinfo", &mimetype_app_texinfo },
{ ".texi", &mimetype_app_texinfo },
{ ".t", &mimetype_app_troff },
{ ".tr", &mimetype_app_troff },
{ ".roff", &mimetype_app_troff },
{ ".man", &mimetype_app_troff_man },
{ ".me", &mimetype_app_troff_me },
{ ".ms", &mimetype_app_troff_ms },
{ ".nc", &mimetype_app_netcdf },
{ ".cdf", &mimetype_app_netcdf },
{ ".src", &mimetype_app_wais_source },
{ ".jar", &mimetype_app_jar },
{ ".jnlp", &mimetype_app_jnlp },
{ ".wmlc", &mimetype_app_wmlc },
{ ".wrl", &mimetype_app_wrl },
{ ".au", &mimetype_audio_basic },
{ ".snd", &mimetype_audio_basic },
{ ".aif", &mimetype_audio_aiff },
{ ".aiff", &mimetype_audio_aiff },
{ ".aifc", &mimetype_audio_aiff },
{ ".dus", &mimetype_audio_dspeeh },
{ ".cht", &mimetype_audio_dspeeh },
{ ".midi", &mimetype_audio_midi },
{ ".mid", &mimetype_audio_midi },
{ ".ram", &mimetype_audio_realaudio },
{ ".ra", &mimetype_audio_realaudio },
{ ".rpm", &mimetype_audio_realaudio_plugin },
{ ".mpeg", &mimetype_video_mpeg },
{ ".mpg", &mimetype_video_mpeg },
{ ".mpe", &mimetype_video_mpeg },
{ ".qt", &mimetype_video_quicktime },
{ ".mov", &mimetype_video_quicktime },
{ ".avi", &mimetype_video_msvideo },
{ ".movie", &mimetype_video_sgi_movie }
};
static gate_size_t const gate_http_default_mime_types_count = sizeof(gate_http_default_mime_types) / sizeof(gate_http_default_mime_types[0]);
gate_result_t gate_http_resolve_mime_type(gate_string_t const* file_ext, gate_string_t* mine_type)
{
gate_size_t ndx;
for (ndx = 0; ndx != gate_http_default_mime_types_count; ++ndx)
{
if (gate_string_ends_with_str_ic(file_ext, gate_http_default_mime_types[ndx].file_ext))
{
gate_string_duplicate(mine_type, gate_http_default_mime_types[ndx].mime_type);
return GATE_RESULT_OK;
}
}
gate_string_duplicate(mine_type, &mimetype_app_octet_stream);
return GATE_RESULT_OK;
}
char const* gate_http_resolve_mime_type_str(char const* file_ext)
{
gate_result_t result;
gate_string_t ext;
gate_string_t mime = GATE_STRING_INIT_EMPTY;
gate_string_create_static(&ext, file_ext);
result = gate_http_resolve_mime_type(&ext, &mime);
if (GATE_FAILED(result))
{
return NULL;
}
else
{
return mime.str;
}
}
|