multipart_parser.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * Copyright (c) 2019 Trevor Herselman. All rights reserved.
  3. *
  4. * MIT License
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #ifndef multipart_parser_h
  25. #define multipart_parser_h
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #include <stddef.h>
  30. #if defined(_WIN32) && !defined(__MINGW32__) && \
  31. (!defined(_MSC_VER) || _MSC_VER<1600) && !defined(__WINE__)
  32. #include <BaseTsd.h>
  33. typedef __int8 int8_t;
  34. typedef unsigned __int8 uint8_t;
  35. typedef __int16 int16_t;
  36. typedef unsigned __int16 uint16_t;
  37. typedef __int32 int32_t;
  38. typedef unsigned __int32 uint32_t;
  39. typedef __int64 int64_t;
  40. typedef unsigned __int64 uint64_t;
  41. #else
  42. #include <stdint.h>
  43. #endif
  44. typedef struct multipart_parser multipart_parser;
  45. typedef struct multipart_parser_settings multipart_parser_settings;
  46. /* Callbacks should return non-zero to indicate an error. The parser will
  47. * then halt execution.
  48. */
  49. typedef int (*multipart_data_cb) (multipart_parser*, const char *at, size_t length);
  50. typedef int (*multipart_cb) (multipart_parser*);
  51. struct multipart_parser_settings {
  52. multipart_cb on_boundary_begin;
  53. multipart_data_cb on_header_field;
  54. multipart_data_cb on_header_value;
  55. multipart_cb on_headers_complete;
  56. multipart_data_cb on_body;
  57. multipart_cb on_body_parts_complete;
  58. };
  59. struct multipart_parser {
  60. /** PRIVATE **/
  61. unsigned char state; /* enum state from http_parser.c */
  62. uint64_t nread; /* # bytes read in various scenarios */
  63. /** READ-ONLY **/
  64. unsigned char multipart_errno;
  65. /** PUBLIC **/
  66. void *data; /* A pointer to get hook to the "connection" or "socket" object */
  67. const char* boundary; /* set this to a boundary string taken from headers */
  68. size_t boundary_len;
  69. char header_field[64];
  70. char header_value[128];
  71. };
  72. void multipart_parser_init(multipart_parser *parser);
  73. /* Initialize multipart_parser_settings members to 0
  74. */
  75. void multipart_parser_settings_init(multipart_parser_settings *settings);
  76. /* `return -1` on error
  77. * Sets `parser->multipart_errno` on error.
  78. */
  79. int multipart_parser_execute(multipart_parser *parser,
  80. const multipart_parser_settings *settings,
  81. const char *data,
  82. size_t len);
  83. /* Helper method to get the boundary string from Content-Type header */
  84. //const char* get_boundary(const char* str, size_t str_len, size_t* boundary_len);
  85. /**
  86. */
  87. const char* multipart_get_name(const char* str, size_t len, size_t* value_len);
  88. /**
  89. * Helper function to get the `filename` value from a header string value
  90. * eg. `form-data; name="file1"; filename="我a私aaaa\"xxx\"abc"`
  91. * returns: `我a私aaaa"xxx"abc`
  92. * Extracts and decodes the `filename` string into buffer,
  93. * performs URL decoding, and slash string decoding!
  94. * Firefox will encode a double quote `"` character into \"
  95. * Chrome will use %22
  96. */
  97. const char* multipart_get_filename(const char* str, size_t len,
  98. size_t* value_len);
  99. #ifdef __cplusplus
  100. }
  101. #endif
  102. #endif