gethead.c 737 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bin.h>
  4. #include <httpd.h>
  5. /*
  6. * read in some header lines, either one or all of them.
  7. * copy results into header log buffer.
  8. */
  9. int
  10. hgethead(HConnect *c, int many)
  11. {
  12. Hio *hin;
  13. char *s, *p, *pp;
  14. int n;
  15. hin = &c->hin;
  16. for(;;){
  17. s = (char*)hin->pos;
  18. pp = s;
  19. while(p = memchr(pp, '\n', (char*)hin->stop - pp)){
  20. if(!many || p == pp || (p == pp + 1 && *pp == '\r')){
  21. pp = p + 1;
  22. break;
  23. }
  24. pp = p + 1;
  25. }
  26. hin->pos = (uchar*)pp;
  27. n = pp - s;
  28. if(c->hstop + n > &c->header[HBufSize])
  29. return -1;
  30. memmove(c->hstop, s, n);
  31. c->hstop += n;
  32. *c->hstop = '\0';
  33. if(p != nil)
  34. return 0;
  35. if(hreadbuf(hin, hin->pos) == nil || hin->state == Hend)
  36. return -1;
  37. }
  38. }