diff options
Diffstat (limited to 'xbmc/utils/HttpResponse.cpp')
| -rw-r--r-- | xbmc/utils/HttpResponse.cpp | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/xbmc/utils/HttpResponse.cpp b/xbmc/utils/HttpResponse.cpp new file mode 100644 index 0000000..33c7c27 --- /dev/null +++ b/xbmc/utils/HttpResponse.cpp | |||
| @@ -0,0 +1,166 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2011-2018 Team Kodi | ||
| 3 | * This file is part of Kodi - https://kodi.tv | ||
| 4 | * | ||
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later | ||
| 6 | * See LICENSES/README.md for more information. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include "HttpResponse.h" | ||
| 10 | |||
| 11 | #include <stdio.h> | ||
| 12 | |||
| 13 | #define SPACE " " | ||
| 14 | #define SEPARATOR ": " | ||
| 15 | #define LINEBREAK "\r\n" | ||
| 16 | |||
| 17 | #define HEADER_CONTENT_LENGTH "Content-Length" | ||
| 18 | |||
| 19 | std::map<HTTP::StatusCode, std::string> CHttpResponse::m_statusCodeText = CHttpResponse::createStatusCodes(); | ||
| 20 | |||
| 21 | CHttpResponse::CHttpResponse(HTTP::Method method, HTTP::StatusCode status, HTTP::Version version /* = HTTPVersion1_1 */) | ||
| 22 | { | ||
| 23 | m_method = method; | ||
| 24 | m_status = status; | ||
| 25 | m_version = version; | ||
| 26 | |||
| 27 | m_content = NULL; | ||
| 28 | m_contentLength = 0; | ||
| 29 | } | ||
| 30 | |||
| 31 | void CHttpResponse::AddHeader(const std::string &field, const std::string &value) | ||
| 32 | { | ||
| 33 | if (field.empty()) | ||
| 34 | return; | ||
| 35 | |||
| 36 | m_headers.emplace_back(field, value); | ||
| 37 | } | ||
| 38 | |||
| 39 | void CHttpResponse::SetContent(const char* data, unsigned int length) | ||
| 40 | { | ||
| 41 | m_content = data; | ||
| 42 | |||
| 43 | if (m_content == NULL) | ||
| 44 | m_contentLength = 0; | ||
| 45 | else | ||
| 46 | m_contentLength = length; | ||
| 47 | } | ||
| 48 | |||
| 49 | std::string CHttpResponse::Create() | ||
| 50 | { | ||
| 51 | m_buffer.clear(); | ||
| 52 | |||
| 53 | m_buffer.append("HTTP/"); | ||
| 54 | switch (m_version) | ||
| 55 | { | ||
| 56 | case HTTP::Version1_0: | ||
| 57 | m_buffer.append("1.0"); | ||
| 58 | break; | ||
| 59 | |||
| 60 | case HTTP::Version1_1: | ||
| 61 | m_buffer.append("1.1"); | ||
| 62 | break; | ||
| 63 | |||
| 64 | default: | ||
| 65 | return 0; | ||
| 66 | } | ||
| 67 | |||
| 68 | char statusBuffer[4]; | ||
| 69 | sprintf(statusBuffer, "%d", (int)m_status); | ||
| 70 | m_buffer.append(SPACE); | ||
| 71 | m_buffer.append(statusBuffer); | ||
| 72 | |||
| 73 | m_buffer.append(SPACE); | ||
| 74 | m_buffer.append(m_statusCodeText.find(m_status)->second); | ||
| 75 | m_buffer.append(LINEBREAK); | ||
| 76 | |||
| 77 | bool hasContentLengthHeader = false; | ||
| 78 | for (unsigned int index = 0; index < m_headers.size(); index++) | ||
| 79 | { | ||
| 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); | ||
| 84 | |||
| 85 | if (m_headers[index].first.compare(HEADER_CONTENT_LENGTH) == 0) | ||
| 86 | hasContentLengthHeader = true; | ||
| 87 | } | ||
| 88 | |||
| 89 | if (!hasContentLengthHeader && m_content != NULL && m_contentLength > 0) | ||
| 90 | { | ||
| 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); | ||
| 97 | } | ||
| 98 | |||
| 99 | m_buffer.append(LINEBREAK); | ||
| 100 | if (m_content != NULL && m_contentLength > 0) | ||
| 101 | m_buffer.append(m_content, m_contentLength); | ||
| 102 | |||
| 103 | return m_buffer; | ||
| 104 | } | ||
| 105 | |||
| 106 | std::map<HTTP::StatusCode, std::string> CHttpResponse::createStatusCodes() | ||
| 107 | { | ||
| 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"; | ||
| 164 | |||
| 165 | return map; | ||
| 166 | } | ||
