HttpResponse.cpp 2.5 KB

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