httpd.h 5.6 KB

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