1 /* Copyright 2002-2004 Justin Erenkrantz and Greg Stein
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <apr_pools.h>
19 #include "serf_bucket_util.h"
20 #include "serf_private.h"
22 serf_bucket_t *serf_bucket_create(
23 const serf_bucket_type_t *type,
24 serf_bucket_alloc_t *allocator,
27 serf_bucket_t *bkt = serf_bucket_mem_alloc(allocator, sizeof(*bkt));
31 bkt->allocator = allocator;
37 apr_status_t serf_default_read_iovec(
38 serf_bucket_t *bucket,
47 /* Read some data from the bucket.
49 * Because we're an internal 'helper' to the bucket, we can't call the
50 * normal serf_bucket_read() call because the debug allocator tracker will
51 * end up marking the bucket as read *twice* - once for us and once for
52 * our caller - which is reading the same bucket. This leads to premature
53 * abort()s if we ever see EAGAIN. Instead, we'll go directly to the
54 * vtable and bypass the debug tracker.
56 apr_status_t status = bucket->type->read(bucket, requested, &data, &len);
58 /* assert that vecs_size >= 1 ? */
60 /* Return that data as a single iovec. */
62 vecs[0].iov_base = (void *)data; /* loses the 'const' */
63 vecs[0].iov_len = len;
74 apr_status_t serf_default_read_for_sendfile(
75 serf_bucket_t *bucket,
82 /* Read a bunch of stuff into the headers.
84 * See serf_default_read_iovec as to why we call into the vtable
87 apr_status_t status = bucket->type->read_iovec(bucket, requested,
92 /* There isn't a file, and there are no trailers. */
94 hdtr->numtrailers = 0;
100 serf_bucket_t *serf_default_read_bucket(
101 serf_bucket_t *bucket,
102 const serf_bucket_type_t *type)
108 void serf_default_destroy(serf_bucket_t *bucket)
110 #ifdef SERF_DEBUG_BUCKET_USE
111 serf_debug__bucket_destroy(bucket);
114 serf_bucket_mem_free(bucket->allocator, bucket);
118 void serf_default_destroy_and_data(serf_bucket_t *bucket)
120 serf_bucket_mem_free(bucket->allocator, bucket->data);
121 serf_default_destroy(bucket);
125 /* ==================================================================== */
128 char *serf_bstrmemdup(serf_bucket_alloc_t *allocator,
132 char *newstr = serf_bucket_mem_alloc(allocator, size + 1);
133 memcpy(newstr, str, size);
139 void *serf_bmemdup(serf_bucket_alloc_t *allocator,
143 void *newmem = serf_bucket_mem_alloc(allocator, size);
144 memcpy(newmem, mem, size);
149 char *serf_bstrdup(serf_bucket_alloc_t *allocator,
152 apr_size_t size = strlen(str) + 1;
153 char *newstr = serf_bucket_mem_alloc(allocator, size);
154 memcpy(newstr, str, size);
158 char *serf_bstrcatv(serf_bucket_alloc_t *allocator, struct iovec *vec,
159 int vecs, apr_size_t *bytes_written)
162 apr_size_t new_len = 0;
165 for (i = 0; i < vecs; i++) {
166 new_len += vec[i].iov_len;
169 /* It's up to the caller to free this memory later. */
170 newstr = serf_bucket_mem_alloc(allocator, new_len);
173 for (i = 0; i < vecs; i++) {
174 memcpy(c, vec[i].iov_base, vec[i].iov_len);
179 *bytes_written = c - newstr;
185 /* ==================================================================== */
188 static void find_crlf(const char **data, apr_size_t *len, int *found)
190 const char *start = *data;
191 const char *end = start + *len;
193 while (start < end) {
194 const char *cr = memchr(start, '\r', *len);
201 if (cr < end && cr[0] == '\n') {
202 *len -= cr + 1 - start;
204 *found = SERF_NEWLINE_CRLF;
210 *found = SERF_NEWLINE_CRLF_SPLIT;
214 /* It was a bare CR without an LF. Just move past it. */
219 *data = start + *len;
220 *len -= *data - start;
221 *found = SERF_NEWLINE_NONE;
225 void serf_util_readline(
238 /* If _only_ CRLF is acceptable, then the scanning needs a loop to
239 * skip false hits on CR characters. Use a separate function.
241 if (acceptable == SERF_NEWLINE_CRLF) {
242 find_crlf(data, len, found);
248 want_cr = acceptable & SERF_NEWLINE_CR;
249 want_crlf = acceptable & SERF_NEWLINE_CRLF;
250 want_lf = acceptable & SERF_NEWLINE_LF;
252 if (want_cr || want_crlf) {
253 cr = memchr(start, '\r', *len);
256 lf = memchr(start, '\n', *len);
262 *found = want_crlf ? SERF_NEWLINE_CRLF : SERF_NEWLINE_CR;
263 else if (want_cr && cr < lf)
264 *found = SERF_NEWLINE_CR;
266 *found = SERF_NEWLINE_LF;
268 else if (cr == start + *len - 1) {
269 /* the CR occurred in the last byte of the buffer. this could be
270 * a CRLF split across the data boundary.
271 * ### FIX THIS LOGIC? does caller need to detect?
273 *found = want_crlf ? SERF_NEWLINE_CRLF_SPLIT : SERF_NEWLINE_CR;
276 *found = SERF_NEWLINE_CR;
278 *found = SERF_NEWLINE_NONE;
281 *found = SERF_NEWLINE_LF;
283 *found = SERF_NEWLINE_NONE;
286 case SERF_NEWLINE_LF:
289 case SERF_NEWLINE_CR:
290 case SERF_NEWLINE_CRLF:
291 case SERF_NEWLINE_CRLF_SPLIT:
292 *data = cr + 1 + (*found == SERF_NEWLINE_CRLF);
294 case SERF_NEWLINE_NONE:
302 *len -= *data - start;
306 /* ==================================================================== */
309 void serf_databuf_init(serf_databuf_t *databuf)
311 /* nothing is sitting in the buffer */
312 databuf->remaining = 0;
314 /* avoid thinking we have hit EOF */
315 databuf->status = APR_SUCCESS;
318 /* Ensure the buffer is prepared for reading. Will return APR_SUCCESS,
319 * APR_EOF, or some failure code. *len is only set for EOF. */
320 static apr_status_t common_databuf_prep(serf_databuf_t *databuf,
326 /* if there is data in the buffer, then we're happy. */
327 if (databuf->remaining > 0)
330 /* if we already hit EOF, then keep returning that. */
331 if (APR_STATUS_IS_EOF(databuf->status)) {
332 /* *data = NULL; ?? */
337 /* refill the buffer */
338 status = (*databuf->read)(databuf->read_baton, sizeof(databuf->buf),
339 databuf->buf, &readlen);
340 if (SERF_BUCKET_READ_ERROR(status)) {
344 databuf->current = databuf->buf;
345 databuf->remaining = readlen;
346 databuf->status = status;
352 apr_status_t serf_databuf_read(
353 serf_databuf_t *databuf,
354 apr_size_t requested,
358 apr_status_t status = common_databuf_prep(databuf, len);
362 /* peg the requested amount to what we have remaining */
363 if (requested == SERF_READ_ALL_AVAIL || requested > databuf->remaining)
364 requested = databuf->remaining;
366 /* return the values */
367 *data = databuf->current;
370 /* adjust our internal state to note we've consumed some data */
371 databuf->current += requested;
372 databuf->remaining -= requested;
374 /* If we read everything, then we need to return whatever the data
375 * read returned to us. This is going to be APR_EOF or APR_EGAIN.
376 * If we have NOT read everything, then return APR_SUCCESS to indicate
377 * that we're ready to return some more if asked.
379 return databuf->remaining ? APR_SUCCESS : databuf->status;
383 apr_status_t serf_databuf_readline(
384 serf_databuf_t *databuf,
390 apr_status_t status = common_databuf_prep(databuf, len);
394 /* the returned line will start at the current position. */
395 *data = databuf->current;
397 /* read a line from the buffer, and adjust the various pointers. */
398 serf_util_readline(&databuf->current, &databuf->remaining, acceptable,
401 /* the length matches the amount consumed by the readline */
402 *len = databuf->current - *data;
404 /* see serf_databuf_read's return condition */
405 return databuf->remaining ? APR_SUCCESS : databuf->status;
409 apr_status_t serf_databuf_peek(
410 serf_databuf_t *databuf,
414 apr_status_t status = common_databuf_prep(databuf, len);
418 /* return everything we have */
419 *data = databuf->current;
420 *len = databuf->remaining;
422 /* If the last read returned EOF, then the peek should return the same.
423 * The other possibility in databuf->status is APR_EAGAIN, which we
424 * should never return. Thus, just return APR_SUCCESS for non-EOF cases.
426 if (APR_STATUS_IS_EOF(databuf->status))
432 /* ==================================================================== */
435 void serf_linebuf_init(serf_linebuf_t *linebuf)
437 linebuf->state = SERF_LINEBUF_EMPTY;
442 apr_status_t serf_linebuf_fetch(
443 serf_linebuf_t *linebuf,
444 serf_bucket_t *bucket,
447 /* If we had a complete line, then assume the caller has used it, so
448 * we can now reset the state.
450 if (linebuf->state == SERF_LINEBUF_READY) {
451 linebuf->state = SERF_LINEBUF_EMPTY;
453 /* Reset the line_used, too, so we don't have to test the state
454 * before using this value.
464 if (linebuf->state == SERF_LINEBUF_CRLF_SPLIT) {
465 /* On the previous read, we received just a CR. The LF might
466 * be present, but the bucket couldn't see it. We need to
467 * examine a single character to determine how to handle the
471 status = serf_bucket_peek(bucket, &data, &len);
472 if (SERF_BUCKET_READ_ERROR(status))
477 /* We saw the second part of CRLF. We don't need to
478 * save that character, so do an actual read to suck
481 /* ### check status */
482 (void) serf_bucket_read(bucket, 1, &data, &len);
485 * We saw the first character of the next line. Thus,
486 * the current line is terminated by the CR. Just
487 * ignore whatever we peeked at. The next reader will
488 * see it and handle it as appropriate.
491 /* Whatever was read, the line is now ready for use. */
492 linebuf->state = SERF_LINEBUF_READY;
494 /* no data available, try again later. */
501 status = serf_bucket_readline(bucket, acceptable, &found,
503 if (SERF_BUCKET_READ_ERROR(status)) {
506 /* Some bucket types (socket) might need an extra read to find
507 out EOF state, so they'll return no data in that read. This
508 means we're done reading, return what we got. */
509 if (APR_STATUS_IS_EOF(status) && len == 0) {
512 if (linebuf->used + len > sizeof(linebuf->line)) {
513 /* ### need a "line too long" error */
517 /* Note: our logic doesn't change for SERF_LINEBUF_PARTIAL. That
518 * only affects how we fill the buffer. It is a communication to
519 * our caller on whether the line is ready or not.
522 /* If we didn't see a newline, then we should mark the line
523 * buffer as partially complete.
525 if (found == SERF_NEWLINE_NONE) {
526 linebuf->state = SERF_LINEBUF_PARTIAL;
528 else if (found == SERF_NEWLINE_CRLF_SPLIT) {
529 linebuf->state = SERF_LINEBUF_CRLF_SPLIT;
531 /* Toss the partial CR. We won't ever need it. */
535 /* We got a newline (of some form). We don't need it
536 * in the line buffer, so back up the length. Then
537 * mark the line as ready.
539 len -= 1 + (found == SERF_NEWLINE_CRLF);
541 linebuf->state = SERF_LINEBUF_READY;
544 /* ### it would be nice to avoid this copy if at all possible,
545 ### and just return the a data/len pair to the caller. we're
546 ### keeping it simple for now. */
547 memcpy(&linebuf->line[linebuf->used], data, len);
548 linebuf->used += len;
551 /* If we saw anything besides "success. please read again", then
552 * we should return that status. If the line was completed, then
553 * we should also return.
555 if (status || linebuf->state == SERF_LINEBUF_READY)
558 /* We got APR_SUCCESS and the line buffer is not complete. Let's
559 * loop to read some more data.
565 /* Logging functions.
566 Use with one of the [COMP]_VERBOSE defines so that the compiler knows to
567 optimize this code out when no logging is needed. */
568 static void log_time()
572 apr_time_exp_lt(&tm, apr_time_now());
573 fprintf(stderr, "[%d-%02d-%02dT%02d:%02d:%02d.%06d%+03d] ",
574 1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday,
575 tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_usec,
579 void serf__log(int verbose_flag, const char *filename, const char *fmt, ...)
587 fprintf(stderr, "%s: ", filename);
590 vfprintf(stderr, fmt, argp);
595 void serf__log_nopref(int verbose_flag, const char *fmt, ...)
601 vfprintf(stderr, fmt, argp);
606 void serf__log_skt(int verbose_flag, const char *filename, apr_socket_t *skt,
607 const char *fmt, ...)
616 /* Log local and remote ip address:port */
617 fprintf(stderr, "[l:");
618 if (apr_socket_addr_get(&sa, APR_LOCAL, skt) == APR_SUCCESS) {
620 apr_sockaddr_ip_getbuf(buf, 32, sa);
621 fprintf(stderr, "%s:%d", buf, sa->port);
623 fprintf(stderr, " r:");
624 if (apr_socket_addr_get(&sa, APR_REMOTE, skt) == APR_SUCCESS) {
626 apr_sockaddr_ip_getbuf(buf, 32, sa);
627 fprintf(stderr, "%s:%d", buf, sa->port);
629 fprintf(stderr, "] ");
633 fprintf(stderr, "%s: ", filename);
636 vfprintf(stderr, fmt, argp);