agoo 0.9.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of agoo might be problematic. Click here for more details.

data/ext/agoo/http.c ADDED
@@ -0,0 +1,617 @@
1
+ // Copyright (c) 2018, Peter Ohler, All rights reserved.
2
+
3
+ #include <stdint.h>
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+
7
+ #include <ruby.h>
8
+
9
+ #include "http.h"
10
+
11
+ #define BUCKET_SIZE 1024
12
+ #define BUCKET_MASK 1023
13
+ #define MAX_KEY_UNIQ 9
14
+
15
+ typedef struct _Slot {
16
+ struct _Slot *next;
17
+ const char *key;
18
+ uint64_t hash;
19
+ int klen;
20
+ } *Slot;
21
+
22
+ typedef struct _Cache {
23
+ Slot buckets[BUCKET_SIZE];
24
+ } *Cache;
25
+
26
+ struct _Cache key_cache;
27
+
28
+ // The rack spec indicates the characters (),/:;<=>?@[]{} are invalid which
29
+ // clearly is not consisten with RFC7230 so stick with the RFC.
30
+ static char header_value_chars[256] = "\
31
+ xxxxxxxxxxoxxxxxxxxxxxxxxxxxxxxx\
32
+ oooooooooooooooooooooooooooooooo\
33
+ oooooooooooooooooooooooooooooooo\
34
+ ooooooooooooooooooooooooooooooox\
35
+ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
36
+ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
37
+ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
38
+ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
39
+
40
+ static const char *header_keys[] = {
41
+ "A-IM",
42
+ "ALPN",
43
+ "ARC-Authentication-Results",
44
+ "ARC-Message-Signature",
45
+ "ARC-Seal",
46
+ "Accept",
47
+ "Accept-Additions",
48
+ "Accept-Charset",
49
+ "Accept-Datetime",
50
+ "Accept-Encoding",
51
+ "Accept-Features",
52
+ "Accept-Language",
53
+ "Accept-Language",
54
+ "Accept-Patch",
55
+ "Accept-Post",
56
+ "Accept-Ranges",
57
+ "Access-Control",
58
+ "Access-Control-Allow-Credentials",
59
+ "Access-Control-Allow-Headers",
60
+ "Access-Control-Allow-Methods",
61
+ "Access-Control-Allow-Origin",
62
+ "Access-Control-Max-Age",
63
+ "Access-Control-Request-Headers",
64
+ "Access-Control-Request-Method",
65
+ "Age",
66
+ "Allow",
67
+ "Also-Control",
68
+ "Alt-Svc",
69
+ "Alt-Used",
70
+ "Alternate-Recipient",
71
+ "Alternates",
72
+ "Apparently-To",
73
+ "Apply-To-Redirect-Ref",
74
+ "Approved",
75
+ "Archive",
76
+ "Archived-At",
77
+ "Archived-At",
78
+ "Article-Names",
79
+ "Article-Updates",
80
+ "Authentication-Control",
81
+ "Authentication-Info",
82
+ "Authentication-Results",
83
+ "Authorization",
84
+ "Auto-Submitted",
85
+ "Autoforwarded",
86
+ "Autosubmitted",
87
+ "Base",
88
+ "Bcc",
89
+ "Body",
90
+ "C-Ext",
91
+ "C-Man",
92
+ "C-Opt",
93
+ "C-PEP",
94
+ "C-PEP-Info",
95
+ "Cache-Control",
96
+ "CalDAV-Timezones",
97
+ "Cancel-Key",
98
+ "Cancel-Lock",
99
+ "Cc",
100
+ "Close",
101
+ "Comments",
102
+ "Comments",
103
+ "Compliance",
104
+ "Connection",
105
+ "Content-Alternative",
106
+ "Content-Base",
107
+ "Content-Base",
108
+ "Content-Description",
109
+ "Content-Disposition",
110
+ "Content-Disposition",
111
+ "Content-Duration",
112
+ "Content-Encoding",
113
+ "Content-ID",
114
+ "Content-ID",
115
+ "Content-Identifier",
116
+ "Content-Language",
117
+ "Content-Language",
118
+ "Content-Length",
119
+ "Content-Location",
120
+ "Content-Location",
121
+ "Content-MD5",
122
+ "Content-MD5",
123
+ "Content-Range",
124
+ "Content-Return",
125
+ "Content-Script-Type",
126
+ "Content-Style-Type",
127
+ "Content-Transfer-Encoding",
128
+ "Content-Transfer-Encoding",
129
+ "Content-Translation-Type",
130
+ "Content-Type",
131
+ "Content-Type",
132
+ "Content-Version",
133
+ "Content-features",
134
+ "Control",
135
+ "Conversion",
136
+ "Conversion-With-Loss",
137
+ "Cookie",
138
+ "Cookie2",
139
+ "Cost",
140
+ "DASL",
141
+ "DAV",
142
+ "DKIM-Signature",
143
+ "DL-Expansion-History",
144
+ "Date",
145
+ "Date",
146
+ "Date",
147
+ "Date-Received",
148
+ "Default-Style",
149
+ "Deferred-Delivery",
150
+ "Delivery-Date",
151
+ "Delta-Base",
152
+ "Depth",
153
+ "Derived-From",
154
+ "Destination",
155
+ "Differential-ID",
156
+ "Digest",
157
+ "Discarded-X400-IPMS-Extensions",
158
+ "Discarded-X400-MTS-Extensions",
159
+ "Disclose-Recipients",
160
+ "Disposition-Notification-Options",
161
+ "Disposition-Notification-To",
162
+ "Distribution",
163
+ "Downgraded-Bcc",
164
+ "Downgraded-Cc",
165
+ "Downgraded-Disposition-Notification-To",
166
+ "Downgraded-Final-Recipient",
167
+ "Downgraded-From",
168
+ "Downgraded-In-Reply-To",
169
+ "Downgraded-Mail-From",
170
+ "Downgraded-Message-Id",
171
+ "Downgraded-Original-Recipient",
172
+ "Downgraded-Rcpt-To",
173
+ "Downgraded-References",
174
+ "Downgraded-Reply-To",
175
+ "Downgraded-Resent-Bcc",
176
+ "Downgraded-Resent-Cc",
177
+ "Downgraded-Resent-From",
178
+ "Downgraded-Resent-Reply-To",
179
+ "Downgraded-Resent-Sender",
180
+ "Downgraded-Resent-To",
181
+ "Downgraded-Return-Path",
182
+ "Downgraded-Sender",
183
+ "Downgraded-To",
184
+ "EDIINT-Features",
185
+ "EDIINT-Features",
186
+ "ETag",
187
+ "Eesst-Version",
188
+ "Encoding",
189
+ "Encrypted",
190
+ "Errors-To",
191
+ "Expect",
192
+ "Expires",
193
+ "Expires",
194
+ "Expires",
195
+ "Expiry-Date",
196
+ "Ext",
197
+ "Followup-To",
198
+ "Form-Sub",
199
+ "Forwarded",
200
+ "From",
201
+ "From",
202
+ "From",
203
+ "Generate-Delivery-Report",
204
+ "GetProfile",
205
+ "HTTP2-Settings",
206
+ "Hobareg",
207
+ "Host",
208
+ "IM",
209
+ "If",
210
+ "If-Match",
211
+ "If-Modified-Since",
212
+ "If-None-Match",
213
+ "If-Range",
214
+ "If-Schedule-Tag-Match",
215
+ "If-Unmodified-Since",
216
+ "Importance",
217
+ "In-Reply-To",
218
+ "Incomplete-Copy",
219
+ "Injection-Date",
220
+ "Injection-Info",
221
+ "Jabber-ID",
222
+ "Jabber-ID",
223
+ "Keep-Alive",
224
+ "Keywords",
225
+ "Keywords",
226
+ "Label",
227
+ "Language",
228
+ "Last-Modified",
229
+ "Latest-Delivery-Time",
230
+ "Lines",
231
+ "Link",
232
+ "List-Archive",
233
+ "List-Help",
234
+ "List-ID",
235
+ "List-Owner",
236
+ "List-Post",
237
+ "List-Subscribe",
238
+ "List-Unsubscribe",
239
+ "List-Unsubscribe-Post",
240
+ "Location",
241
+ "Lock-Token",
242
+ "MIME-Version",
243
+ "MIME-Version",
244
+ "MMHS-Acp127-Message-Identifier",
245
+ "MMHS-Authorizing-Users",
246
+ "MMHS-Codress-Message-Indicator",
247
+ "MMHS-Copy-Precedence",
248
+ "MMHS-Exempted-Address",
249
+ "MMHS-Extended-Authorisation-Info",
250
+ "MMHS-Handling-Instructions",
251
+ "MMHS-Message-Instructions",
252
+ "MMHS-Message-Type",
253
+ "MMHS-Originator-PLAD",
254
+ "MMHS-Originator-Reference",
255
+ "MMHS-Other-Recipients-Indicator-CC",
256
+ "MMHS-Other-Recipients-Indicator-To",
257
+ "MMHS-Primary-Precedence",
258
+ "MMHS-Subject-Indicator-Codes",
259
+ "MT-Priority",
260
+ "Man",
261
+ "Max-Forwards",
262
+ "Memento-Datetime",
263
+ "Message-Context",
264
+ "Message-ID",
265
+ "Message-ID",
266
+ "Message-ID",
267
+ "Message-Type",
268
+ "Meter",
269
+ "Method-Check",
270
+ "Method-Check-Expires",
271
+ "NNTP-Posting-Date",
272
+ "NNTP-Posting-Host",
273
+ "Negotiate",
274
+ "Newsgroups",
275
+ "Non-Compliance",
276
+ "Obsoletes",
277
+ "Opt",
278
+ "Optional",
279
+ "Optional-WWW-Authenticate",
280
+ "Ordering-Type",
281
+ "Organization",
282
+ "Organization",
283
+ "Origin",
284
+ "Original-Encoded-Information-Types",
285
+ "Original-From",
286
+ "Original-Message-ID",
287
+ "Original-Recipient",
288
+ "Original-Sender",
289
+ "Original-Subject",
290
+ "Originator-Return-Address",
291
+ "Overwrite",
292
+ "P3P",
293
+ "PEP",
294
+ "PICS-Label",
295
+ "PICS-Label",
296
+ "Path",
297
+ "Pep-Info",
298
+ "Position",
299
+ "Posting-Version",
300
+ "Pragma",
301
+ "Prefer",
302
+ "Preference-Applied",
303
+ "Prevent-NonDelivery-Report",
304
+ "Priority",
305
+ "Privicon",
306
+ "ProfileObject",
307
+ "Protocol",
308
+ "Protocol-Info",
309
+ "Protocol-Query",
310
+ "Protocol-Request",
311
+ "Proxy-Authenticate",
312
+ "Proxy-Authentication-Info",
313
+ "Proxy-Authorization",
314
+ "Proxy-Features",
315
+ "Proxy-Instruction",
316
+ "Public",
317
+ "Public-Key-Pins",
318
+ "Public-Key-Pins-Report-Only",
319
+ "Range",
320
+ "Received",
321
+ "Received-SPF",
322
+ "Redirect-Ref",
323
+ "References",
324
+ "References",
325
+ "Referer",
326
+ "Referer-Root",
327
+ "Relay-Version",
328
+ "Reply-By",
329
+ "Reply-To",
330
+ "Reply-To",
331
+ "Require-Recipient-Valid-Since",
332
+ "Resent-Bcc",
333
+ "Resent-Cc",
334
+ "Resent-Date",
335
+ "Resent-From",
336
+ "Resent-Message-ID",
337
+ "Resent-Reply-To",
338
+ "Resent-Sender",
339
+ "Resent-To",
340
+ "Resolution-Hint",
341
+ "Resolver-Location",
342
+ "Retry-After",
343
+ "Return-Path",
344
+ "SIO-Label",
345
+ "SIO-Label-History",
346
+ "SLUG",
347
+ "Safe",
348
+ "Schedule-Reply",
349
+ "Schedule-Tag",
350
+ "Sec-WebSocket-Accept",
351
+ "Sec-WebSocket-Extensions",
352
+ "Sec-WebSocket-Key",
353
+ "Sec-WebSocket-Protocol",
354
+ "Sec-WebSocket-Version",
355
+ "Security-Scheme",
356
+ "See-Also",
357
+ "Sender",
358
+ "Sender",
359
+ "Sensitivity",
360
+ "Server",
361
+ "Set-Cookie",
362
+ "Set-Cookie2",
363
+ "SetProfile",
364
+ "SoapAction",
365
+ "Solicitation",
366
+ "Status-URI",
367
+ "Strict-Transport-Security",
368
+ "SubOK",
369
+ "Subject",
370
+ "Subject",
371
+ "Subst",
372
+ "Summary",
373
+ "Supersedes",
374
+ "Supersedes",
375
+ "Surrogate-Capability",
376
+ "Surrogate-Control",
377
+ "TCN",
378
+ "TE",
379
+ "TTL",
380
+ "Timeout",
381
+ "Title",
382
+ "To",
383
+ "Topic",
384
+ "Trailer",
385
+ "Transfer-Encoding",
386
+ "UA-Color",
387
+ "UA-Media",
388
+ "UA-Pixels",
389
+ "UA-Resolution",
390
+ "UA-Windowpixels",
391
+ "URI",
392
+ "Upgrade",
393
+ "Urgency",
394
+ "User-Agent",
395
+ "User-Agent",
396
+ "VBR-Info",
397
+ "Variant-Vary",
398
+ "Vary",
399
+ "Version",
400
+ "Via",
401
+ "WWW-Authenticate",
402
+ "Want-Digest",
403
+ "Warning",
404
+ "X-Archived-At",
405
+ "X-Archived-At",
406
+ "X-Content-Type-Options",
407
+ "X-Device-Accept",
408
+ "X-Device-Accept-Charset",
409
+ "X-Device-Accept-Encoding",
410
+ "X-Device-Accept-Language",
411
+ "X-Device-User-Agent",
412
+ "X-Frame-Options",
413
+ "X-Mittente",
414
+ "X-PGP-Sig",
415
+ "X-Ricevuta",
416
+ "X-Riferimento-Message-ID",
417
+ "X-TipoRicevuta",
418
+ "X-Trasporto",
419
+ "X-VerificaSicurezza",
420
+ "X400-Content-Identifier",
421
+ "X400-Content-Return",
422
+ "X400-Content-Type",
423
+ "X400-MTS-Identifier",
424
+ "X400-Originator",
425
+ "X400-Received",
426
+ "X400-Recipients",
427
+ "X400-Trace",
428
+ "Xref",
429
+ NULL
430
+ };
431
+
432
+ static uint64_t
433
+ calc_hash(const char *key, int *lenp) {
434
+ int len = 0;
435
+ int klen = *lenp;
436
+ uint64_t h = 0;
437
+ bool special = false;
438
+
439
+ if (NULL != key) {
440
+ const uint8_t *k = (const uint8_t*)key;
441
+
442
+ for (; len < klen; k++) {
443
+ // narrow to most used range of 0x4D (77) in size
444
+ if (*k < 0x2D || 0x7A < *k) {
445
+ special = true;
446
+ }
447
+ // fast, just spread it out
448
+ h = 77 * h + ((*k | 0x20) - 0x2D);
449
+ len++;
450
+ }
451
+ }
452
+ if (special) {
453
+ *lenp = -len;
454
+ } else {
455
+ *lenp = len;
456
+ }
457
+ return h;
458
+ }
459
+
460
+ static Slot*
461
+ get_bucketp(uint64_t h) {
462
+ return key_cache.buckets + (BUCKET_MASK & (h ^ (h << 5) ^ (h >> 7)));
463
+ }
464
+
465
+ static void
466
+ key_set(const char *key) {
467
+ int len = strlen(key);
468
+ int64_t h = calc_hash(key, &len);
469
+ Slot *bucket = get_bucketp(h);
470
+ Slot s;
471
+
472
+ if (NULL != (s = (Slot)malloc(sizeof(struct _Slot)))) {
473
+ s->hash = h;
474
+ s->klen = len;
475
+ s->key = key;
476
+ s->next = *bucket;
477
+ *bucket = s;
478
+ }
479
+ }
480
+
481
+ void
482
+ http_init() {
483
+ const char **kp = header_keys;
484
+
485
+ memset(&key_cache, 0, sizeof(struct _Cache));
486
+ for (; NULL != *kp; kp++) {
487
+ key_set(*kp);
488
+ }
489
+ }
490
+
491
+ void
492
+ http_cleanup() {
493
+ Slot *sp = key_cache.buckets;
494
+ Slot s;
495
+ Slot n;
496
+
497
+ for (int i = BUCKET_SIZE; 0 < i; i--, sp++) {
498
+ for (s = *sp; NULL != s; s = n) {
499
+ n = s->next;
500
+ free(s);
501
+ }
502
+ *sp = NULL;
503
+ }
504
+ }
505
+
506
+ void
507
+ http_header_ok(const char *key, int klen, const char *value, int vlen) {
508
+ int len = klen;
509
+ int64_t h = calc_hash(key, &len);
510
+ Slot *bucket = get_bucketp(h);
511
+ Slot s;
512
+ bool found = false;
513
+
514
+ for (s = *bucket; NULL != s; s = s->next) {
515
+ if (h == (int64_t)s->hash && len == (int)s->klen &&
516
+ ((0 <= len && len <= MAX_KEY_UNIQ) || 0 == strncasecmp(s->key, key, klen))) {
517
+ found = true;
518
+ break;
519
+ }
520
+ }
521
+ if (!found) {
522
+ char buf[256];
523
+
524
+ if ((int)sizeof(buf) <= klen) {
525
+ klen = sizeof(buf) - 1;
526
+ }
527
+ strncpy(buf, key, klen);
528
+ buf[klen] = '\0';
529
+ rb_raise(rb_eArgError, "%s is not a valid HTTP header key.", buf);
530
+ }
531
+ // Now check the value.
532
+ found = false; // reuse as indicator for in a quoted string
533
+ for (; 0 < vlen; vlen--, value++) {
534
+ if ('o' != header_value_chars[(uint8_t)*value]) {
535
+ rb_raise(rb_eArgError, "%02x is not a valid HTTP header value character.", *value);
536
+ }
537
+ if ('"' == *value) {
538
+ found = !found;
539
+ }
540
+ }
541
+ if (found) {
542
+ rb_raise(rb_eArgError, "HTTP header has unmatched quote.");
543
+ }
544
+ }
545
+
546
+ const char*
547
+ http_code_message(int code) {
548
+ const char *msg = "";
549
+
550
+ switch (code) {
551
+ case 100: msg = "Continue"; break;
552
+ case 101: msg = "Switching Protocols"; break;
553
+ case 102: msg = "Processing"; break;
554
+ case 200: msg = "OK"; break;
555
+ case 201: msg = "Created"; break;
556
+ case 202: msg = "Accepted"; break;
557
+ case 203: msg = "Non-authoritative Information"; break;
558
+ case 204: msg = "No Content"; break;
559
+ case 205: msg = "Reset Content"; break;
560
+ case 206: msg = "Partial Content"; break;
561
+ case 207: msg = "Multi-Status"; break;
562
+ case 208: msg = "Already Reported"; break;
563
+ case 226: msg = "IM Used"; break;
564
+ case 300: msg = "Multiple Choices"; break;
565
+ case 301: msg = "Moved Permanently"; break;
566
+ case 302: msg = "Found"; break;
567
+ case 303: msg = "See Other"; break;
568
+ case 304: msg = "Not Modified"; break;
569
+ case 305: msg = "Use Proxy"; break;
570
+ case 307: msg = "Temporary Redirect"; break;
571
+ case 308: msg = "Permanent Redirect"; break;
572
+ case 400: msg = "Bad Request"; break;
573
+ case 401: msg = "Unauthorized"; break;
574
+ case 402: msg = "Payment Required"; break;
575
+ case 403: msg = "Forbidden"; break;
576
+ case 404: msg = "Not Found"; break;
577
+ case 405: msg = "Method Not Allowed"; break;
578
+ case 406: msg = "Not Acceptable"; break;
579
+ case 407: msg = "Proxy Authentication Required"; break;
580
+ case 408: msg = "Request Timeout"; break;
581
+ case 409: msg = "Conflict"; break;
582
+ case 410: msg = "Gone"; break;
583
+ case 411: msg = "Length Required"; break;
584
+ case 412: msg = "Precondition Failed"; break;
585
+ case 413: msg = "Payload Too Large"; break;
586
+ case 414: msg = "Request-URI Too Long"; break;
587
+ case 415: msg = "Unsupported Media Type"; break;
588
+ case 416: msg = "Requested Range Not Satisfiable"; break;
589
+ case 417: msg = "Expectation Failed"; break;
590
+ case 418: msg = "I'm a teapot"; break;
591
+ case 421: msg = "Misdirected Request"; break;
592
+ case 422: msg = "Unprocessable Entity"; break;
593
+ case 423: msg = "Locked"; break;
594
+ case 424: msg = "Failed Dependency"; break;
595
+ case 426: msg = "Upgrade Required"; break;
596
+ case 428: msg = "Precondition Required"; break;
597
+ case 429: msg = "Too Many Requests"; break;
598
+ case 431: msg = "Request Header Fields Too Large"; break;
599
+ case 444: msg = "Connection Closed Without Response"; break;
600
+ case 451: msg = "Unavailable For Legal Reasons"; break;
601
+ case 499: msg = "Client Closed Request"; break;
602
+ case 500: msg = "Internal Server Error"; break;
603
+ case 501: msg = "Not Implemented"; break;
604
+ case 502: msg = "Bad Gateway"; break;
605
+ case 503: msg = "Service Unavailable"; break;
606
+ case 504: msg = "Gateway Timeout"; break;
607
+ case 505: msg = "HTTP Version Not Supported"; break;
608
+ case 506: msg = "Variant Also Negotiates"; break;
609
+ case 507: msg = "Insufficient Storage"; break;
610
+ case 508: msg = "Loop Detected"; break;
611
+ case 510: msg = "Not Extended"; break;
612
+ case 511: msg = "Network Authentication Required"; break;
613
+ case 599: msg = "Network Connect Timeout Error"; break;
614
+ default: break;
615
+ }
616
+ return msg;
617
+ }