hio.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <httpd.h>
  4. static char hstates[] = "nrewE";
  5. static char hxfers[] = " x";
  6. static int _hflush(Hio*, int, int);
  7. int
  8. hinit(Hio *h, int fd, int mode)
  9. {
  10. if(fd == -1 || mode != Hread && mode != Hwrite)
  11. return -1;
  12. h->hh = nil;
  13. h->fd = fd;
  14. h->seek = 0;
  15. h->state = mode;
  16. h->start = h->buf + 16; /* leave space for chunk length */
  17. h->stop = h->pos = h->start;
  18. if(mode == Hread){
  19. h->bodylen = ~0UL;
  20. *h->pos = '\0';
  21. }else
  22. h->stop = h->start + Hsize;
  23. return 0;
  24. }
  25. int
  26. hiserror(Hio *h)
  27. {
  28. return h->state == Herr;
  29. }
  30. int
  31. hgetc(Hio *h)
  32. {
  33. uchar *p;
  34. p = h->pos;
  35. if(p < h->stop){
  36. h->pos = p + 1;
  37. return *p;
  38. }
  39. p -= UTFmax;
  40. if(p < h->start)
  41. p = h->start;
  42. if(!hreadbuf(h, p) || h->pos == h->stop)
  43. return -1;
  44. return *h->pos++;
  45. }
  46. int
  47. hungetc(Hio *h)
  48. {
  49. if(h->state == Hend)
  50. h->state = Hread;
  51. else if(h->state == Hread)
  52. h->pos--;
  53. if(h->pos < h->start || h->state != Hread){
  54. h->state = Herr;
  55. h->pos = h->stop;
  56. return -1;
  57. }
  58. return 0;
  59. }
  60. /*
  61. * fill the buffer, saving contents from vsave onwards.
  62. * nothing is saved if vsave is nil.
  63. * returns the beginning of the buffer.
  64. *
  65. * understands message body sizes and chunked transfer encoding
  66. */
  67. void *
  68. hreadbuf(Hio *h, void *vsave)
  69. {
  70. Hio *hh;
  71. uchar *save;
  72. int c, in, cpy, dpos;
  73. save = vsave;
  74. if(save && (save < h->start || save > h->stop)
  75. || h->state != Hread && h->state != Hend){
  76. h->state = Herr;
  77. h->pos = h->stop;
  78. return nil;
  79. }
  80. dpos = 0;
  81. if(save && h->pos > save)
  82. dpos = h->pos - save;
  83. cpy = 0;
  84. if(save){
  85. cpy = h->stop - save;
  86. memmove(h->start, save, cpy);
  87. }
  88. h->seek += h->stop - h->start - cpy;
  89. h->pos = h->start + dpos;
  90. in = Hsize - cpy;
  91. if(h->state == Hend)
  92. in = 0;
  93. else if(in > h->bodylen)
  94. in = h->bodylen;
  95. /*
  96. * for chunked encoding, fill buffer,
  97. * then read in new chunk length and wipe out that line
  98. */
  99. hh = h->hh;
  100. if(hh != nil){
  101. if(!in && h->xferenc && h->state != Hend){
  102. if(h->xferenc == 2){
  103. c = hgetc(hh);
  104. if(c == '\r')
  105. c = hgetc(hh);
  106. if(c != '\n'){
  107. h->pos = h->stop;
  108. h->state = Herr;
  109. return nil;
  110. }
  111. }
  112. h->xferenc = 2;
  113. in = 0;
  114. while((c = hgetc(hh)) != '\n'){
  115. if(c >= '0' && c <= '9')
  116. c -= '0';
  117. else if(c >= 'a' && c <= 'f')
  118. c -= 'a' - 10;
  119. else if(c >= 'A' && c <= 'F')
  120. c -= 'A' - 10;
  121. else
  122. break;
  123. in = in * 16 + c;
  124. }
  125. while(c != '\n'){
  126. if(c < 0){
  127. h->pos = h->stop;
  128. h->state = Herr;
  129. return nil;
  130. }
  131. c = hgetc(hh);
  132. }
  133. h->bodylen = in;
  134. in = Hsize - cpy;
  135. if(in > h->bodylen)
  136. in = h->bodylen;
  137. }
  138. if(in){
  139. while(hh->pos + in > hh->stop){
  140. if(hreadbuf(hh, hh->pos) == nil){
  141. h->pos = h->stop;
  142. h->state = Herr;
  143. return nil;
  144. }
  145. }
  146. memmove(h->start + cpy, hh->pos, in);
  147. hh->pos += in;
  148. }
  149. }else if(in){
  150. if((in = read(h->fd, h->start + cpy, in)) < 0){
  151. h->state = Herr;
  152. h->pos = h->stop;
  153. return nil;
  154. }
  155. }
  156. if(in == 0)
  157. h->state = Hend;
  158. h->bodylen -= in;
  159. h->stop = h->start + cpy + in;
  160. *h->stop = '\0';
  161. if(h->pos == h->stop)
  162. return nil;
  163. return h->start;
  164. }
  165. int
  166. hbuflen(Hio *h, void *p)
  167. {
  168. return h->stop - (uchar*)p;
  169. }
  170. /*
  171. * prepare to receive a message body
  172. * len is the content length (~0 => unspecified)
  173. * te is the transfer encoding
  174. * returns < 0 if setup failed
  175. */
  176. Hio*
  177. hbodypush(Hio *hh, ulong len, HFields *te)
  178. {
  179. Hio *h;
  180. int xe;
  181. if(hh->state != Hread)
  182. return nil;
  183. xe = 0;
  184. if(te != nil){
  185. if(te->params != nil || te->next != nil)
  186. return nil;
  187. if(cistrcmp(te->s, "chunked") == 0){
  188. xe = 1;
  189. len = 0;
  190. }else if(cistrcmp(te->s, "identity") == 0){
  191. ;
  192. }else
  193. return nil;
  194. }
  195. h = malloc(sizeof *h);
  196. if(h == nil)
  197. return nil;
  198. h->hh = hh;
  199. h->fd = -1;
  200. h->seek = 0;
  201. h->state = Hread;
  202. h->xferenc = xe;
  203. h->start = h->buf + 16; /* leave space for chunk length */
  204. h->stop = h->pos = h->start;
  205. *h->pos = '\0';
  206. h->bodylen = len;
  207. return h;
  208. }
  209. /*
  210. * dump the state of the io buffer into a string
  211. */
  212. char *
  213. hunload(Hio *h)
  214. {
  215. uchar *p, *t, *stop, *buf;
  216. int ne, n, c;
  217. stop = h->stop;
  218. ne = 0;
  219. for(p = h->pos; p < stop; p++){
  220. c = *p;
  221. if(c == 0x80)
  222. ne++;
  223. }
  224. p = h->pos;
  225. n = (stop - p) + ne + 3;
  226. buf = mallocz(n, 1);
  227. if(buf == nil)
  228. return nil;
  229. buf[0] = hstates[h->state];
  230. buf[1] = hxfers[h->xferenc];
  231. t = &buf[2];
  232. for(; p < stop; p++){
  233. c = *p;
  234. if(c == 0 || c == 0x80){
  235. *t++ = 0x80;
  236. if(c == 0x80)
  237. *t++ = 0x80;
  238. }else
  239. *t++ = c;
  240. }
  241. *t++ = '\0';
  242. if(t != buf + n)
  243. return nil;
  244. return (char*)buf;
  245. }
  246. /*
  247. * read the io buffer state from a string
  248. */
  249. int
  250. hload(Hio *h, char *buf)
  251. {
  252. uchar *p, *t, *stop;
  253. char *s;
  254. int c;
  255. s = strchr(hstates, buf[0]);
  256. if(s == nil)
  257. return -1;
  258. h->state = s - hstates;
  259. s = strchr(hxfers, buf[1]);
  260. if(s == nil)
  261. return -1;
  262. h->xferenc = s - hxfers;
  263. t = h->start;
  264. stop = t + Hsize;
  265. for(p = (uchar*)&buf[2]; c = *p; p++){
  266. if(c == 0x80){
  267. if(p[1] != 0x80)
  268. c = 0;
  269. else
  270. p++;
  271. }
  272. *t++ = c;
  273. if(t >= stop)
  274. return -1;
  275. }
  276. *t = '\0';
  277. h->pos = h->start;
  278. h->stop = t;
  279. h->seek = 0;
  280. return 0;
  281. }
  282. void
  283. hclose(Hio *h)
  284. {
  285. if(h->fd >= 0){
  286. if(h->state == Hwrite)
  287. hxferenc(h, 0);
  288. close(h->fd);
  289. }
  290. h->stop = h->pos = nil;
  291. h->fd = -1;
  292. }
  293. /*
  294. * flush the buffer and possibly change encoding modes
  295. */
  296. int
  297. hxferenc(Hio *h, int on)
  298. {
  299. if(h->xferenc && !on && h->pos != h->start)
  300. hflush(h);
  301. if(_hflush(h, 1, 0) < 0)
  302. return -1;
  303. h->xferenc = !!on;
  304. return 0;
  305. }
  306. int
  307. hputc(Hio *h, int c)
  308. {
  309. uchar *p;
  310. p = h->pos;
  311. if(p < h->stop){
  312. h->pos = p + 1;
  313. return *p = c;
  314. }
  315. if(hflush(h) < 0)
  316. return -1;
  317. return *h->pos++ = c;
  318. }
  319. static int
  320. fmthflush(Fmt *f)
  321. {
  322. Hio *h;
  323. h = f->farg;
  324. h->pos = f->to;
  325. if(hflush(h) < 0)
  326. return 0;
  327. f->stop = h->stop;
  328. f->to = h->pos;
  329. f->start = h->pos;
  330. return 1;
  331. }
  332. int
  333. hvprint(Hio *h, char *fmt, va_list args)
  334. {
  335. int n;
  336. Fmt f;
  337. f.runes = 0;
  338. f.stop = h->stop;
  339. f.to = h->pos;
  340. f.start = h->pos;
  341. f.flush = fmthflush;
  342. f.farg = h;
  343. f.nfmt = 0;
  344. // fmtlocaleinit(&f, nil, nil, nil);
  345. n = fmtvprint(&f, fmt, args);
  346. h->pos = f.to;
  347. return n;
  348. }
  349. int
  350. hprint(Hio *h, char *fmt, ...)
  351. {
  352. int n;
  353. va_list arg;
  354. va_start(arg, fmt);
  355. n = hvprint(h, fmt, arg);
  356. va_end(arg);
  357. return n;
  358. }
  359. static int
  360. _hflush(Hio *h, int force, int dolength)
  361. {
  362. uchar *s;
  363. int w;
  364. if(h->state != Hwrite){
  365. h->state = Herr;
  366. h->stop = h->pos;
  367. return -1;
  368. }
  369. s = h->start;
  370. w = h->pos - s;
  371. if(w == 0 && !force)
  372. return 0;
  373. if(h->xferenc){
  374. *--s = '\n';
  375. *--s = '\r';
  376. do{
  377. *--s = "0123456789abcdef"[w & 0xf];
  378. w >>= 4;
  379. }while(w);
  380. h->pos[0] = '\r';
  381. h->pos[1] = '\n';
  382. w = &h->pos[2] - s;
  383. }
  384. if(dolength)
  385. fprint(h->fd, "Content-Length: %d\r\n\r\n", w);
  386. if(write(h->fd, s, w) != w){
  387. h->state = Herr;
  388. h->stop = h->pos;
  389. return -1;
  390. }
  391. h->seek += w;
  392. h->pos = h->start;
  393. return 0;
  394. }
  395. int
  396. hflush(Hio *h)
  397. {
  398. return _hflush(h, 0, 0);
  399. }
  400. int
  401. hlflush(Hio* h)
  402. {
  403. return _hflush(h, 0, 1);
  404. }
  405. int
  406. hwrite(Hio *h, void *vbuf, int len)
  407. {
  408. uchar *buf;
  409. int n, m;
  410. buf = vbuf;
  411. n = len;
  412. if(n < 0 || h->state != Hwrite){
  413. h->state = Herr;
  414. h->stop = h->pos;
  415. return -1;
  416. }
  417. if(h->pos + n >= h->stop){
  418. if(h->start != h->pos)
  419. if(hflush(h) < 0)
  420. return -1;
  421. while(h->pos + n >= h->stop){
  422. m = h->stop - h->pos;
  423. if(h->xferenc){
  424. memmove(h->pos, buf, m);
  425. h->pos += m;
  426. if(hflush(h) < 0)
  427. return -1;
  428. }else{
  429. if(write(h->fd, buf, m) != m){
  430. h->state = Herr;
  431. h->stop = h->pos;
  432. return -1;
  433. }
  434. h->seek += m;
  435. }
  436. n -= m;
  437. buf += m;
  438. }
  439. }
  440. memmove(h->pos, buf, n);
  441. h->pos += n;
  442. return len;
  443. }