| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2009 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
| Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 16 |  | 
|  | 17 | // Implementation of common HTTP related functions. | 
|  | 18 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 19 | #include "update_engine/common/http_common.h" | 
| Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 20 |  | 
| Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 21 | #include <base/macros.h> | 
| Alex Deymo | 2447c67 | 2014-04-02 21:09:10 -0700 | [diff] [blame] | 22 |  | 
|  | 23 | namespace chromeos_update_engine { | 
| Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 24 |  | 
|  | 25 | const char *GetHttpResponseDescription(HttpResponseCode code) { | 
|  | 26 | static const struct { | 
|  | 27 | HttpResponseCode code; | 
|  | 28 | const char* description; | 
|  | 29 | } http_response_table[] = { | 
|  | 30 | { kHttpResponseOk,                  "OK" }, | 
|  | 31 | { kHttpResponseCreated,             "Created" }, | 
|  | 32 | { kHttpResponseAccepted,            "Accepted" }, | 
|  | 33 | { kHttpResponseNonAuthInfo,         "Non-Authoritative Information" }, | 
|  | 34 | { kHttpResponseNoContent,           "No Content" }, | 
|  | 35 | { kHttpResponseResetContent,        "Reset Content" }, | 
|  | 36 | { kHttpResponsePartialContent,      "Partial Content" }, | 
|  | 37 | { kHttpResponseMultipleChoices,     "Multiple Choices" }, | 
|  | 38 | { kHttpResponseMovedPermanently,    "Moved Permanently" }, | 
|  | 39 | { kHttpResponseFound,               "Found" }, | 
|  | 40 | { kHttpResponseSeeOther,            "See Other" }, | 
|  | 41 | { kHttpResponseNotModified,         "Not Modified" }, | 
|  | 42 | { kHttpResponseUseProxy,            "Use Proxy" }, | 
|  | 43 | { kHttpResponseTempRedirect,        "Temporary Redirect" }, | 
|  | 44 | { kHttpResponseBadRequest,          "Bad Request" }, | 
|  | 45 | { kHttpResponseUnauth,              "Unauthorized" }, | 
|  | 46 | { kHttpResponseForbidden,           "Forbidden" }, | 
|  | 47 | { kHttpResponseNotFound,            "Not Found" }, | 
|  | 48 | { kHttpResponseRequestTimeout,      "Request Timeout" }, | 
|  | 49 | { kHttpResponseInternalServerError, "Internal Server Error" }, | 
|  | 50 | { kHttpResponseNotImplemented,      "Not Implemented" }, | 
|  | 51 | { kHttpResponseServiceUnavailable,  "Service Unavailable" }, | 
|  | 52 | { kHttpResponseVersionNotSupported, "HTTP Version Not Supported" }, | 
|  | 53 | }; | 
|  | 54 |  | 
|  | 55 | bool is_found = false; | 
|  | 56 | size_t i; | 
| Alex Vakulenko | 9c155d2 | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 57 | for (i = 0; i < arraysize(http_response_table); i++) | 
| Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 58 | if ((is_found = (http_response_table[i].code == code))) | 
|  | 59 | break; | 
|  | 60 |  | 
|  | 61 | return (is_found ? http_response_table[i].description : "(unsupported)"); | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | HttpResponseCode StringToHttpResponseCode(const char *s) { | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 65 | return static_cast<HttpResponseCode>(strtoul(s, nullptr, 10)); | 
| Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 66 | } | 
| Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 67 |  | 
|  | 68 |  | 
|  | 69 | const char *GetHttpContentTypeString(HttpContentType type) { | 
|  | 70 | static const struct { | 
|  | 71 | HttpContentType type; | 
|  | 72 | const char* str; | 
|  | 73 | } http_content_type_table[] = { | 
|  | 74 | { kHttpContentTypeTextXml, "text/xml" }, | 
|  | 75 | }; | 
|  | 76 |  | 
|  | 77 | bool is_found = false; | 
|  | 78 | size_t i; | 
| Alex Vakulenko | 9c155d2 | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 79 | for (i = 0; i < arraysize(http_content_type_table); i++) | 
| Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 80 | if ((is_found = (http_content_type_table[i].type == type))) | 
|  | 81 | break; | 
|  | 82 |  | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 83 | return (is_found ? http_content_type_table[i].str : nullptr); | 
| Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 84 | } | 
| Alex Deymo | 2447c67 | 2014-04-02 21:09:10 -0700 | [diff] [blame] | 85 |  | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 86 | }  // namespace chromeos_update_engine |