blob: 7737f4c4e61e176bec9da44bfeec46dc91cec4bd [file] [log] [blame]
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001// 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 Arnold9bedeb52011-11-17 16:19:57 -08007#include "update_engine/http_common.h"
8
Ben Chan05735a12014-09-03 07:48:22 -07009#include <base/macros.h>
Alex Deymo2447c672014-04-02 21:09:10 -070010
11namespace chromeos_update_engine {
Gilad Arnold9bedeb52011-11-17 16:19:57 -080012
13const 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 Vakulenko9c155d22014-12-10 12:52:31 -080045 for (i = 0; i < arraysize(http_response_table); i++)
Gilad Arnold9bedeb52011-11-17 16:19:57 -080046 if ((is_found = (http_response_table[i].code == code)))
47 break;
48
49 return (is_found ? http_response_table[i].description : "(unsupported)");
50}
51
52HttpResponseCode StringToHttpResponseCode(const char *s) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -070053 return static_cast<HttpResponseCode>(strtoul(s, nullptr, 10));
Gilad Arnold9bedeb52011-11-17 16:19:57 -080054}
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -080055
56
57const 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 Vakulenko9c155d22014-12-10 12:52:31 -080067 for (i = 0; i < arraysize(http_content_type_table); i++)
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -080068 if ((is_found = (http_content_type_table[i].type == type)))
69 break;
70
Alex Vakulenko88b591f2014-08-28 16:48:57 -070071 return (is_found ? http_content_type_table[i].str : nullptr);
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -080072}
Alex Deymo2447c672014-04-02 21:09:10 -070073
Alex Vakulenkod2779df2014-06-16 13:19:00 -070074} // namespace chromeos_update_engine