httpd.h 5.7 KB

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