| 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 |  | 
| Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 21 | #include <cstdlib> | 
|  | 22 |  | 
| Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 23 | #include <base/macros.h> | 
| hscham | 00b6aa2 | 2020-02-20 12:32:06 +0900 | [diff] [blame] | 24 | #include <base/stl_util.h> | 
| Alex Deymo | 2447c67 | 2014-04-02 21:09:10 -0700 | [diff] [blame] | 25 |  | 
|  | 26 | namespace chromeos_update_engine { | 
| Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 27 |  | 
| Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 28 | const char* GetHttpResponseDescription(HttpResponseCode code) { | 
| Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 29 | static const struct { | 
|  | 30 | HttpResponseCode code; | 
|  | 31 | const char* description; | 
|  | 32 | } http_response_table[] = { | 
| Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 33 | {kHttpResponseOk, "OK"}, | 
|  | 34 | {kHttpResponseCreated, "Created"}, | 
|  | 35 | {kHttpResponseAccepted, "Accepted"}, | 
|  | 36 | {kHttpResponseNonAuthInfo, "Non-Authoritative Information"}, | 
|  | 37 | {kHttpResponseNoContent, "No Content"}, | 
|  | 38 | {kHttpResponseResetContent, "Reset Content"}, | 
|  | 39 | {kHttpResponsePartialContent, "Partial Content"}, | 
|  | 40 | {kHttpResponseMultipleChoices, "Multiple Choices"}, | 
|  | 41 | {kHttpResponseMovedPermanently, "Moved Permanently"}, | 
|  | 42 | {kHttpResponseFound, "Found"}, | 
|  | 43 | {kHttpResponseSeeOther, "See Other"}, | 
|  | 44 | {kHttpResponseNotModified, "Not Modified"}, | 
|  | 45 | {kHttpResponseUseProxy, "Use Proxy"}, | 
|  | 46 | {kHttpResponseTempRedirect, "Temporary Redirect"}, | 
|  | 47 | {kHttpResponseBadRequest, "Bad Request"}, | 
|  | 48 | {kHttpResponseUnauth, "Unauthorized"}, | 
|  | 49 | {kHttpResponseForbidden, "Forbidden"}, | 
|  | 50 | {kHttpResponseNotFound, "Not Found"}, | 
|  | 51 | {kHttpResponseRequestTimeout, "Request Timeout"}, | 
|  | 52 | {kHttpResponseInternalServerError, "Internal Server Error"}, | 
|  | 53 | {kHttpResponseNotImplemented, "Not Implemented"}, | 
|  | 54 | {kHttpResponseServiceUnavailable, "Service Unavailable"}, | 
|  | 55 | {kHttpResponseVersionNotSupported, "HTTP Version Not Supported"}, | 
| Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 56 | }; | 
|  | 57 |  | 
|  | 58 | bool is_found = false; | 
|  | 59 | size_t i; | 
| hscham | 00b6aa2 | 2020-02-20 12:32:06 +0900 | [diff] [blame] | 60 | for (i = 0; i < base::size(http_response_table); i++) | 
| Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 61 | if ((is_found = (http_response_table[i].code == code))) | 
|  | 62 | break; | 
|  | 63 |  | 
|  | 64 | return (is_found ? http_response_table[i].description : "(unsupported)"); | 
|  | 65 | } | 
|  | 66 |  | 
| Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 67 | HttpResponseCode StringToHttpResponseCode(const char* s) { | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 68 | return static_cast<HttpResponseCode>(strtoul(s, nullptr, 10)); | 
| Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 69 | } | 
| Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 70 |  | 
| Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 71 | const char* GetHttpContentTypeString(HttpContentType type) { | 
| Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 72 | static const struct { | 
|  | 73 | HttpContentType type; | 
|  | 74 | const char* str; | 
|  | 75 | } http_content_type_table[] = { | 
| Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 76 | {kHttpContentTypeTextXml, "text/xml"}, | 
| Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 77 | }; | 
|  | 78 |  | 
|  | 79 | bool is_found = false; | 
|  | 80 | size_t i; | 
| hscham | 00b6aa2 | 2020-02-20 12:32:06 +0900 | [diff] [blame] | 81 | for (i = 0; i < base::size(http_content_type_table); i++) | 
| Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 82 | if ((is_found = (http_content_type_table[i].type == type))) | 
|  | 83 | break; | 
|  | 84 |  | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 85 | return (is_found ? http_content_type_table[i].str : nullptr); | 
| Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 86 | } | 
| Alex Deymo | 2447c67 | 2014-04-02 21:09:10 -0700 | [diff] [blame] | 87 |  | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 88 | }  // namespace chromeos_update_engine |