2 * $Id: fcgi_buf.c,v 1.8 2000/05/10 05:15:47 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
) {
238 bytes_sent
= send(fd
, buf
, len
, 0);
239 if (bytes_sent
== SOCKET_ERROR
) {
240 errno
= WSAGetLastError();
254 static int fd_write(int fd
, char * buf
, int len
)
258 bytes_sent
= write(fd
, buf
, len
);
259 } while (bytes_sent
== -1 && errno
== EINTR
);
265 /*******************************************************************************
266 * Write from the buffer to an open file descriptor.
268 * The caller should disable the default Apache SIGPIPE handler,
269 * otherwise a bad script could cause the request to abort appearing
270 * as though the client's fd caused it.
273 * <0 if an error occured (bytes may or may not have been written)
274 * =0 if no bytes were written
275 * >0 successful write
278 int fcgi_buf_get_to_fd(Buffer
*buf
, SOCKET fd
)
280 int fcgi_buf_get_to_fd(Buffer
*buf
, int fd
)
291 if (buf
->length
== 0)
294 len
= min(buf
->length
, buf
->data
+ buf
->size
- buf
->begin
);
297 if (len
== buf
->length
) {
298 /* the buffer is not wrapped, we don't need to use writev() */
301 len
= fd_write(fd
, buf
->begin
, len
);
309 if (buf
->begin
== buf
->data
+ buf
->size
) {
310 /* the buffer needs to be wrapped */
311 buf
->begin
= buf
->data
;
316 /* the buffer is wrapped, use writev() */
319 vec
[0].iov_base
= buf
->begin
;
320 vec
[0].iov_len
= len
;
321 vec
[1].iov_base
= buf
->data
;
322 vec
[1].iov_len
= buf
->length
- len
;
325 len
= writev(fd
, vec
, 2);
326 while (len
== -1 && errno
== EINTR
);
334 if (buf
->begin
>= buf
->data
+ buf
->size
)
335 buf
->begin
-= buf
->size
;
338 if (buf
->length
> 0) {
339 /* there's still more data to write */
344 struct timeval timeOut
;
347 FD_SET(fd
, &write_set
);
352 status
= ap_select(numFDs
, NULL
, &write_set
, NULL
, &timeOut
);
355 len
= status
; /* error, errno is set */
359 if (status
> 0 && FD_ISSET(fd
, &write_set
)) {
362 len2
= fd_write(fd
, buf
->begin
, buf
->length
);
380 if (buf
->length
== 0)
381 buf
->begin
= buf
->end
= buf
->data
;
386 /*******************************************************************************
387 * Return the data block start address and the length of the block.
389 void fcgi_buf_get_block_info(Buffer
*buf
, char **beginPtr
, size_t *countPtr
)
392 *beginPtr
= buf
->begin
;
393 *countPtr
= min(buf
->length
,
394 buf
->data
+ buf
->size
- buf
->begin
);
397 /*******************************************************************************
398 * Throw away bytes from buffer.
400 void fcgi_buf_toss(Buffer
*buf
, size_t count
)
403 ap_assert(count
>= 0 && (int) count
<= buf
->length
);
405 buf
->length
-= count
;
407 if(buf
->begin
>= buf
->data
+ buf
->size
) {
408 buf
->begin
-= buf
->size
;
412 /*******************************************************************************
413 * Return the free data block start address and the length of the block.
415 void fcgi_buf_get_free_block_info(Buffer
*buf
, char **endPtr
, size_t *countPtr
)
419 *countPtr
= min(buf
->size
- buf
->length
,
420 buf
->data
+ buf
->size
- buf
->end
);
423 /*******************************************************************************
424 * Updates the buf to reflect recently added data.
426 void fcgi_buf_add_update(Buffer
*buf
, size_t count
)
429 ap_assert(count
>= 0 && (int) count
<= BufferFree(buf
));
431 buf
->length
+= count
;
433 if(buf
->end
>= buf
->data
+ buf
->size
) {
434 buf
->end
-= buf
->size
;
440 /*******************************************************************************
441 * Adds a block of data to a buffer, returning the number of bytes added.
443 int fcgi_buf_add_block(Buffer
*buf
, char *data
, size_t datalen
)
446 int copied
= 0; /* Number of bytes actually copied. */
447 size_t canCopy
; /* Number of bytes to copy in a given op. */
449 ap_assert(data
!= NULL
);
454 ap_assert(datalen
> 0);
456 end
= buf
->data
+ buf
->size
;
459 * Copy the first part of the data: from here to the end of the
460 * buffer, or the end of the data, whichever comes first.
462 datalen
= min(BufferFree(buf
), (int) datalen
);
463 canCopy
= min((int) datalen
, end
- buf
->end
);
464 memcpy(buf
->end
, data
, canCopy
);
465 buf
->length
+= canCopy
;
468 if (buf
->end
>= end
) {
469 buf
->end
= buf
->data
;
474 * If there's more to go, copy the second part starting from the
475 * beginning of the buffer.
479 memcpy(buf
->end
, data
, datalen
);
480 buf
->length
+= datalen
;
487 /*******************************************************************************
488 * Add a string to a buffer, returning the number of bytes added.
490 int fcgi_buf_add_string(Buffer
*buf
, char *str
)
492 return fcgi_buf_add_block(buf
, str
, strlen(str
));
495 /*******************************************************************************
496 * Gets a data block from a buffer, returning the number of bytes copied.
498 int fcgi_buf_get_to_block(Buffer
*buf
, char *data
, int datalen
)
501 int copied
= 0; /* Number of bytes actually copied. */
502 size_t canCopy
; /* Number of bytes to copy in a given op. */
504 ap_assert(data
!= NULL
);
505 ap_assert(datalen
> 0);
507 end
= buf
->data
+ buf
->size
;
510 * Copy the first part out of the buffer: from here to the end
511 * of the buffer, or all of the requested data.
513 canCopy
= min(buf
->length
, datalen
);
514 canCopy
= min((int) canCopy
, end
- buf
->begin
);
515 memcpy(data
, buf
->begin
, canCopy
);
516 buf
->length
-= canCopy
;
517 buf
->begin
+= canCopy
;
519 if (buf
->begin
>= end
) {
520 buf
->begin
= buf
->data
;
524 * If there's more to go, copy the second part starting from the
525 * beginning of the buffer.
527 if (copied
< datalen
&& buf
->length
> 0) {
529 canCopy
= min(buf
->length
, datalen
- copied
);
530 memcpy(data
, buf
->begin
, canCopy
);
531 buf
->length
-= canCopy
;
532 buf
->begin
+= canCopy
;
539 /*******************************************************************************
540 * Move 'len' bytes from 'src' buffer to 'dest' buffer. There must be at
541 * least 'len' bytes available in the source buffer and space for 'len'
542 * bytes in the destination buffer.
544 void fcgi_buf_get_to_buf(Buffer
*dest
, Buffer
*src
, int len
)
546 char *dest_end
, *src_begin
;
547 size_t dest_len
, src_len
, move_len
;
550 ap_assert(BufferLength(src
) >= len
);
551 ap_assert(BufferFree(dest
) >= len
);
554 fcgi_buf_check(dest
);
560 fcgi_buf_get_free_block_info(dest
, &dest_end
, &dest_len
);
561 fcgi_buf_get_block_info(src
, &src_begin
, &src_len
);
563 move_len
= min(dest_len
, src_len
);
564 move_len
= min((int) move_len
, len
);
569 memcpy(dest_end
, src_begin
, move_len
);
570 fcgi_buf_toss(src
, move_len
);
571 fcgi_buf_add_update(dest
, move_len
);
576 static void array_grow(array_header
*arr
, size_t n
)
581 if ((int) (arr
->nelts
+ n
) > arr
->nalloc
) {
583 int new_nalloc
= (arr
->nalloc
<= 0) ? n
: arr
->nelts
+ n
;
585 new_elts
= ap_pcalloc(arr
->pool
, arr
->elt_size
* new_nalloc
);
586 memcpy(new_elts
, arr
->elts
, arr
->nelts
* arr
->elt_size
);
588 arr
->elts
= new_elts
;
589 arr
->nalloc
= new_nalloc
;
593 static void array_cat_block(array_header
*arr
, void *block
, size_t n
)
596 memcpy(arr
->elts
+ arr
->nelts
* arr
->elt_size
, block
, n
* arr
->elt_size
);
600 /*----------------------------------------------------------------------
601 * Append "len" bytes from "buf" into "arr". Apache arrays are used
602 * whenever the data being handled is binary (may contain null chars).
604 void fcgi_buf_get_to_array(Buffer
*buf
, array_header
*arr
, size_t len
)
606 int len1
= min(buf
->length
, buf
->data
+ buf
->size
- buf
->begin
);
610 ap_assert((int) len
<= BufferLength(buf
));
612 array_grow(arr
, len
);
614 len1
= min(len1
, (int) len
);
615 array_cat_block(arr
, buf
->begin
, len1
);
617 if (len1
< (int) len
)
618 array_cat_block(arr
, buf
->data
, len
- len1
);
620 fcgi_buf_toss(buf
, len
);