2 * $Id: fcgi_buf.c,v 1.12 2001/11/20 01:51:27 robs Exp $
8 #pragma warning( disable : 4127 )
11 /*******************************************************************************
12 * Check buffer consistency with assertions.
14 void fcgi_buf_check(Buffer
*buf
)
16 ap_assert(buf
->size
> 0);
17 ap_assert(buf
->length
>= 0);
18 ap_assert(buf
->length
<= buf
->size
);
20 ap_assert(buf
->begin
>= buf
->data
);
21 ap_assert(buf
->begin
< buf
->data
+ buf
->size
);
22 ap_assert(buf
->end
>= buf
->data
);
23 ap_assert(buf
->end
< buf
->data
+ buf
->size
);
25 ap_assert(((buf
->end
- buf
->begin
+ buf
->size
) % buf
->size
)
26 == (buf
->length
% buf
->size
));
29 /*******************************************************************************
30 * Reset buffer, losing any data that's in it.
32 void fcgi_buf_reset(Buffer
*buf
)
35 buf
->begin
= buf
->end
= buf
->data
;
38 /*******************************************************************************
39 * Allocate and intialize a new buffer of the specified size.
41 Buffer
*fcgi_buf_new(pool
*p
, int size
)
45 buf
= (Buffer
*)ap_pcalloc(p
, sizeof(Buffer
) + size
);
53 static int fd_read(SOCKET fd
, char *buf
, int len
)
57 // HACK - we don't know if its a pipe or socket..
58 if (ReadFile((HANDLE
) fd
, buf
, len
, &bytes_read
, NULL
))
60 return (int) bytes_read
;
64 int rv
= GetLastError();
66 if (rv
== ERROR_PIPE_NOT_CONNECTED
)
70 else if (rv
== ERROR_INVALID_PARAMETER
)
72 // Then it must be a real socket
74 SetLastError(ERROR_SUCCESS
);
76 rv
= recv(fd
, buf
, len
, 0);
77 if (rv
== SOCKET_ERROR
)
79 errno
= WSAGetLastError();
95 static int fd_read(int fd
, char * buf
, int len
)
99 bytes_read
= read(fd
, buf
, len
);
100 } while (bytes_read
== -1 && errno
== EINTR
);
106 /*******************************************************************************
107 * Read from an open file descriptor into buffer.
109 * The caller should disable the default Apache SIGPIPE handler,
110 * otherwise a bad script could cause the request to abort and appear
111 * as though the client's fd caused it.
114 * <0 error, errno is set
116 * >0 successful read or no room in buffer (NOT # of bytes read)
119 int fcgi_buf_add_fd(Buffer
*buf
, SOCKET fd
)
121 int fcgi_buf_add_fd(Buffer
*buf
, int fd
)
128 if (buf
->length
== buf
->size
)
129 /* there's no room in the buffer, return "success" */
132 if (buf
->length
== 0)
133 /* the buffer is empty so defrag */
134 buf
->begin
= buf
->end
= buf
->data
;
136 len
= min(buf
->size
- buf
->length
, buf
->data
+ buf
->size
- buf
->end
);
139 /* assume there is a readv() if there is a writev() */
140 if (len
== buf
->size
- buf
->length
) {
141 /* its not wrapped, use read() instead of readv() */
144 len
= fd_read(fd
, buf
->end
, len
);
152 if (buf
->end
== (buf
->data
+ buf
->size
)) {
153 /* the buffer needs to be wrapped */
154 buf
->end
= buf
->data
;
158 /* the buffer is wrapped, use readv() */
161 vec
[0].iov_base
= buf
->end
;
162 vec
[0].iov_len
= len
;
163 vec
[1].iov_base
= buf
->data
;
164 vec
[1].iov_len
= buf
->size
- buf
->length
- len
;
167 len
= readv(fd
, vec
, 2);
168 while (len
== -1 && errno
== EINTR
);
174 if (buf
->end
>= (buf
->data
+ buf
->size
))
175 buf
->end
-= buf
->size
;
181 if (buf
->length
< buf
->size
) {
182 /* There's still more buffer space to read into. */
187 struct timeval timeOut
;
190 FD_SET(fd
, &read_set
);
195 status
= ap_select(numFDs
, &read_set
, NULL
, NULL
, &timeOut
);
200 if (WSAGetLastError() == WSAENOTSOCK
)
203 if (PeekNamedPipe((HANDLE
) fd
, NULL
, 0, NULL
, &bytesavail
, NULL
))
207 len
= fd_read(fd
, buf
->end
, buf
->size
- buf
->length
);
219 return status
; /* error, errno is set */
222 if (status
> 0 && FD_ISSET(fd
, &read_set
)) {
224 len
= fd_read(fd
, buf
->end
, buf
->size
- buf
->length
);
236 return len
; /* this may not contain the number of bytes read */
241 static int fd_write(SOCKET fd
, char * buf
, int len
)
245 // HACK - We don't know if its a pipe or socket..
246 if (WriteFile((HANDLE
) fd
, buf
, len
, &bytes_sent
, NULL
))
248 return (int) bytes_sent
;
252 int rv
= GetLastError();
254 if (rv
== ERROR_INVALID_PARAMETER
)
256 // Then it must be a real socket..
258 SetLastError(ERROR_SUCCESS
);
260 rv
= send(fd
, buf
, len
, 0);
262 if (rv
== SOCKET_ERROR
)
264 rv
= WSAGetLastError();
265 if (rv
== WSAEWOULDBLOCK
)
278 else if (rv
== WSAEWOULDBLOCK
)
292 static int fd_write(int fd
, char * buf
, int len
)
297 bytes_sent
= write(fd
, buf
, len
);
300 if (bytes_sent
== -1 && errno
== EWOULDBLOCK
) {
304 } while (bytes_sent
== -1 && errno
== EINTR
);
311 /*******************************************************************************
312 * Write from the buffer to an open file descriptor.
314 * The caller should disable the default Apache SIGPIPE handler,
315 * otherwise a bad script could cause the request to abort appearing
316 * as though the client's fd caused it.
319 * <0 if an error occured (bytes may or may not have been written)
320 * =0 if no bytes were written
321 * >0 successful write
324 int fcgi_buf_get_to_fd(Buffer
*buf
, SOCKET fd
)
326 int fcgi_buf_get_to_fd(Buffer
*buf
, int fd
)
333 if (buf
->length
== 0)
336 len
= min(buf
->length
, buf
->data
+ buf
->size
- buf
->begin
);
339 if (len
== buf
->length
) {
340 /* the buffer is not wrapped, we don't need to use writev() */
343 len
= fd_write(fd
, buf
->begin
, len
);
351 if (buf
->begin
== buf
->data
+ buf
->size
) {
352 /* the buffer needs to be wrapped */
353 buf
->begin
= buf
->data
;
358 /* the buffer is wrapped, use writev() */
361 vec
[0].iov_base
= buf
->begin
;
362 vec
[0].iov_len
= len
;
363 vec
[1].iov_base
= buf
->data
;
364 vec
[1].iov_len
= buf
->length
- len
;
367 len
= writev(fd
, vec
, 2);
368 while (len
== -1 && errno
== EINTR
);
376 if (buf
->begin
>= buf
->data
+ buf
->size
)
377 buf
->begin
-= buf
->size
;
380 if (buf
->length
> 0) {
381 /* there's still more data to write */
386 struct timeval timeOut
;
389 FD_SET(fd
, &write_set
);
394 status
= ap_select(numFDs
, NULL
, &write_set
, NULL
, &timeOut
);
397 len
= status
; /* error, errno is set */
401 if (status
> 0 && FD_ISSET(fd
, &write_set
)) {
404 len2
= fd_write(fd
, buf
->begin
, buf
->length
);
422 if (buf
->length
== 0)
423 buf
->begin
= buf
->end
= buf
->data
;
428 /*******************************************************************************
429 * Return the data block start address and the length of the block.
431 void fcgi_buf_get_block_info(Buffer
*buf
, char **beginPtr
, int *countPtr
)
435 *beginPtr
= buf
->begin
;
436 *countPtr
= min(buf
->length
, buf
->data
+ buf
->size
- buf
->begin
);
439 /*******************************************************************************
440 * Throw away bytes from buffer.
442 void fcgi_buf_toss(Buffer
*buf
, int count
)
445 ap_assert(count
>= 0);
446 ap_assert(count
<= buf
->length
);
448 buf
->length
-= count
;
450 if(buf
->begin
>= buf
->data
+ buf
->size
) {
451 buf
->begin
-= buf
->size
;
455 /*******************************************************************************
456 * Return the free data block start address and the length of the block.
458 void fcgi_buf_get_free_block_info(Buffer
*buf
, char **endPtr
, int *countPtr
)
463 *countPtr
= min(buf
->size
- buf
->length
,
464 buf
->data
+ buf
->size
- buf
->end
);
467 /*******************************************************************************
468 * Updates the buf to reflect recently added data.
470 void fcgi_buf_add_update(Buffer
*buf
, int count
)
473 ap_assert(count
>= 0);
474 ap_assert(count
<= BufferFree(buf
));
476 buf
->length
+= count
;
478 if(buf
->end
>= buf
->data
+ buf
->size
) {
479 buf
->end
-= buf
->size
;
485 /*******************************************************************************
486 * Adds a block of data to a buffer, returning the number of bytes added.
488 int fcgi_buf_add_block(Buffer
*buf
, char *data
, int datalen
)
491 int copied
= 0; /* Number of bytes actually copied. */
492 int canCopy
; /* Number of bytes to copy in a given op. */
494 ap_assert(data
!= NULL
);
495 ap_assert(datalen
>= 0);
501 ap_assert(datalen
> 0);
503 end
= buf
->data
+ buf
->size
;
506 * Copy the first part of the data: from here to the end of the
507 * buffer, or the end of the data, whichever comes first.
509 datalen
= min(BufferFree(buf
), datalen
);
510 canCopy
= min(datalen
, end
- buf
->end
);
511 memcpy(buf
->end
, data
, canCopy
);
512 buf
->length
+= canCopy
;
515 if (buf
->end
>= end
) {
516 buf
->end
= buf
->data
;
521 * If there's more to go, copy the second part starting from the
522 * beginning of the buffer.
526 memcpy(buf
->end
, data
, datalen
);
527 buf
->length
+= datalen
;
534 /*******************************************************************************
535 * Add a string to a buffer, returning the number of bytes added.
537 int fcgi_buf_add_string(Buffer
*buf
, char *str
)
539 return fcgi_buf_add_block(buf
, str
, strlen(str
));
542 /*******************************************************************************
543 * Gets a data block from a buffer, returning the number of bytes copied.
545 int fcgi_buf_get_to_block(Buffer
*buf
, char *data
, int datalen
)
548 int copied
= 0; /* Number of bytes actually copied. */
549 int canCopy
; /* Number of bytes to copy in a given op. */
551 ap_assert(data
!= NULL
);
552 ap_assert(datalen
> 0);
555 end
= buf
->data
+ buf
->size
;
558 * Copy the first part out of the buffer: from here to the end
559 * of the buffer, or all of the requested data.
561 canCopy
= min(buf
->length
, datalen
);
562 canCopy
= min(canCopy
, end
- buf
->begin
);
564 memcpy(data
, buf
->begin
, canCopy
);
566 buf
->length
-= canCopy
;
567 buf
->begin
+= canCopy
;
569 if (buf
->begin
>= end
) {
570 buf
->begin
= buf
->data
;
574 * If there's more to go, copy the second part starting from the
575 * beginning of the buffer.
577 if (copied
< datalen
&& buf
->length
> 0) {
579 canCopy
= min(buf
->length
, datalen
- copied
);
581 memcpy(data
, buf
->begin
, canCopy
);
583 buf
->length
-= canCopy
;
584 buf
->begin
+= canCopy
;
592 /*******************************************************************************
593 * Move 'len' bytes from 'src' buffer to 'dest' buffer. There must be at
594 * least 'len' bytes available in the source buffer and space for 'len'
595 * bytes in the destination buffer.
597 void fcgi_buf_get_to_buf(Buffer
*dest
, Buffer
*src
, int len
)
599 char *dest_end
, *src_begin
;
600 int dest_len
, src_len
, move_len
;
603 ap_assert(BufferLength(src
) >= len
);
604 ap_assert(BufferFree(dest
) >= len
);
607 fcgi_buf_check(dest
);
613 fcgi_buf_get_free_block_info(dest
, &dest_end
, &dest_len
);
614 fcgi_buf_get_block_info(src
, &src_begin
, &src_len
);
616 move_len
= min(dest_len
, src_len
);
617 move_len
= min(move_len
, len
);
622 memcpy(dest_end
, src_begin
, move_len
);
623 fcgi_buf_toss(src
, move_len
);
624 fcgi_buf_add_update(dest
, move_len
);
629 static void array_grow(array_header
*arr
, int n
)
634 if (arr
->nelts
+ n
> arr
->nalloc
) {
636 int new_nalloc
= (arr
->nalloc
<= 0) ? n
: arr
->nelts
+ n
;
638 new_elts
= ap_pcalloc(arr
->pool
, arr
->elt_size
* new_nalloc
);
639 memcpy(new_elts
, arr
->elts
, arr
->nelts
* arr
->elt_size
);
641 arr
->elts
= new_elts
;
642 arr
->nalloc
= new_nalloc
;
646 static void array_cat_block(array_header
*arr
, void *block
, int n
)
649 memcpy(arr
->elts
+ arr
->nelts
* arr
->elt_size
, block
, n
* arr
->elt_size
);
653 /*----------------------------------------------------------------------
654 * Append "len" bytes from "buf" into "arr". Apache arrays are used
655 * whenever the data being handled is binary (may contain null chars).
657 void fcgi_buf_get_to_array(Buffer
*buf
, array_header
*arr
, int len
)
659 int len1
= min(buf
->length
, buf
->data
+ buf
->size
- buf
->begin
);
663 ap_assert(len
<= BufferLength(buf
));
665 array_grow(arr
, len
);
667 len1
= min(len1
, len
);
668 array_cat_block(arr
, buf
->begin
, len1
);
671 array_cat_block(arr
, buf
->data
, len
- len1
);
673 fcgi_buf_toss(buf
, len
);