Blob


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