HttpResponse.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibHTTP/HttpResponse.h>
  7. namespace HTTP {
  8. HttpResponse::HttpResponse(int code, HashMap<String, String, CaseInsensitiveStringTraits>&& headers)
  9. : m_code(code)
  10. , m_headers(move(headers))
  11. {
  12. }
  13. HttpResponse::~HttpResponse()
  14. {
  15. }
  16. StringView HttpResponse::reason_phrase_for_code(int code)
  17. {
  18. VERIFY(code >= 100 && code <= 599);
  19. static HashMap<int, StringView> s_reason_phrases = {
  20. { 100, "Continue"sv },
  21. { 101, "Switching Protocols"sv },
  22. { 200, "OK"sv },
  23. { 201, "Created"sv },
  24. { 202, "Accepted"sv },
  25. { 203, "Non-Authoritative Information"sv },
  26. { 204, "No Content"sv },
  27. { 205, "Reset Content"sv },
  28. { 206, "Partial Content"sv },
  29. { 300, "Multiple Choices"sv },
  30. { 301, "Moved Permanently"sv },
  31. { 302, "Found"sv },
  32. { 303, "See Other"sv },
  33. { 304, "Not Modified"sv },
  34. { 305, "Use Proxy"sv },
  35. { 307, "Temporary Redirect"sv },
  36. { 400, "Bad Request"sv },
  37. { 401, "Unauthorized"sv },
  38. { 402, "Payment Required"sv },
  39. { 403, "Forbidden"sv },
  40. { 404, "Not Found"sv },
  41. { 405, "Method Not Allowed"sv },
  42. { 406, "Not Acceptable"sv },
  43. { 407, "Proxy Authentication Required"sv },
  44. { 408, "Request Timeout"sv },
  45. { 409, "Conflict"sv },
  46. { 410, "Gone"sv },
  47. { 411, "Length Required"sv },
  48. { 412, "Precondition Failed"sv },
  49. { 413, "Payload Too Large"sv },
  50. { 414, "URI Too Long"sv },
  51. { 415, "Unsupported Media Type"sv },
  52. { 416, "Range Not Satisfiable"sv },
  53. { 417, "Expectation Failed"sv },
  54. { 426, "Upgrade Required"sv },
  55. { 500, "Internal Server Error"sv },
  56. { 501, "Not Implemented"sv },
  57. { 502, "Bad Gateway"sv },
  58. { 503, "Service Unavailable"sv },
  59. { 504, "Gateway Timeout"sv },
  60. { 505, "HTTP Version Not Supported"sv }
  61. };
  62. if (s_reason_phrases.contains(code))
  63. return s_reason_phrases.ensure(code);
  64. // NOTE: "A client MUST understand the class of any status code, as indicated by the first
  65. // digit, and treat an unrecognized status code as being equivalent to the x00 status
  66. // code of that class." (RFC 7231, section 6)
  67. auto generic_code = (code / 100) * 100;
  68. VERIFY(s_reason_phrases.contains(generic_code));
  69. return s_reason_phrases.ensure(generic_code);
  70. }
  71. }