1 |
/* |
2 |
* Hydra, an http server |
3 |
* Based on code Copyright (C) 1995 Paul Phillips <paulp@go2net.com> |
4 |
* Some changes Copyright (C) 1997-1999 Jon Nelson <jnelson@boa.org> |
5 |
* Portions Copyright (C) 2002 Nikos Mavroyanopoulos <nmav@gnutls.org> |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 1, or (at your option) |
10 |
* any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 |
* |
21 |
*/ |
22 |
|
23 |
/* $Id: pipe.c,v 1.7 2002/10/21 19:36:54 nmav Exp $*/ |
24 |
|
25 |
#include "boa.h" |
26 |
#include "socket.h" |
27 |
|
28 |
/* |
29 |
* Name: read_from_pipe |
30 |
* Description: Reads data from a pipe |
31 |
* |
32 |
* Return values: |
33 |
* -1: request blocked, move to blocked queue |
34 |
* 0: EOF or error, close it down |
35 |
* 1: successful read, recycle in ready queue |
36 |
*/ |
37 |
|
38 |
int read_from_pipe(request * req) |
39 |
{ |
40 |
int bytes_read, bytes_to_read; |
41 |
|
42 |
if (req->is_cgi) |
43 |
bytes_to_read = BUFFER_SIZE - (req->header_end - req->buffer); |
44 |
else { |
45 |
/* if not a cgi, then read up to range_stop value. The init_get() should have |
46 |
* used lseek() for the range_start. |
47 |
*/ |
48 |
bytes_to_read = BUFFER_SIZE - (req->header_end - req->buffer); |
49 |
|
50 |
if (req->pipe_range_stop >= bytes_to_read) |
51 |
req->pipe_range_stop -= bytes_to_read; |
52 |
else { |
53 |
bytes_to_read = req->pipe_range_stop; |
54 |
|
55 |
if (bytes_to_read == 0) { /* no need to move below */ |
56 |
req->status = PIPE_WRITE; |
57 |
req->cgi_status = CGI_DONE; |
58 |
return 1; |
59 |
} |
60 |
req->pipe_range_stop = 0; |
61 |
} |
62 |
} |
63 |
|
64 |
if (bytes_to_read == 0) { /* buffer full */ |
65 |
if (req->cgi_status == CGI_PARSE) { /* got+parsed header */ |
66 |
req->cgi_status = CGI_BUFFER; |
67 |
*req->header_end = '\0'; /* points to end of read data */ |
68 |
/* Could the above statement overwrite data??? |
69 |
No, because req->header_end points to where new data |
70 |
should begin, not where old data is. |
71 |
*/ |
72 |
return process_cgi_header(req); /* cgi_status will change */ |
73 |
} |
74 |
req->status = PIPE_WRITE; |
75 |
return 1; |
76 |
} |
77 |
|
78 |
bytes_read = read(req->data_fd, req->header_end, bytes_to_read); |
79 |
#ifdef FASCIST_LOGGING |
80 |
if (bytes_read > 0) { |
81 |
*(req->header_end + bytes_read) = '\0'; |
82 |
fprintf(stderr, "pipe.c - read %d bytes: \"%s\"\n", |
83 |
bytes_read, req->header_end); |
84 |
} else |
85 |
fprintf(stderr, "pipe.c - read %d bytes\n", bytes_read); |
86 |
fprintf(stderr, "status, cgi_status: %d, %d\n", req->status, |
87 |
req->cgi_status); |
88 |
#endif |
89 |
|
90 |
if (bytes_read == -1) { |
91 |
if (errno == EINTR) |
92 |
return 1; |
93 |
else if (errno == EWOULDBLOCK || errno == EAGAIN) |
94 |
return -1; /* request blocked at the pipe level, but keep going */ |
95 |
else { |
96 |
req->status = DEAD; |
97 |
log_error_doc(req); |
98 |
perror("pipe read"); |
99 |
return 0; |
100 |
} |
101 |
} |
102 |
*(req->header_end + bytes_read) = '\0'; |
103 |
|
104 |
if (bytes_read == 0) { /* eof, write rest of buffer */ |
105 |
req->status = PIPE_WRITE; |
106 |
if (req->cgi_status == CGI_PARSE) { /* hasn't processed header yet */ |
107 |
req->cgi_status = CGI_DONE; |
108 |
*req->header_end = '\0'; /* points to end of read data */ |
109 |
return process_cgi_header(req); /* cgi_status will change */ |
110 |
} |
111 |
req->cgi_status = CGI_DONE; |
112 |
return 1; |
113 |
} |
114 |
|
115 |
req->header_end += bytes_read; |
116 |
|
117 |
return 1; |
118 |
} |
119 |
|
120 |
/* |
121 |
* Name: write_from_pipe |
122 |
* Description: Writes data previously read from a pipe |
123 |
* |
124 |
* Return values: |
125 |
* -1: request blocked, move to blocked queue |
126 |
* 0: EOF or error, close it down |
127 |
* 1: successful write, recycle in ready queue |
128 |
*/ |
129 |
|
130 |
int write_from_pipe(request * req) |
131 |
{ |
132 |
int bytes_written, bytes_to_write = req->header_end - req->header_line; |
133 |
|
134 |
if (bytes_to_write == 0) { |
135 |
if (req->cgi_status == CGI_DONE) |
136 |
return 0; |
137 |
|
138 |
req->status = PIPE_READ; |
139 |
req->header_end = req->header_line = req->buffer; |
140 |
return 1; |
141 |
} |
142 |
|
143 |
bytes_written = socket_send(req, req->header_line, bytes_to_write); |
144 |
|
145 |
if (bytes_written < 0) { |
146 |
if (bytes_written == BOA_E_AGAIN) |
147 |
return -1; /* request blocked at the pipe level, but keep going */ |
148 |
else if (bytes_written == BOA_E_INTR) |
149 |
return 1; |
150 |
else { |
151 |
req->status = DEAD; |
152 |
send_r_error(req); /* maybe superfluous */ |
153 |
log_error_doc(req); |
154 |
perror("pipe write"); |
155 |
return 0; |
156 |
} |
157 |
} |
158 |
|
159 |
req->header_line += bytes_written; |
160 |
req->filepos += bytes_written; |
161 |
|
162 |
/* if there won't be anything to write next time, switch state */ |
163 |
if (bytes_written == bytes_to_write) { |
164 |
req->status = PIPE_READ; |
165 |
req->header_end = req->header_line = req->buffer; |
166 |
} |
167 |
|
168 |
return 1; |
169 |
} |
170 |
|
171 |
#ifdef HAVE_SENDFILE |
172 |
int io_shuffle(request *req) |
173 |
{ |
174 |
int foo; |
175 |
off_t filepos; |
176 |
|
177 |
foo = req->pipe_range_stop - req->filepos; |
178 |
if (foo > system_bufsize) |
179 |
foo = system_bufsize; |
180 |
|
181 |
retrysendfile: |
182 |
filepos = req->filepos; |
183 |
#ifdef HAVE_BSDSENDFILE |
184 |
foo = sendfile(req->fd, req->data_fd, req->filepos, foo, NULL, &filepos, 0); |
185 |
#else /* Linux sendfile */ |
186 |
foo = sendfile(req->fd, req->data_fd, &filepos, foo); |
187 |
#endif |
188 |
req->filepos = filepos; |
189 |
|
190 |
if (foo >= 0) { |
191 |
if (req->filepos >= req->pipe_range_stop) |
192 |
return 0; |
193 |
return 1; |
194 |
} else { |
195 |
if (errno == EWOULDBLOCK || errno == EAGAIN) |
196 |
return -1; /* request blocked at the pipe level, but keep going */ |
197 |
else if (errno == EINTR) |
198 |
goto retrysendfile; |
199 |
else { |
200 |
req->status = DEAD; |
201 |
send_r_error(req); /* maybe superfluous */ |
202 |
log_error_doc(req); |
203 |
perror("sendfile write"); |
204 |
return 0; |
205 |
} |
206 |
} |
207 |
} |
208 |
#else |
209 |
|
210 |
/* always try to read unless data_fs is 0 (and there is space) |
211 |
* then try to write |
212 |
* |
213 |
* Return values: |
214 |
* -1: request blocked, move to blocked queue |
215 |
* 0: EOF or error, close it down |
216 |
* 1: successful read, recycle in ready queue |
217 |
*/ |
218 |
|
219 |
int io_shuffle(request * req) |
220 |
{ |
221 |
int bytes_to_read; |
222 |
int bytes_written, bytes_to_write; |
223 |
|
224 |
bytes_to_read = BUFFER_SIZE - req->buffer_end; |
225 |
|
226 |
if (bytes_to_read > 0 && req->data_fd) { |
227 |
int bytes_read; |
228 |
restartread: |
229 |
bytes_read = read(req->data_fd, req->buffer + req->buffer_end, bytes_to_read); |
230 |
|
231 |
if (bytes_read == -1) { |
232 |
if (errno == EINTR) |
233 |
goto restartread; |
234 |
else if (errno == EWOULDBLOCK || errno == EAGAIN) { |
235 |
/* not a fatal error, don't worry about it */ |
236 |
/* buffer is empty, we're blocking on read! */ |
237 |
if (req->buffer_end - req->buffer_start == 0) |
238 |
return -1; |
239 |
} else { |
240 |
req->status = DEAD; |
241 |
log_error_doc(req); |
242 |
perror("ioshuffle read"); |
243 |
return 0; |
244 |
} |
245 |
} else if (bytes_read == 0) { /* eof, write rest of buffer */ |
246 |
close(req->data_fd); |
247 |
req->data_fd = -1; |
248 |
} else { |
249 |
req->buffer_end += bytes_read; |
250 |
} |
251 |
} |
252 |
|
253 |
bytes_to_write = req->buffer_end - req->buffer_start; |
254 |
if (bytes_to_write == 0) { |
255 |
if (req->data_fd == 0) |
256 |
return 0; /* done */ |
257 |
req->buffer_end = req->buffer_start = 0; |
258 |
return 1; |
259 |
} |
260 |
|
261 |
restartwrite: |
262 |
bytes_written = write(req->fd, req->buffer + req->buffer_start, bytes_to_write); |
263 |
|
264 |
if (bytes_written == -1) { |
265 |
if (errno == EWOULDBLOCK || errno == EAGAIN) |
266 |
return -1; /* request blocked at the pipe level, but keep going */ |
267 |
else if (errno == EINTR) |
268 |
goto restartwrite; |
269 |
else { |
270 |
req->status = DEAD; |
271 |
send_r_error(req); /* maybe superfluous */ |
272 |
log_error_doc(req); |
273 |
perror("ioshuffle write"); |
274 |
return 0; |
275 |
} |
276 |
} else if (bytes_written == 0) { |
277 |
} |
278 |
|
279 |
req->buffer_start += bytes_written; |
280 |
req->filepos += bytes_written; |
281 |
|
282 |
if (bytes_to_write == bytes_written) { |
283 |
req->buffer_end = req->buffer_start = 0; |
284 |
} |
285 |
|
286 |
return 1; |
287 |
} |
288 |
|
289 |
#endif |