blob: 9789fba45282d7643e3c259d90f3e03f57119495 [file] [log] [blame]
rscf7012582003-11-25 01:40:27 +00001#ifndef _HTTPD_H_
2#define _HTTPD_H_ 1
3#if defined(__cplusplus)
4extern "C" {
5#endif
rsc1a0954a2005-01-04 21:18:08 +00006
7AUTOLIB(httpd)
rsc97a5e5f2003-11-23 18:25:35 +00008/*
9#pragma lib "libhttpd.a"
10#pragma src "/sys/src/libhttpd"
11*/
12
13typedef struct HConnect HConnect;
14typedef struct HContent HContent;
15typedef struct HContents HContents;
16typedef struct HETag HETag;
17typedef struct HFields HFields;
18typedef struct Hio Hio;
19typedef struct Htmlesc Htmlesc;
20typedef struct HttpHead HttpHead;
21typedef struct HttpReq HttpReq;
22typedef struct HRange HRange;
23typedef struct HSPairs HSPairs;
24
25#ifndef _HAVE_BIN
26typedef struct Bin Bin;
27#define _HAVE_BIN
28#endif
29
30enum
31{
32 HMaxWord = 32*1024,
33 HBufSize = 32*1024,
34
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,
rsccbeb0b22006-04-01 19:24:03 +000051 HOK
rsc97a5e5f2003-11-23 18:25:35 +000052};
53
54/*
55 * table of html character escape codes
56 */
57struct Htmlesc
58{
59 char *name;
60 Rune value;
61};
62
63struct 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};
71
72struct HContents
73{
74 HContent *type;
75 HContent *encoding;
76};
77
78/*
79 * generic http header with a list of tokens,
80 * each with an optional list of parameters
81 */
82struct HFields
83{
84 char *s;
85 HSPairs *params;
86 HFields *next;
87};
88
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 */
94struct HSPairs
95{
96 char *s;
97 char *t;
98 HSPairs *next;
99};
100
101/*
102 * byte ranges within a file
103 */
104struct HRange
105{
106 int suffix; /* is this a suffix request? */
107 ulong start;
108 ulong stop; /* ~0UL -> not given */
109 HRange *next;
110};
111
112/*
113 * list of http/1.1 entity tags
114 */
115struct HETag
116{
117 char *etag;
118 int weak;
119 HETag *next;
120};
121
122/*
123 * HTTP custom IO
124 * supports chunked transfer encoding
125 * and initialization of the input buffer from a string.
126 */
127enum
128{
129 Hnone,
130 Hread,
131 Hend,
132 Hwrite,
133 Herr,
134
135 Hsize = HBufSize
136};
137
138struct 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};
150
151/*
152 * request line
153 */
154struct HttpReq
155{
156 char *meth;
157 char *uri;
158 char *urihost;
159 char *search;
160 int vermaj;
161 int vermin;
rsc946b8d72007-04-21 20:41:08 +0000162 HSPairs *searchpairs;
rsc97a5e5f2003-11-23 18:25:35 +0000163};
164
165/*
166 * header lines
167 */
168struct HttpHead
169{
170 int closeit; /* http1.1 close connection after this request? */
171 uchar persist; /* http/1.1 requests a persistent connection */
172
173 uchar expectcont; /* expect a 100-continue */
174 uchar expectother; /* expect anything else; should reject with ExpectFail */
175 ulong contlen; /* if != ~0UL, length of included message body */
176 HFields *transenc; /* if present, encoding of included message body */
177 char *client;
178 char *host;
179 HContent *okencode;
180 HContent *oklang;
181 HContent *oktype;
182 HContent *okchar;
183 ulong ifmodsince;
184 ulong ifunmodsince;
185 ulong ifrangedate;
186 HETag *ifmatch;
187 HETag *ifnomatch;
188 HETag *ifrangeetag;
189 HRange *range;
190 char *authuser; /* authorization info */
191 char *authpass;
192
193 /*
194 * experimental headers
195 */
196 int fresh_thresh;
197 int fresh_have;
198};
199
200/*
201 * all of the state for a particular connection
202 */
203struct HConnect
204{
205 void *private; /* for the library clients */
206 void (*replog)(HConnect*, char*, ...); /* called when reply sent */
207
208 HttpReq req;
209 HttpHead head;
210
211 Bin *bin;
212
213 ulong reqtime; /* time at start of request */
214 char xferbuf[HBufSize]; /* buffer for making up or transferring data */
215 uchar header[HBufSize + 2]; /* room for \n\0 */
216 uchar *hpos;
217 uchar *hstop;
218 Hio hin;
219 Hio hout;
220};
221
222/*
223 * configuration for all connections within the server
224 */
225extern char* hmydomain;
226extern char* hversion;
227extern Htmlesc htmlesc[];
228
229/*
230 * .+2,/^$/ | sort -bd +1
231 */
232void *halloc(HConnect *c, ulong size);
233Hio *hbodypush(Hio *hh, ulong len, HFields *te);
234int hbuflen(Hio *h, void *p);
235int hcheckcontent(HContent*, HContent*, char*, int);
236void hclose(Hio*);
237ulong hdate2sec(char*);
238int hdatefmt(Fmt*);
239int hfail(HConnect*, int, ...);
240int hflush(Hio*);
rsc678ede72004-12-27 00:14:43 +0000241int hlflush(Hio*);
rsc97a5e5f2003-11-23 18:25:35 +0000242int hgetc(Hio*);
243int hgethead(HConnect *c, int many);
244int hinit(Hio*, int, int);
245int hiserror(Hio *h);
246int hload(Hio*, char*);
247char *hlower(char*);
248HContent *hmkcontent(HConnect *c, char *generic, char *specific, HContent *next);
249HFields *hmkhfields(HConnect *c, char *s, HSPairs *p, HFields *next);
250char *hmkmimeboundary(HConnect *c);
251HSPairs *hmkspairs(HConnect *c, char *s, char *t, HSPairs *next);
252int hmoved(HConnect *c, char *uri);
253void hokheaders(HConnect *c);
254int hparseheaders(HConnect*, int timeout);
255HSPairs *hparsequery(HConnect *c, char *search);
256int hparsereq(HConnect *c, int timeout);
257int hprint(Hio*, char*, ...);
258int hputc(Hio*, int);
259void *hreadbuf(Hio *h, void *vsave);
260int hredirected(HConnect *c, char *how, char *uri);
261void hreqcleanup(HConnect *c);
262HFields *hrevhfields(HFields *hf);
263HSPairs *hrevspairs(HSPairs *sp);
264char *hstrdup(HConnect *c, char *s);
265int http11(HConnect*);
266int httpfmt(Fmt*);
267char *httpunesc(HConnect *c, char *s);
268int hunallowed(HConnect *, char *allowed);
269int hungetc(Hio *h);
270char *hunload(Hio*);
271int hurlfmt(Fmt*);
272char *hurlunesc(HConnect *c, char *s);
273int hwrite(Hio*, void*, int);
274int hxferenc(Hio*, int);
275
276/*
277#pragma varargck argpos hprint 2
278*/
279/*
280 * D is httpd format date conversion
281 * U is url escape convertsion
282 * H is html escape conversion
283 */
284/*
285#pragma varargck type "D" long
286#pragma varargck type "D" ulong
287#pragma varargck type "U" char*
288#pragma varargck type "H" char*
289*/
rscf7012582003-11-25 01:40:27 +0000290
291#if defined(__cplusplus)
292}
293#endif
294#endif