httpd.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. HSPairs *cookie; /* if present, list of cookies */
  165. HSPairs *authinfo; /* digest authorization */
  166. /*
  167. * experimental headers
  168. */
  169. int fresh_thresh;
  170. int fresh_have;
  171. };
  172. /*
  173. * all of the state for a particular connection
  174. */
  175. struct HConnect
  176. {
  177. void *private; /* for the library clients */
  178. void (*replog)(HConnect*, char*, ...); /* called when reply sent */
  179. char *scheme; /* "http" vs. "https" */
  180. char *port; /* may be arbitrary, i.e., neither 80 nor 443 */
  181. HttpReq req;
  182. HttpHead head;
  183. Bin *bin;
  184. ulong reqtime; /* time at start of request */
  185. char xferbuf[HBufSize]; /* buffer for making up or transferring data */
  186. uchar header[HBufSize + 2]; /* room for \n\0 */
  187. uchar *hpos;
  188. uchar *hstop;
  189. Hio hin;
  190. Hio hout;
  191. };
  192. /*
  193. * configuration for all connections within the server
  194. */
  195. extern char* hmydomain;
  196. extern char* hversion;
  197. extern Htmlesc htmlesc[];
  198. /*
  199. * .+2,/^$/ | sort -bd +1
  200. */
  201. void *halloc(HConnect *c, ulong size);
  202. Hio *hbodypush(Hio *hh, ulong len, HFields *te);
  203. int hbuflen(Hio *h, void *p);
  204. int hcheckcontent(HContent*, HContent*, char*, int);
  205. void hclose(Hio*);
  206. ulong hdate2sec(char*);
  207. int hdatefmt(Fmt*);
  208. int hfail(HConnect*, int, ...);
  209. int hflush(Hio*);
  210. int hgetc(Hio*);
  211. int hgethead(HConnect *c, int many);
  212. int hinit(Hio*, int, int);
  213. int hiserror(Hio *h);
  214. int hlflush(Hio*);
  215. int hload(Hio*, char*);
  216. char *hlower(char*);
  217. HContent *hmkcontent(HConnect *c, char *generic, char *specific, HContent *next);
  218. HFields *hmkhfields(HConnect *c, char *s, HSPairs *p, HFields *next);
  219. char *hmkmimeboundary(HConnect *c);
  220. HSPairs *hmkspairs(HConnect *c, char *s, char *t, HSPairs *next);
  221. int hmoved(HConnect *c, char *uri);
  222. void hokheaders(HConnect *c);
  223. int hparseheaders(HConnect*, int timeout);
  224. HSPairs *hparsequery(HConnect *c, char *search);
  225. int hparsereq(HConnect *c, int timeout);
  226. int hprint(Hio*, char*, ...);
  227. int hputc(Hio*, int);
  228. void *hreadbuf(Hio *h, void *vsave);
  229. int hredirected(HConnect *c, char *how, char *uri);
  230. void hreqcleanup(HConnect *c);
  231. HFields *hrevhfields(HFields *hf);
  232. HSPairs *hrevspairs(HSPairs *sp);
  233. char *hstrdup(HConnect *c, char *s);
  234. int http11(HConnect*);
  235. int httpfmt(Fmt*);
  236. char *httpunesc(HConnect *c, char *s);
  237. int hunallowed(HConnect *, char *allowed);
  238. int hungetc(Hio *h);
  239. char *hunload(Hio*);
  240. int hurlfmt(Fmt*);
  241. char *hurlunesc(HConnect *c, char *s);
  242. int hwrite(Hio*, void*, int);
  243. int hxferenc(Hio*, int);
  244. #pragma varargck argpos hprint 2
  245. /*
  246. * D is httpd format date conversion
  247. * U is url escape convertsion
  248. * H is html escape conversion
  249. */
  250. #pragma varargck type "D" long
  251. #pragma varargck type "D" ulong
  252. #pragma varargck type "U" char*
  253. #pragma varargck type "H" char*