-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.c
2284 lines (1878 loc) · 51.6 KB
/
http.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
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _POSIX_C_SOURCE 200809L
#include "libweb/http.h"
#include <dynstr.h>
#include <sys/types.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#define HTTP_VERSION "HTTP/1.1"
struct http_ctx
{
struct ctx
{
enum state
{
START_LINE,
HEADER_CR_LINE,
BODY_LINE
} state;
enum
{
LINE_CR,
LINE_LF
} lstate;
enum http_op op;
char *resource, *field, *value, *boundary;
size_t len;
struct payload
{
char *path;
unsigned long long len, read;
} payload;
union
{
struct start_line
{
enum
{
START_LINE_OP,
START_LINE_RESOURCE,
START_LINE_PROTOCOL
} state;
} sl;
struct multiform
{
enum mf_state
{
MF_START_BOUNDARY,
MF_HEADER_CR_LINE,
MF_BODY_BOUNDARY_LINE,
MF_END_BOUNDARY_CR_LINE
} state;
enum
{
BODY_CR,
BODY_LF,
BODY_DATA
} bstate;
off_t len, written;
char *boundary;
size_t blen, nforms, nfiles, npairs;
int fd;
struct http_post_file *files;
struct http_post_pair *pairs;
struct form
{
char *name, *filename, *tmpname, *value;
} *forms;
} mf;
struct put
{
char *tmpname;
int fd;
off_t written;
} put;
} u;
struct http_arg *args;
size_t n_args, n_headers;
struct http_header *headers;
bool has_length, expect_continue;
} ctx;
struct write_ctx
{
bool pending, close;
enum state state;
struct http_response r;
off_t n;
struct dynstr d;
enum http_op op;
} wctx;
/* From RFC9112, section 3 (Request line):
* It is RECOMMENDED that all HTTP senders and recipients support,
* at a minimum, request-line lengths of 8000 octets. */
char line[8000];
struct http_cfg cfg;
};
static void arg_free(struct http_arg *const a)
{
if (a)
{
free(a->key);
free(a->value);
}
}
static size_t chrcnt(const char *s, const int c)
{
size_t ret = 0;
while (*s++ == c)
ret++;
return ret;
}
static int parse_arg(struct ctx *const c, const char *const arg,
const size_t n)
{
int ret = -1;
struct http_arg a = {0}, *args = NULL;
const char *sep = memchr(arg, '=', n);
char *enckey = NULL, *encvalue = NULL, *deckey = NULL, *decvalue = NULL;
if (!sep)
{
fprintf(stderr, "%s: expected '='\n", __func__);
ret = 1;
goto end;
}
else if (sep == arg)
{
fprintf(stderr, "%s: expected key\n", __func__);
ret = 1;
goto end;
}
const char *const value = sep + 1;
if (!*value)
{
fprintf(stderr, "%s: missing value: %.*s\n", __func__, (int)n, arg);
ret = 1;
goto end;
}
const size_t keylen = sep - arg, valuelen = n - keylen - 1;
if (!(enckey = strndup(arg, keylen)))
{
fprintf(stderr, "%s: strndup(3) key: %s\n", __func__, strerror(errno));
goto end;
}
else if (!(encvalue = strndup(value, valuelen)))
{
fprintf(stderr, "%s: strndup(3) value: %s\n",
__func__, strerror(errno));
goto end;
}
/* URL parameters use '+' for whitespace, rather than %20. */
else if ((ret = http_decode_url(enckey, true, &deckey)))
{
fprintf(stderr, "%s: http_decode_url enckey failed\n", __func__);
goto end;
}
else if ((ret = http_decode_url(encvalue, true, &decvalue)))
{
fprintf(stderr, "%s: http_decode_url encvalue failed\n", __func__);
goto end;
}
a = (const struct http_arg)
{
.key = deckey,
.value = decvalue
};
if (!(args = realloc(c->args, (c->n_args + 1) * sizeof *args)))
{
fprintf(stderr, "%s: realloc(3): %s\n", __func__, strerror(errno));
goto end;
}
args[c->n_args++] = a;
c->args = args;
ret = 0;
end:
if (ret)
{
arg_free(&a);
free(deckey);
free(decvalue);
}
free(enckey);
free(encvalue);
return ret;
}
static int parse_first_arg(struct ctx *const c, const char *const arg,
const char *const ad_arg, const char *const res)
{
int error;
const char *const next = arg + 1;
if (chrcnt(next, '?'))
{
fprintf(stderr, "%s: more than one argument indicator '?' found: %s\n",
__func__, res);
return 1;
}
const size_t n = ad_arg ? ad_arg - next : strlen(next);
if (!n)
{
fprintf(stderr, "%s: unterminated argument: %s\n", __func__, res);
return 1;
}
else if ((error = parse_arg(c, next, n)))
{
fprintf(stderr, "%s: parse_arg failed: %s\n", __func__, res);
return error;
}
return 0;
}
static int parse_adargs(struct ctx *const c, const char *const start,
const char *const res)
{
for (const char *arg = start, *next; arg; arg = next)
{
next = strchr(++arg, '&');
int error;
const size_t n = next ? next - arg : strlen(arg);
if ((error = parse_arg(c, arg, n)))
{
fprintf(stderr, "%s: parse_arg failed: %s\n", __func__, res);
return error;
}
}
return 0;
}
static int parse_args(struct ctx *const c, const char *const res,
size_t *const reslen)
{
int error;
const char *const tmp_arg_start = strrchr(res, '?'),
*const arg_start = tmp_arg_start && *(tmp_arg_start + 1) ?
tmp_arg_start : NULL,
*const ad_arg = arg_start ? strchr(arg_start, '&') : NULL;
if (!arg_start)
{
if (!ad_arg)
{
*reslen = strlen(res);
return 0;
}
else
{
fprintf(stderr, "%s: expected argument indicator '?': %s\n",
__func__, res);
return 1;
}
}
else if (arg_start == res)
{
fprintf(stderr, "%s: expected resource: %s\n", __func__, res);
return 1;
}
else if (ad_arg && ad_arg <= arg_start)
{
fprintf(stderr, "%s: expected '?' before '&': %s\n", __func__, res);
return 1;
}
else if ((error = parse_first_arg(c, arg_start, ad_arg, res)))
{
fprintf(stderr, "%s: parse_first_arg failed\n", __func__);
return error;
}
else if ((error = parse_adargs(c, ad_arg, res)))
{
fprintf(stderr, "%s: parse_adargs failed\n", __func__);
return error;
}
*reslen = arg_start - res;
return 0;
}
static int parse_resource(struct ctx *const c, const char *const enc_res)
{
int ret = -1, error;
size_t reslen;
char *trimmed_encres = NULL, *resource = NULL;
if ((error = parse_args(c, enc_res, &reslen)))
{
fprintf(stderr, "%s: parse_args failed\n", __func__);
ret = error;
goto end;
}
else if (!(trimmed_encres = strndup(enc_res, reslen)))
{
fprintf(stderr, "%s: strndup(3): %s\n", __func__, strerror(errno));
goto end;
}
else if ((ret = http_decode_url(trimmed_encres, false, &resource)))
{
fprintf(stderr, "%s: http_decode_url failed\n", __func__);
goto end;
}
c->resource = resource;
ret = 0;
end:
free(trimmed_encres);
return ret;
}
static int get_op(const char *const line, const size_t n,
enum http_op *const out)
{
static const char *const ops[] =
{
[HTTP_OP_GET] = "GET",
[HTTP_OP_POST] = "POST",
[HTTP_OP_HEAD] = "HEAD",
[HTTP_OP_PUT] = "PUT"
};
for (enum http_op op = 0; op < sizeof ops / sizeof *ops; op++)
if (!strncmp(line, ops[op], n))
{
*out = op;
return 0;
}
return -1;
}
static int start_line(struct http_ctx *const h)
{
const char *const line = h->line;
if (!*line)
{
fprintf(stderr, "%s: expected non-empty line\n", __func__);
return 1;
}
const char *const op = strchr(line, ' ');
if (!op || op == line)
{
fprintf(stderr, "%s: expected resource\n", __func__);
return 1;
}
struct ctx *const c = &h->ctx;
const size_t n = op - line;
if (get_op(line, n, &c->op))
{
fprintf(stderr, "%s: unsupported HTTP op %.*s\n",
__func__, (int)n, line);
return 1;
}
const char *const resource = op + 1,
*const res_end = strchr(resource, ' ');
if (!res_end)
{
fprintf(stderr, "%s: expected protocol version\n", __func__);
return 1;
}
const size_t res_n = res_end - resource;
if (memchr(resource, '*', res_n))
{
fprintf(stderr, "%s: illegal character * in resource %.*s\n",
__func__, (int)res_n, resource);
return 1;
}
const char *const protocol = res_end + 1;
if (!*protocol)
{
fprintf(stderr, "%s: expected protocol version\n", __func__);
return 1;
}
else if (strchr(protocol, ' '))
{
fprintf(stderr, "%s: unexpected field after protocol version\n",
__func__);
return 1;
}
int ret = 1, error;
char *enc_res = NULL;
if (strcmp(protocol, HTTP_VERSION))
{
fprintf(stderr, "%s: unsupported protocol %s\n", __func__, protocol);
goto end;
}
else if (!(enc_res = strndup(resource, res_n)))
{
fprintf(stderr, "%s: strndup(3): %s\n", __func__, strerror(errno));
ret = -1;
goto end;
}
else if ((error = parse_resource(c, enc_res)))
{
fprintf(stderr, "%s: parse_resource failed\n", __func__);
ret = error;
goto end;
}
printf("%.*s %s %s\n", (int)n, line, c->resource, protocol);
ret = 0;
c->state = HEADER_CR_LINE;
end:
free(enc_res);
return ret;
}
static void ctx_free(struct ctx *const c)
{
if (c->boundary)
{
struct multiform *const m = &c->u.mf;
free(m->files);
free(m->pairs);
free(m->boundary);
if (m->fd >= 0 && close(m->fd))
fprintf(stderr, "%s: close(2) m->fd: %s\n",
__func__, strerror(errno));
for (size_t i = 0; i < m->nforms; i++)
{
struct form *const f = &m->forms[i];
free(f->name);
free(f->filename);
free(f->value);
if (f->tmpname && remove(f->tmpname) && errno != ENOENT)
fprintf(stderr, "%s: remove(3) %s: %s\n",
__func__, f->tmpname, strerror(errno));
free(f->tmpname);
}
free(m->forms);
}
else if (c->op == HTTP_OP_PUT)
{
struct put *const p = &c->u.put;
if (p->fd >= 0 && close(p->fd))
fprintf(stderr, "%s: close(2) p->fd: %s\n",
__func__, strerror(errno));
free(c->u.put.tmpname);
}
free(c->field);
free(c->value);
free(c->resource);
free(c->boundary);
for (size_t i = 0; i < c->n_args; i++)
arg_free(&c->args[i]);
for (size_t i = 0; i < c->n_headers; i++)
{
const struct http_header *const hdr = &c->headers[i];
free(hdr->header);
free(hdr->value);
}
free(c->headers);
free(c->args);
*c = (const struct ctx){0};
}
static void free_response_headers(struct http_response *const r)
{
for (size_t i = 0; i < r->n_headers; i++)
{
const struct http_header *const hdr = &r->headers[i];
free(hdr->header);
free(hdr->value);
}
free(r->headers);
r->headers = NULL;
r->n_headers = 0;
}
static int prepare_headers(struct http_ctx *const h)
{
int ret = -1;
struct write_ctx *const w = &h->wctx;
struct dynstr *const d = &w->d;
struct http_response *const r = &w->r;
dynstr_init(d);
for (size_t i = 0; i < w->r.n_headers; i++)
{
const struct http_header *const hdr = &r->headers[i];
if (dynstr_append(d, "%s: %s\r\n", hdr->header, hdr->value))
{
fprintf(stderr, "%s: dynstr_append hdr failed\n", __func__);
goto end;
}
}
if (dynstr_append(d, "\r\n"))
{
fprintf(stderr, "%s: dynstr_append crlf failed\n", __func__);
goto end;
}
ret = 0;
end:
free_response_headers(r);
return ret;
}
static int rw_error(const int r, bool *const close)
{
if (r < 0)
{
switch (errno)
{
case EPIPE:
/* Fall through. */
case ECONNRESET:
*close = true;
return 1;
case EAGAIN:
return 0;
default:
break;
}
fprintf(stderr, "%s: %s\n", __func__, strerror(errno));
return -1;
}
else if (!r)
{
*close = true;
return 0;
}
fprintf(stderr, "%s: unexpected value %d\n", __func__, r);
return -1;
}
static int write_start_line(struct http_ctx *const h, bool *const close)
{
struct write_ctx *const w = &h->wctx;
struct dynstr *const d = &w->d;
const size_t rem = d->len - w->n;
const int res = h->cfg.write(d->str + w->n, rem, h->cfg.user);
if (res <= 0)
return rw_error(res, close);
else if ((w->n += res) >= d->len)
{
char len[sizeof "18446744073709551615"];
const int res = snprintf(len, sizeof len, "%llu", w->r.n);
dynstr_free(d);
if (res < 0 || res >= sizeof len)
{
fprintf(stderr, "%s: snprintf(3) failed\n", __func__);
return -1;
}
else if (http_response_add_header(&w->r, "Content-Length", len))
{
fprintf(stderr, "%s: http_response_add_header failed\n", __func__);
return -1;
}
else if (prepare_headers(h))
{
fprintf(stderr, "%s: prepare_headers failed\n", __func__);
return -1;
}
w->state = HEADER_CR_LINE;
w->n = 0;
}
return 0;
}
static int write_ctx_free(struct write_ctx *const w)
{
int ret = 0;
const struct http_response *const r = &w->r;
if (r->free)
r->free(r->buf.rw);
if (r->f && (ret = fclose(r->f)))
fprintf(stderr, "%s: fclose(3): %s\n", __func__, strerror(errno));
dynstr_free(&w->d);
free_response_headers(&w->r);
*w = (const struct write_ctx){0};
return ret;
}
static bool must_write_body(const struct write_ctx *const w)
{
return w->op != HTTP_OP_HEAD;
}
static int write_header_cr_line(struct http_ctx *const h, bool *const close)
{
struct write_ctx *const w = &h->wctx;
struct dynstr *const d = &w->d;
const size_t rem = d->len - w->n;
const int res = h->cfg.write(d->str + w->n, rem, h->cfg.user);
if (res <= 0)
return rw_error(res, close);
else if ((w->n += res) >= d->len)
{
const bool close_pending = w->close;
dynstr_free(d);
if (w->r.n && must_write_body(w))
{
w->state = BODY_LINE;
w->n = 0;
}
else if (write_ctx_free(w))
{
fprintf(stderr, "%s: write_ctx_free failed\n", __func__);
return -1;
}
else if (close_pending)
*close = true;
}
return 0;
}
static int write_body_mem(struct http_ctx *const h, bool *const close)
{
struct write_ctx *const w = &h->wctx;
const struct http_response *const r = &w->r;
const size_t rem = r->n - w->n;
const int res = h->cfg.write((const char *)r->buf.ro + w->n, rem,
h->cfg.user);
if (res <= 0)
return rw_error(res, close);
else if ((w->n += res) >= r->n)
{
const bool close_pending = w->close;
if (write_ctx_free(w))
{
fprintf(stderr, "%s: write_ctx_free failed\n", __func__);
return -1;
}
else if (close_pending)
*close = true;
return res;
}
return 0;
}
static int write_body_file(struct http_ctx *const h, bool *const close)
{
struct write_ctx *const w = &h->wctx;
const struct http_response *const r = &w->r;
const unsigned long long left = r->n - w->n;
char buf[BUFSIZ];
const size_t rem = left > sizeof buf ? sizeof buf : left;
if (!fread(buf, 1, rem, r->f))
{
fprintf(stderr, "%s: fread(3) failed, ferror=%d, feof=%d\n",
__func__, ferror(r->f), feof(r->f));
return -1;
}
const int res = h->cfg.write(buf, rem, h->cfg.user);
if (res <= 0)
return rw_error(res, close);
else if ((w->n += res) >= r->n)
{
const bool close_pending = w->close;
if (write_ctx_free(w))
{
fprintf(stderr, "%s: write_ctx_free failed\n", __func__);
return -1;
}
else if (close_pending)
*close = true;
}
return 0;
}
static int write_body_line(struct http_ctx *const h, bool *const close)
{
const struct http_response *const r = &h->wctx.r;
if (r->buf.ro)
return write_body_mem(h, close);
else if (r->f)
return write_body_file(h, close);
fprintf(stderr, "%s: expected either buffer or file path\n", __func__);
return -1;
}
static int http_write(struct http_ctx *const h, bool *const close)
{
static int (*const fn[])(struct http_ctx *, bool *) =
{
[START_LINE] = write_start_line,
[HEADER_CR_LINE] = write_header_cr_line,
[BODY_LINE] = write_body_line,
};
struct write_ctx *const w = &h->wctx;
const int ret = fn[w->state](h, close);
if (ret)
write_ctx_free(w);
return ret;
}
int http_response_add_header(struct http_response *const r,
const char *const header, const char *const value)
{
const size_t n = r->n_headers + 1;
struct http_header *const headers = realloc(r->headers,
n * sizeof *r->headers), *h = NULL;
if (!headers)
{
fprintf(stderr, "%s: realloc(3): %s\n", __func__, strerror(errno));
return -1;
}
h = &headers[r->n_headers];
*h = (const struct http_header)
{
.header = strdup(header),
.value = strdup(value)
};
if (!h->header || !h->value)
{
fprintf(stderr, "%s: strdup(3): %s\n", __func__, strerror(errno));
free(h->header);
free(h->value);
return -1;
}
r->headers = headers;
r->n_headers = n;
return 0;
}
static int start_response(struct http_ctx *const h)
{
static const struct code
{
const char *descr;
int code;
} codes[] =
{
#define X(x, y, z) [HTTP_STATUS_##x] = {.descr = y, .code = z},
HTTP_STATUSES
#undef X
};
struct write_ctx *const w = &h->wctx;
const struct code *const c = &codes[w->r.status];
w->pending = true;
dynstr_init(&w->d);
if (dynstr_append(&w->d, HTTP_VERSION " %d %s\r\n", c->code, c->descr))
{
fprintf(stderr, "%s: dynstr_append failed\n", __func__);
return -1;
}
return 0;
}
static int set_cookie(struct http_ctx *const h, const char *const cookie)
{
struct ctx *const c = &h->ctx;
const char *const value = strchr(cookie, '=');
if (!value)
{
fprintf(stderr, "%s: expected field=value for cookie %s\n",
__func__, cookie);
goto failure;
}
else if (!*(value + 1))
{
fprintf(stderr, "%s: expected non-empty value for cookie %s\n",
__func__, cookie);
goto failure;
}
else if (!(c->field = strndup(cookie, value - cookie)))
{
fprintf(stderr, "%s: malloc(3) field: %s\n", __func__, strerror(errno));
goto failure;
}
else if (!(c->value = strdup(value + 1)))
{
fprintf(stderr, "%s: malloc(3) value: %s\n", __func__, strerror(errno));
goto failure;
}
return 0;
failure:
free(c->field);
free(c->value);
return -1;
}
static int set_length(struct http_ctx *const h, const char *const len)
{
char *end;
errno = 0;
const unsigned long long value = strtoull(len, &end, 10);
if (errno || *end != '\0')
{
fprintf(stderr, "%s: invalid length %s: %s\n",
__func__, len, strerror(errno));
return 1;
}
struct ctx *const c = &h->ctx;
switch (c->op)
{
case HTTP_OP_POST:
/* Fall through. */
case HTTP_OP_PUT:
c->payload.len = value;
c->u.put = (const struct put){.fd = -1};
break;
case HTTP_OP_GET:
/* Fall through. */
case HTTP_OP_HEAD:
fprintf(stderr, "%s: unexpected header for HTTP op %d\n",
__func__, c->op);
return 1;
}
c->has_length = true;
return 0;
}
static int set_content_type(struct http_ctx *const h, const char *const type)
{
const char *const sep = strchr(type, ';');
if (!sep)
/* No multipart/form-data expected. */
return 0;
const size_t n = sep - type;
if (strncmp(type, "multipart/form-data", n))
{
fprintf(stderr, "%s: unsupported Content-Type %.*s\n",
__func__, (int)n, type);
return 1;
}
else if (h->ctx.op != HTTP_OP_POST)
{
fprintf(stderr, "%s: multipart/form-data only expected for POST\n",
__func__);
return 1;
}
const char *boundary = sep + 1;
while (*boundary == ' ')
boundary++;
if (!*boundary)
{
fprintf(stderr, "%s: expected boundary\n", __func__);
return 1;
}
const char *const eq = strchr(boundary, '=');
if (!eq)
{
fprintf(stderr, "%s: expected = after boundary\n", __func__);
return 1;
}
const size_t bn = eq - boundary;
if (strncmp(boundary, "boundary", bn))
{
fprintf(stderr, "%s: expected boundary, got %.*s\n",
__func__, (int)bn, boundary);
return 1;
}
const char *val = eq + 1;
while (*val == ' ')
val++;
if (!*val)
{
fprintf(stderr, "%s: expected value after boundary\n", __func__);
return 1;
}
struct ctx *const c = &h->ctx;
struct dynstr b;
dynstr_init(&b);
if (dynstr_append(&b, "\r\n--%s", val))
{
fprintf(stderr, "%s: dynstr_append failed\n", __func__);
return -1;
}
c->boundary = b.str;