blob: bf4e69d5d58927cd3d99daf15d0e35e90920420b [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
7#include "base/basictypes.h"
8
9#include "update_engine/http_common.h"
10
11
12const char *GetHttpResponseDescription(HttpResponseCode code) {
13 static const struct {
14 HttpResponseCode code;
15 const char* description;
16 } http_response_table[] = {
17 { kHttpResponseOk, "OK" },
18 { kHttpResponseCreated, "Created" },
19 { kHttpResponseAccepted, "Accepted" },
20 { kHttpResponseNonAuthInfo, "Non-Authoritative Information" },
21 { kHttpResponseNoContent, "No Content" },
22 { kHttpResponseResetContent, "Reset Content" },
23 { kHttpResponsePartialContent, "Partial Content" },
24 { kHttpResponseMultipleChoices, "Multiple Choices" },
25 { kHttpResponseMovedPermanently, "Moved Permanently" },
26 { kHttpResponseFound, "Found" },
27 { kHttpResponseSeeOther, "See Other" },
28 { kHttpResponseNotModified, "Not Modified" },
29 { kHttpResponseUseProxy, "Use Proxy" },
30 { kHttpResponseTempRedirect, "Temporary Redirect" },
31 { kHttpResponseBadRequest, "Bad Request" },
32 { kHttpResponseUnauth, "Unauthorized" },
33 { kHttpResponseForbidden, "Forbidden" },
34 { kHttpResponseNotFound, "Not Found" },
35 { kHttpResponseRequestTimeout, "Request Timeout" },
36 { kHttpResponseInternalServerError, "Internal Server Error" },
37 { kHttpResponseNotImplemented, "Not Implemented" },
38 { kHttpResponseServiceUnavailable, "Service Unavailable" },
39 { kHttpResponseVersionNotSupported, "HTTP Version Not Supported" },
40 };
41
42 bool is_found = false;
43 size_t i;
44 for (i = 0; i < ARRAYSIZE_UNSAFE(http_response_table); i++)
45 if ((is_found = (http_response_table[i].code == code)))
46 break;
47
48 return (is_found ? http_response_table[i].description : "(unsupported)");
49}
50
51HttpResponseCode StringToHttpResponseCode(const char *s) {
52 return static_cast<HttpResponseCode>(strtoul(s, NULL, 10));
53}
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -080054
55
56const char *GetHttpContentTypeString(HttpContentType type) {
57 static const struct {
58 HttpContentType type;
59 const char* str;
60 } http_content_type_table[] = {
61 { kHttpContentTypeTextXml, "text/xml" },
62 };
63
64 bool is_found = false;
65 size_t i;
66 for (i = 0; i < ARRAYSIZE_UNSAFE(http_content_type_table); i++)
67 if ((is_found = (http_content_type_table[i].type == type)))
68 break;
69
70 return (is_found ? http_content_type_table[i].str : NULL);
71}