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