Blob


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