blob: d2df454dee5be44ef35d3a6f349ca8d0805c89be [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Arnold9bedeb52011-11-17 16:19:57 -080016
17// Implementation of common HTTP related functions.
18
Gilad Arnold9bedeb52011-11-17 16:19:57 -080019#include "update_engine/http_common.h"
20
Ben Chan05735a12014-09-03 07:48:22 -070021#include <base/macros.h>
Alex Deymo2447c672014-04-02 21:09:10 -070022
23namespace chromeos_update_engine {
Gilad Arnold9bedeb52011-11-17 16:19:57 -080024
25const 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 Vakulenko9c155d22014-12-10 12:52:31 -080057 for (i = 0; i < arraysize(http_response_table); i++)
Gilad Arnold9bedeb52011-11-17 16:19:57 -080058 if ((is_found = (http_response_table[i].code == code)))
59 break;
60
61 return (is_found ? http_response_table[i].description : "(unsupported)");
62}
63
64HttpResponseCode StringToHttpResponseCode(const char *s) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -070065 return static_cast<HttpResponseCode>(strtoul(s, nullptr, 10));
Gilad Arnold9bedeb52011-11-17 16:19:57 -080066}
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -080067
68
69const 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 Vakulenko9c155d22014-12-10 12:52:31 -080079 for (i = 0; i < arraysize(http_content_type_table); i++)
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -080080 if ((is_found = (http_content_type_table[i].type == type)))
81 break;
82
Alex Vakulenko88b591f2014-08-28 16:48:57 -070083 return (is_found ? http_content_type_table[i].str : nullptr);
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -080084}
Alex Deymo2447c672014-04-02 21:09:10 -070085
Alex Vakulenkod2779df2014-06-16 13:19:00 -070086} // namespace chromeos_update_engine