httpd.h 6.3 KB

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