UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
statcodes.cpp
Go to the documentation of this file.
1/*******************************************************************************
2 *
3 * Copyright (c) 2000-2003 Intel Corporation
4 * All rights reserved.
5 * Copyright (c) 2012 France Telecom All rights reserved.
6 * Copyright (C) 2021+ GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
7 * Redistribution only with this Copyright remark. Last modified: 2025-05-29
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * - Neither name of Intel Corporation nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
29 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 ******************************************************************************/
42#include <statcodes.hpp>
43#include <upnp.hpp>
44
46#include <string.h>
48
49namespace {
50
52constexpr int NUM_1XX_CODES{2};
53const char* Http1xxCodes[NUM_1XX_CODES];
54const char* Http1xxStr = "Continue\0"
55 "Switching Protocols\0";
56
57constexpr int NUM_2XX_CODES{7};
58const char* Http2xxCodes[NUM_2XX_CODES];
59const char* Http2xxStr = "OK\0"
60 "Created\0"
61 "Accepted\0"
62 "Non-Authoratative Information\0"
63 "No Content\0"
64 "Reset Content\0"
65 "Partial Content\0";
66
67constexpr int NUM_3XX_CODES{8};
68const char* Http3xxCodes[NUM_3XX_CODES];
69const char* Http3xxStr = "Multiple Choices\0"
70 "Moved Permanently\0"
71 "Found\0"
72 "See Other\0"
73 "Not Modified\0"
74 "Use Proxy\0"
75 "\0"
76 "Temporary Redirect\0";
77
78constexpr int NUM_4XX_CODES{18};
79const char* Http4xxCodes[NUM_4XX_CODES];
80const char* Http4xxStr = "Bad Request\0"
81 "Unauthorized\0"
82 "Payment Required\0"
83 "Forbidden\0"
84 "Not Found\0"
85 "Method Not Allowed\0"
86 "Not Acceptable\0"
87 "Proxy Authentication Required\0"
88 "Request Timeout\0"
89 "Conflict\0"
90 "Gone\0"
91 "Length Required\0"
92 "Precondition Failed\0"
93 "Request Entity Too Large\0"
94 "Request-URI Too Long\0"
95 "Unsupported Media Type\0"
96 "Requested Range Not Satisfiable\0"
97 "Expectation Failed\0";
98
99constexpr int NUM_5XX_CODES{11};
100const char* Http5xxCodes[NUM_5XX_CODES];
101const char* Http5xxStr = "Internal Server Error\0"
102 "Not Implemented\0"
103 "Bad Gateway\0"
104 "Service Unavailable\0"
105 "Gateway Timeout\0"
106 "HTTP Version Not Supported\0"
107 "Variant Also Negotiates\0"
108 "Insufficient Storage\0"
109 "Loop Detected\0"
110 "\0"
111 "Not Extended\0";
112
113bool gInitialized{false};
115
127inline void init_table( //
128 const char* encoded_str,
129 const char* table[],
131 int tbl_size
132) {
133 int i;
134 const char* s = encoded_str;
135
136 for (i = 0; i < tbl_size; i++) {
137 table[i] = s;
138 s += strlen(s) + (size_t)1; /* next entry */
139 }
140}
141
146inline void init_tables() {
147 init_table(Http1xxStr, Http1xxCodes, NUM_1XX_CODES);
148 init_table(Http2xxStr, Http2xxCodes, NUM_2XX_CODES);
149 init_table(Http3xxStr, Http3xxCodes, NUM_3XX_CODES);
150 init_table(Http4xxStr, Http4xxCodes, NUM_4XX_CODES);
151 init_table(Http5xxStr, Http5xxCodes, NUM_5XX_CODES);
152
153 gInitialized = true; /* mark only after complete */
154}
155
157} // anonymous namespace
158
159
167 int statusCode
169) {
170 int index;
171 int table_num;
172
173 if (!gInitialized) {
174 init_tables();
175 }
176
177 if (statusCode < 100 || statusCode >= 600) {
178 return NULL;
179 }
180
181 index = statusCode % 100;
182 table_num = statusCode / 100;
183
184 if (table_num == 1 && index < NUM_1XX_CODES) {
185 return Http1xxCodes[index];
186 }
187
188 if (table_num == 2 && index < NUM_2XX_CODES) {
189 return Http2xxCodes[index];
190 }
191
192 if (table_num == 3 && index < NUM_3XX_CODES) {
193 return Http3xxCodes[index];
194 }
195
196 if (table_num == 4 && index < NUM_4XX_CODES) {
197 return Http4xxCodes[index];
198 }
199
200 if (table_num == 5 && index < NUM_5XX_CODES) {
201 return Http5xxCodes[index];
202 }
203
204 return NULL;
205}
void init_tables()
Initializing tables with HTTP strings and different HTTP codes.
void init_table(const char *encoded_str, const char *table[], int tbl_size)
Initializing table representing an array of string pointers.
const char * http_get_code_text(int statusCode)
Return the right status message based on the passed in int statusCode input parameter.
HTTP status codes.