-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrequest.c
927 lines (802 loc) · 26.4 KB
/
request.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
/*
* Boa, an http server
* Copyright (C) 1995 Paul Phillips <[email protected]>
* Some changes Copyright (C) 1996,97 Larry Doolittle <[email protected]>
* Some changes Copyright (C) 1996-2002 Jon Nelson <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/* $Id: request.c,v 1.112.2.3 2002/07/24 03:03:59 jnelson Exp $*/
#include "boa.h"
#include <stddef.h> /* for offsetof */
char *g_onvif_buffer;
int total_connections;
struct status status;
static int sockbufsize = SOCKETBUF_SIZE;
/* function prototypes located in this file only */
static void free_request(request ** list_head_addr, request * req);
/*
* Name: new_request
* Description: Obtains a request struct off the free list, or if the
* free list is empty, allocates memory
*
* Return value: pointer to initialized request
*/
request *new_request(void)
{
request *req;
if (request_free) {
req = request_free; /* first on free list */
dequeue(&request_free, request_free); /* dequeue the head */
} else {
req = (request *) malloc(sizeof (request));
if (!req) {
log_error_time();
perror("malloc for new request");
return NULL;
}
}
memset(req, 0, offsetof(request, buffer) + 1);
return req;
}
/*
* Name: get_request
*
* Description: Polls the server socket for a request. If one exists,
* does some basic initialization and adds it to the ready queue;.
*/
void get_request(int server_s)
{
int fd; /* socket */
struct SOCKADDR remote_addr; /* address */
struct SOCKADDR salocal;
int remote_addrlen = sizeof (struct SOCKADDR);
request *conn; /* connection */
size_t len;
static int system_bufsize = 0; /* Default size of SNDBUF given by system */
remote_addr.S_FAMILY = 0xdead;
fd = accept(server_s, (struct sockaddr *) &remote_addr,
&remote_addrlen);
if (fd == -1) {
if (errno != EAGAIN && errno != EWOULDBLOCK)
/* abnormal error */
WARN("accept");
else
/* no requests */
pending_requests = 0;
return;
}
if (fd >= FD_SETSIZE) {
WARN("Got fd >= FD_SETSIZE.");
close(fd);
return;
}
#ifdef DEBUGNONINET
/* This shows up due to race conditions in some Linux kernels
when the client closes the socket sometime between
the select() and accept() syscalls.
Code and description by Larry Doolittle <[email protected]>
*/
#define HEX(x) (((x)>9)?(('a'-10)+(x)):('0'+(x)))
if (remote_addr.sin_family != AF_INET) {
struct sockaddr *bogus = (struct sockaddr *) &remote_addr;
char *ap, ablock[44];
int i;
close(fd);
log_error_time();
for (ap = ablock, i = 0; i < remote_addrlen && i < 14; i++) {
*ap++ = ' ';
*ap++ = HEX((bogus->sa_data[i] >> 4) & 0x0f);
*ap++ = HEX(bogus->sa_data[i] & 0x0f);
}
*ap = '\0';
fprintf(stderr, "non-INET connection attempt: socket %d, "
"sa_family = %hu, sa_data[%d] = %s\n",
fd, bogus->sa_family, remote_addrlen, ablock);
return;
}
#endif
/* XXX Either delete this, or document why it's needed */
/* Pointed out 3-Oct-1999 by Paul Saab <[email protected]> */
#ifdef REUSE_EACH_CLIENT_CONNECTION_SOCKET
if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &sock_opt,
sizeof (sock_opt))) == -1) {
DIE("setsockopt: unable to set SO_REUSEADDR");
}
#endif
len = sizeof(salocal);
if (getsockname(fd, (struct sockaddr *) &salocal, &len) != 0) {
WARN("getsockname");
close(fd);
return;
}
conn = new_request();
if (!conn) {
close(fd);
return;
}
conn->fd = fd;
conn->status = READ_HEADER;
conn->header_line = conn->client_stream;
conn->time_last = current_time;
conn->kacount = ka_max;
ascii_sockaddr(&salocal, conn->local_ip_addr, NI_MAXHOST);
/* nonblocking socket */
if (set_nonblock_fd(conn->fd) == -1)
WARN("fcntl: unable to set new socket to non-block");
/* set close on exec to true */
if (fcntl(conn->fd, F_SETFD, 1) == -1)
WARN("fctnl: unable to set close-on-exec for new socket");
/* Increase buffer size if we have to.
* Only ask the system the buffer size on the first request,
* and assume all subsequent sockets have the same size.
*/
if (system_bufsize == 0) {
len = sizeof (system_bufsize);
if (getsockopt
(conn->fd, SOL_SOCKET, SO_SNDBUF, &system_bufsize, &len) == 0
&& len == sizeof (system_bufsize)) {
/*
fprintf(stderr, "%sgetsockopt reports SNDBUF %d\n",
get_commonlog_time(), system_bufsize);
*/
;
} else {
WARN("getsockopt(SNDBUF)");
system_bufsize = 1;
}
}
if (system_bufsize < sockbufsize) {
if (setsockopt
(conn->fd, SOL_SOCKET, SO_SNDBUF, (void *) &sockbufsize,
sizeof (sockbufsize)) == -1) {
WARN("setsockopt: unable to set socket buffer size");
#ifdef DIE_ON_ERROR_TUNING_SNDBUF
exit(errno);
#endif
}
}
/* for log file and possible use by CGI programs */
ascii_sockaddr(&remote_addr, conn->remote_ip_addr, NI_MAXHOST);
/* for possible use by CGI programs */
conn->remote_port = net_port(&remote_addr);
status.requests++;
#ifdef USE_TCPNODELAY
/* Thanks to Jef Poskanzer <[email protected]> for this tweak */
{
int one = 1;
if (setsockopt(conn->fd, IPPROTO_TCP, TCP_NODELAY,
(void *) &one, sizeof (one)) == -1) {
DIE("setsockopt: unable to set TCP_NODELAY");
}
}
#endif
#ifndef NO_RATE_LIMIT
if (conn->fd > max_connections) {
send_r_service_unavailable(conn);
conn->status = DONE;
pending_requests = 0;
}
#endif /* NO_RATE_LIMIT */
total_connections++;
enqueue(&request_ready, conn);
}
/*
* Name: free_request
*
* Description: Deallocates memory for a finished request and closes
* down socket.
*/
static void free_request(request ** list_head_addr, request * req)
{
int i;
/* free_request should *never* get called by anything but
process_requests */
if (req->buffer_end && req->status != DEAD) {
req->status = DONE;
return;
}
/* put request on the free list */
dequeue(list_head_addr, req); /* dequeue from ready or block list */
if (req->logline) /* access log */
log_access(req);
if (req->mmap_entry_var)
release_mmap(req->mmap_entry_var);
else if (req->data_mem)
munmap(req->data_mem, req->filesize);
if (req->data_fd)
close(req->data_fd);
if (req->post_data_fd)
close(req->post_data_fd);
if (req->response_status >= 400)
status.errors++;
for (i = COMMON_CGI_COUNT; i < req->cgi_env_index; ++i) {
if (req->cgi_env[i]) {
free(req->cgi_env[i]);
} else {
log_error_time();
fprintf(stderr, "Warning: CGI Environment contains NULL value" \
"(index %d of %d).\n", i, req->cgi_env_index);
}
}
if (req->pathname)
free(req->pathname);
if (req->path_info)
free(req->path_info);
if (req->path_translated)
free(req->path_translated);
if (req->script_name)
free(req->script_name);
if ((req->keepalive == KA_ACTIVE) &&
(req->response_status < 500) && req->kacount > 0) {
int bytes_to_move;
request *conn = new_request();
if (!conn) {
/* errors already reported */
enqueue(&request_free, req);
close(req->fd);
total_connections--;
return;
}
conn->fd = req->fd;
conn->status = READ_HEADER;
conn->header_line = conn->client_stream;
conn->kacount = req->kacount - 1;
/* close enough and we avoid a call to time(NULL) */
conn->time_last = req->time_last;
/* for log file and possible use by CGI programs */
memcpy(conn->remote_ip_addr, req->remote_ip_addr, NI_MAXHOST);
memcpy(conn->local_ip_addr, req->local_ip_addr, NI_MAXHOST);
/* for possible use by CGI programs */
conn->remote_port = req->remote_port;
status.requests++;
/* we haven't parsed beyond req->parse_pos, so... */
bytes_to_move = req->client_stream_pos - req->parse_pos;
if (bytes_to_move) {
memcpy(conn->client_stream,
req->client_stream + req->parse_pos, bytes_to_move);
conn->client_stream_pos = bytes_to_move;
}
enqueue(&request_block, conn);
BOA_FD_SET(conn->fd, &block_read_fdset);
enqueue(&request_free, req);
return;
}
/*
While debugging some weird errors, Jon Nelson learned that
some versions of Netscape Navigator break the
HTTP specification.
Some research on the issue brought up:
http://www.apache.org/docs/misc/known_client_problems.html
As quoted here:
"
Trailing CRLF on POSTs
This is a legacy issue. The CERN webserver required POST
data to have an extra CRLF following it. Thus many
clients send an extra CRLF that is not included in the
Content-Length of the request. Apache works around this
problem by eating any empty lines which appear before a
request.
"
Boa will (for now) hack around this stupid bug in Netscape
(and Internet Exploder)
by reading up to 32k after the connection is all but closed.
This should eliminate any remaining spurious crlf sent
by the client.
Building bugs *into* software to be compatable is
just plain wrong
*/
if (req->method == M_POST) {
char buf[32768];
read(req->fd, buf, 32768);
}
close(req->fd);
total_connections--;
enqueue(&request_free, req);
return;
}
/* :TODO:Monday, December 01, 2014 11:19:27 HKT:SeanHou: */
int lookupservices(char *service, int *lookupindex)
{
int i=0;
char lookup[10][30]={"device",
"imaging",
"media",
"onvif",
"test"
""};
for(i = 0; i<6; i++)
{
if(strcasecmp(lookup[i], service) == 0)
{
*lookupindex = i;
return 1;
}
}
*lookupindex = -1;
return 0;
}
/**
* @brief parse url
*
* @param in
* @param dl
* @param list[][100]
*/
void url_request(char *in, char dl, char list[][100])
{
int i = 0;
int j = 0;
int k = 0;
int len = strlen(in);
for(i = 0; i<len; i++)//SeanHou mark
{
if(in[i] == dl)
{
*( *(list + j) + k) = 0;
j++;
k = 0;
}
else
{
*( *(list + j) + k) = in[i];
k++;
}
}
*(*(list + j) + k) = 0;
j++;
if(j < 3)
**(list + j) = 0;
return;
}
/**
* @brief check is onvif or not.
*
* @param current_client_stream
* @param service_uri
* @param lookupindex
*
* @return
*/
int isonvif(char *current_client_stream , char *service_uri, int *lookupindex)
{
char segments[3][100];
char post_req[80];
char service_name[40];
int service_index = 0;
int ret;
int index_post = 0;
int stream_index = 0;
if (strncmp(current_client_stream, "POST ", 5)==0)
{
index_post = 0;
stream_index = 0;
while(current_client_stream[stream_index] != '\n' &¤t_client_stream[stream_index] != '\r')
{
post_req[index_post++] = current_client_stream[stream_index++];
}
post_req[index_post] = '\0';
url_request(post_req, ' ', segments);
index_post = 1;
service_index = 0;
while(segments[1][index_post] != '/' && segments[1][index_post] != '\0')
{
service_name[service_index++] = segments[1][index_post++];
}
service_name[service_index] = '\0';
ret = lookupservices(service_name,lookupindex);
if(ret)
{
strcpy(service_uri,segments[1]);
}
return ret;
}
else
{
return 0;
}
}
/* :TODO:End--- */
/*
* Name: process_requests
*
* Description: Iterates through the ready queue, passing each request
* to the appropriate handler for processing. It monitors the
* return value from handler functions, all of which return -1
* to indicate a block, 0 on completion and 1 to remain on the
* ready list for more procesing.
*/
void process_requests(int server_s, struct soap *soap)/*by SeanHou*/
{
/* :TODO:Monday, December 01, 2014 11:17:36 HKT:SeanHou: */
int OnvifEN = 0;
int lookupindex = 0;
char service_uri[100] = "";
memset((void*)&soap->peer, 0, sizeof(soap->peer));
soap->socket = SOAP_INVALID_SOCKET;
soap->error = SOAP_OK;
soap->errmode = 0;
soap->keep_alive = 0;
fprintf(stderr, "Warning:" \
"(==>%s).\n", __func__);
/* :TODO:End--- */
int retval = 0;
request *current, *trailer;
if (pending_requests) {
get_request(server_s);
#ifdef ORIGINAL_BEHAVIOR
pending_requests = 0;
#endif
}
current = request_ready;
while (current) {
/* :TODO:Monday, December 01, 2014 11:18:42 HKT:SeanHou: juge is onvif */
OnvifEN = isonvif(current->client_stream, service_uri, &lookupindex);
if(OnvifEN == 1)
{
fprintf(stderr, "[boa:onvif] Warning: is onvif line[%d]remote port[%d]h2ns[%d]remote ip[%s]\n", __LINE__, current->remote_port, htons(current->remote_port), current->remote_ip_addr);
struct sockaddr_in onvif_client_addr;
memset(&onvif_client_addr, 0, sizeof(onvif_client_addr));
onvif_client_addr.sin_family = AF_INET;
onvif_client_addr.sin_port = htons(current->remote_port);//随机端口
onvif_client_addr.sin_addr.s_addr = inet_addr(current->remote_ip_addr);//
soap->socket = current->fd;
soap->peer = onvif_client_addr;
if (soap_valid_socket(soap->socket))
{
soap->ip = ntohl(soap->peer.sin_addr.s_addr);
soap->port = (int)ntohs(soap->peer.sin_port);
soap->keep_alive = (((soap->imode | soap->omode) & SOAP_IO_KEEPALIVE) != 0);
}
g_onvif_buffer = (char *)soap_malloc(soap, sizeof(current->client_stream));
strcpy(g_onvif_buffer, current->client_stream);//mark
soap_begin_recv(soap);
if (soap_envelope_begin_in(soap))
{
soap_send_fault(soap);
}
if (soap_recv_header(soap))
{
soap_send_fault(soap);
}
if (soap_body_begin_in(soap))
{
soap_send_fault(soap);
}
int errorCode = 0;
if (errorCode = soap_serve_request(soap))
{
fprintf(stderr, "[boa:onvif]soap_serve_request fail, errorCode %d \n", errorCode);
soap_send_fault(soap);
}
memset(current->client_stream, 0, CLIENT_STREAM_SIZE );
soap_dealloc(soap, NULL);
soap_destroy(soap);
soap_end(soap);
current->status = DONE;
close(soap->socket);
continue;
}
/* :TODO:End--- */
time(¤t_time);
if (current->buffer_end && /* there is data in the buffer */
current->status != DEAD && current->status != DONE) {
retval = req_flush(current);
/*
* retval can be -2=error, -1=blocked, or bytes left
*/
if (retval == -2) { /* error */
current->status = DEAD;
retval = 0;
} else if (retval >= 0) {
/* notice the >= which is different from below?
Here, we may just be flushing headers.
We don't want to return 0 because we are not DONE
or DEAD */
retval = 1;
}
} else {
switch (current->status) {
case READ_HEADER:
case ONE_CR:
case ONE_LF:
case TWO_CR:
retval = read_header(current);
break;
case BODY_READ:
retval = read_body(current);
break;
case BODY_WRITE:
retval = write_body(current);
break;
case WRITE:
retval = process_get(current);
break;
case PIPE_READ:
retval = read_from_pipe(current);
break;
case PIPE_WRITE:
retval = write_from_pipe(current);
break;
case DONE:
/* a non-status that will terminate the request */
retval = req_flush(current);
/*
* retval can be -2=error, -1=blocked, or bytes left
*/
if (retval == -2) { /* error */
current->status = DEAD;
retval = 0;
} else if (retval > 0) {
retval = 1;
}
break;
case DEAD:
retval = 0;
current->buffer_end = 0;
SQUASH_KA(current);
break;
default:
retval = 0;
fprintf(stderr, "Unknown status (%d), "
"closing!\n", current->status);
current->status = DEAD;
break;
}
}
if (sigterm_flag)
SQUASH_KA(current);
/* we put this here instead of after the switch so that
* if we are on the last request, and get_request is successful,
* current->next is valid!
*/
if (pending_requests)
get_request(server_s);
switch (retval) {
case -1: /* request blocked */
trailer = current;
current = current->next;
block_request(trailer);
break;
case 0: /* request complete */
current->time_last = current_time;
trailer = current;
current = current->next;
free_request(&request_ready, trailer);
break;
case 1: /* more to do */
current->time_last = current_time;
current = current->next;
break;
default:
log_error_time();
fprintf(stderr, "Unknown retval in process.c - "
"Status: %d, retval: %d\n", current->status, retval);
current = current->next;
break;
}
}
}
/*
* Name: process_logline
*
* Description: This is called with the first req->header_line received
* by a request, called "logline" because it is logged to a file.
* It is parsed to determine request type and method, then passed to
* translate_uri for further parsing. Also sets up CGI environment if
* needed.
*/
int process_logline(request * req)
{
char *stop, *stop2;
static char *SIMPLE_HTTP_VERSION = "HTTP/0.9";
req->logline = req->client_stream;
if (!memcmp(req->logline, "GET ", 4))
req->method = M_GET;
else if (!memcmp(req->logline, "HEAD ", 5))
/* head is just get w/no body */
req->method = M_HEAD;
else if (!memcmp(req->logline, "POST ", 5))
req->method = M_POST;
else {
log_error_time();
fprintf(stderr, "malformed request: \"%s\"\n", req->logline);
send_r_not_implemented(req);
return 0;
}
req->http_version = SIMPLE_HTTP_VERSION;
req->simple = 1;
/* Guaranteed to find ' ' since we matched a method above */
stop = req->logline + 3;
if (*stop != ' ')
++stop;
/* scan to start of non-whitespace */
while (*(++stop) == ' ');
stop2 = stop;
/* scan to end of non-whitespace */
while (*stop2 != '\0' && *stop2 != ' ')
++stop2;
if (stop2 - stop > MAX_HEADER_LENGTH) {
log_error_time();
fprintf(stderr, "URI too long %d: \"%s\"\n", MAX_HEADER_LENGTH,
req->logline);
send_r_bad_request(req);
return 0;
}
memcpy(req->request_uri, stop, stop2 - stop);
req->request_uri[stop2 - stop] = '\0';
if (*stop2 == ' ') {
/* if found, we should get an HTTP/x.x */
unsigned int p1, p2;
/* scan to end of whitespace */
++stop2;
while (*stop2 == ' ' && *stop2 != '\0')
++stop2;
/* scan in HTTP/major.minor */
if (sscanf(stop2, "HTTP/%u.%u", &p1, &p2) == 2) {
/* HTTP/{0.9,1.0,1.1} */
if (p1 == 1 && (p2 == 0 || p2 == 1)) {
req->http_version = stop2;
req->simple = 0;
} else if (p1 > 1 || (p1 != 0 && p2 > 1)) {
goto BAD_VERSION;
}
} else {
goto BAD_VERSION;
}
}
if (req->method == M_HEAD && req->simple) {
send_r_bad_request(req);
return 0;
}
req->cgi_env_index = COMMON_CGI_COUNT;
return 1;
BAD_VERSION:
log_error_time();
fprintf(stderr, "bogus HTTP version: \"%s\"\n", stop2);
send_r_bad_request(req);
return 0;
}
/*
* Name: process_header_end
*
* Description: takes a request and performs some final checking before
* init_cgi or init_get
* Returns 0 for error or NPH, or 1 for success
*/
int process_header_end(request * req)
{
if (!req->logline) {
send_r_error(req);
return 0;
}
/* Percent-decode request */
if (unescape_uri(req->request_uri, &(req->query_string)) == 0) {
log_error_doc(req);
fputs("Problem unescaping uri\n", stderr);
send_r_bad_request(req);
return 0;
}
/* clean pathname */
clean_pathname(req->request_uri);
if (req->request_uri[0] != '/') {
send_r_bad_request(req);
return 0;
}
if (translate_uri(req) == 0) { /* unescape, parse uri */
SQUASH_KA(req);
return 0; /* failure, close down */
}
if (req->method == M_POST) {
req->post_data_fd = create_temporary_file(1, NULL, 0);
if (req->post_data_fd == 0)
return(0);
return(1); /* success */
}
if (req->is_cgi) {
return init_cgi(req);
}
req->status = WRITE;
return init_get(req); /* get and head */
}
/*
* Name: process_option_line
*
* Description: Parses the contents of req->header_line and takes
* appropriate action.
*/
int process_option_line(request * req)
{
char c, *value, *line = req->header_line;
/* Start by aggressively hacking the in-place copy of the header line */
#ifdef FASCIST_LOGGING
log_error_time();
fprintf(stderr, "%s:%d - Parsing \"%s\"\n", __FILE__, __LINE__, line);
#endif
value = strchr(line, ':');
if (value == NULL)
return 0;
*value++ = '\0'; /* overwrite the : */
to_upper(line); /* header types are case-insensitive */
while ((c = *value) && (c == ' ' || c == '\t'))
value++;
if (!memcmp(line, "IF_MODIFIED_SINCE", 18) && !req->if_modified_since)
req->if_modified_since = value;
else if (!memcmp(line, "CONTENT_TYPE", 13) && !req->content_type)
req->content_type = value;
else if (!memcmp(line, "CONTENT_LENGTH", 15) && !req->content_length)
req->content_length = value;
else if (!memcmp(line, "CONNECTION", 11) &&
ka_max && req->keepalive != KA_STOPPED) {
req->keepalive = (!strncasecmp(value, "Keep-Alive", 10) ?
KA_ACTIVE : KA_STOPPED);
}
/* #ifdef ACCEPT_ON */
else if (!memcmp(line, "ACCEPT", 7))
add_accept_header(req, value);
/* #endif */
/* Need agent and referer for logs */
else if (!memcmp(line, "REFERER", 8)) {
req->header_referer = value;
if (!add_cgi_env(req, "REFERER", value, 1))
return 0;
} else if (!memcmp(line, "USER_AGENT", 11)) {
req->header_user_agent = value;
if (!add_cgi_env(req, "USER_AGENT", value, 1))
return 0;
} else {
if (!add_cgi_env(req, line, value, 1))
return 0;
}
return 1;
}
/*
* Name: add_accept_header
* Description: Adds a mime_type to a requests accept char buffer
* silently ignore any that don't fit -
* shouldn't happen because of relative buffer sizes
*/
void add_accept_header(request * req, char *mime_type)
{
#ifdef ACCEPT_ON
int l = strlen(req->accept);
int l2 = strlen(mime_type);
if ((l + l2 + 2) >= MAX_HEADER_LENGTH)
return;
if (req->accept[0] == '\0')
strcpy(req->accept, mime_type);
else {
req->accept[l] = ',';
req->accept[l + 1] = ' ';
memcpy(req->accept + l + 2, mime_type, l2 + 1);
/* the +1 is for the '\0' */
/*
sprintf(req->accept + l, ", %s", mime_type);
*/
}
#endif
}
void free_requests(void)
{
request *ptr, *next;
ptr = request_free;
while (ptr != NULL) {
next = ptr->next;
free(ptr);
ptr = next;
}
request_free = NULL;
}