1 |
/* |
2 |
* Boa, 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.3 2002/09/23 13:24:19 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 |
} else if (bytes_read == 0) { /* eof, write rest of buffer */ |
102 |
req->status = PIPE_WRITE; |
103 |
if (req->cgi_status == CGI_PARSE) { /* hasn't processed header yet */ |
104 |
req->cgi_status = CGI_DONE; |
105 |
*req->header_end = '\0'; /* points to end of read data */ |
106 |
return process_cgi_header(req); /* cgi_status will change */ |
107 |
} |
108 |
req->cgi_status = CGI_DONE; |
109 |
return 1; |
110 |
} |
111 |
req->header_end += bytes_read; |
112 |
return 1; |
113 |
} |
114 |
|
115 |
/* |
116 |
* Name: write_from_pipe |
117 |
* Description: Writes data previously read from a pipe |
118 |
* |
119 |
* Return values: |
120 |
* -1: request blocked, move to blocked queue |
121 |
* 0: EOF or error, close it down |
122 |
* 1: successful write, recycle in ready queue |
123 |
*/ |
124 |
|
125 |
int write_from_pipe(request * req) |
126 |
{ |
127 |
int bytes_written, bytes_to_write = req->header_end - req->header_line; |
128 |
|
129 |
if (bytes_to_write == 0) { |
130 |
if (req->cgi_status == CGI_DONE) |
131 |
return 0; |
132 |
|
133 |
req->status = PIPE_READ; |
134 |
req->header_end = req->header_line = req->buffer; |
135 |
return 1; |
136 |
} |
137 |
|
138 |
bytes_written = socket_send(req, req->header_line, bytes_to_write); |
139 |
|
140 |
if (bytes_written < 0) { |
141 |
if (bytes_written == BOA_E_AGAIN) |
142 |
return -1; /* request blocked at the pipe level, but keep going */ |
143 |
else if (bytes_written == BOA_E_INTR) |
144 |
return 1; |
145 |
else { |
146 |
req->status = DEAD; |
147 |
send_r_error(req); /* maybe superfluous */ |
148 |
log_error_doc(req); |
149 |
perror("pipe write"); |
150 |
return 0; |
151 |
} |
152 |
} |
153 |
|
154 |
req->header_line += bytes_written; |
155 |
req->filepos += bytes_written; |
156 |
|
157 |
return 1; |
158 |
} |