Mon, 25 Apr 2022 18:33:03 +0200
remove createtestdb from build
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
4 | * Copyright 2016 Olaf Wintermann. All rights reserved. |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29 | #include "cgi.h" |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
30 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
31 | #include <stdio.h> |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
32 | #include <stdlib.h> |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
33 | #include <unistd.h> |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | |
162
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
35 | #include <sys/types.h> |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
36 | #include <signal.h> |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
37 | #include <sys/wait.h> |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
38 | |
254 | 39 | #include <ucx/string.h> |
40 | ||
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
41 | #include "../util/util.h" |
119
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
42 | #include "../util/pblock.h" |
171
af7e2d80dee6
adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
164
diff
changeset
|
43 | #include "../daemon/netsite.h" |
125
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
44 | #include "../util/io.h" |
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
45 | |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
46 | #include "cgiutils.h" |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
47 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48 | #define CGI_VARS 32 |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
49 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
50 | #define CGI_RESPONSE_PARSER_BUFLEN 2048 |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51 | #define CGI_RESPONSE_MAX_LINE_LENGTH 512 |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
52 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
53 | int send_cgi(pblock *pb, Session *sn, Request *rq) { |
119
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
54 | char *path = pblock_findkeyval(pb_key_path, rq->vars); |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
55 | char *ctlen = pblock_findkeyval(pb_key_content_length, rq->headers); |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
56 | int64_t content_length = 0; |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
57 | |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
58 | if(ctlen) { |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
59 | if(!util_strtoint(ctlen, &content_length)) { |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
60 | log_ereport( |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
61 | LOG_FAILURE, |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
62 | "send-cgi: content-length header is not an integer"); |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
63 | protocol_status(sn, rq, 400, NULL); |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
64 | return REQ_ABORTED; |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
65 | } |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
66 | } |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68 | struct stat s; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69 | if(stat(path, &s)) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70 | int statuscode = util_errno2status(errno); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
71 | protocol_status(sn, rq, statuscode, NULL); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
72 | return REQ_ABORTED; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
73 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
74 | if(S_ISDIR(s.st_mode)) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
75 | protocol_status(sn, rq, 403, NULL); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
76 | return REQ_ABORTED; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
77 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
78 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
79 | param_free(pblock_remove("content-type", rq->srvhdrs)); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
80 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
81 | const char *args = pblock_findval("query", rq->reqpb); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
82 | char **argv = cgi_create_argv(path, NULL, args); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
83 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
84 | char **env = http_hdrs2env(rq->headers); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
85 | env = cgi_common_vars(sn, rq, env); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
86 | env = cgi_specific_vars(sn, rq, args, env, 1); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
87 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
88 | CGIProcess cgip; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
89 | int ret = cgi_start(&cgip, path, argv, env); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
90 | if(ret != REQ_PROCEED) { |
145
1c93281ca4bf
fixes memory leaks in request_stat_path and send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
144
diff
changeset
|
91 | util_env_free(env); |
1c93281ca4bf
fixes memory leaks in request_stat_path and send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
144
diff
changeset
|
92 | cgi_free_argv(argv); |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
93 | return ret; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
94 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
95 | |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
96 | util_env_free(env); |
145
1c93281ca4bf
fixes memory leaks in request_stat_path and send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
144
diff
changeset
|
97 | cgi_free_argv(argv); |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
98 | |
119
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
99 | char buf[4096]; // I/O buffer |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
100 | ssize_t r; |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
101 | |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
102 | if(content_length > 0) { |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
103 | ssize_t n = 0; |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
104 | while(n < content_length) { |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
105 | r = netbuf_getbytes(sn->inbuf, buf, 4096); |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
106 | if(r <= 0) { |
162
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
107 | // TODO: handle error |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
108 | log_ereport( |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
109 | LOG_FAILURE, |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
110 | "send-cgi: script: %s: cannot read request body", |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
111 | path); |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
112 | kill(cgip.pid, SIGTERM); |
144
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
113 | cgi_close(&cgip); |
162
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
114 | return REQ_ABORTED; |
119
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
115 | } |
162
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
116 | ssize_t w = write(cgip.in[1], buf, r); |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
117 | if(w <= 0) { |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
118 | // TODO: handle error |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
119 | log_ereport( |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
120 | LOG_FAILURE, |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
121 | "send-cgi: script: %s: cannot send request body to cgi process", |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
122 | path); |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
123 | kill(cgip.pid, SIGKILL); |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
124 | cgi_close(&cgip); |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
125 | return REQ_ABORTED; |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
126 | } |
119
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
127 | n += r; |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
128 | } |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
129 | } |
171
af7e2d80dee6
adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
164
diff
changeset
|
130 | system_close(cgip.in[1]); |
144
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
131 | cgip.in[1] = -1; |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
132 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
133 | // read from child |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
134 | CGIResponseParser *parser = cgi_parser_new(sn, rq); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
135 | WSBool cgiheader = TRUE; |
125
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
136 | ssize_t wr = 0; |
162
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
137 | int result = REQ_PROCEED; |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
138 | size_t response_length = 0; |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
139 | while((r = read(cgip.out[0], buf, 4096)) > 0) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
140 | if(cgiheader) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
141 | size_t pos; |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
142 | ret = cgi_parse_response(parser, buf, r, &pos); |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
143 | if(ret == -1) { |
162
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
144 | log_ereport( |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
145 | LOG_FAILURE, |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
146 | "broken cgi script response: path: %s", path); |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
147 | protocol_status(sn, rq, 500, NULL); |
125
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
148 | result = REQ_ABORTED; |
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
149 | break; |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
150 | } else if(ret == 1) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
151 | cgiheader = FALSE; |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
152 | if(parser->status > 0) { |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
153 | protocol_status(sn, rq, parser->status, parser->msg); |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
154 | } |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
155 | http_start_response(sn, rq); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
156 | if(pos < r) { |
162
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
157 | response_length += r-pos; |
125
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
158 | wr = net_write(sn->csd, &buf[pos], r-pos); |
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
159 | if(wr <= 0) { |
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
160 | result = REQ_ABORTED; |
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
161 | break; |
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
162 | } |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
163 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
164 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
165 | } else { |
164
6f47eb624665
fixes cgi content length check
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
162
diff
changeset
|
166 | response_length += r; |
125
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
167 | wr = net_write(sn->csd, buf, r); |
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
168 | if(wr <= 0) { |
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
169 | result = REQ_ABORTED; |
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
170 | break; |
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
171 | } |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
172 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
173 | } |
144
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
174 | |
162
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
175 | char *ctlen_header = pblock_findkeyval(pb_key_content_length, rq->srvhdrs); |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
176 | if(ctlen_header) { |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
177 | int64_t ctlenhdr; |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
178 | if(util_strtoint(ctlen_header, &ctlenhdr)) { |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
179 | if(ctlenhdr != response_length) { |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
180 | log_ereport( |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
181 | LOG_FAILURE, |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
182 | "cgi-send: script: %s: content length mismatch", |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
183 | path); |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
184 | rq->rq_attr.keep_alive = 0; |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
185 | result = REQ_ABORTED; |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
186 | } |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
187 | } |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
188 | } |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
189 | |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
190 | if(result == REQ_ABORTED) { |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
191 | log_ereport(LOG_FAILURE, "cgi-send: kill script: %s", path); |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
192 | kill(cgip.pid, SIGKILL); |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
193 | } |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
194 | cgi_close(&cgip); // TODO: check return value |
126
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
125
diff
changeset
|
195 | |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
196 | cgi_parser_free(parser); |
125
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
197 | return result; |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
198 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
199 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
200 | int cgi_start(CGIProcess *p, char *path, char *const argv[], char *const envp[]) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
201 | if(pipe(p->in) || pipe(p->out)) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
202 | log_ereport( |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
203 | LOG_FAILURE, |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
204 | "send-cgi: cannot create pipe: %s", |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
205 | strerror(errno)); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
206 | return REQ_ABORTED; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
207 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
208 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
209 | p->pid = fork(); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
210 | if(p->pid == 0) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
211 | // child |
120
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
212 | |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
213 | // get script directory and script name |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
214 | sstr_t script = sstr(path); |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
215 | sstr_t parent; |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
216 | int len = strlen(path); |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
217 | for(int i=len-1;i>=0;i--) { |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
218 | if(path[i] == '/') { |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
219 | script = sstrn(path + i + 1, len - i); |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
220 | parent = sstrdup(sstrn(path, i)); |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
221 | if(chdir(parent.ptr)) { |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
222 | perror("cgi_start: chdir"); |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
223 | free(parent.ptr); |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
224 | exit(-1); |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
225 | } |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
226 | free(parent.ptr); |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
227 | break; |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
228 | } |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
229 | } |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
230 | |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
231 | if(dup2(p->in[0], STDIN_FILENO) == -1) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
232 | perror("cgi_start: dup2"); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
233 | exit(EXIT_FAILURE); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
234 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
235 | if(dup2(p->out[1], STDOUT_FILENO) == -1) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
236 | perror("cgi_start: dup2"); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
237 | exit(EXIT_FAILURE); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
238 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
239 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
240 | // we need to close this unused pipe |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
241 | // otherwise stdin cannot reach EOF |
171
af7e2d80dee6
adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
164
diff
changeset
|
242 | system_close(p->in[1]); |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
243 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
244 | // execute program |
120
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
245 | exit(execve(script.ptr, argv, envp)); |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
246 | } else { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
247 | // parent |
171
af7e2d80dee6
adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
164
diff
changeset
|
248 | system_close(p->out[1]); |
144
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
249 | p->out[1] = -1; |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
250 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
251 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
252 | return REQ_PROCEED; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
253 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
254 | |
162
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
255 | int cgi_close(CGIProcess *p) { |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
256 | int status = -1; |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
257 | waitpid(p->pid, &status, 0); |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
258 | |
144
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
259 | if(p->in[0] != -1) { |
171
af7e2d80dee6
adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
164
diff
changeset
|
260 | system_close(p->in[0]); |
144
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
261 | } |
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
262 | if(p->in[1] != -1) { |
171
af7e2d80dee6
adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
164
diff
changeset
|
263 | system_close(p->in[1]); |
144
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
264 | } |
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
265 | if(p->out[0] != -1) { |
171
af7e2d80dee6
adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
164
diff
changeset
|
266 | system_close(p->out[0]); |
144
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
267 | } |
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
268 | if(p->out[1] != -1) { |
171
af7e2d80dee6
adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
164
diff
changeset
|
269 | system_close(p->out[1]); |
144
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
270 | } |
162
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
271 | |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
272 | return 0; |
144
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
273 | } |
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
274 | |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
275 | CGIResponseParser* cgi_parser_new(Session *sn, Request *rq) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
276 | CGIResponseParser* parser = pool_malloc(sn->pool, sizeof(CGIResponseParser)); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
277 | parser->sn = sn; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
278 | parser->rq = rq; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
279 | parser->tmp = ucx_buffer_new(NULL, 64, UCX_BUFFER_AUTOEXTEND); |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
280 | parser->status = 0; |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
281 | parser->msg = NULL; |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
282 | return parser; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
283 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
284 | |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
285 | void cgi_parser_free(CGIResponseParser *parser) { |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
286 | if(parser->tmp) { |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
287 | ucx_buffer_free(parser->tmp); |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
288 | } |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
289 | pool_free(parser->sn->pool, parser); |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
290 | } |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
291 | |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
292 | /* |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
293 | * parses a cgi response line and adds the response header to rq->srvhdrs |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
294 | * returns 0: incomplete line |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
295 | * 1: successfully parsed lines |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
296 | * 2: cgi response header complete (empty line) |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
297 | * -1: error |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
298 | */ |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
299 | static int parse_lines(CGIResponseParser *parser, char *buf, size_t len, int *pos) { |
145
1c93281ca4bf
fixes memory leaks in request_stat_path and send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
144
diff
changeset
|
300 | UcxAllocator a = util_pool_allocator(parser->sn->pool); |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
301 | sstr_t name; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
302 | sstr_t value; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
303 | WSBool space = TRUE; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
304 | int i; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
305 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
306 | int line_begin = 0; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
307 | int value_begin = 0; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
308 | for(i=0;i<len;i++) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
309 | char c = buf[i]; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
310 | if(value_begin == line_begin && c == ':') { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
311 | name = sstrn(buf + line_begin, i - line_begin); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
312 | value_begin = i + 1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
313 | } else if(c == '\n') { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
314 | if(value_begin == line_begin) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
315 | if(space) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
316 | *pos = i + 1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
317 | return 2; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
318 | } else { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
319 | // line ends with content but without ':' -> error |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
320 | return -1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
321 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
322 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
323 | value = sstrn(buf + value_begin, i - value_begin); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
324 | |
145
1c93281ca4bf
fixes memory leaks in request_stat_path and send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
144
diff
changeset
|
325 | name = sstrlower_a(&a, sstrtrim(name)); |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
326 | value = sstrtrim(value); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
327 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
328 | if(name.length == 0 || value.length == 0) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
329 | return -1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
330 | } |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
331 | |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
332 | if(!sstrcmp(name, S("status"))) { |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
333 | sstr_t codestr = value; |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
334 | int j; |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
335 | for(j=0;j<codestr.length;j++) { |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
336 | if(!isdigit(codestr.ptr[j])) { |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
337 | break; |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
338 | } |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
339 | if(j > 2) { |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
340 | break; |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
341 | } |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
342 | } |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
343 | codestr.ptr[j] = '\0'; |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
344 | |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
345 | int64_t s = 0; |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
346 | util_strtoint(codestr.ptr, &s); |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
347 | parser->status = (int)s; |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
348 | |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
349 | sstr_t msg = sstrtrim(sstrsubs(value, j + 1)); |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
350 | |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
351 | if(msg.length > 0) { |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
352 | parser->msg = sstrdup_pool(parser->sn->pool, msg).ptr; |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
353 | } |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
354 | } else { |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
355 | pblock_nvlinsert( |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
356 | name.ptr, |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
357 | name.length, |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
358 | value.ptr, |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
359 | value.length, |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
360 | parser->rq->srvhdrs); |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
361 | } |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
362 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
363 | line_begin = i+1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
364 | value_begin = line_begin; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
365 | space = TRUE; |
121
a881dc866e23
adds more CGI stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
120
diff
changeset
|
366 | } else if(!isspace(c)) { |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
367 | space = FALSE; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
368 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
369 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
370 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
371 | if(i < len) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
372 | *pos = i; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
373 | return 0; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
374 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
375 | return 1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
376 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
377 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
378 | /* |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
379 | * returns -1: error |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
380 | * 0: response header incomplete |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
381 | * 1: complete |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
382 | */ |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
383 | int cgi_parse_response(CGIResponseParser *parser, char *buf, size_t len, size_t *bpos) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
384 | *bpos = 0; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
385 | int pos = 0; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
386 | if(parser->tmp->pos > 0) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
387 | // the tmp buffer contains an unfinished line |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
388 | // fill up the buffer until the line is complete |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
389 | WSBool nb = FALSE; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
390 | for(pos=0;pos<len;pos++) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
391 | if(buf[pos] == '\n') { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
392 | nb = TRUE; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
393 | break; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
394 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
395 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
396 | ucx_buffer_write(buf, 1, pos, parser->tmp); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
397 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
398 | if(nb) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
399 | // line complete |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
400 | int npos; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
401 | int r = parse_lines(parser, parser->tmp->space, parser->tmp->pos, &npos); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
402 | switch(r) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
403 | case -1: return -1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
404 | case 0: return -1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
405 | case 1: break; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
406 | case 2: { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
407 | *bpos = pos + 1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
408 | return 1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
409 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
410 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
411 | // reset tmp buffer |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
412 | parser->tmp->pos = 0; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
413 | } else { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
414 | if(parser->tmp->pos > CGI_RESPONSE_MAX_LINE_LENGTH) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
415 | return -1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
416 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
417 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
418 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
419 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
420 | int npos = 0; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
421 | int r = parse_lines(parser, buf + pos, len - pos, &npos); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
422 | switch(r) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
423 | default: return -1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
424 | case 0: |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
425 | case 1: { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
426 | int newlen = len - npos; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
427 | if(npos > 0) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
428 | ucx_buffer_write(buf + npos, 1, newlen, parser->tmp); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
429 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
430 | return 0; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
431 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
432 | case 2: { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
433 | *bpos = pos + npos; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
434 | return 1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
435 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
436 | } |
171
af7e2d80dee6
adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
164
diff
changeset
|
437 | } |