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
|
/* $OpenBSD: server_fcgi.c,v 1.47 2015/01/18 14:01:17 florian Exp $ */
/*
* Copyright (c) 2014 Florian Obser <florian@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/tree.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <err.h>
#include <event.h>
#include "httpd.h"
#include "http.h"
#define FCGI_PADDING_SIZE 255
#define FCGI_RECORD_SIZE \
(sizeof(struct fcgi_record_header) + FCGI_CONTENT_SIZE + FCGI_PADDING_SIZE)
#define FCGI_BEGIN_REQUEST 1
#define FCGI_ABORT_REQUEST 2
#define FCGI_END_REQUEST 3
#define FCGI_PARAMS 4
#define FCGI_STDIN 5
#define FCGI_STDOUT 6
#define FCGI_STDERR 7
#define FCGI_DATA 8
#define FCGI_GET_VALUES 9
#define FCGI_GET_VALUES_RESULT 10
#define FCGI_UNKNOWN_TYPE 11
#define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
#define FCGI_RESPONDER 1
struct fcgi_record_header {
uint8_t version;
uint8_t type;
uint16_t id;
uint16_t content_len;
uint8_t padding_len;
uint8_t reserved;
} __packed;
struct fcgi_begin_request_body {
uint16_t role;
uint8_t flags;
uint8_t reserved[5];
} __packed;
struct server_fcgi_param {
int total_len;
uint8_t buf[FCGI_RECORD_SIZE];
};
int server_fcgi_header(struct client *, u_int);
void server_fcgi_read(struct bufferevent *, void *);
int server_fcgi_writeheader(struct client *, struct kv *, void *);
int server_fcgi_writechunk(struct client *);
int server_fcgi_getheaders(struct client *);
int fcgi_add_param(struct server_fcgi_param *, const char *, const char *,
struct client *);
int
server_fcgi(struct httpd *env, struct client *clt)
{
struct server_fcgi_param param;
struct server_config *srv_conf = clt->clt_srv_conf;
struct http_descriptor *desc = clt->clt_descreq;
struct fcgi_record_header *h;
struct fcgi_begin_request_body *begin;
char hbuf[HOST_NAME_MAX+1];
size_t scriptlen;
int pathlen;
int fd = -1, ret;
const char *stripped, *p, *alias, *errstr = NULL;
char *str, *script = NULL;
if (srv_conf->socket[0] == ':') {
struct sockaddr_storage ss;
in_port_t port;
p = srv_conf->socket + 1;
port = strtonum(p, 0, 0xffff, &errstr);
if (errstr != NULL) {
log_warn("%s: strtonum %s, %s", __func__, p, errstr);
goto fail;
}
memset(&ss, 0, sizeof(ss));
ss.ss_family = AF_INET;
((struct sockaddr_in *)
&ss)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
port = htons(port);
if ((fd = server_socket_connect(&ss, port, srv_conf)) == -1)
goto fail;
} else {
struct sockaddr_un sun;
size_t len;
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
goto fail;
memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_UNIX;
len = strlcpy(sun.sun_path,
srv_conf->socket, sizeof(sun.sun_path));
if (len >= sizeof(sun.sun_path)) {
errstr = "socket path to long";
goto fail;
}
sun.sun_len = len;
if (connect(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1)
goto fail;
}
socket_set_blockmode(fd, BM_NONBLOCK);
memset(hbuf, 0, sizeof(hbuf));
clt->clt_fcgi_state = FCGI_READ_HEADER;
clt->clt_fcgi_toread = sizeof(struct fcgi_record_header);
if (clt->clt_srvevb != NULL)
evbuffer_free(clt->clt_srvevb);
clt->clt_srvevb = evbuffer_new();
if (clt->clt_srvevb == NULL) {
errstr = "failed to allocate evbuffer";
goto fail;
}
clt->clt_fd = fd;
if (clt->clt_srvbev != NULL)
bufferevent_free(clt->clt_srvbev);
clt->clt_srvbev = bufferevent_new(fd, server_fcgi_read,
NULL, server_file_error, clt);
if (clt->clt_srvbev == NULL) {
errstr = "failed to allocate fcgi buffer event";
goto fail;
}
memset(¶m, 0, sizeof(param));
h = (struct fcgi_record_header *)¶m.buf;
h->version = 1;
h->type = FCGI_BEGIN_REQUEST;
h->id = htons(1);
h->content_len = htons(sizeof(struct fcgi_begin_request_body));
h->padding_len = 0;
begin = (struct fcgi_begin_request_body *)¶m.buf[sizeof(struct
fcgi_record_header)];
begin->role = htons(FCGI_RESPONDER);
bufferevent_write(clt->clt_srvbev, ¶m.buf,
sizeof(struct fcgi_record_header) +
sizeof(struct fcgi_begin_request_body));
h->type = FCGI_PARAMS;
h->content_len = param.total_len = 0;
alias = desc->http_path_alias != NULL
? desc->http_path_alias
: desc->http_path;
stripped = server_root_strip(alias, srv_conf->strip);
if ((pathlen = asprintf(&script, "%s%s", srv_conf->root, stripped))
== -1) {
errstr = "failed to get script name";
goto fail;
}
scriptlen = path_info(script);
/*
* no part of root should show up in PATH_INFO.
* therefore scriptlen should be >= strlen(root)
*/
if (scriptlen < strlen(srv_conf->root))
scriptlen = strlen(srv_conf->root);
if ((int)scriptlen < pathlen) {
if (fcgi_add_param(¶m, "PATH_INFO",
script + scriptlen, clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
script[scriptlen] = '\0';
}
/*
* calculate length of http SCRIPT_NAME:
* add length of stripped prefix,
* subtract length of prepended local root
*/
scriptlen += (stripped - alias) - strlen(srv_conf->root);
if ((str = strndup(alias, scriptlen)) == NULL)
goto fail;
ret = fcgi_add_param(¶m, "SCRIPT_NAME", str, clt);
free(str);
if (ret == -1) {
errstr = "failed to encode param";
goto fail;
}
if (fcgi_add_param(¶m, "SCRIPT_FILENAME", script, clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (desc->http_query)
if (fcgi_add_param(¶m, "QUERY_STRING", desc->http_query,
clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (fcgi_add_param(¶m, "DOCUMENT_ROOT", srv_conf->root,
clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (fcgi_add_param(¶m, "DOCUMENT_URI", desc->http_path,
clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (fcgi_add_param(¶m, "GATEWAY_INTERFACE", "CGI/1.1",
clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (srv_conf->flags & SRVFLAG_AUTH_BASIC) {
if (fcgi_add_param(¶m, "REMOTE_USER",
clt->clt_fcgi_remote_user, clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
}
/* Add HTTP_* headers */
if (server_headers(clt, desc, server_fcgi_writeheader, ¶m) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (srv_conf->flags & SRVFLAG_TLS)
if (fcgi_add_param(¶m, "HTTPS", "on", clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
(void)print_host(&clt->clt_ss, hbuf, sizeof(hbuf));
if (fcgi_add_param(¶m, "REMOTE_ADDR", hbuf, clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
(void)snprintf(hbuf, sizeof(hbuf), "%d", ntohs(clt->clt_port));
if (fcgi_add_param(¶m, "REMOTE_PORT", hbuf, clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (fcgi_add_param(¶m, "REQUEST_METHOD",
server_httpmethod_byid(desc->http_method), clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (!desc->http_query) {
if (fcgi_add_param(¶m, "REQUEST_URI", desc->http_path,
clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
} else if (asprintf(&str, "%s?%s", desc->http_path,
desc->http_query) != -1) {
ret = fcgi_add_param(¶m, "REQUEST_URI", str, clt);
free(str);
if (ret == -1) {
errstr = "failed to encode param";
goto fail;
}
}
(void)print_host(&clt->clt_srv_ss, hbuf, sizeof(hbuf));
if (fcgi_add_param(¶m, "SERVER_ADDR", hbuf, clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
(void)snprintf(hbuf, sizeof(hbuf), "%d",
ntohs(server_socket_getport(&clt->clt_srv_ss)));
if (fcgi_add_param(¶m, "SERVER_PORT", hbuf, clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (fcgi_add_param(¶m, "SERVER_NAME", srv_conf->name,
clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (fcgi_add_param(¶m, "SERVER_PROTOCOL", desc->http_version,
clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (fcgi_add_param(¶m, "SERVER_SOFTWARE", HTTPD_SERVERNAME,
clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (param.total_len != 0) { /* send last params record */
bufferevent_write(clt->clt_srvbev, ¶m.buf,
sizeof(struct fcgi_record_header) +
ntohs(h->content_len));
}
/* send "no more params" message */
h->content_len = 0;
bufferevent_write(clt->clt_srvbev, ¶m.buf,
sizeof(struct fcgi_record_header));
bufferevent_settimeout(clt->clt_srvbev,
srv_conf->timeout.tv_sec, srv_conf->timeout.tv_sec);
bufferevent_enable(clt->clt_srvbev, EV_READ|EV_WRITE);
if (clt->clt_toread != 0) {
server_read_httpcontent(clt->clt_bev, clt);
bufferevent_enable(clt->clt_bev, EV_READ);
} else {
bufferevent_disable(clt->clt_bev, EV_READ);
fcgi_add_stdin(clt, NULL);
}
if (strcmp(desc->http_version, "HTTP/1.1") == 0) {
clt->clt_fcgi_chunked = 1;
} else {
/* HTTP/1.0 does not support chunked encoding */
clt->clt_fcgi_chunked = 0;
clt->clt_persist = 0;
}
clt->clt_fcgi_end = 0;
clt->clt_done = 0;
free(script);
return (0);
fail:
free(script);
if (errstr == NULL)
errstr = strerror(errno);
server_abort_http(clt, 500, errstr);
return (-1);
}
int
fcgi_add_stdin(struct client *clt, struct evbuffer *evbuf)
{
struct fcgi_record_header h;
memset(&h, 0, sizeof(h));
h.version = 1;
h.type = FCGI_STDIN;
h.id = htons(1);
h.padding_len = 0;
if (evbuf == NULL) {
h.content_len = 0;
return bufferevent_write(clt->clt_srvbev, &h,
sizeof(struct fcgi_record_header));
} else {
h.content_len = htons(EVBUFFER_LENGTH(evbuf));
if (bufferevent_write(clt->clt_srvbev, &h,
sizeof(struct fcgi_record_header)) == -1)
return -1;
return bufferevent_write_buffer(clt->clt_srvbev, evbuf);
}
return (0);
}
int
fcgi_add_param(struct server_fcgi_param *p, const char *key,
const char *val, struct client *clt)
{
struct fcgi_record_header *h;
int len = 0;
int key_len = strlen(key);
int val_len = strlen(val);
uint8_t *param;
len += key_len + val_len;
len += key_len > 127 ? 4 : 1;
len += val_len > 127 ? 4 : 1;
DPRINTF("%s: %s[%d] => %s[%d], total_len: %d", __func__, key, key_len,
val, val_len, p->total_len);
if (len > FCGI_CONTENT_SIZE)
return (-1);
if (p->total_len + len > FCGI_CONTENT_SIZE) {
bufferevent_write(clt->clt_srvbev, p->buf,
sizeof(struct fcgi_record_header) + p->total_len);
p->total_len = 0;
}
h = (struct fcgi_record_header *)p->buf;
param = p->buf + sizeof(*h) + p->total_len;
if (key_len > 127) {
*param++ = ((key_len >> 24) & 0xff) | 0x80;
*param++ = ((key_len >> 16) & 0xff);
*param++ = ((key_len >> 8) & 0xff);
*param++ = (key_len & 0xff);
} else
*param++ = key_len;
if (val_len > 127) {
*param++ = ((val_len >> 24) & 0xff) | 0x80;
*param++ = ((val_len >> 16) & 0xff);
*param++ = ((val_len >> 8) & 0xff);
*param++ = (val_len & 0xff);
} else
*param++ = val_len;
memcpy(param, key, key_len);
param += key_len;
memcpy(param, val, val_len);
p->total_len += len;
h->content_len = htons(p->total_len);
return (0);
}
void
server_fcgi_read(struct bufferevent *bev, void *arg)
{
uint8_t buf[FCGI_RECORD_SIZE];
struct client *clt = (struct client *) arg;
struct fcgi_record_header *h;
size_t len;
char *ptr;
do {
len = bufferevent_read(bev, buf, clt->clt_fcgi_toread);
/* XXX error handling */
evbuffer_add(clt->clt_srvevb, buf, len);
clt->clt_fcgi_toread -= len;
DPRINTF("%s: len: %lu toread: %d state: %d type: %d",
__func__, len, clt->clt_fcgi_toread,
clt->clt_fcgi_state, clt->clt_fcgi_type);
if (clt->clt_fcgi_toread != 0)
return;
switch (clt->clt_fcgi_state) {
case FCGI_READ_HEADER:
clt->clt_fcgi_state = FCGI_READ_CONTENT;
h = (struct fcgi_record_header *)
EVBUFFER_DATA(clt->clt_srvevb);
DPRINTF("%s: record header: version %d type %d id %d "
"content len %d padding %d", __func__,
h->version, h->type, ntohs(h->id),
ntohs(h->content_len), h->padding_len);
clt->clt_fcgi_type = h->type;
clt->clt_fcgi_toread = ntohs(h->content_len);
clt->clt_fcgi_padding_len = h->padding_len;
evbuffer_drain(clt->clt_srvevb,
EVBUFFER_LENGTH(clt->clt_srvevb));
if (clt->clt_fcgi_toread != 0)
break;
else if (clt->clt_fcgi_type == FCGI_STDOUT &&
!clt->clt_chunk) {
server_abort_http(clt, 500, "empty stdout");
return;
}
/* fallthrough if content_len == 0 */
case FCGI_READ_CONTENT:
switch (clt->clt_fcgi_type) {
case FCGI_STDERR:
if (EVBUFFER_LENGTH(clt->clt_srvevb) > 0 &&
(ptr = get_string(
EVBUFFER_DATA(clt->clt_srvevb),
EVBUFFER_LENGTH(clt->clt_srvevb)))
!= NULL) {
server_sendlog(clt->clt_srv_conf,
IMSG_LOG_ERROR, "%s", ptr);
free(ptr);
}
break;
case FCGI_STDOUT:
if (++clt->clt_chunk == 1) {
if (server_fcgi_header(clt,
server_fcgi_getheaders(clt))
== -1) {
server_abort_http(clt, 500,
"malformed fcgi headers");
return;
}
if (!EVBUFFER_LENGTH(clt->clt_srvevb))
break;
}
/* FALLTHROUGH */
case FCGI_END_REQUEST:
if (server_fcgi_writechunk(clt) == -1) {
server_abort_http(clt, 500,
"encoding error");
return;
}
break;
}
evbuffer_drain(clt->clt_srvevb,
EVBUFFER_LENGTH(clt->clt_srvevb));
if (!clt->clt_fcgi_padding_len) {
clt->clt_fcgi_state = FCGI_READ_HEADER;
clt->clt_fcgi_toread =
sizeof(struct fcgi_record_header);
} else {
clt->clt_fcgi_state = FCGI_READ_PADDING;
clt->clt_fcgi_toread =
clt->clt_fcgi_padding_len;
}
break;
case FCGI_READ_PADDING:
evbuffer_drain(clt->clt_srvevb,
EVBUFFER_LENGTH(clt->clt_srvevb));
clt->clt_fcgi_state = FCGI_READ_HEADER;
clt->clt_fcgi_toread =
sizeof(struct fcgi_record_header);
break;
}
} while (len > 0);
}
int
server_fcgi_header(struct client *clt, u_int code)
{
struct http_descriptor *desc = clt->clt_descreq;
struct http_descriptor *resp = clt->clt_descresp;
const char *error;
char tmbuf[32];
struct kv *kv, key;
if (desc == NULL || (error = server_httperror_byid(code)) == NULL)
return (-1);
if (server_log_http(clt, code, 0) == -1)
return (-1);
/* Add error codes */
if (kv_setkey(&resp->http_pathquery, "%lu", code) == -1 ||
kv_set(&resp->http_pathquery, "%s", error) == -1)
return (-1);
/* Add headers */
if (kv_add(&resp->http_headers, "Server", HTTPD_SERVERNAME) == NULL)
return (-1);
/* Set chunked encoding */
if (clt->clt_fcgi_chunked) {
/* XXX Should we keep and handle Content-Length instead? */
key.kv_key = "Content-Length";
if ((kv = kv_find(&resp->http_headers, &key)) != NULL)
kv_delete(&resp->http_headers, kv);
/*
* XXX What if the FastCGI added some kind of Transfer-Encoding?
* XXX like gzip, deflate or even "chunked"?
*/
if (kv_add(&resp->http_headers,
"Transfer-Encoding", "chunked") == NULL)
return (-1);
}
/* Is it a persistent connection? */
if (clt->clt_persist) {
if (kv_add(&resp->http_headers,
"Connection", "keep-alive") == NULL)
return (-1);
} else if (kv_add(&resp->http_headers, "Connection", "close") == NULL)
return (-1);
/* Date header is mandatory and should be added as late as possible */
if (server_http_time(time(NULL), tmbuf, sizeof(tmbuf)) <= 0 ||
kv_add(&resp->http_headers, "Date", tmbuf) == NULL)
return (-1);
/* Write initial header (fcgi might append more) */
if (server_writeresponse_http(clt) == -1 ||
server_bufferevent_print(clt, "\r\n") == -1 ||
server_headers(clt, resp, server_writeheader_http, NULL) == -1 ||
server_bufferevent_print(clt, "\r\n") == -1)
return (-1);
return (0);
}
int
server_fcgi_writeheader(struct client *clt, struct kv *hdr, void *arg)
{
struct server_fcgi_param *param = arg;
char *val, *name, *p;
const char *key;
int ret;
if (hdr->kv_flags & KV_FLAG_INVALID)
return (0);
/* The key might have been updated in the parent */
if (hdr->kv_parent != NULL && hdr->kv_parent->kv_key != NULL)
key = hdr->kv_parent->kv_key;
else
key = hdr->kv_key;
val = hdr->kv_value;
if (strcasecmp(key, "Content-Length") == 0 ||
strcasecmp(key, "Content-Type") == 0) {
if ((name = strdup(key)) == NULL)
return (-1);
} else {
if (asprintf(&name, "HTTP_%s", key) == -1)
return (-1);
}
for (p = name; *p != '\0'; p++) {
if (isalpha((unsigned char)*p))
*p = toupper((unsigned char)*p);
else
*p = '_';
}
ret = fcgi_add_param(param, name, val, clt);
free(name);
return (ret);
}
int
server_fcgi_writechunk(struct client *clt)
{
struct evbuffer *evb = clt->clt_srvevb;
size_t len;
if (clt->clt_fcgi_type == FCGI_END_REQUEST) {
len = 0;
} else
len = EVBUFFER_LENGTH(evb);
if (clt->clt_fcgi_chunked) {
/* If len is 0, make sure to write the end marker only once */
if (len == 0 && clt->clt_fcgi_end++)
return (0);
if (server_bufferevent_printf(clt, "%zx\r\n", len) == -1 ||
server_bufferevent_write_chunk(clt, evb, len) == -1 ||
server_bufferevent_print(clt, "\r\n") == -1)
return (-1);
} else if (len)
return (server_bufferevent_write_buffer(clt, evb));
return (0);
}
int
server_fcgi_getheaders(struct client *clt)
{
struct http_descriptor *resp = clt->clt_descresp;
struct evbuffer *evb = clt->clt_srvevb;
int code = 200;
char *line, *key, *value;
const char *errstr;
while ((line = evbuffer_getline(evb)) != NULL && *line != '\0') {
key = line;
if ((value = strchr(key, ':')) == NULL)
break;
if (*value == ':') {
*value++ = '\0';
value += strspn(value, " \t");
} else {
*value++ = '\0';
}
DPRINTF("%s: %s: %s", __func__, key, value);
if (strcasecmp("Status", key) == 0) {
value[strcspn(value, " \t")] = '\0';
code = (int)strtonum(value, 100, 600, &errstr);
if (errstr != NULL || server_httperror_byid(
code) == NULL)
code = 200;
} else {
(void)kv_add(&resp->http_headers, key, value);
}
free(line);
}
return (code);
}
|