gethead.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bin.h>
  12. #include <httpd.h>
  13. /*
  14. * read in some header lines, either one or all of them.
  15. * copy results into header log buffer.
  16. */
  17. int
  18. hgethead(HConnect *c, int many)
  19. {
  20. Hio *hin;
  21. char *s, *p, *pp;
  22. int n;
  23. hin = &c->hin;
  24. for(;;){
  25. s = (char*)hin->pos;
  26. pp = s;
  27. while(p = memchr(pp, '\n', (char*)hin->stop - pp)){
  28. if(!many || p == pp || (p == pp + 1 && *pp == '\r')){
  29. pp = p + 1;
  30. break;
  31. }
  32. pp = p + 1;
  33. }
  34. hin->pos = (uint8_t*)pp;
  35. n = pp - s;
  36. if(c->hstop + n > &c->header[HBufSize])
  37. return -1;
  38. memmove(c->hstop, s, n);
  39. c->hstop += n;
  40. *c->hstop = '\0';
  41. if(p != nil)
  42. return 0;
  43. if(hreadbuf(hin, hin->pos) == nil || hin->state == Hend)
  44. return -1;
  45. }
  46. }