📄 apr_brigade.c
字号:
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as * applicable. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */#include "apr.h"#include "apr_lib.h"#include "apr_strings.h"#include "apr_pools.h"#include "apr_tables.h"#include "apr_buckets.h"#include "apr_errno.h"#define APR_WANT_MEMFUNC#define APR_WANT_STRFUNC#include "apr_want.h"#if APR_HAVE_SYS_UIO_H#include <sys/uio.h>#endifstatic apr_status_t brigade_cleanup(void *data) { return apr_brigade_cleanup(data);}APU_DECLARE(apr_status_t) apr_brigade_cleanup(void *data){ apr_bucket_brigade *b = data; apr_bucket *e; /* * Bah! We can't use APR_RING_FOREACH here because this bucket has * gone away when we dig inside it to get the next one. */ while (!APR_BRIGADE_EMPTY(b)) { e = APR_BRIGADE_FIRST(b); apr_bucket_delete(e); } /* * We don't need to free(bb) because it's allocated from a pool. */ return APR_SUCCESS;}APU_DECLARE(apr_status_t) apr_brigade_destroy(apr_bucket_brigade *b){ apr_pool_cleanup_kill(b->p, b, brigade_cleanup); return apr_brigade_cleanup(b);}APU_DECLARE(apr_bucket_brigade *) apr_brigade_create(apr_pool_t *p, apr_bucket_alloc_t *list){ apr_bucket_brigade *b; b = apr_palloc(p, sizeof(*b)); b->p = p; b->bucket_alloc = list; APR_RING_INIT(&b->list, apr_bucket, link); apr_pool_cleanup_register(b->p, b, brigade_cleanup, apr_pool_cleanup_null); return b;}APU_DECLARE(apr_bucket_brigade *) apr_brigade_split(apr_bucket_brigade *b, apr_bucket *e){ apr_bucket_brigade *a; apr_bucket *f; a = apr_brigade_create(b->p, b->bucket_alloc); /* Return an empty brigade if there is nothing left in * the first brigade to split off */ if (e != APR_BRIGADE_SENTINEL(b)) { f = APR_RING_LAST(&b->list); APR_RING_UNSPLICE(e, f, link); APR_RING_SPLICE_HEAD(&a->list, e, f, apr_bucket, link); } APR_BRIGADE_CHECK_CONSISTENCY(a); APR_BRIGADE_CHECK_CONSISTENCY(b); return a;}APU_DECLARE(apr_status_t) apr_brigade_partition(apr_bucket_brigade *b, apr_off_t point, apr_bucket **after_point){ apr_bucket *e; const char *s; apr_size_t len; apr_status_t rv; if (point < 0) { /* this could cause weird (not necessarily SEGV) things to happen */ return APR_EINVAL; } if (point == 0) { *after_point = APR_BRIGADE_FIRST(b); return APR_SUCCESS; } APR_BRIGADE_CHECK_CONSISTENCY(b); APR_BRIGADE_FOREACH(e, b) { if ((e->length == (apr_size_t)(-1)) && (point > (apr_size_t)(-1))) { /* XXX: point is too far out to simply split this bucket, * we must fix this bucket's size and keep going... */ rv = apr_bucket_read(e, &s, &len, APR_BLOCK_READ); if (rv != APR_SUCCESS) { *after_point = e; return rv; } } if ((point < e->length) || (e->length == (apr_size_t)(-1))) { /* We already checked e->length -1 above, so we now * trust e->length < MAX_APR_SIZE_T. * First try to split the bucket natively... */ if ((rv = apr_bucket_split(e, (apr_size_t)point)) != APR_ENOTIMPL) { *after_point = APR_BUCKET_NEXT(e); return rv; } /* if the bucket cannot be split, we must read from it, * changing its type to one that can be split */ rv = apr_bucket_read(e, &s, &len, APR_BLOCK_READ); if (rv != APR_SUCCESS) { *after_point = e; return rv; } /* this assumes that len == e->length, which is okay because e * might have been morphed by the apr_bucket_read() above, but * if it was, the length would have been adjusted appropriately */ if (point < e->length) { rv = apr_bucket_split(e, (apr_size_t)point); *after_point = APR_BUCKET_NEXT(e); return rv; } } if (point == e->length) { *after_point = APR_BUCKET_NEXT(e); return APR_SUCCESS; } point -= e->length; } *after_point = APR_BRIGADE_SENTINEL(b); return APR_INCOMPLETE;}APU_DECLARE(apr_status_t) apr_brigade_length(apr_bucket_brigade *bb, int read_all, apr_off_t *length){ apr_off_t total = 0; apr_bucket *bkt; APR_BRIGADE_FOREACH(bkt, bb) { if (bkt->length == (apr_size_t)(-1)) { const char *ignore; apr_size_t len; apr_status_t status; if (!read_all) { *length = -1; return APR_SUCCESS; } if ((status = apr_bucket_read(bkt, &ignore, &len, APR_BLOCK_READ)) != APR_SUCCESS) { return status; } } total += bkt->length; } *length = total; return APR_SUCCESS;}APU_DECLARE(apr_status_t) apr_brigade_flatten(apr_bucket_brigade *bb, char *c, apr_size_t *len){ apr_size_t actual = 0; apr_bucket *b; APR_BRIGADE_FOREACH(b, bb) { const char *str; apr_size_t str_len; apr_status_t status; status = apr_bucket_read(b, &str, &str_len, APR_BLOCK_READ); if (status != APR_SUCCESS) { return status; } /* If we would overflow. */ if (str_len + actual > *len) { str_len = *len - actual; } /* XXX: It appears that overflow of the final bucket * is DISCARDED without any warning to the caller. * * No, we only copy the data up to their requested size. -- jre */ memcpy(c, str, str_len); c += str_len; actual += str_len; /* This could probably be actual == *len, but be safe from stray * photons. */ if (actual >= *len) { break; } } *len = actual; return APR_SUCCESS;}APU_DECLARE(apr_status_t) apr_brigade_pflatten(apr_bucket_brigade *bb, char **c, apr_size_t *len, apr_pool_t *pool){ apr_off_t actual; apr_size_t total; apr_status_t rv; apr_brigade_length(bb, 1, &actual); /* XXX: This is dangerous beyond belief. At least in the * apr_brigade_flatten case, the user explicitly stated their * buffer length - so we don't up and palloc 4GB for a single * file bucket. This API must grow a useful max boundry, * either compiled-in or preset via the *len value. * * Shouldn't both fn's grow an additional return value for * the case that the brigade couldn't be flattened into the * provided or allocated buffer (such as APR_EMOREDATA?) * Not a failure, simply an advisory result. */ total = (apr_size_t)actual; *c = apr_palloc(pool, total); rv = apr_brigade_flatten(bb, *c, &total); if (rv != APR_SUCCESS) { return rv; } *len = total; return APR_SUCCESS;}APU_DECLARE(apr_status_t) apr_brigade_split_line(apr_bucket_brigade *bbOut, apr_bucket_brigade *bbIn, apr_read_type_e block, apr_off_t maxbytes){ apr_off_t readbytes = 0; while (!APR_BRIGADE_EMPTY(bbIn)) { const char *pos; const char *str; apr_size_t len; apr_status_t rv; apr_bucket *e; e = APR_BRIGADE_FIRST(bbIn); rv = apr_bucket_read(e, &str, &len, block); if (rv != APR_SUCCESS) { return rv; } pos = memchr(str, APR_ASCII_LF, len); /* We found a match. */ if (pos != NULL) { apr_bucket_split(e, pos - str + 1); APR_BUCKET_REMOVE(e); APR_BRIGADE_INSERT_TAIL(bbOut, e); return APR_SUCCESS; } APR_BUCKET_REMOVE(e); APR_BRIGADE_INSERT_TAIL(bbOut, e); readbytes += len; /* We didn't find an APR_ASCII_LF within the maximum line length. */ if (readbytes >= maxbytes) { break; } } return APR_SUCCESS;}APU_DECLARE(apr_status_t) apr_brigade_to_iovec(apr_bucket_brigade *b, struct iovec *vec, int *nvec){ int left = *nvec; apr_bucket *e; struct iovec *orig; apr_size_t iov_len; apr_status_t rv; orig = vec; APR_BRIGADE_FOREACH(e, b) { if (left-- == 0) break; rv = apr_bucket_read(e, (const char **)&vec->iov_base, &iov_len, APR_NONBLOCK_READ); if (rv != APR_SUCCESS)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -