Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Implementation of common HTTP related functions. |
| 6 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 7 | #include "update_engine/http_common.h" |
| 8 | |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 9 | #include <base/macros.h> |
Alex Deymo | 2447c67 | 2014-04-02 21:09:10 -0700 | [diff] [blame] | 10 | |
| 11 | namespace chromeos_update_engine { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 12 | |
| 13 | const char *GetHttpResponseDescription(HttpResponseCode code) { |
| 14 | static const struct { |
| 15 | HttpResponseCode code; |
| 16 | const char* description; |
| 17 | } http_response_table[] = { |
| 18 | { kHttpResponseOk, "OK" }, |
| 19 | { kHttpResponseCreated, "Created" }, |
| 20 | { kHttpResponseAccepted, "Accepted" }, |
| 21 | { kHttpResponseNonAuthInfo, "Non-Authoritative Information" }, |
| 22 | { kHttpResponseNoContent, "No Content" }, |
| 23 | { kHttpResponseResetContent, "Reset Content" }, |
| 24 | { kHttpResponsePartialContent, "Partial Content" }, |
| 25 | { kHttpResponseMultipleChoices, "Multiple Choices" }, |
| 26 | { kHttpResponseMovedPermanently, "Moved Permanently" }, |
| 27 | { kHttpResponseFound, "Found" }, |
| 28 | { kHttpResponseSeeOther, "See Other" }, |
| 29 | { kHttpResponseNotModified, "Not Modified" }, |
| 30 | { kHttpResponseUseProxy, "Use Proxy" }, |
| 31 | { kHttpResponseTempRedirect, "Temporary Redirect" }, |
| 32 | { kHttpResponseBadRequest, "Bad Request" }, |
| 33 | { kHttpResponseUnauth, "Unauthorized" }, |
| 34 | { kHttpResponseForbidden, "Forbidden" }, |
| 35 | { kHttpResponseNotFound, "Not Found" }, |
| 36 | { kHttpResponseRequestTimeout, "Request Timeout" }, |
| 37 | { kHttpResponseInternalServerError, "Internal Server Error" }, |
| 38 | { kHttpResponseNotImplemented, "Not Implemented" }, |
| 39 | { kHttpResponseServiceUnavailable, "Service Unavailable" }, |
| 40 | { kHttpResponseVersionNotSupported, "HTTP Version Not Supported" }, |
| 41 | }; |
| 42 | |
| 43 | bool is_found = false; |
| 44 | size_t i; |
Alex Vakulenko | 9c155d2 | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 45 | for (i = 0; i < arraysize(http_response_table); i++) |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 46 | if ((is_found = (http_response_table[i].code == code))) |
| 47 | break; |
| 48 | |
| 49 | return (is_found ? http_response_table[i].description : "(unsupported)"); |
| 50 | } |
| 51 | |
| 52 | HttpResponseCode StringToHttpResponseCode(const char *s) { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 53 | return static_cast<HttpResponseCode>(strtoul(s, nullptr, 10)); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 54 | } |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 55 | |
| 56 | |
| 57 | const char *GetHttpContentTypeString(HttpContentType type) { |
| 58 | static const struct { |
| 59 | HttpContentType type; |
| 60 | const char* str; |
| 61 | } http_content_type_table[] = { |
| 62 | { kHttpContentTypeTextXml, "text/xml" }, |
| 63 | }; |
| 64 | |
| 65 | bool is_found = false; |
| 66 | size_t i; |
Alex Vakulenko | 9c155d2 | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 67 | for (i = 0; i < arraysize(http_content_type_table); i++) |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 68 | if ((is_found = (http_content_type_table[i].type == type))) |
| 69 | break; |
| 70 | |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 71 | return (is_found ? http_content_type_table[i].str : nullptr); |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 72 | } |
Alex Deymo | 2447c67 | 2014-04-02 21:09:10 -0700 | [diff] [blame] | 73 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 74 | } // namespace chromeos_update_engine |