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