2 * Copyright (C) 2011-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "HttpResponse.h"
14 #define SEPARATOR ": "
15 #define LINEBREAK "\r\n"
17 #define HEADER_CONTENT_LENGTH "Content-Length"
19 std::map
<HTTP::StatusCode
, std::string
> CHttpResponse::m_statusCodeText
= CHttpResponse::createStatusCodes();
21 CHttpResponse::CHttpResponse(HTTP::Method method
, HTTP::StatusCode status
, HTTP::Version version
/* = HTTPVersion1_1 */)
31 void CHttpResponse::AddHeader(const std::string
&field
, const std::string
&value
)
36 m_headers
.emplace_back(field
, value
);
39 void CHttpResponse::SetContent(const char* data
, unsigned int length
)
43 if (m_content
== NULL
)
46 m_contentLength
= length
;
49 std::string
CHttpResponse::Create()
53 m_buffer
.append("HTTP/");
56 case HTTP::Version1_0
:
57 m_buffer
.append("1.0");
60 case HTTP::Version1_1
:
61 m_buffer
.append("1.1");
69 sprintf(statusBuffer
, "%d", (int)m_status
);
70 m_buffer
.append(SPACE
);
71 m_buffer
.append(statusBuffer
);
73 m_buffer
.append(SPACE
);
74 m_buffer
.append(m_statusCodeText
.find(m_status
)->second
);
75 m_buffer
.append(LINEBREAK
);
77 bool hasContentLengthHeader
= false;
78 for (unsigned int index
= 0; index
< m_headers
.size(); index
++)
80 m_buffer
.append(m_headers
[index
].first
);
81 m_buffer
.append(SEPARATOR
);
82 m_buffer
.append(m_headers
[index
].second
);
83 m_buffer
.append(LINEBREAK
);
85 if (m_headers
[index
].first
.compare(HEADER_CONTENT_LENGTH
) == 0)
86 hasContentLengthHeader
= true;
89 if (!hasContentLengthHeader
&& m_content
!= NULL
&& m_contentLength
> 0)
91 m_buffer
.append(HEADER_CONTENT_LENGTH
);
92 m_buffer
.append(SEPARATOR
);
93 char lengthBuffer
[11];
94 sprintf(lengthBuffer
, "%u", m_contentLength
);
95 m_buffer
.append(lengthBuffer
);
96 m_buffer
.append(LINEBREAK
);
99 m_buffer
.append(LINEBREAK
);
100 if (m_content
!= NULL
&& m_contentLength
> 0)
101 m_buffer
.append(m_content
, m_contentLength
);
106 std::map
<HTTP::StatusCode
, std::string
> CHttpResponse::createStatusCodes()
108 std::map
<HTTP::StatusCode
, std::string
> map
;
109 map
[HTTP::Continue
] = "Continue";
110 map
[HTTP::SwitchingProtocols
] = "Switching Protocols";
111 map
[HTTP::Processing
] = "Processing";
112 map
[HTTP::ConnectionTimedOut
] = "Connection timed out";
113 map
[HTTP::OK
] = "OK";
114 map
[HTTP::Created
] = "Created";
115 map
[HTTP::Accepted
] = "Accepted";
116 map
[HTTP::NonAuthoritativeInformation
] = "Non-Authoritative Information";
117 map
[HTTP::NoContent
] = "No Content";
118 map
[HTTP::ResetContent
] = "Reset Content";
119 map
[HTTP::PartialContent
] = "Partial Content";
120 map
[HTTP::MultiStatus
] = "Multi-Status";
121 map
[HTTP::MultipleChoices
] = "Multiple Choices";
122 map
[HTTP::MovedPermanently
] = "Moved Permanently";
123 map
[HTTP::Found
] = "Found";
124 map
[HTTP::SeeOther
] = "See Other";
125 map
[HTTP::NotModified
] = "Not Modified";
126 map
[HTTP::UseProxy
] = "Use Proxy";
127 //map[HTTP::SwitchProxy] = "Switch Proxy";
128 map
[HTTP::TemporaryRedirect
] = "Temporary Redirect";
129 map
[HTTP::BadRequest
] = "Bad Request";
130 map
[HTTP::Unauthorized
] = "Unauthorized";
131 map
[HTTP::PaymentRequired
] = "Payment Required";
132 map
[HTTP::Forbidden
] = "Forbidden";
133 map
[HTTP::NotFound
] = "Not Found";
134 map
[HTTP::MethodNotAllowed
] = "Method Not Allowed";
135 map
[HTTP::NotAcceptable
] = "Not Acceptable";
136 map
[HTTP::ProxyAuthenticationRequired
] = "Proxy Authentication Required";
137 map
[HTTP::RequestTimeout
] = "Request Time-out";
138 map
[HTTP::Conflict
] = "Conflict";
139 map
[HTTP::Gone
] = "Gone";
140 map
[HTTP::LengthRequired
] = "Length Required";
141 map
[HTTP::PreconditionFailed
] = "Precondition Failed";
142 map
[HTTP::RequestEntityTooLarge
] = "Request Entity Too Large";
143 map
[HTTP::RequestURITooLong
] = "Request-URI Too Long";
144 map
[HTTP::UnsupportedMediaType
] = "Unsupported Media Type";
145 map
[HTTP::RequestedRangeNotSatisfiable
] = "Requested range not satisfiable";
146 map
[HTTP::ExpectationFailed
] = "Expectation Failed";
147 map
[HTTP::ImATeapot
] = "I'm a Teapot";
148 map
[HTTP::TooManyConnections
] = "There are too many connections from your internet address";
149 map
[HTTP::UnprocessableEntity
] = "Unprocessable Entity";
150 map
[HTTP::Locked
] = "Locked";
151 map
[HTTP::FailedDependency
] = "Failed Dependency";
152 map
[HTTP::UnorderedCollection
] = "UnorderedCollection";
153 map
[HTTP::UpgradeRequired
] = "Upgrade Required";
154 map
[HTTP::InternalServerError
] = "Internal Server Error";
155 map
[HTTP::NotImplemented
] = "Not Implemented";
156 map
[HTTP::BadGateway
] = "Bad Gateway";
157 map
[HTTP::ServiceUnavailable
] = "Service Unavailable";
158 map
[HTTP::GatewayTimeout
] = "Gateway Time-out";
159 map
[HTTP::HTTPVersionNotSupported
] = "HTTP Version not supported";
160 map
[HTTP::VariantAlsoNegotiates
] = "Variant Also Negotiates";
161 map
[HTTP::InsufficientStorage
] = "Insufficient Storage";
162 map
[HTTP::BandwidthLimitExceeded
] = "Bandwidth Limit Exceeded";
163 map
[HTTP::NotExtended
] = "Not Extended";