picohttpparser.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2009-2014 Kazuho Oku, Tokuhiro Matsuno, Daisuke Murase,
  3. * Shigeo Mitsunari
  4. *
  5. * The software is licensed under either the MIT License (below) or the Perl
  6. * license.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to
  10. * deal in the Software without restriction, including without limitation the
  11. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  24. * IN THE SOFTWARE.
  25. */
  26. #ifndef picohttpparser_h
  27. #define picohttpparser_h
  28. #include <sys/types.h>
  29. #if defined(_MSC_VER) && !defined(ssize_t)
  30. #define ssize_t intptr_t
  31. #endif
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /* contains name and value of a header (name == NULL if is a continuing line
  36. * of a multiline header */
  37. struct phr_header {
  38. const char* name;
  39. size_t name_len;
  40. const char* value;
  41. size_t value_len;
  42. };
  43. /* returns number of bytes consumed if successful, -2 if request is partial,
  44. * -1 if failed */
  45. int phr_parse_request(const char* buf, size_t len, const char** method,
  46. size_t* method_len, const char** path, size_t* path_len,
  47. int* minor_version, struct phr_header* headers,
  48. size_t* num_headers, size_t last_len);
  49. /* ditto */
  50. int phr_parse_response(const char* _buf, size_t len, int* minor_version,
  51. int* status, const char** msg, size_t* msg_len,
  52. struct phr_header* headers, size_t* num_headers,
  53. size_t last_len);
  54. /* ditto */
  55. int phr_parse_headers(const char* buf, size_t len, struct phr_header* headers,
  56. size_t* num_headers, size_t last_len);
  57. /* should be zero-filled before start */
  58. struct phr_chunked_decoder {
  59. size_t bytes_left_in_chunk; /* number of bytes left in current chunk */
  60. char consume_trailer; /* if trailing headers should be consumed */
  61. char _hex_count;
  62. char _state;
  63. };
  64. /* the function rewrites the buffer given as (buf, bufsz) removing the chunked-
  65. * encoding headers. When the function returns without an error, bufsz is
  66. * updated to the length of the decoded data available. Applications should
  67. * repeatedly call the function while it returns -2 (incomplete) every time
  68. * supplying newly arrived data. If the end of the chunked-encoded data is
  69. * found, the function returns a non-negative number indicating the number of
  70. * octets left undecoded, that starts from the offset returned by `*bufsz`.
  71. * Returns -1 on error.
  72. */
  73. ssize_t phr_decode_chunked(struct phr_chunked_decoder* decoder, char* buf,
  74. size_t* bufsz);
  75. /* returns if the chunked decoder is in middle of chunked data */
  76. int phr_decode_chunked_is_in_data(struct phr_chunked_decoder* decoder);
  77. #ifdef __cplusplus
  78. }
  79. #endif
  80. #endif