2 * $Id: fcgi_buf.c,v 1.7 2000/04/29 21:01:43 robs Exp $
7 /*******************************************************************************
8 * Check buffer consistency with assertions.
10 void fcgi_buf_check(Buffer
*buf
)
12 ap_assert(buf
->size
> 0);
13 ap_assert(buf
->length
>= 0);
14 ap_assert(buf
->length
<= buf
->size
);
16 ap_assert(buf
->begin
>= buf
->data
);
17 ap_assert(buf
->begin
< buf
->data
+ buf
->size
);
18 ap_assert(buf
->end
>= buf
->data
);
19 ap_assert(buf
->end
< buf
->data
+ buf
->size
);
21 ap_assert(((buf
->end
- buf
->begin
+ buf
->size
) % buf
->size
)
22 == (buf
->length
% buf
->size
));
25 /*******************************************************************************
26 * Reset buffer, losing any data that's in it.
28 void fcgi_buf_reset(Buffer
*buf
)
31 buf
->begin
= buf
->end
= buf
->data
;
34 /*******************************************************************************
35 * Allocate and intialize a new buffer of the specified size.
37 Buffer
*fcgi_buf_new(pool
*p
, int size
)
41 buf
= (Buffer
*)ap_pcalloc(p
, sizeof(Buffer
) + size
);
49 static int fd_read(SOCKET fd
, char *buf
, int len
)
53 // HACK - we don't know if its a pipe or socket..
54 if (!ReadFile((HANDLE
) fd
, buf
, len
, (unsigned long *) &bytes_read
, NULL
)) {
55 bytes_read
= recv(fd
, buf
, len
, 0);
56 if (bytes_read
== SOCKET_ERROR
) {
57 errno
= WSAGetLastError();
66 static int fd_read(int fd
, char * buf
, int len
)
70 bytes_read
= read(fd
, buf
, len
);
71 } while (bytes_read
== -1 && errno
== EINTR
);
77 /*******************************************************************************
78 * Read from an open file descriptor into buffer.
80 * The caller should disable the default Apache SIGPIPE handler,
81 * otherwise a bad script could cause the request to abort and appear
82 * as though the client's fd caused it.
85 * <0 error, errno is set
87 * >0 successful read or no room in buffer (NOT # of bytes read)
90 int fcgi_buf_add_fd(Buffer
*buf
, SOCKET fd
)
92 int fcgi_buf_add_fd(Buffer
*buf
, int fd
)
103 if (buf
->length
== buf
->size
)
104 /* there's no room in the buffer, return "success" */
107 if (buf
->length
== 0)
108 /* the buffer is empty so defrag */
109 buf
->begin
= buf
->end
= buf
->data
;
111 len
= min(buf
->size
- buf
->length
, buf
->data
+ buf
->size
- buf
->end
);
114 /* assume there is a readv() if there is a writev() */
115 if (len
== buf
->size
- buf
->length
) {
116 /* its not wrapped, use read() instead of readv() */
119 len
= fd_read(fd
, buf
->end
, len
);
127 if (buf
->end
== (buf
->data
+ buf
->size
)) {
128 /* the buffer needs to be wrapped */
129 buf
->end
= buf
->data
;
133 /* the buffer is wrapped, use readv() */
136 vec
[0].iov_base
= buf
->end
;
137 vec
[0].iov_len
= len
;
138 vec
[1].iov_base
= buf
->data
;
139 vec
[1].iov_len
= buf
->size
- buf
->length
- len
;
142 len
= readv(fd
, vec
, 2);
143 while (len
== -1 && errno
== EINTR
);
149 if (buf
->end
>= (buf
->data
+ buf
->size
))
150 buf
->end
-= buf
->size
;
155 if (buf
->length
< buf
->size
) {
156 /* There's still more buffer space to read into. */
161 struct timeval timeOut
;
164 FD_SET(fd
, &read_set
);
169 status
= ap_select(numFDs
, &read_set
, NULL
, NULL
, &timeOut
);
172 return status
; /* error, errno is set */
174 if (status
> 0 && FD_ISSET(fd
, &read_set
)) {
176 len
= fd_read(fd
, buf
->end
, buf
->size
- buf
->length
);
187 return len
; /* this may not contain the number of bytes read */
192 static int fd_write(SOCKET fd
, char * buf
, int len
)
196 // HACK - We don't know if its a pipe or socket..
197 if (!WriteFile((HANDLE
) fd
, (LPVOID
) buf
, len
, (unsigned long *) &bytes_sent
, NULL
)) {
198 bytes_sent
= send(fd
, buf
, len
, 0);
199 if (bytes_sent
== SOCKET_ERROR
) {
200 errno
= WSAGetLastError();
209 static int fd_write(int fd
, char * buf
, int len
)
213 bytes_sent
= write(fd
, buf
, len
);
214 } while (bytes_sent
== -1 && errno
== EINTR
);
220 /*******************************************************************************
221 * Write from the buffer to an open file descriptor.
223 * The caller should disable the default Apache SIGPIPE handler,
224 * otherwise a bad script could cause the request to abort appearing
225 * as though the client's fd caused it.
228 * <0 if an error occured (bytes may or may not have been written)
229 * =0 if no bytes were written
230 * >0 successful write
233 int fcgi_buf_get_to_fd(Buffer
*buf
, SOCKET fd
)
235 int fcgi_buf_get_to_fd(Buffer
*buf
, int fd
)
246 if (buf
->length
== 0)
249 len
= min(buf
->length
, buf
->data
+ buf
->size
- buf
->begin
);
252 if (len
== buf
->length
) {
253 /* the buffer is not wrapped, we don't need to use writev() */
256 len
= fd_write(fd
, buf
->begin
, len
);
264 if (buf
->begin
== buf
->data
+ buf
->size
) {
265 /* the buffer needs to be wrapped */
266 buf
->begin
= buf
->data
;
271 /* the buffer is wrapped, use writev() */
274 vec
[0].iov_base
= buf
->begin
;
275 vec
[0].iov_len
= len
;
276 vec
[1].iov_base
= buf
->data
;
277 vec
[1].iov_len
= buf
->length
- len
;
280 len
= writev(fd
, vec
, 2);
281 while (len
== -1 && errno
== EINTR
);
289 if (buf
->begin
>= buf
->data
+ buf
->size
)
290 buf
->begin
-= buf
->size
;
293 if (buf
->length
> 0) {
294 /* there's still more data to write */
299 struct timeval timeOut
;
302 FD_SET(fd
, &write_set
);
307 status
= ap_select(numFDs
, NULL
, &write_set
, NULL
, &timeOut
);
310 len
= status
; /* error, errno is set */
314 if (status
> 0 && FD_ISSET(fd
, &write_set
)) {
317 len2
= fd_write(fd
, buf
->begin
, buf
->length
);
335 if (buf
->length
== 0)
336 buf
->begin
= buf
->end
= buf
->data
;
341 /*******************************************************************************
342 * Return the data block start address and the length of the block.
344 void fcgi_buf_get_block_info(Buffer
*buf
, char **beginPtr
, size_t *countPtr
)
347 *beginPtr
= buf
->begin
;
348 *countPtr
= min(buf
->length
,
349 buf
->data
+ buf
->size
- buf
->begin
);
352 /*******************************************************************************
353 * Throw away bytes from buffer.
355 void fcgi_buf_toss(Buffer
*buf
, size_t count
)
358 ap_assert(count
>= 0 && (int) count
<= buf
->length
);
360 buf
->length
-= count
;
362 if(buf
->begin
>= buf
->data
+ buf
->size
) {
363 buf
->begin
-= buf
->size
;
367 /*******************************************************************************
368 * Return the free data block start address and the length of the block.
370 void fcgi_buf_get_free_block_info(Buffer
*buf
, char **endPtr
, size_t *countPtr
)
374 *countPtr
= min(buf
->size
- buf
->length
,
375 buf
->data
+ buf
->size
- buf
->end
);
378 /*******************************************************************************
379 * Updates the buf to reflect recently added data.
381 void fcgi_buf_add_update(Buffer
*buf
, size_t count
)
384 ap_assert(count
>= 0 && (int) count
<= BufferFree(buf
));
386 buf
->length
+= count
;
388 if(buf
->end
>= buf
->data
+ buf
->size
) {
389 buf
->end
-= buf
->size
;
395 /*******************************************************************************
396 * Adds a block of data to a buffer, returning the number of bytes added.
398 int fcgi_buf_add_block(Buffer
*buf
, char *data
, size_t datalen
)
401 int copied
= 0; /* Number of bytes actually copied. */
402 size_t canCopy
; /* Number of bytes to copy in a given op. */
404 ap_assert(data
!= NULL
);
409 ap_assert(datalen
> 0);
411 end
= buf
->data
+ buf
->size
;
414 * Copy the first part of the data: from here to the end of the
415 * buffer, or the end of the data, whichever comes first.
417 datalen
= min(BufferFree(buf
), (int) datalen
);
418 canCopy
= min((int) datalen
, end
- buf
->end
);
419 memcpy(buf
->end
, data
, canCopy
);
420 buf
->length
+= canCopy
;
423 if (buf
->end
>= end
) {
424 buf
->end
= buf
->data
;
429 * If there's more to go, copy the second part starting from the
430 * beginning of the buffer.
434 memcpy(buf
->end
, data
, datalen
);
435 buf
->length
+= datalen
;
442 /*******************************************************************************
443 * Add a string to a buffer, returning the number of bytes added.
445 int fcgi_buf_add_string(Buffer
*buf
, char *str
)
447 return fcgi_buf_add_block(buf
, str
, strlen(str
));
450 /*******************************************************************************
451 * Gets a data block from a buffer, returning the number of bytes copied.
453 int fcgi_buf_get_to_block(Buffer
*buf
, char *data
, int datalen
)
456 int copied
= 0; /* Number of bytes actually copied. */
457 size_t canCopy
; /* Number of bytes to copy in a given op. */
459 ap_assert(data
!= NULL
);
460 ap_assert(datalen
> 0);
462 end
= buf
->data
+ buf
->size
;
465 * Copy the first part out of the buffer: from here to the end
466 * of the buffer, or all of the requested data.
468 canCopy
= min(buf
->length
, datalen
);
469 canCopy
= min((int) canCopy
, end
- buf
->begin
);
470 memcpy(data
, buf
->begin
, canCopy
);
471 buf
->length
-= canCopy
;
472 buf
->begin
+= canCopy
;
474 if (buf
->begin
>= end
) {
475 buf
->begin
= buf
->data
;
479 * If there's more to go, copy the second part starting from the
480 * beginning of the buffer.
482 if (copied
< datalen
&& buf
->length
> 0) {
484 canCopy
= min(buf
->length
, datalen
- copied
);
485 memcpy(data
, buf
->begin
, canCopy
);
486 buf
->length
-= canCopy
;
487 buf
->begin
+= canCopy
;
494 /*******************************************************************************
495 * Move 'len' bytes from 'src' buffer to 'dest' buffer. There must be at
496 * least 'len' bytes available in the source buffer and space for 'len'
497 * bytes in the destination buffer.
499 void fcgi_buf_get_to_buf(Buffer
*dest
, Buffer
*src
, int len
)
501 char *dest_end
, *src_begin
;
502 size_t dest_len
, src_len
, move_len
;
505 ap_assert(BufferLength(src
) >= len
);
506 ap_assert(BufferFree(dest
) >= len
);
509 fcgi_buf_check(dest
);
515 fcgi_buf_get_free_block_info(dest
, &dest_end
, &dest_len
);
516 fcgi_buf_get_block_info(src
, &src_begin
, &src_len
);
518 move_len
= min(dest_len
, src_len
);
519 move_len
= min((int) move_len
, len
);
524 memcpy(dest_end
, src_begin
, move_len
);
525 fcgi_buf_toss(src
, move_len
);
526 fcgi_buf_add_update(dest
, move_len
);
531 static void array_grow(array_header
*arr
, size_t n
)
536 if ((int) (arr
->nelts
+ n
) > arr
->nalloc
) {
538 int new_nalloc
= (arr
->nalloc
<= 0) ? n
: arr
->nelts
+ n
;
540 new_elts
= ap_pcalloc(arr
->pool
, arr
->elt_size
* new_nalloc
);
541 memcpy(new_elts
, arr
->elts
, arr
->nelts
* arr
->elt_size
);
543 arr
->elts
= new_elts
;
544 arr
->nalloc
= new_nalloc
;
548 static void array_cat_block(array_header
*arr
, void *block
, size_t n
)
551 memcpy(arr
->elts
+ arr
->nelts
* arr
->elt_size
, block
, n
* arr
->elt_size
);
555 /*----------------------------------------------------------------------
556 * Append "len" bytes from "buf" into "arr". Apache arrays are used
557 * whenever the data being handled is binary (may contain null chars).
559 void fcgi_buf_get_to_array(Buffer
*buf
, array_header
*arr
, size_t len
)
561 int len1
= min(buf
->length
, buf
->data
+ buf
->size
- buf
->begin
);
565 ap_assert((int) len
<= BufferLength(buf
));
567 array_grow(arr
, len
);
569 len1
= min(len1
, (int) len
);
570 array_cat_block(arr
, buf
->begin
, len1
);
572 if (len1
< (int) len
)
573 array_cat_block(arr
, buf
->data
, len
- len1
);
575 fcgi_buf_toss(buf
, len
);