blob: 5f234b0bf1d5d704ddd6a5de5ef7daf7d3d6fab0 [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
Alex Deymo39910dc2015-11-09 17:04:30 -080019#include "update_engine/common/http_common.h"
Gilad Arnold9bedeb52011-11-17 16:19:57 -080020
Alex Deymo80f70ff2016-02-10 16:08:11 -080021#include <cstdlib>
22
Ben Chan05735a12014-09-03 07:48:22 -070023#include <base/macros.h>
Alex Deymo2447c672014-04-02 21:09:10 -070024
25namespace chromeos_update_engine {
Gilad Arnold9bedeb52011-11-17 16:19:57 -080026
Amin Hassanib2689592019-01-13 17:04:28 -080027const char* GetHttpResponseDescription(HttpResponseCode code) {
Gilad Arnold9bedeb52011-11-17 16:19:57 -080028 static const struct {
29 HttpResponseCode code;
30 const char* description;
31 } http_response_table[] = {
Amin Hassanib2689592019-01-13 17:04:28 -080032 {kHttpResponseOk, "OK"},
33 {kHttpResponseCreated, "Created"},
34 {kHttpResponseAccepted, "Accepted"},
35 {kHttpResponseNonAuthInfo, "Non-Authoritative Information"},
36 {kHttpResponseNoContent, "No Content"},
37 {kHttpResponseResetContent, "Reset Content"},
38 {kHttpResponsePartialContent, "Partial Content"},
39 {kHttpResponseMultipleChoices, "Multiple Choices"},
40 {kHttpResponseMovedPermanently, "Moved Permanently"},
41 {kHttpResponseFound, "Found"},
42 {kHttpResponseSeeOther, "See Other"},
43 {kHttpResponseNotModified, "Not Modified"},
44 {kHttpResponseUseProxy, "Use Proxy"},
45 {kHttpResponseTempRedirect, "Temporary Redirect"},
46 {kHttpResponseBadRequest, "Bad Request"},
47 {kHttpResponseUnauth, "Unauthorized"},
48 {kHttpResponseForbidden, "Forbidden"},
49 {kHttpResponseNotFound, "Not Found"},
50 {kHttpResponseRequestTimeout, "Request Timeout"},
51 {kHttpResponseInternalServerError, "Internal Server Error"},
52 {kHttpResponseNotImplemented, "Not Implemented"},
53 {kHttpResponseServiceUnavailable, "Service Unavailable"},
54 {kHttpResponseVersionNotSupported, "HTTP Version Not Supported"},
Gilad Arnold9bedeb52011-11-17 16:19:57 -080055 };
56
57 bool is_found = false;
58 size_t i;
Alex Vakulenko9c155d22014-12-10 12:52:31 -080059 for (i = 0; i < arraysize(http_response_table); i++)
Gilad Arnold9bedeb52011-11-17 16:19:57 -080060 if ((is_found = (http_response_table[i].code == code)))
61 break;
62
63 return (is_found ? http_response_table[i].description : "(unsupported)");
64}
65
Amin Hassanib2689592019-01-13 17:04:28 -080066HttpResponseCode StringToHttpResponseCode(const char* s) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -070067 return static_cast<HttpResponseCode>(strtoul(s, nullptr, 10));
Gilad Arnold9bedeb52011-11-17 16:19:57 -080068}
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -080069
Amin Hassanib2689592019-01-13 17:04:28 -080070const char* GetHttpContentTypeString(HttpContentType type) {
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -080071 static const struct {
72 HttpContentType type;
73 const char* str;
74 } http_content_type_table[] = {
Amin Hassanib2689592019-01-13 17:04:28 -080075 {kHttpContentTypeTextXml, "text/xml"},
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -080076 };
77
78 bool is_found = false;
79 size_t i;
Alex Vakulenko9c155d22014-12-10 12:52:31 -080080 for (i = 0; i < arraysize(http_content_type_table); i++)
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -080081 if ((is_found = (http_content_type_table[i].type == type)))
82 break;
83
Alex Vakulenko88b591f2014-08-28 16:48:57 -070084 return (is_found ? http_content_type_table[i].str : nullptr);
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -080085}
Alex Deymo2447c672014-04-02 21:09:10 -070086
Alex Vakulenkod2779df2014-06-16 13:19:00 -070087} // namespace chromeos_update_engine