Blob


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