http_defs.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "../libnetdata.h"
  3. const char *http_request_method2string(HTTP_REQUEST_MODE mode) {
  4. switch(mode) {
  5. case HTTP_REQUEST_MODE_OPTIONS:
  6. return "OPTIONS";
  7. case HTTP_REQUEST_MODE_GET:
  8. return "GET";
  9. case HTTP_REQUEST_MODE_FILECOPY:
  10. return "FILECOPY";
  11. case HTTP_REQUEST_MODE_POST:
  12. return "POST";
  13. case HTTP_REQUEST_MODE_PUT:
  14. return "PUT";
  15. case HTTP_REQUEST_MODE_DELETE:
  16. return "DELETE";
  17. case HTTP_REQUEST_MODE_STREAM:
  18. return "STREAM";
  19. default:
  20. return "UNKNOWN";
  21. }
  22. }
  23. const char *http_response_code2string(int code) {
  24. switch(code) {
  25. case 100:
  26. return "Continue";
  27. case 101:
  28. return "Switching Protocols";
  29. case 102:
  30. return "Processing";
  31. case 103:
  32. return "Early Hints";
  33. case 200:
  34. return "OK";
  35. case 201:
  36. return "Created";
  37. case 202:
  38. return "Accepted";
  39. case 203:
  40. return "Non-Authoritative Information";
  41. case 204:
  42. return "No Content";
  43. case 205:
  44. return "Reset Content";
  45. case 206:
  46. return "Partial Content";
  47. case 207:
  48. return "Multi-Status";
  49. case 208:
  50. return "Already Reported";
  51. case 226:
  52. return "IM Used";
  53. case 300:
  54. return "Multiple Choices";
  55. case 301:
  56. return "Moved Permanently";
  57. case 302:
  58. return "Found";
  59. case 303:
  60. return "See Other";
  61. case 304:
  62. return "Not Modified";
  63. case 305:
  64. return "Use Proxy";
  65. case 306:
  66. return "Switch Proxy";
  67. case 307:
  68. return "Temporary Redirect";
  69. case 308:
  70. return "Permanent Redirect";
  71. case 400:
  72. return "Bad Request";
  73. case 401:
  74. return "Unauthorized";
  75. case 402:
  76. return "Payment Required";
  77. case 403:
  78. return "Forbidden";
  79. case 404:
  80. return "Not Found";
  81. case 405:
  82. return "Method Not Allowed";
  83. case 406:
  84. return "Not Acceptable";
  85. case 407:
  86. return "Proxy Authentication Required";
  87. case 408:
  88. return "Request Timeout";
  89. case 409:
  90. return "Conflict";
  91. case 410:
  92. return "Gone";
  93. case 411:
  94. return "Length Required";
  95. case 412:
  96. return "Precondition Failed";
  97. case 413:
  98. return "Payload Too Large";
  99. case 414:
  100. return "URI Too Long";
  101. case 415:
  102. return "Unsupported Media Type";
  103. case 416:
  104. return "Range Not Satisfiable";
  105. case 417:
  106. return "Expectation Failed";
  107. case 418:
  108. return "I'm a teapot";
  109. case 421:
  110. return "Misdirected Request";
  111. case 422:
  112. return "Unprocessable Entity";
  113. case 423:
  114. return "Locked";
  115. case 424:
  116. return "Failed Dependency";
  117. case 425:
  118. return "Too Early";
  119. case 426:
  120. return "Upgrade Required";
  121. case 428:
  122. return "Precondition Required";
  123. case 429:
  124. return "Too Many Requests";
  125. case 431:
  126. return "Request Header Fields Too Large";
  127. case 451:
  128. return "Unavailable For Legal Reasons";
  129. case 499: // nginx's extension to the standard
  130. return "Client Closed Request";
  131. case 500:
  132. return "Internal Server Error";
  133. case 501:
  134. return "Not Implemented";
  135. case 502:
  136. return "Bad Gateway";
  137. case 503:
  138. return "Service Unavailable";
  139. case 504:
  140. return "Gateway Timeout";
  141. case 505:
  142. return "HTTP Version Not Supported";
  143. case 506:
  144. return "Variant Also Negotiates";
  145. case 507:
  146. return "Insufficient Storage";
  147. case 508:
  148. return "Loop Detected";
  149. case 510:
  150. return "Not Extended";
  151. case 511:
  152. return "Network Authentication Required";
  153. default:
  154. if(code >= 100 && code < 200)
  155. return "Informational";
  156. if(code >= 200 && code < 300)
  157. return "Successful";
  158. if(code >= 300 && code < 400)
  159. return "Redirection";
  160. if(code >= 400 && code < 500)
  161. return "Client Error";
  162. if(code >= 500 && code < 600)
  163. return "Server Error";
  164. return "Undefined Error";
  165. }
  166. }
  167. static struct {
  168. const char *extension;
  169. uint32_t hash;
  170. HTTP_CONTENT_TYPE contenttype;
  171. } mime_types[] = {
  172. { "html" , 0 , CT_TEXT_HTML }
  173. , { "js" , 0 , CT_APPLICATION_X_JAVASCRIPT }
  174. , { "css" , 0 , CT_TEXT_CSS }
  175. , { "xml" , 0 , CT_TEXT_XML }
  176. , { "xsl" , 0 , CT_TEXT_XSL }
  177. , { "txt" , 0 , CT_TEXT_PLAIN }
  178. , { "svg" , 0 , CT_IMAGE_SVG_XML }
  179. , { "ttf" , 0 , CT_APPLICATION_X_FONT_TRUETYPE }
  180. , { "otf" , 0 , CT_APPLICATION_X_FONT_OPENTYPE }
  181. , { "woff2", 0 , CT_APPLICATION_FONT_WOFF2 }
  182. , { "woff" , 0 , CT_APPLICATION_FONT_WOFF }
  183. , { "eot" , 0 , CT_APPLICATION_VND_MS_FONTOBJ }
  184. , { "png" , 0 , CT_IMAGE_PNG }
  185. , { "jpg" , 0 , CT_IMAGE_JPG }
  186. , { "jpeg" , 0 , CT_IMAGE_JPG }
  187. , { "gif" , 0 , CT_IMAGE_GIF }
  188. , { "bmp" , 0 , CT_IMAGE_BMP }
  189. , { "ico" , 0 , CT_IMAGE_XICON }
  190. , { "icns" , 0 , CT_IMAGE_ICNS }
  191. // terminator
  192. , { NULL , 0 , 0 }
  193. };
  194. HTTP_CONTENT_TYPE contenttype_for_filename(const char *filename) {
  195. // netdata_log_info("checking filename '%s'", filename);
  196. static int initialized = 0;
  197. int i;
  198. if(unlikely(!initialized)) {
  199. for (i = 0; mime_types[i].extension; i++)
  200. mime_types[i].hash = simple_hash(mime_types[i].extension);
  201. initialized = 1;
  202. }
  203. const char *s = filename, *last_dot = NULL;
  204. // find the last dot
  205. while(*s) {
  206. if(unlikely(*s == '.')) last_dot = s;
  207. s++;
  208. }
  209. if(unlikely(!last_dot || !*last_dot || !last_dot[1])) {
  210. // netdata_log_info("no extension for filename '%s'", filename);
  211. return CT_APPLICATION_OCTET_STREAM;
  212. }
  213. last_dot++;
  214. // netdata_log_info("extension for filename '%s' is '%s'", filename, last_dot);
  215. uint32_t hash = simple_hash(last_dot);
  216. for(i = 0; mime_types[i].extension ; i++) {
  217. if(unlikely(hash == mime_types[i].hash && !strcmp(last_dot, mime_types[i].extension))) {
  218. // netdata_log_info("matched extension for filename '%s': '%s'", filename, last_dot);
  219. return mime_types[i].contenttype;
  220. }
  221. }
  222. // netdata_log_info("not matched extension for filename '%s': '%s'", filename, last_dot);
  223. return CT_APPLICATION_OCTET_STREAM;
  224. }