httpd.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. };
  137. /*
  138. * header lines
  139. */
  140. struct HttpHead
  141. {
  142. int closeit; /* http1.1 close connection after this request? */
  143. uchar persist; /* http/1.1 requests a persistent connection */
  144. uchar expectcont; /* expect a 100-continue */
  145. uchar expectother; /* expect anything else; should reject with ExpectFail */
  146. ulong contlen; /* if != ~0UL, length of included message body */
  147. HFields *transenc; /* if present, encoding of included message body */
  148. char *client;
  149. char *host;
  150. HContent *okencode;
  151. HContent *oklang;
  152. HContent *oktype;
  153. HContent *okchar;
  154. ulong ifmodsince;
  155. ulong ifunmodsince;
  156. ulong ifrangedate;
  157. HETag *ifmatch;
  158. HETag *ifnomatch;
  159. HETag *ifrangeetag;
  160. HRange *range;
  161. char *authuser; /* authorization info */
  162. char *authpass;
  163. /*
  164. * experimental headers
  165. */
  166. int fresh_thresh;
  167. int fresh_have;
  168. };
  169. /*
  170. * all of the state for a particular connection
  171. */
  172. struct HConnect
  173. {
  174. void *private; /* for the library clients */
  175. void (*replog)(HConnect*, char*, ...); /* called when reply sent */
  176. HttpReq req;
  177. HttpHead head;
  178. Bin *bin;
  179. ulong reqtime; /* time at start of request */
  180. char xferbuf[HBufSize]; /* buffer for making up or transferring data */
  181. uchar header[HBufSize + 2]; /* room for \n\0 */
  182. uchar *hpos;
  183. uchar *hstop;
  184. Hio hin;
  185. Hio hout;
  186. };
  187. /*
  188. * configuration for all connections within the server
  189. */
  190. extern char* hmydomain;
  191. extern char* hversion;
  192. extern Htmlesc htmlesc[];
  193. /*
  194. * .+2,/^$/ | sort -bd +1
  195. */
  196. void *halloc(HConnect *c, ulong size);
  197. Hio *hbodypush(Hio *hh, ulong len, HFields *te);
  198. int hbuflen(Hio *h, void *p);
  199. int hcheckcontent(HContent*, HContent*, char*, int);
  200. void hclose(Hio*);
  201. ulong hdate2sec(char*);
  202. int hdatefmt(Fmt*);
  203. int hfail(HConnect*, int, ...);
  204. int hflush(Hio*);
  205. int hgetc(Hio*);
  206. int hgethead(HConnect *c, int many);
  207. int hinit(Hio*, int, int);
  208. int hiserror(Hio *h);
  209. int hlflush(Hio*);
  210. int hload(Hio*, char*);
  211. char *hlower(char*);
  212. HContent *hmkcontent(HConnect *c, char *generic, char *specific, HContent *next);
  213. HFields *hmkhfields(HConnect *c, char *s, HSPairs *p, HFields *next);
  214. char *hmkmimeboundary(HConnect *c);
  215. HSPairs *hmkspairs(HConnect *c, char *s, char *t, HSPairs *next);
  216. int hmoved(HConnect *c, char *uri);
  217. void hokheaders(HConnect *c);
  218. int hparseheaders(HConnect*, int timeout);
  219. HSPairs *hparsequery(HConnect *c, char *search);
  220. int hparsereq(HConnect *c, int timeout);
  221. int hprint(Hio*, char*, ...);
  222. int hputc(Hio*, int);
  223. void *hreadbuf(Hio *h, void *vsave);
  224. int hredirected(HConnect *c, char *how, char *uri);
  225. void hreqcleanup(HConnect *c);
  226. HFields *hrevhfields(HFields *hf);
  227. HSPairs *hrevspairs(HSPairs *sp);
  228. char *hstrdup(HConnect *c, char *s);
  229. int http11(HConnect*);
  230. int httpfmt(Fmt*);
  231. char *httpunesc(HConnect *c, char *s);
  232. int hunallowed(HConnect *, char *allowed);
  233. int hungetc(Hio *h);
  234. char *hunload(Hio*);
  235. int hurlfmt(Fmt*);
  236. char *hurlunesc(HConnect *c, char *s);
  237. int hwrite(Hio*, void*, int);
  238. int hxferenc(Hio*, int);
  239. #pragma varargck argpos hprint 2
  240. /*
  241. * D is httpd format date conversion
  242. * U is url escape convertsion
  243. * H is html escape conversion
  244. */
  245. #pragma varargck type "D" long
  246. #pragma varargck type "D" ulong
  247. #pragma varargck type "U" char*
  248. #pragma varargck type "H" char*