Sat, 12 Nov 2022 20:50:45 +0100
refactor send_cgi into non-blocking SAF (wip)
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 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
39 | #include <cx/string.h> |
254 | 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" |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
45 | #include "../daemon/event.h" |
125
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
46 | |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
47 | #include "cgiutils.h" |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
49 | #define CGI_VARS 32 |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
50 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51 | #define CGI_RESPONSE_PARSER_BUFLEN 2048 |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
52 | #define CGI_RESPONSE_MAX_LINE_LENGTH 512 |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
53 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
54 | 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
|
55 | 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
|
56 | 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
|
57 | int64_t content_length = 0; |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
58 | |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
59 | if(ctlen) { |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
60 | if(!util_strtoint(ctlen, &content_length)) { |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
61 | log_ereport( |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
62 | LOG_FAILURE, |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
63 | "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
|
64 | protocol_status(sn, rq, 400, NULL); |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
65 | return REQ_ABORTED; |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
66 | } |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
67 | } |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69 | struct stat s; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70 | if(stat(path, &s)) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
71 | int statuscode = util_errno2status(errno); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
72 | protocol_status(sn, rq, statuscode, NULL); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
73 | return REQ_ABORTED; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
74 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
75 | if(S_ISDIR(s.st_mode)) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
76 | protocol_status(sn, rq, 403, NULL); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
77 | return REQ_ABORTED; |
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 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
80 | param_free(pblock_remove("content-type", rq->srvhdrs)); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
81 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
82 | const char *args = pblock_findval("query", rq->reqpb); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
83 | char **argv = cgi_create_argv(path, NULL, args); |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
84 | if(!argv) { |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
85 | return REQ_ABORTED; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
86 | } |
118
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 | char **env = http_hdrs2env(rq->headers); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
89 | env = cgi_common_vars(sn, rq, env); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
90 | env = cgi_specific_vars(sn, rq, args, env, 1); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
91 | |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
92 | // event handler object for non-blocking io event handler |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
93 | CGIHandler *handler = pool_malloc(sn->pool, sizeof(CGIHandler)); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
94 | if(!handler) { |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
95 | return REQ_ABORTED; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
96 | } |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
97 | ZERO(handler, sizeof(CGIHandler)); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
98 | handler->path = path; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
99 | |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
100 | int ret = cgi_start(&handler->process, path, argv, env); |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
101 | 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
|
102 | 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
|
103 | cgi_free_argv(argv); |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
104 | return ret; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
105 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
106 | |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
107 | 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
|
108 | cgi_free_argv(argv); |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
109 | |
119
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
110 | char buf[4096]; // I/O buffer |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
111 | ssize_t r; |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
112 | |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
113 | if(content_length > 0) { |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
114 | ssize_t n = 0; |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
115 | while(n < content_length) { |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
116 | 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
|
117 | 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
|
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 read request body", |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
122 | path); |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
123 | kill(handler->process.pid, SIGTERM); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
124 | cgi_close(&handler->process); |
162
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; |
119
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
126 | } |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
127 | ssize_t w = write(handler->process.in[1], buf, r); |
162
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
128 | if(w <= 0) { |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
129 | // TODO: handle error |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
130 | log_ereport( |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
131 | LOG_FAILURE, |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
132 | "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
|
133 | path); |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
134 | kill(handler->process.pid, SIGKILL); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
135 | cgi_close(&handler->process); |
162
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
136 | return REQ_ABORTED; |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
137 | } |
119
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
138 | n += r; |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
139 | } |
155cbab9eefd
adds support for CGI with request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
118
diff
changeset
|
140 | } |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
141 | system_close(handler->process.in[1]); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
142 | handler->process.in[1] = -1; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
143 | |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
144 | handler->parser = cgi_parser_new(sn, rq); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
145 | |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
146 | Event *readev = pool_malloc(sn->pool, sizeof(Event)); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
147 | ZERO(readev, sizeof(Event)); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
148 | readev->cookie = handler; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
149 | readev->fn = cgi_stdout_readevent; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
150 | readev->finish = cgi_event_finish; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
151 | |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
152 | Event *stderr_readev = pool_malloc(sn->pool, sizeof(Event)); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
153 | ZERO(stderr_readev, sizeof(Event)); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
154 | stderr_readev->cookie = handler; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
155 | stderr_readev->fn = cgi_stderr_readevent; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
156 | stderr_readev->finish = NULL; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
157 | |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
158 | Event *writeev = pool_malloc(sn->pool, sizeof(Event)); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
159 | ZERO(writeev, sizeof(Event)); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
160 | writeev->cookie = handler; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
161 | // TODO: fn |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
162 | |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
163 | handler->writeev = writeev; |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
164 | |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
165 | // add poll events for cgi stdout/stderr |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
166 | int error = 0; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
167 | if(ev_pollin(sn->ev, handler->process.err[0], stderr_readev)) { |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
168 | log_ereport(LOG_FAILURE, "send-cgi: stderr ev_pollin failed"); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
169 | error = 1; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
170 | } |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
171 | if(ev_pollin(sn->ev, handler->process.out[0], readev)) { |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
172 | log_ereport(LOG_FAILURE, "send-cgi: stdout ev_pollin failed"); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
173 | error = 1; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
174 | } |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
175 | |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
176 | if(error) { |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
177 | log_ereport(LOG_FAILURE, "cgi-send: kill script: %s", path); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
178 | kill(handler->process.pid, SIGKILL); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
179 | cgi_parser_free(handler->parser); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
180 | return REQ_ABORTED; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
181 | } |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
182 | |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
183 | return REQ_PROCESSING; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
184 | } |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
185 | |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
186 | int cgi_stdout_readevent(EventHandler *ev, Event *event) { |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
187 | CGIHandler *handler = event->cookie; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
188 | CGIResponseParser *parser = handler->parser; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
189 | Session *sn = parser->sn; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
190 | Request *rq = parser->rq; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
191 | |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
192 | char buf[4096]; // I/O buffer |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
193 | ssize_t r; |
125
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
194 | ssize_t wr = 0; |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
195 | |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
196 | handler->result = REQ_PROCEED; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
197 | while((r = read(handler->process.out[0], buf, 4096)) > 0) { |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
198 | if(parser->cgiheader) { |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
199 | size_t pos; |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
200 | int ret = cgi_parse_response(parser, buf, r, &pos); |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
201 | 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
|
202 | log_ereport( |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
203 | LOG_FAILURE, |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
204 | "broken cgi script response: path: %s", handler->path); |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
205 | protocol_status(sn, rq, 500, NULL); |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
206 | handler->result = REQ_ABORTED; |
125
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
207 | break; |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
208 | } else if(ret == 1) { |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
209 | parser->cgiheader = FALSE; |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
210 | if(parser->status > 0) { |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
211 | 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
|
212 | } |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
213 | http_start_response(sn, rq); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
214 | if(pos < r) { |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
215 | parser->response_length += r-pos; |
125
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
216 | wr = net_write(sn->csd, &buf[pos], r-pos); |
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
217 | if(wr <= 0) { |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
218 | handler->result = REQ_ABORTED; |
125
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
219 | break; |
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
220 | } |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
221 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
222 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
223 | } else { |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
224 | parser->response_length += r; |
125
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
225 | wr = net_write(sn->csd, buf, r); |
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
226 | if(wr <= 0) { |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
227 | handler->result = REQ_ABORTED; |
125
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
228 | break; |
c913d515be1e
adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
229 | } |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
230 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
231 | } |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
232 | if(r < 0 && errno == EWOULDBLOCK) { |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
233 | event->events = EVENT_POLLIN; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
234 | return 1; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
235 | } |
144
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
236 | |
162
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
237 | 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
|
238 | if(ctlen_header) { |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
239 | int64_t ctlenhdr; |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
240 | if(util_strtoint(ctlen_header, &ctlenhdr)) { |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
241 | if(ctlenhdr != parser->response_length) { |
162
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
242 | log_ereport( |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
243 | LOG_FAILURE, |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
244 | "cgi-send: script: %s: content length mismatch", |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
245 | handler->path); |
162
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
246 | rq->rq_attr.keep_alive = 0; |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
247 | handler->result = REQ_ABORTED; |
162
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
248 | } |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
249 | } |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
250 | } |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
251 | |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
252 | return 0; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
253 | } |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
254 | |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
255 | int cgi_stderr_readevent(EventHandler *ev, Event *event) { |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
256 | CGIHandler *handler = event->cookie; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
257 | |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
258 | char buf[4096]; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
259 | ssize_t r = read(handler->process.err[0], buf, 4096); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
260 | log_ereport(LOG_INFORM, "cgi pid %d %s stderr: %.*s", (int)handler->process.pid, handler->path, (int)r, buf); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
261 | |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
262 | return 0; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
263 | } |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
264 | |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
265 | int cgi_event_finish(EventHandler *ev, Event *event) { |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
266 | CGIHandler *handler = event->cookie; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
267 | CGIResponseParser *parser = handler->parser; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
268 | Session *sn = parser->sn; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
269 | Request *rq = parser->rq; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
270 | |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
271 | if(handler->result == REQ_ABORTED) { |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
272 | log_ereport(LOG_FAILURE, "cgi-send: kill script: %s", handler->path); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
273 | kill(handler->process.pid, SIGKILL); |
162
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
274 | } |
359
79b28ae7bfbd
check cgi exit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
254
diff
changeset
|
275 | |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
276 | int exit_code = cgi_close(&handler->process); |
359
79b28ae7bfbd
check cgi exit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
254
diff
changeset
|
277 | if(exit_code != 0) { |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
278 | log_ereport(LOG_FAILURE, "send-cgi: script: %s exited with code %d", handler->path, exit_code); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
279 | handler->result = REQ_ABORTED; |
359
79b28ae7bfbd
check cgi exit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
254
diff
changeset
|
280 | } |
126
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
125
diff
changeset
|
281 | |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
282 | cgi_parser_free(parser); |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
283 | // return to nsapi loop |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
284 | nsapi_function_return(sn, rq, handler->result); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
285 | return 0; |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
286 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
287 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
288 | int cgi_start(CGIProcess *p, char *path, char *const argv[], char *const envp[]) { |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
289 | if(pipe(p->in) || pipe(p->out) || pipe(p->err)) { |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
290 | log_ereport( |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
291 | LOG_FAILURE, |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
292 | "send-cgi: cannot create pipe: %s", |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
293 | strerror(errno)); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
294 | return REQ_ABORTED; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
295 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
296 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
297 | p->pid = fork(); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
298 | if(p->pid == 0) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
299 | // child |
120
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
300 | |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
301 | // get script directory and script name |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
302 | cxstring script = cx_str(path); |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
303 | cxmutstr parent; |
120
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
304 | int len = strlen(path); |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
305 | for(int i=len-1;i>=0;i--) { |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
306 | if(path[i] == '/') { |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
307 | script = cx_strn(path + i + 1, len - i); |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
308 | parent = cx_strdup(cx_strn(path, i)); |
120
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
309 | if(chdir(parent.ptr)) { |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
310 | perror("cgi_start: chdir"); |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
311 | free(parent.ptr); |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
312 | exit(-1); |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
313 | } |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
314 | free(parent.ptr); |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
315 | break; |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
316 | } |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
317 | } |
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
318 | |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
319 | if(dup2(p->in[0], STDIN_FILENO) == -1) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
320 | perror("cgi_start: dup2"); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
321 | exit(EXIT_FAILURE); |
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 | if(dup2(p->out[1], STDOUT_FILENO) == -1) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
324 | perror("cgi_start: dup2"); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
325 | exit(EXIT_FAILURE); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
326 | } |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
327 | if(dup2(p->err[1], STDERR_FILENO) == -1) { |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
328 | perror("cgi_start: dup2"); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
329 | exit(EXIT_FAILURE); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
330 | } |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
331 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
332 | // we need to close this unused pipe |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
333 | // 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
|
334 | system_close(p->in[1]); |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
335 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
336 | // execute program |
120
d2eb5fd97df0
adds chdir before cgi execution
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
337 | exit(execve(script.ptr, argv, envp)); |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
338 | } else { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
339 | // parent |
171
af7e2d80dee6
adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
164
diff
changeset
|
340 | system_close(p->out[1]); |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
341 | system_close(p->err[1]); |
144
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
342 | p->out[1] = -1; |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
343 | p->err[1] = -1; |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
344 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
345 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
346 | return REQ_PROCEED; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
347 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
348 | |
162
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
349 | 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
|
350 | int status = -1; |
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
351 | 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
|
352 | |
144
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
353 | 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
|
354 | system_close(p->in[0]); |
144
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
355 | } |
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
356 | 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
|
357 | system_close(p->in[1]); |
144
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
358 | } |
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
359 | 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
|
360 | system_close(p->out[0]); |
144
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
361 | } |
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
362 | 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
|
363 | system_close(p->out[1]); |
144
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
364 | } |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
365 | if(p->err[0] != -1) { |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
366 | system_close(p->err[0]); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
367 | } |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
368 | if(p->err[1] != -1) { |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
369 | system_close(p->err[1]); |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
370 | } |
162
b169992137a8
improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
161
diff
changeset
|
371 | |
359
79b28ae7bfbd
check cgi exit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
254
diff
changeset
|
372 | return status; |
144
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
373 | } |
4b546c4f25ed
fixes cgi fd leak
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
374 | |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
375 | CGIResponseParser* cgi_parser_new(Session *sn, Request *rq) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
376 | CGIResponseParser* parser = pool_malloc(sn->pool, sizeof(CGIResponseParser)); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
377 | parser->sn = sn; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
378 | parser->rq = rq; |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
379 | parser->status = 0; |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
380 | parser->msg = NULL; |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
381 | parser->response_length = 0; |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
382 | parser->cgiheader = TRUE; |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
383 | cxBufferInit(&parser->tmp, NULL, 64, pool_allocator(sn->pool), CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS); |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
384 | return parser; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
385 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
386 | |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
387 | void cgi_parser_free(CGIResponseParser *parser) { |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
388 | if(parser->tmp.space) { |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
389 | cxBufferDestroy(&parser->tmp); |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
390 | } |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
391 | 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
|
392 | } |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
393 | |
118
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 | * 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
|
396 | * returns 0: incomplete line |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
397 | * 1: successfully parsed lines |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
398 | * 2: cgi response header complete (empty line) |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
399 | * -1: error |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
400 | */ |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
401 | static int parse_lines(CGIResponseParser *parser, char *buf, size_t len, int *pos) { |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
402 | CxAllocator *a = pool_allocator(parser->sn->pool); |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
403 | cxmutstr name; |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
404 | cxmutstr value; |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
405 | WSBool space = TRUE; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
406 | int i; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
407 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
408 | int line_begin = 0; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
409 | int value_begin = 0; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
410 | for(i=0;i<len;i++) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
411 | char c = buf[i]; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
412 | if(value_begin == line_begin && c == ':') { |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
413 | name = cx_mutstrn(buf + line_begin, i - line_begin); |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
414 | value_begin = i + 1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
415 | } else if(c == '\n') { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
416 | if(value_begin == line_begin) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
417 | if(space) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
418 | *pos = i + 1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
419 | return 2; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
420 | } else { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
421 | // line ends with content but without ':' -> error |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
422 | return -1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
423 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
424 | } |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
425 | value = cx_mutstrn(buf + value_begin, i - value_begin); |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
426 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
427 | cx_strlower(name); |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
428 | name = cx_strdup_a(a, cx_strtrim((cxstring){name.ptr, name.length})); |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
429 | value = cx_strtrim_m(value); |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
430 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
431 | if(name.length == 0 || value.length == 0) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
432 | return -1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
433 | } |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
434 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
435 | if(!cx_strcmp((cxstring){name.ptr, name.length}, (cxstring)CX_STR("status"))) { |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
436 | cxmutstr codestr = value; |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
437 | int j; |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
438 | 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
|
439 | 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
|
440 | break; |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
441 | } |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
442 | if(j > 2) { |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
443 | break; |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
444 | } |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
445 | } |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
446 | codestr.ptr[j] = '\0'; |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
447 | |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
448 | int64_t s = 0; |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
449 | util_strtoint(codestr.ptr, &s); |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
450 | parser->status = (int)s; |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
451 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
452 | cxmutstr msg = cx_strtrim_m(cx_strsubs_m(value, j + 1)); |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
453 | |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
454 | if(msg.length > 0) { |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
455 | parser->msg = cx_strdup_pool(parser->sn->pool, msg).ptr; |
124
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
456 | } |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
457 | } else { |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
458 | pblock_nvlinsert( |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
459 | name.ptr, |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
460 | name.length, |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
461 | value.ptr, |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
462 | value.length, |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
463 | parser->rq->srvhdrs); |
85985e88f63b
using the status code from cgi output now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
123
diff
changeset
|
464 | } |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
465 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
466 | line_begin = i+1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
467 | value_begin = line_begin; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
468 | space = TRUE; |
121
a881dc866e23
adds more CGI stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
120
diff
changeset
|
469 | } else if(!isspace(c)) { |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
470 | space = FALSE; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
471 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
472 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
473 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
474 | if(i < len) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
475 | *pos = i; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
476 | return 0; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
477 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
478 | return 1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
479 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
480 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
481 | /* |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
482 | * returns -1: error |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
483 | * 0: response header incomplete |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
484 | * 1: complete |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
485 | */ |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
486 | 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
|
487 | *bpos = 0; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
488 | int pos = 0; |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
489 | if(parser->tmp.pos > 0) { |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
490 | // the tmp buffer contains an unfinished line |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
491 | // fill up the buffer until the line is complete |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
492 | WSBool nb = FALSE; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
493 | for(pos=0;pos<len;pos++) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
494 | if(buf[pos] == '\n') { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
495 | nb = TRUE; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
496 | break; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
497 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
498 | } |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
499 | cxBufferWrite(buf, 1, pos, &parser->tmp); |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
500 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
501 | if(nb) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
502 | // line complete |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
503 | int npos; |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
504 | int r = parse_lines(parser, parser->tmp.space, parser->tmp.pos, &npos); |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
505 | switch(r) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
506 | case -1: return -1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
507 | case 0: return -1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
508 | case 1: break; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
509 | case 2: { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
510 | *bpos = pos + 1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
511 | return 1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
512 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
513 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
514 | // reset tmp buffer |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
515 | parser->tmp.pos = 0; |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
516 | } else { |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
517 | if(parser->tmp.pos > CGI_RESPONSE_MAX_LINE_LENGTH) { |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
518 | return -1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
519 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
520 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
521 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
522 | |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
523 | int npos = 0; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
524 | int r = parse_lines(parser, buf + pos, len - pos, &npos); |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
525 | switch(r) { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
526 | default: return -1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
527 | case 0: |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
528 | case 1: { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
529 | int newlen = len - npos; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
530 | if(npos > 0) { |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
359
diff
changeset
|
531 | cxBufferWrite(buf + npos, 1, newlen, &parser->tmp); |
118
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
532 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
533 | return 0; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
534 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
535 | case 2: { |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
536 | *bpos = pos + npos; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
537 | return 1; |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
538 | } |
38bf6dd8f4e7
adds minimal cgi implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
539 | } |
171
af7e2d80dee6
adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
164
diff
changeset
|
540 | } |