httpdef.h 8.6 KB

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