2 * $Id: fcgi_buf.c,v 1.9 2001/02/19 03:18:56 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
)) {
56 int rv
= GetLastError();
58 if (rv
== ERROR_PIPE_NOT_CONNECTED
) {
61 else if (rv
== ERROR_INVALID_PARAMETER
) {
62 bytes_read
= recv(fd
, buf
, len
, 0);
63 if (bytes_read
== SOCKET_ERROR
) {
64 errno
= WSAGetLastError();
78 static int fd_read(int fd
, char * buf
, int len
)
82 bytes_read
= read(fd
, buf
, len
);
83 } while (bytes_read
== -1 && errno
== EINTR
);
89 /*******************************************************************************
90 * Read from an open file descriptor into buffer.
92 * The caller should disable the default Apache SIGPIPE handler,
93 * otherwise a bad script could cause the request to abort and appear
94 * as though the client's fd caused it.
97 * <0 error, errno is set
99 * >0 successful read or no room in buffer (NOT # of bytes read)
102 int fcgi_buf_add_fd(Buffer
*buf
, SOCKET fd
)
104 int fcgi_buf_add_fd(Buffer
*buf
, int fd
)
115 if (buf
->length
== buf
->size
)
116 /* there's no room in the buffer, return "success" */
119 if (buf
->length
== 0)
120 /* the buffer is empty so defrag */
121 buf
->begin
= buf
->end
= buf
->data
;
123 len
= min(buf
->size
- buf
->length
, buf
->data
+ buf
->size
- buf
->end
);
126 /* assume there is a readv() if there is a writev() */
127 if (len
== buf
->size
- buf
->length
) {
128 /* its not wrapped, use read() instead of readv() */
131 len
= fd_read(fd
, buf
->end
, len
);
139 if (buf
->end
== (buf
->data
+ buf
->size
)) {
140 /* the buffer needs to be wrapped */
141 buf
->end
= buf
->data
;
145 /* the buffer is wrapped, use readv() */
148 vec
[0].iov_base
= buf
->end
;
149 vec
[0].iov_len
= len
;
150 vec
[1].iov_base
= buf
->data
;
151 vec
[1].iov_len
= buf
->size
- buf
->length
- len
;
154 len
= readv(fd
, vec
, 2);
155 while (len
== -1 && errno
== EINTR
);
161 if (buf
->end
>= (buf
->data
+ buf
->size
))
162 buf
->end
-= buf
->size
;
168 if (buf
->length
< buf
->size
) {
169 /* There's still more buffer space to read into. */
174 struct timeval timeOut
;
177 FD_SET(fd
, &read_set
);
182 status
= ap_select(numFDs
, &read_set
, NULL
, NULL
, &timeOut
);
187 if (WSAGetLastError() == WSAENOTSOCK
)
190 if (PeekNamedPipe((HANDLE
) fd
, NULL
, 0, NULL
, &bytesavail
, NULL
))
194 len
= fd_read(fd
, buf
->end
, buf
->size
- buf
->length
);
206 return status
; /* error, errno is set */
209 if (status
> 0 && FD_ISSET(fd
, &read_set
)) {
211 len
= fd_read(fd
, buf
->end
, buf
->size
- buf
->length
);
223 return len
; /* this may not contain the number of bytes read */
228 static int fd_write(SOCKET fd
, char * buf
, int len
)
232 // HACK - We don't know if its a pipe or socket..
233 if (!WriteFile((HANDLE
) fd
, (LPVOID
) buf
, len
, (unsigned long *) &bytes_sent
, NULL
)) {
235 int rv
= GetLastError();
237 if (rv
== ERROR_INVALID_PARAMETER
) {
239 // Then it must be a real socket..
240 bytes_sent
= send(fd
, buf
, len
, 0);
241 if (bytes_sent
== SOCKET_ERROR
) {
242 rv
= WSAGetLastError();
243 if (rv
== WSAEWOULDBLOCK
) {
252 else if (rv
== WSAEWOULDBLOCK
) {
266 static int fd_write(int fd
, char * buf
, int len
)
271 bytes_sent
= write(fd
, buf
, len
);
274 if (bytes_sent
== -1 && errno
== EWOULDBLOCK
) {
278 } while (bytes_sent
== -1 && errno
== EINTR
);
285 /*******************************************************************************
286 * Write from the buffer to an open file descriptor.
288 * The caller should disable the default Apache SIGPIPE handler,
289 * otherwise a bad script could cause the request to abort appearing
290 * as though the client's fd caused it.
293 * <0 if an error occured (bytes may or may not have been written)
294 * =0 if no bytes were written
295 * >0 successful write
298 int fcgi_buf_get_to_fd(Buffer
*buf
, SOCKET fd
)
302 int fcgi_buf_get_to_fd(Buffer
*buf
, int fd
)
309 if (buf
->length
== 0)
312 len
= min(buf
->length
, buf
->data
+ buf
->size
- buf
->begin
);
315 if (len
== buf
->length
) {
316 /* the buffer is not wrapped, we don't need to use writev() */
319 len
= fd_write(fd
, buf
->begin
, len
);
327 if (buf
->begin
== buf
->data
+ buf
->size
) {
328 /* the buffer needs to be wrapped */
329 buf
->begin
= buf
->data
;
334 /* the buffer is wrapped, use writev() */
337 vec
[0].iov_base
= buf
->begin
;
338 vec
[0].iov_len
= len
;
339 vec
[1].iov_base
= buf
->data
;
340 vec
[1].iov_len
= buf
->length
- len
;
343 len
= writev(fd
, vec
, 2);
344 while (len
== -1 && errno
== EINTR
);
352 if (buf
->begin
>= buf
->data
+ buf
->size
)
353 buf
->begin
-= buf
->size
;
356 if (buf
->length
> 0) {
357 /* there's still more data to write */
362 struct timeval timeOut
;
365 FD_SET(fd
, &write_set
);
370 status
= ap_select(numFDs
, NULL
, &write_set
, NULL
, &timeOut
);
373 len
= status
; /* error, errno is set */
377 if (status
> 0 && FD_ISSET(fd
, &write_set
)) {
380 len2
= fd_write(fd
, buf
->begin
, buf
->length
);
398 if (buf
->length
== 0)
399 buf
->begin
= buf
->end
= buf
->data
;
404 /*******************************************************************************
405 * Return the data block start address and the length of the block.
407 void fcgi_buf_get_block_info(Buffer
*buf
, char **beginPtr
, size_t *countPtr
)
410 *beginPtr
= buf
->begin
;
411 *countPtr
= min(buf
->length
,
412 buf
->data
+ buf
->size
- buf
->begin
);
415 /*******************************************************************************
416 * Throw away bytes from buffer.
418 void fcgi_buf_toss(Buffer
*buf
, size_t count
)
421 ap_assert(count
>= 0 && (int) count
<= buf
->length
);
423 buf
->length
-= count
;
425 if(buf
->begin
>= buf
->data
+ buf
->size
) {
426 buf
->begin
-= buf
->size
;
430 /*******************************************************************************
431 * Return the free data block start address and the length of the block.
433 void fcgi_buf_get_free_block_info(Buffer
*buf
, char **endPtr
, size_t *countPtr
)
437 *countPtr
= min(buf
->size
- buf
->length
,
438 buf
->data
+ buf
->size
- buf
->end
);
441 /*******************************************************************************
442 * Updates the buf to reflect recently added data.
444 void fcgi_buf_add_update(Buffer
*buf
, size_t count
)
447 ap_assert(count
>= 0 && (int) count
<= BufferFree(buf
));
449 buf
->length
+= count
;
451 if(buf
->end
>= buf
->data
+ buf
->size
) {
452 buf
->end
-= buf
->size
;
458 /*******************************************************************************
459 * Adds a block of data to a buffer, returning the number of bytes added.
461 int fcgi_buf_add_block(Buffer
*buf
, char *data
, size_t datalen
)
464 int copied
= 0; /* Number of bytes actually copied. */
465 size_t canCopy
; /* Number of bytes to copy in a given op. */
467 ap_assert(data
!= NULL
);
472 ap_assert(datalen
> 0);
474 end
= buf
->data
+ buf
->size
;
477 * Copy the first part of the data: from here to the end of the
478 * buffer, or the end of the data, whichever comes first.
480 datalen
= min(BufferFree(buf
), (int) datalen
);
481 canCopy
= min((int) datalen
, end
- buf
->end
);
482 memcpy(buf
->end
, data
, canCopy
);
483 buf
->length
+= canCopy
;
486 if (buf
->end
>= end
) {
487 buf
->end
= buf
->data
;
492 * If there's more to go, copy the second part starting from the
493 * beginning of the buffer.
497 memcpy(buf
->end
, data
, datalen
);
498 buf
->length
+= datalen
;
505 /*******************************************************************************
506 * Add a string to a buffer, returning the number of bytes added.
508 int fcgi_buf_add_string(Buffer
*buf
, char *str
)
510 return fcgi_buf_add_block(buf
, str
, strlen(str
));
513 /*******************************************************************************
514 * Gets a data block from a buffer, returning the number of bytes copied.
516 int fcgi_buf_get_to_block(Buffer
*buf
, char *data
, int datalen
)
519 int copied
= 0; /* Number of bytes actually copied. */
520 size_t canCopy
; /* Number of bytes to copy in a given op. */
522 ap_assert(data
!= NULL
);
523 ap_assert(datalen
> 0);
525 end
= buf
->data
+ buf
->size
;
528 * Copy the first part out of the buffer: from here to the end
529 * of the buffer, or all of the requested data.
531 canCopy
= min(buf
->length
, datalen
);
532 canCopy
= min((int) canCopy
, end
- buf
->begin
);
533 memcpy(data
, buf
->begin
, canCopy
);
534 buf
->length
-= canCopy
;
535 buf
->begin
+= canCopy
;
537 if (buf
->begin
>= end
) {
538 buf
->begin
= buf
->data
;
542 * If there's more to go, copy the second part starting from the
543 * beginning of the buffer.
545 if (copied
< datalen
&& buf
->length
> 0) {
547 canCopy
= min(buf
->length
, datalen
- copied
);
548 memcpy(data
, buf
->begin
, canCopy
);
549 buf
->length
-= canCopy
;
550 buf
->begin
+= canCopy
;
557 /*******************************************************************************
558 * Move 'len' bytes from 'src' buffer to 'dest' buffer. There must be at
559 * least 'len' bytes available in the source buffer and space for 'len'
560 * bytes in the destination buffer.
562 void fcgi_buf_get_to_buf(Buffer
*dest
, Buffer
*src
, int len
)
564 char *dest_end
, *src_begin
;
565 size_t dest_len
, src_len
, move_len
;
568 ap_assert(BufferLength(src
) >= len
);
569 ap_assert(BufferFree(dest
) >= len
);
572 fcgi_buf_check(dest
);
578 fcgi_buf_get_free_block_info(dest
, &dest_end
, &dest_len
);
579 fcgi_buf_get_block_info(src
, &src_begin
, &src_len
);
581 move_len
= min(dest_len
, src_len
);
582 move_len
= min((int) move_len
, len
);
587 memcpy(dest_end
, src_begin
, move_len
);
588 fcgi_buf_toss(src
, move_len
);
589 fcgi_buf_add_update(dest
, move_len
);
594 static void array_grow(array_header
*arr
, size_t n
)
599 if ((int) (arr
->nelts
+ n
) > arr
->nalloc
) {
601 int new_nalloc
= (arr
->nalloc
<= 0) ? n
: arr
->nelts
+ n
;
603 new_elts
= ap_pcalloc(arr
->pool
, arr
->elt_size
* new_nalloc
);
604 memcpy(new_elts
, arr
->elts
, arr
->nelts
* arr
->elt_size
);
606 arr
->elts
= new_elts
;
607 arr
->nalloc
= new_nalloc
;
611 static void array_cat_block(array_header
*arr
, void *block
, size_t n
)
614 memcpy(arr
->elts
+ arr
->nelts
* arr
->elt_size
, block
, n
* arr
->elt_size
);
618 /*----------------------------------------------------------------------
619 * Append "len" bytes from "buf" into "arr". Apache arrays are used
620 * whenever the data being handled is binary (may contain null chars).
622 void fcgi_buf_get_to_array(Buffer
*buf
, array_header
*arr
, size_t len
)
624 int len1
= min(buf
->length
, buf
->data
+ buf
->size
- buf
->begin
);
628 ap_assert((int) len
<= BufferLength(buf
));
630 array_grow(arr
, len
);
632 len1
= min(len1
, (int) len
);
633 array_cat_block(arr
, buf
->begin
, len1
);
635 if (len1
< (int) len
)
636 array_cat_block(arr
, buf
->data
, len
- len1
);
638 fcgi_buf_toss(buf
, len
);