1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #ifndef CGI_H
30 #define CGI_H
31
32 #include "../public/nsapi.h"
33 #include <cx/buffer.h>
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 enum CgiIOResult {
40 CGI_IO_COMPLETE =
0,
41 CGI_IO_NEED_READ,
42 CGI_IO_NEED_WRITE,
43 CGI_IO_ERROR
44 };
45
46 typedef enum CgiIOResult CgiIOResult;
47
48 typedef struct {
49 int in[
2];
50 int out[
2];
51 int err[
2];
52 pid_t pid;
53 } CGIProcess;
54
55 typedef struct {
56 Session *sn;
57 Request *rq;
58 CxBuffer tmp;
59 int status;
60 char *msg;
61 size_t response_length;
62 WSBool cgiheader;
63 } CGIResponseParser;
64
65 typedef struct CGIHandler {
66 CGIProcess process;
67 CGIResponseParser *parser;
68 HttpResponseWriter *response;
69
70
71
72
73 char *path;
74
75
76
77
78
79 Event *readev;
80
81
82
83
84
85 Event *writeev;
86
87
88
89
90 char *stderr_tmp;
91
92
93
94 int stderr_tmplen;
95
96
97
98
99 int stderr_tmpalloc;
100
101
102
103
104 char *writebuf;
105
106
107
108
109 size_t writebuf_alloc;
110
111
112
113
114 size_t writebuf_size;
115
116
117
118
119 size_t writebuf_pos;
120
121
122
123
124
125 size_t count_write;
126
127
128
129
130 WSBool poll_out;
131
132
133
134
135
136 WSBool wait_read;
137
138
139
140
141 WSBool cgi_eof;
142
143
144
145
146 int cgi_kill;
147
148 WSBool debug_finished;
149
150
151
152
153 int events;
154
155
156
157
158 int result;
159 } CGIHandler;
160
161 int send_cgi(pblock *pb, Session *sn, Request *rq);
162
163 char** cgi_add_vars(
char **env, Session *sn, Request *rq);
164
165 int cgi_start(CGIProcess *p,
char *path,
char *
const argv[],
char *
const envp[]);
166
167 int cgi_close(CGIProcess *p);
168
169 int cgi_stdout_readevent(EventHandler *ev, Event *event);
170 int cgi_stderr_readevent(EventHandler *ev, Event *event);
171 int cgi_event_finish(EventHandler *ev, Event *event);
172 int cgi_writeevent(EventHandler *ev, Event *event);
173
174 CgiIOResult cgi_read_output(CGIHandler *handler, EventHandler *ev,
const char *debug_log);
175
176 CGIResponseParser* cgi_parser_new(Session *sn, Request *rq);
177 void cgi_parser_free(CGIResponseParser *parser);
178 int cgi_parse_response(CGIResponseParser *parser,
char *buf,
size_t len,
size_t *bpos);
179
180 #ifdef __cplusplus
181 }
182 #endif
183
184 #endif
185
186