1 #pragma lib "libhttpd.a" 2 #pragma src "/sys/src/libhttpd" 3 4 typedef struct HConnect HConnect; 5 typedef struct HContent HContent; 6 typedef struct HContents HContents; 7 typedef struct HETag HETag; 8 typedef struct HFields HFields; 9 typedef struct Hio Hio; 10 typedef struct Htmlesc Htmlesc; 11 typedef struct HttpHead HttpHead; 12 typedef struct HttpReq HttpReq; 13 typedef struct HRange HRange; 14 typedef struct HSPairs HSPairs; 15 16 typedef struct Bin Bin; 17 18 #pragma incomplete Bin 19 20 enum 21 { 22 HMaxWord = 32*1024, 23 HBufSize = 32*1024, 24 25 /* 26 * error messages 27 */ 28 HInternal = 0, 29 HTempFail, 30 HUnimp, 31 HBadReq, 32 HBadSearch, 33 HNotFound, 34 HUnauth, 35 HSyntax, 36 HNoSearch, 37 HNoData, 38 HExpectFail, 39 HUnkVers, 40 HBadCont, 41 HOK, 42 }; 43 44 /* 45 * table of html character escape codes 46 */ 47 struct Htmlesc 48 { 49 char *name; 50 Rune value; 51 }; 52 53 struct HContent 54 { 55 HContent *next; 56 char *generic; 57 char *specific; 58 float q; /* desirability of this kind of file */ 59 int mxb; /* max uchars until worthless */ 60 }; 61 62 struct HContents 63 { 64 HContent *type; 65 HContent *encoding; 66 }; 67 68 /* 69 * generic http header with a list of tokens, 70 * each with an optional list of parameters 71 */ 72 struct HFields 73 { 74 char *s; 75 HSPairs *params; 76 HFields *next; 77 }; 78 79 /* 80 * list of pairs a strings 81 * used for tag=val pairs for a search or form submission, 82 * and attribute=value pairs in headers. 83 */ 84 struct HSPairs 85 { 86 char *s; 87 char *t; 88 HSPairs *next; 89 }; 90 91 /* 92 * byte ranges within a file 93 */ 94 struct HRange 95 { 96 int suffix; /* is this a suffix request? */ 97 ulong start; 98 ulong stop; /* ~0UL -> not given */ 99 HRange *next; 100 }; 101 102 /* 103 * list of http/1.1 entity tags 104 */ 105 struct HETag 106 { 107 char *etag; 108 int weak; 109 HETag *next; 110 }; 111 112 /* 113 * HTTP custom IO 114 * supports chunked transfer encoding 115 * and initialization of the input buffer from a string. 116 */ 117 enum 118 { 119 Hnone, 120 Hread, 121 Hend, 122 Hwrite, 123 Herr, 124 125 Hsize = HBufSize 126 }; 127 128 struct Hio { 129 Hio *hh; /* next lower layer Hio, or nil if reads from fd */ 130 int fd; /* associated file descriptor */ 131 ulong seek; /* of start */ 132 uchar state; /* state of the file */ 133 uchar xferenc; /* chunked transfer encoding state */ 134 uchar *pos; /* current position in the buffer */ 135 uchar *stop; /* last character active in the buffer */ 136 uchar *start; /* start of data buffer */ 137 ulong bodylen; /* remaining length of message body */ 138 uchar buf[Hsize+32]; 139 }; 140 141 /* 142 * request line 143 */ 144 struct HttpReq 145 { 146 char *meth; 147 char *uri; 148 char *urihost; 149 char *search; 150 int vermaj; 151 int vermin; 152 HSPairs *searchpairs; 153 }; 154 155 /* 156 * header lines 157 */ 158 struct HttpHead 159 { 160 int closeit; /* http1.1 close connection after this request? */ 161 uchar persist; /* http/1.1 requests a persistent connection */ 162 163 uchar expectcont; /* expect a 100-continue */ 164 uchar expectother; /* expect anything else; should reject with ExpectFail */ 165 ulong contlen; /* if != ~0UL, length of included message body */ 166 HFields *transenc; /* if present, encoding of included message body */ 167 char *client; 168 char *host; 169 HContent *okencode; 170 HContent *oklang; 171 HContent *oktype; 172 HContent *okchar; 173 ulong ifmodsince; 174 ulong ifunmodsince; 175 ulong ifrangedate; 176 HETag *ifmatch; 177 HETag *ifnomatch; 178 HETag *ifrangeetag; 179 HRange *range; 180 char *authuser; /* authorization info */ 181 char *authpass; 182 HSPairs *cookie; /* if present, list of cookies */ 183 184 /* 185 * experimental headers 186 */ 187 int fresh_thresh; 188 int fresh_have; 189 }; 190 191 /* 192 * all of the state for a particular connection 193 */ 194 struct HConnect 195 { 196 void *private; /* for the library clients */ 197 void (*replog)(HConnect*, char*, ...); /* called when reply sent */ 198 199 HttpReq req; 200 HttpHead head; 201 202 Bin *bin; 203 204 ulong reqtime; /* time at start of request */ 205 char xferbuf[HBufSize]; /* buffer for making up or transferring data */ 206 uchar header[HBufSize + 2]; /* room for \n\0 */ 207 uchar *hpos; 208 uchar *hstop; 209 Hio hin; 210 Hio hout; 211 }; 212 213 /* 214 * configuration for all connections within the server 215 */ 216 extern char* hmydomain; 217 extern char* hversion; 218 extern Htmlesc htmlesc[]; 219 220 /* 221 * .+2,/^$/ | sort -bd +1 222 */ 223 void *halloc(HConnect *c, ulong size); 224 Hio *hbodypush(Hio *hh, ulong len, HFields *te); 225 int hbuflen(Hio *h, void *p); 226 int hcheckcontent(HContent*, HContent*, char*, int); 227 void hclose(Hio*); 228 ulong hdate2sec(char*); 229 int hdatefmt(Fmt*); 230 int hfail(HConnect*, int, ...); 231 int hflush(Hio*); 232 int hgetc(Hio*); 233 int hgethead(HConnect *c, int many); 234 int hinit(Hio*, int, int); 235 int hiserror(Hio *h); 236 int hlflush(Hio*); 237 int hload(Hio*, char*); 238 char *hlower(char*); 239 HContent *hmkcontent(HConnect *c, char *generic, char *specific, HContent *next); 240 HFields *hmkhfields(HConnect *c, char *s, HSPairs *p, HFields *next); 241 char *hmkmimeboundary(HConnect *c); 242 HSPairs *hmkspairs(HConnect *c, char *s, char *t, HSPairs *next); 243 int hmoved(HConnect *c, char *uri); 244 void hokheaders(HConnect *c); 245 int hparseheaders(HConnect*, int timeout); 246 HSPairs *hparsequery(HConnect *c, char *search); 247 int hparsereq(HConnect *c, int timeout); 248 int hprint(Hio*, char*, ...); 249 int hputc(Hio*, int); 250 void *hreadbuf(Hio *h, void *vsave); 251 int hredirected(HConnect *c, char *how, char *uri); 252 void hreqcleanup(HConnect *c); 253 HFields *hrevhfields(HFields *hf); 254 HSPairs *hrevspairs(HSPairs *sp); 255 char *hstrdup(HConnect *c, char *s); 256 int http11(HConnect*); 257 int httpfmt(Fmt*); 258 char *httpunesc(HConnect *c, char *s); 259 int hunallowed(HConnect *, char *allowed); 260 int hungetc(Hio *h); 261 char *hunload(Hio*); 262 int hurlfmt(Fmt*); 263 char *hurlunesc(HConnect *c, char *s); 264 int hwrite(Hio*, void*, int); 265 int hxferenc(Hio*, int); 266 267 #pragma varargck argpos hprint 2 268 269 /* 270 * D is httpd format date conversion 271 * U is url escape convertsion 272 * H is html escape conversion 273 */ 274 #pragma varargck type "D" long 275 #pragma varargck type "D" ulong 276 #pragma varargck type "U" char* 277 #pragma varargck type "H" char* 278