httpdef.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #ifndef HV_HTTP_DEF_H_
  2. #define HV_HTTP_DEF_H_
  3. #include "hexport.h"
  4. #define DEFAULT_HTTP_PORT 80
  5. #define DEFAULT_HTTPS_PORT 443
  6. enum http_version { HTTP_V1 = 1, HTTP_V2 = 2 };
  7. enum http_session_type { HTTP_CLIENT, HTTP_SERVER };
  8. enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
  9. enum http_parser_state {
  10. HP_START_REQ_OR_RES,
  11. HP_MESSAGE_BEGIN,
  12. HP_URL,
  13. HP_STATUS,
  14. HP_HEADER_FIELD,
  15. HP_HEADER_VALUE,
  16. HP_HEADERS_COMPLETE,
  17. HP_CHUNK_HEADER,
  18. HP_BODY,
  19. HP_CHUNK_COMPLETE,
  20. HP_MESSAGE_COMPLETE
  21. };
  22. // http_status
  23. // XX(num, name, string)
  24. #define HTTP_STATUS_MAP(XX) \
  25. XX(100, CONTINUE, Continue) \
  26. XX(101, SWITCHING_PROTOCOLS, Switching Protocols) \
  27. XX(102, PROCESSING, Processing) \
  28. XX(200, OK, OK) \
  29. XX(201, CREATED, Created) \
  30. XX(202, ACCEPTED, Accepted) \
  31. XX(203, NON_AUTHORITATIVE_INFORMATION, Non-Authoritative Information) \
  32. XX(204, NO_CONTENT, No Content) \
  33. XX(205, RESET_CONTENT, Reset Content) \
  34. XX(206, PARTIAL_CONTENT, Partial Content) \
  35. XX(207, MULTI_STATUS, Multi-Status) \
  36. XX(208, ALREADY_REPORTED, Already Reported) \
  37. XX(226, IM_USED, IM Used) \
  38. XX(300, MULTIPLE_CHOICES, Multiple Choices) \
  39. XX(301, MOVED_PERMANENTLY, Moved Permanently) \
  40. XX(302, FOUND, Found) \
  41. XX(303, SEE_OTHER, See Other) \
  42. XX(304, NOT_MODIFIED, Not Modified) \
  43. XX(305, USE_PROXY, Use Proxy) \
  44. XX(307, TEMPORARY_REDIRECT, Temporary Redirect) \
  45. XX(308, PERMANENT_REDIRECT, Permanent Redirect) \
  46. XX(400, BAD_REQUEST, Bad Request) \
  47. XX(401, UNAUTHORIZED, Unauthorized) \
  48. XX(402, PAYMENT_REQUIRED, Payment Required) \
  49. XX(403, FORBIDDEN, Forbidden) \
  50. XX(404, NOT_FOUND, Not Found) \
  51. XX(405, METHOD_NOT_ALLOWED, Method Not Allowed) \
  52. XX(406, NOT_ACCEPTABLE, Not Acceptable) \
  53. XX(407, PROXY_AUTHENTICATION_REQUIRED, Proxy Authentication Required) \
  54. XX(408, REQUEST_TIMEOUT, Request Timeout) \
  55. XX(409, CONFLICT, Conflict) \
  56. XX(410, GONE, Gone) \
  57. XX(411, LENGTH_REQUIRED, Length Required) \
  58. XX(412, PRECONDITION_FAILED, Precondition Failed) \
  59. XX(413, PAYLOAD_TOO_LARGE, Payload Too Large) \
  60. XX(414, URI_TOO_LONG, URI Too Long) \
  61. XX(415, UNSUPPORTED_MEDIA_TYPE, Unsupported Media Type) \
  62. XX(416, RANGE_NOT_SATISFIABLE, Range Not Satisfiable) \
  63. XX(417, EXPECTATION_FAILED, Expectation Failed) \
  64. XX(421, MISDIRECTED_REQUEST, Misdirected Request) \
  65. XX(422, UNPROCESSABLE_ENTITY, Unprocessable Entity) \
  66. XX(423, LOCKED, Locked) \
  67. XX(424, FAILED_DEPENDENCY, Failed Dependency) \
  68. XX(426, UPGRADE_REQUIRED, Upgrade Required) \
  69. XX(428, PRECONDITION_REQUIRED, Precondition Required) \
  70. XX(429, TOO_MANY_REQUESTS, Too Many Requests) \
  71. XX(431, REQUEST_HEADER_FIELDS_TOO_LARGE, Request Header Fields Too Large) \
  72. XX(451, UNAVAILABLE_FOR_LEGAL_REASONS, Unavailable For Legal Reasons) \
  73. XX(500, INTERNAL_SERVER_ERROR, Internal Server Error) \
  74. XX(501, NOT_IMPLEMENTED, Not Implemented) \
  75. XX(502, BAD_GATEWAY, Bad Gateway) \
  76. XX(503, SERVICE_UNAVAILABLE, Service Unavailable) \
  77. XX(504, GATEWAY_TIMEOUT, Gateway Timeout) \
  78. XX(505, HTTP_VERSION_NOT_SUPPORTED, HTTP Version Not Supported) \
  79. XX(506, VARIANT_ALSO_NEGOTIATES, Variant Also Negotiates) \
  80. XX(507, INSUFFICIENT_STORAGE, Insufficient Storage) \
  81. XX(508, LOOP_DETECTED, Loop Detected) \
  82. XX(510, NOT_EXTENDED, Not Extended) \
  83. XX(511, NETWORK_AUTHENTICATION_REQUIRED, Network Authentication Required) \
  84. // HTTP_STATUS_##name
  85. enum http_status {
  86. #define XX(num, name, string) HTTP_STATUS_##name = num,
  87. HTTP_STATUS_MAP(XX)
  88. #undef XX
  89. HTTP_CUSTOM_STATUS
  90. };
  91. #define HTTP_STATUS_IS_REDIRECT(status) \
  92. (status) == HTTP_STATUS_MOVED_PERMANENTLY || \
  93. (status) == HTTP_STATUS_FOUND || \
  94. (status) == HTTP_STATUS_SEE_OTHER || \
  95. (status) == HTTP_STATUS_TEMPORARY_REDIRECT || \
  96. (status) == HTTP_STATUS_PERMANENT_REDIRECT
  97. // http_mehtod
  98. // XX(num, name, string)
  99. #define HTTP_METHOD_MAP(XX) \
  100. XX(0, DELETE, DELETE) \
  101. XX(1, GET, GET) \
  102. XX(2, HEAD, HEAD) \
  103. XX(3, POST, POST) \
  104. XX(4, PUT, PUT) \
  105. /* pathological */ \
  106. XX(5, CONNECT, CONNECT) \
  107. XX(6, OPTIONS, OPTIONS) \
  108. XX(7, TRACE, TRACE) \
  109. /* WebDAV */ \
  110. XX(8, COPY, COPY) \
  111. XX(9, LOCK, LOCK) \
  112. XX(10, MKCOL, MKCOL) \
  113. XX(11, MOVE, MOVE) \
  114. XX(12, PROPFIND, PROPFIND) \
  115. XX(13, PROPPATCH, PROPPATCH) \
  116. XX(14, SEARCH, SEARCH) \
  117. XX(15, UNLOCK, UNLOCK) \
  118. XX(16, BIND, BIND) \
  119. XX(17, REBIND, REBIND) \
  120. XX(18, UNBIND, UNBIND) \
  121. XX(19, ACL, ACL) \
  122. /* subversion */ \
  123. XX(20, REPORT, REPORT) \
  124. XX(21, MKACTIVITY, MKACTIVITY) \
  125. XX(22, CHECKOUT, CHECKOUT) \
  126. XX(23, MERGE, MERGE) \
  127. /* upnp */ \
  128. XX(24, MSEARCH, M-SEARCH) \
  129. XX(25, NOTIFY, NOTIFY) \
  130. XX(26, SUBSCRIBE, SUBSCRIBE) \
  131. XX(27, UNSUBSCRIBE, UNSUBSCRIBE) \
  132. /* RFC-5789 */ \
  133. XX(28, PATCH, PATCH) \
  134. XX(29, PURGE, PURGE) \
  135. /* CalDAV */ \
  136. XX(30, MKCALENDAR, MKCALENDAR) \
  137. /* RFC-2068, section 19.6.1.2 */ \
  138. XX(31, LINK, LINK) \
  139. XX(32, UNLINK, UNLINK) \
  140. /* icecast */ \
  141. XX(33, SOURCE, SOURCE) \
  142. // HTTP_##name
  143. enum http_method {
  144. #define XX(num, name, string) HTTP_##name = num,
  145. HTTP_METHOD_MAP(XX)
  146. #undef XX
  147. HTTP_CUSTOM_METHOD
  148. };
  149. // MIME: https://www.iana.org/assignments/media-types/media-types.xhtml
  150. // http_content_type
  151. // XX(name, mime, suffix)
  152. #define HTTP_CONTENT_TYPE_MAP(XX) \
  153. XX(TEXT_PLAIN, text/plain, txt) \
  154. XX(TEXT_HTML, text/html, html) \
  155. XX(TEXT_CSS, text/css, css) \
  156. XX(TEXT_EVENT_STREAM, text/event-stream, sse) \
  157. XX(IMAGE_JPEG, image/jpeg, jpg) \
  158. XX(IMAGE_PNG, image/png, png) \
  159. XX(IMAGE_GIF, image/gif, gif) \
  160. XX(IMAGE_BMP, image/bmp, bmp) \
  161. XX(IMAGE_SVG, image/svg, svg) \
  162. XX(APPLICATION_OCTET_STREAM,application/octet-stream, bin) \
  163. XX(APPLICATION_JAVASCRIPT, application/javascript, js) \
  164. XX(APPLICATION_XML, application/xml, xml) \
  165. XX(APPLICATION_JSON, application/json, json) \
  166. XX(APPLICATION_GRPC, application/grpc, grpc) \
  167. XX(APPLICATION_URLENCODED, application/x-www-form-urlencoded, kv) \
  168. XX(MULTIPART_FORM_DATA, multipart/form-data, mp) \
  169. #define X_WWW_FORM_URLENCODED APPLICATION_URLENCODED // for compatibility
  170. enum http_content_type {
  171. #define XX(name, string, suffix) name,
  172. CONTENT_TYPE_NONE,
  173. HTTP_CONTENT_TYPE_MAP(XX)
  174. CONTENT_TYPE_UNDEFINED
  175. #undef XX
  176. };
  177. BEGIN_EXTERN_C
  178. HV_EXPORT const char* http_status_str(enum http_status status);
  179. HV_EXPORT const char* http_method_str(enum http_method method);
  180. HV_EXPORT const char* http_content_type_str(enum http_content_type type);
  181. HV_EXPORT enum http_status http_status_enum(const char* str);
  182. HV_EXPORT enum http_method http_method_enum(const char* str);
  183. HV_EXPORT enum http_content_type http_content_type_enum(const char* str);
  184. HV_EXPORT const char* http_content_type_suffix(enum http_content_type type);
  185. HV_EXPORT const char* http_content_type_str_by_suffix(const char* suffix);
  186. HV_EXPORT enum http_content_type http_content_type_enum_by_suffix(const char* suffix);
  187. END_EXTERN_C
  188. #endif // HV_HTTP_DEF_H_