1 /* $MirOS: src/usr.sbin/httpd/src/include/buff.h,v 1.2 2013/10/31 20:07:21 tg Exp $ */ 2 /* $OpenBSD: buff.h,v 1.12 2005/03/28 23:26:51 niallo Exp $ */ 3 4 /* ==================================================================== 5 * The Apache Software License, Version 1.1 6 * 7 * Copyright © 2013 8 * Thorsten “mirabilos” Glaser <tg@mirbsd.org> 9 * Copyright (c) 2000-2003 The Apache Software Foundation. All rights 10 * reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in 21 * the documentation and/or other materials provided with the 22 * distribution. 23 * 24 * 3. The end-user documentation included with the redistribution, 25 * if any, must include the following acknowledgment: 26 * "This product includes software developed by the 27 * Apache Software Foundation (http://www.apache.org/)." 28 * Alternately, this acknowledgment may appear in the software itself, 29 * if and wherever such third-party acknowledgments normally appear. 30 * 31 * 4. The names "Apache" and "Apache Software Foundation" must 32 * not be used to endorse or promote products derived from this 33 * software without prior written permission. For written 34 * permission, please contact apache@apache.org. 35 * 36 * 5. Products derived from this software may not be called "Apache", 37 * nor may "Apache" appear in their name, without prior written 38 * permission of the Apache Software Foundation. 39 * 40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 42 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 43 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 46 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 47 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 49 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 50 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * ==================================================================== 53 * 54 * This software consists of voluntary contributions made by many 55 * individuals on behalf of the Apache Software Foundation. For more 56 * information on the Apache Software Foundation, please see 57 * <http://www.apache.org/>. 58 * 59 * Portions of this software are based upon public domain software 60 * originally written at the National Center for Supercomputing Applications, 61 * University of Illinois, Urbana-Champaign. 62 */ 63 64 #ifndef APACHE_BUFF_H 65 #define APACHE_BUFF_H 66 67 #ifdef __cplusplus 68 extern "C" { 69 #endif 70 71 #include <stdarg.h> 72 73 /* Reading is buffered */ 74 #define B_RD (1) 75 /* Writing is buffered */ 76 #define B_WR (2) 77 #define B_RDWR (3) 78 /* At end of file, or closed stream; no further input allowed */ 79 #define B_EOF (4) 80 /* No further output possible */ 81 #define B_EOUT (8) 82 /* A read error has occurred */ 83 #define B_RDERR (16) 84 /* A write error has occurred */ 85 #define B_WRERR (32) 86 #ifdef B_ERROR /* in SVR4: sometimes defined in /usr/include/sys/buf.h */ 87 #undef B_ERROR 88 #endif 89 #define B_ERROR (48) 90 /* Use chunked writing */ 91 #define B_CHUNK (64) 92 /* bflush() if a read would block */ 93 #define B_SAFEREAD (128) 94 /* buffer is a socket */ 95 #define B_SOCKET (256) 96 97 typedef struct buff_struct BUFF; 98 99 struct buff_struct { 100 int flags; /* flags */ 101 unsigned char *inptr; /* pointer to next location to read */ 102 int incnt; /* number of bytes left to read from input buffer; 103 * always 0 if had a read error */ 104 int outchunk; /* location of chunk header when chunking */ 105 int outcnt; /* number of byte put in output buffer */ 106 unsigned char *inbase; 107 unsigned char *outbase; 108 int bufsiz; 109 void (*error) (BUFF *fb, int op, void *data); 110 void *error_data; 111 long int bytes_sent; /* number of bytes actually written */ 112 113 ap_pool *pool; 114 115 /* could also put pointers to the basic I/O routines here */ 116 int fd; /* the file descriptor */ 117 int fd_in; /* input file descriptor, if different */ 118 119 /* transport handle, for RPC binding handle or some such */ 120 void *t_handle; 121 122 ap_ctx *ctx; 123 124 void *callback_data; 125 void (*filter_callback)(BUFF *, const void *, int ); 126 }; 127 128 /* Options to bset/getopt */ 129 #define BO_BYTECT (1) 130 131 /* Stream creation and modification */ 132 API_EXPORT(BUFF *) ap_bcreate(pool *p, int flags); 133 API_EXPORT(void) ap_bpushfd(BUFF *fb, int fd_in, int fd_out); 134 API_EXPORT(int) ap_bsetopt(BUFF *fb, int optname, const void *optval); 135 API_EXPORT(int) ap_bgetopt(BUFF *fb, int optname, void *optval); 136 API_EXPORT(int) ap_bsetflag(BUFF *fb, int flag, int value); 137 API_EXPORT(int) ap_bclose(BUFF *fb); 138 139 #define ap_bgetflag(fb, flag) ((fb)->flags & (flag)) 140 141 /* Error handling */ 142 API_EXPORT(void) ap_bonerror(BUFF *fb, void (*error) (BUFF *, int, void *), 143 void *data); 144 145 /* I/O */ 146 API_EXPORT(int) ap_bread(BUFF *fb, void *buf, int nbyte); 147 API_EXPORT(int) ap_bgets(char *s, int n, BUFF *fb); 148 API_EXPORT(int) ap_blookc(char *buff, BUFF *fb); 149 API_EXPORT(int) ap_bskiplf(BUFF *fb); 150 API_EXPORT(int) ap_bwrite(BUFF *fb, const void *buf, int nbyte); 151 API_EXPORT(int) ap_bflush(BUFF *fb); 152 API_EXPORT(int) ap_bputs(const char *x, BUFF *fb); 153 API_EXPORT_NONSTD(int) ap_bvputs(BUFF *fb,...); 154 API_EXPORT_NONSTD(int) ap_bprintf(BUFF *fb, const char *fmt,...) 155 __attribute__((__format__(__printf__, 2, 3))); 156 API_EXPORT(int) ap_vbprintf(BUFF *fb, const char *fmt, va_list vlist); 157 158 /* Internal routines */ 159 API_EXPORT(int) ap_bflsbuf(int c, BUFF *fb); 160 API_EXPORT(int) ap_bfilbuf(BUFF *fb); 161 162 #define ap_bpeekc(fb) ( ((fb)->incnt == 0) ? EOF : *((fb)->inptr) ) 163 164 #define ap_bgetc(fb) ( ((fb)->incnt == 0) ? ap_bfilbuf(fb) : \ 165 ((fb)->incnt--, *((fb)->inptr++)) ) 166 167 #define ap_bputc(c, fb) ((((fb)->flags & (B_EOUT|B_WRERR|B_WR)) != B_WR || \ 168 (fb)->outcnt == (fb)->bufsiz) ? ap_bflsbuf(c, (fb)) : \ 169 ((fb)->outbase[(fb)->outcnt++] = (c), 0)) 170 171 struct child_info { 172 /* 173 * We need to put a dummy member in here to avoid compilation 174 * errors under certain Unix compilers, like SGI's and HPUX's, 175 * which fail to compile a zero-sized struct. Of course 176 * it would be much nicer if there was actually a use for this 177 * structure under Unix. Aah the joys of x-platform code. 178 */ 179 int dummy; 180 }; 181 API_EXPORT(int) ap_bspawn_child(pool *, int (*)(void *, child_info *), void *, 182 enum kill_conditions, BUFF **pipe_in, BUFF **pipe_out, BUFF **pipe_err); 183 184 /* enable non-blocking operations */ 185 API_EXPORT(int) ap_bnonblock(BUFF *fb, int direction); 186 /* and get an fd to select() on */ 187 API_EXPORT(int) ap_bfileno(BUFF *fb, int direction); 188 189 /* bflush() if a read now would block, but don't actually read anything */ 190 API_EXPORT(void) ap_bhalfduplex(BUFF *fb); 191 192 #ifdef __cplusplus 193 } 194 #endif 195 196 #endif /* !APACHE_BUFF_H */ 197