2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 struct bufio *bioopen(void *pdata, struct bufioops *ops)
40 int bioclose(struct bufio *bio)
46 rv = bio->ops->close(bio->pdata);
55 size_t biordata(struct bufio *bio)
57 return(bio->rbuf.d - bio->rh);
60 size_t biorspace(struct bufio *bio)
62 if((bio->rbuf.d - bio->rh) >= bio->bufhint)
64 return(bio->bufhint - (bio->rbuf.d - bio->rh));
67 int bioeof(struct bufio *bio)
69 return(bio->eof && (bio->rh >= bio->rbuf.d));
72 static ssize_t biofill(struct bufio *bio)
83 if(bio->rh == bio->rbuf.d)
84 bio->rh = bio->rbuf.d = 0;
85 if(bio->rbuf.d == bio->rbuf.s) {
87 memmove(bio->rbuf.b, bio->rbuf.b + bio->rh, bio->rbuf.d -= bio->rh);
90 if((ns = bio->rbuf.s * 2) < bio->bufhint)
92 sizebuf(bio->rbuf, ns);
95 if((bio->rbuf.s > bio->bufhint) && (bio->rbuf.d < bio->bufhint))
96 bio->rbuf.b = srealloc(bio->rbuf.b, bio->rbuf.s = bio->bufhint);
97 ret = bio->ops->read(bio->pdata, bio->rbuf.b + bio->rbuf.d, bio->rbuf.s - bio->rbuf.d);
101 } else if(ret == 0) {
106 return(bio->rbuf.d - bio->rh);
109 ssize_t biorensure(struct bufio *bio, size_t bytes)
113 while(bio->rbuf.d - bio->rh < bytes) {
114 if((ret = biofill(bio)) <= 0)
117 return(bio->rbuf.d - bio->rh);
120 ssize_t biofillsome(struct bufio *bio)
122 return(biofill(bio));
125 int biogetc(struct bufio *bio)
129 while(bio->rbuf.d <= bio->rh) {
130 if((ret = biofill(bio)) <= 0)
133 return((unsigned char)bio->rbuf.b[bio->rh++]);
136 ssize_t bioreadsome(struct bufio *bio, void *buf, size_t len)
140 if((bio->rh >= bio->rbuf.d) && ((ret = biofill(bio)) <= 0))
142 ret = min(len, bio->rbuf.d - bio->rh);
143 memcpy(buf, bio->rbuf.b + bio->rh, ret);
148 size_t biowdata(struct bufio *bio)
150 return(bio->wbuf.d - bio->wh);
153 size_t biowspace(struct bufio *bio)
155 if((bio->wbuf.d - bio->wh) >= bio->bufhint)
157 return(bio->bufhint - (bio->wbuf.d - bio->wh));
160 int bioflush(struct bufio *bio)
164 while(bio->wh < bio->wbuf.d) {
165 ret = bio->ops->write(bio->pdata, bio->wbuf.b + bio->wh, bio->wbuf.d - bio->wh);
175 int bioflushsome(struct bufio *bio)
179 if(bio->wh < bio->wbuf.d) {
180 ret = bio->ops->write(bio->pdata, bio->wbuf.b + bio->wh, bio->wbuf.d - bio->wh);
192 ssize_t biowensure(struct bufio *bio, size_t bytes)
194 if(bio->wbuf.s - bio->wbuf.d < bytes) {
195 if(!bio->ops->write) {
196 errno = bio->err = EPIPE;
199 if(bioflush(bio) < 0)
201 bio->wh = bio->wbuf.d = 0;
202 if((bio->wbuf.s > bio->bufhint) && (bytes <= bio->bufhint))
203 bio->wbuf.b = srealloc(bio->wbuf.b, bio->wbuf.s = bio->bufhint);
205 sizebuf(bio->wbuf, (bytes < bio->bufhint)?bio->bufhint:bytes);
210 int bioputc(struct bufio *bio, int c)
212 if(biowensure(bio, 1) < 0)
214 bio->wbuf.b[bio->wbuf.d++] = c;
218 ssize_t biowrite(struct bufio *bio, const void *data, size_t len)
224 if(biowensure(bio, min(len, bio->bufhint)) < 0) {
229 if(len < bio->wbuf.s - bio->wbuf.d) {
230 memcpy(bio->wbuf.b + bio->wbuf.d, data, len);
235 if(bioflush(bio) < 0) {
240 bio->wh = bio->wbuf.d = 0;
241 ret = bio->ops->write(bio->pdata, data, len);
248 data += ret; len -= ret; wb += ret;
254 ssize_t biowritesome(struct bufio *bio, const void *data, size_t len)
258 sizebuf(bio->wbuf, bio->bufhint);
259 if(bio->wh == bio->wbuf.d)
260 bio->wh = bio->wbuf.d = 0;
261 if(bio->wbuf.d == bio->wbuf.s) {
263 memmove(bio->wbuf.b, bio->wbuf.b + bio->wh, bio->wbuf.d -= bio->wh);
267 ret = min(len, bio->wbuf.s - bio->wbuf.d);
268 memcpy(bio->wbuf.b + bio->wbuf.d, data, ret);
270 if(bioflushsome(bio) < 0) {
273 if(ret < bio->wbuf.d - bio->wh) { /* Should never be false */
281 int bioprintf(struct bufio *bio, const char *format, ...)
286 if(biowensure(bio, strlen(format)) < 0)
289 va_start(args, format);
290 ret = vsnprintf(bio->wbuf.b + bio->wbuf.d, bio->wbuf.s - bio->wbuf.d, format, args);
292 if(ret <= bio->wbuf.s - bio->wbuf.d) {
296 if(biowensure(bio, ret) < 0)
301 ssize_t biocopysome(struct bufio *dst, struct bufio *src)
305 if(src->rh >= src->rbuf.d)
307 if((ret = biowritesome(dst, src->rbuf.b + src->rh, src->rbuf.d - src->rh)) < 0)
313 ssize_t biocopybuf(struct bufio *dst, struct bufio *src)
317 sizebuf(dst->wbuf, dst->bufhint);
318 if(dst->wbuf.d == dst->wbuf.s) {
320 memmove(dst->wbuf.b, dst->wbuf.b + dst->wh, dst->wbuf.d -= dst->wh);
324 ret = min(src->rbuf.d - src->rh, dst->wbuf.s - dst->wbuf.d);
325 memcpy(dst->wbuf.b + dst->wbuf.d, src->rbuf.b + src->rh, ret);