1
0

httpdef.h 9.3 KB

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