httpd.h 5.9 KB

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