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 hgetc(Hio*);
239 int hgethead(HConnect *c, int many);
240 int hinit(Hio*, int, int);
241 int hiserror(Hio *h);
242 int hload(Hio*, char*);
243 char *hlower(char*);
244 HContent *hmkcontent(HConnect *c, char *generic, char *specific, HContent *next);
245 HFields *hmkhfields(HConnect *c, char *s, HSPairs *p, HFields *next);
246 char *hmkmimeboundary(HConnect *c);
247 HSPairs *hmkspairs(HConnect *c, char *s, char *t, HSPairs *next);
248 int hmoved(HConnect *c, char *uri);
249 void hokheaders(HConnect *c);
250 int hparseheaders(HConnect*, int timeout);
251 HSPairs *hparsequery(HConnect *c, char *search);
252 int hparsereq(HConnect *c, int timeout);
253 int hprint(Hio*, char*, ...);
254 int hputc(Hio*, int);
255 void *hreadbuf(Hio *h, void *vsave);
256 int hredirected(HConnect *c, char *how, char *uri);
257 void hreqcleanup(HConnect *c);
258 HFields *hrevhfields(HFields *hf);
259 HSPairs *hrevspairs(HSPairs *sp);
260 char *hstrdup(HConnect *c, char *s);
261 int http11(HConnect*);
262 int httpfmt(Fmt*);
263 char *httpunesc(HConnect *c, char *s);
264 int hunallowed(HConnect *, char *allowed);
265 int hungetc(Hio *h);
266 char *hunload(Hio*);
267 int hurlfmt(Fmt*);
268 char *hurlunesc(HConnect *c, char *s);
269 int hwrite(Hio*, void*, int);
270 int hxferenc(Hio*, int);
272 /*
273 #pragma varargck argpos hprint 2
274 */
275 /*
276 * D is httpd format date conversion
277 * U is url escape convertsion
278 * H is html escape conversion
279 */
280 /*
281 #pragma varargck type "D" long
282 #pragma varargck type "D" ulong
283 #pragma varargck type "U" char*
284 #pragma varargck type "H" char*
285 */
287 #if defined(__cplusplus)
289 #endif
290 #endif