1 | /* $Id: http-common.h 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Common (client / server) HTTP API.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 | #ifndef IPRT_INCLUDED_http_common_h
|
---|
38 | #define IPRT_INCLUDED_http_common_h
|
---|
39 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
40 | # pragma once
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #include <iprt/list.h>
|
---|
44 | #include <iprt/types.h>
|
---|
45 |
|
---|
46 | RT_C_DECLS_BEGIN
|
---|
47 |
|
---|
48 | /** HTTP methods. */
|
---|
49 | typedef enum RTHTTPMETHOD
|
---|
50 | {
|
---|
51 | RTHTTPMETHOD_INVALID = 0,
|
---|
52 | RTHTTPMETHOD_GET,
|
---|
53 | RTHTTPMETHOD_PUT,
|
---|
54 | RTHTTPMETHOD_POST,
|
---|
55 | RTHTTPMETHOD_PATCH,
|
---|
56 | RTHTTPMETHOD_DELETE,
|
---|
57 | RTHTTPMETHOD_HEAD,
|
---|
58 | RTHTTPMETHOD_OPTIONS,
|
---|
59 | RTHTTPMETHOD_TRACE,
|
---|
60 | #ifdef IPRT_HTTP_WITH_WEBDAV
|
---|
61 | RTHTTPMETHOD_PROPFIND,
|
---|
62 | #endif
|
---|
63 | RTHTTPMETHOD_END,
|
---|
64 | RTHTTPMETHOD_32BIT_HACK = 0x7fffffff
|
---|
65 | } RTHTTPMETHOD;
|
---|
66 |
|
---|
67 | /** HTTP status codes. */
|
---|
68 | typedef enum RTHTTPSTATUS
|
---|
69 | {
|
---|
70 | RTHTTPSTATUS_INTERNAL_NOT_SET = 0,
|
---|
71 | /**
|
---|
72 | * 2xx - Success / information codes.
|
---|
73 | */
|
---|
74 | RTHTTPSTATUS_OK = 200,
|
---|
75 | RTHTTPSTATUS_CREATED = 201,
|
---|
76 | RTHTTPSTATUS_ACCEPTED = 202,
|
---|
77 | RTHTTPSTATUS_NONAUTHORITATIVEINFORMATION = 203,
|
---|
78 | RTHTTPSTATUS_NOCONTENT = 204,
|
---|
79 | RTHTTPSTATUS_RESETCONTENT = 205,
|
---|
80 | RTHTTPSTATUS_PARTIALCONTENT = 206,
|
---|
81 | RTHTTPSTATUS_MULTISTATUS = 207,
|
---|
82 | RTHTTPSTATUS_ALREADYREPORTED = 208,
|
---|
83 | RTHTTPSTATUS_IMUSED = 226,
|
---|
84 | /**
|
---|
85 | * 4xx - Client error codes.
|
---|
86 | */
|
---|
87 | RTHTTPSTATUS_BADREQUEST = 400,
|
---|
88 | RTHTTPSTATUS_UNAUTHORIZED = 401,
|
---|
89 | RTHTTPSTATUS_PAYMENTREQUIRED = 402,
|
---|
90 | RTHTTPSTATUS_FORBIDDEN = 403,
|
---|
91 | RTHTTPSTATUS_NOTFOUND = 404,
|
---|
92 | RTHTTPSTATUS_METHODNOTALLOWED = 405,
|
---|
93 | RTHTTPSTATUS_NOTACCEPTABLE = 406,
|
---|
94 | RTHTTPSTATUS_PROXYAUTHENTICATIONREQUIRED = 407,
|
---|
95 | RTHTTPSTATUS_REQUESTTIMEOUT = 408,
|
---|
96 | RTHTTPSTATUS_CONFLICT = 409,
|
---|
97 | RTHTTPSTATUS_GONE = 410,
|
---|
98 | RTHTTPSTATUS_LENGTHREQUIRED = 411,
|
---|
99 | RTHTTPSTATUS_PRECONDITIONFAILED = 412,
|
---|
100 | RTHTTPSTATUS_PAYLOADTOOLARGE = 413,
|
---|
101 | RTHTTPSTATUS_URITOOLONG = 414,
|
---|
102 | RTHTTPSTATUS_UNSUPPORTEDMEDIATYPE = 415,
|
---|
103 | RTHTTPSTATUS_RANGENOTSATISFIABLE = 416,
|
---|
104 | RTHTTPSTATUS_EXPECTATIONFAILED = 417,
|
---|
105 | RTHTTPSTATUS_IMATEAPOT = 418,
|
---|
106 | RTHTTPSTATUS_UNPROCESSABLEENTITY = 422,
|
---|
107 | RTHTTPSTATUS_LOCKED = 423,
|
---|
108 | RTHTTPSTATUS_FAILEDDEPENDENCY = 424,
|
---|
109 | RTHTTPSTATUS_UPGRADEREQUIRED = 426,
|
---|
110 | RTHTTPSTATUS_PRECONDITIONREQUIRED = 428,
|
---|
111 | RTHTTPSTATUS_TOOMANYREQUESTS = 429,
|
---|
112 | RTHTTPSTATUS_REQUESTHEADERFIELDSTOOLARGE = 431,
|
---|
113 | RTHTTPSTATUS_UNAVAILABLEFORLEGALREASONS = 451,
|
---|
114 | /**
|
---|
115 | * 5xx - Server error codes.
|
---|
116 | */
|
---|
117 | RTHTTPSTATUS_INTERNALSERVERERROR = 500,
|
---|
118 | RTHTTPSTATUS_NOTIMPLEMENTED = 501,
|
---|
119 | RTHTTPSTATUS_BADGATEWAY = 502,
|
---|
120 | RTHTTPSTATUS_SERVICEUNAVAILABLE = 503,
|
---|
121 | RTHTTPSTATUS_GATEWAYTIMEOUT = 504,
|
---|
122 | RTHTTPSTATUS_HTTPVERSIONNOTSUPPORTED = 505,
|
---|
123 | RTHTTPSTATUS_VARIANTALSONEGOTIATES = 506,
|
---|
124 | RTHTTPSTATUS_INSUFFICIENTSTORAGE = 507,
|
---|
125 | RTHTTPSTATUS_LOOPDETECTED = 508,
|
---|
126 | RTHTTPSTATUS_NOTEXTENDED = 510,
|
---|
127 | RTHTTPSTATUS_NETWORKAUTHENTICATIONREQUIRED = 511,
|
---|
128 |
|
---|
129 | RTHTTPSTATUS_32BIT_HACK = 0x7fffffff
|
---|
130 | } RTHTTPSTATUS;
|
---|
131 |
|
---|
132 | /** Checks whether a HTTP status is of type "informational" or not. */
|
---|
133 | #define RTHTTPSTATUS_IS_INFO(a_Code) (a_Code >= 100 && a_Code < 200)
|
---|
134 | /** Checks whether a HTTP status indicates success or not. */
|
---|
135 | #define RTHTTPSTATUS_IS_OK(a_Code) (a_Code >= 200 && a_Code < 300)
|
---|
136 | /** Checks whether a HTTP status indicates a redirection or not. */
|
---|
137 | #define RTHTTPSTATUS_IS_REDIRECT(a_Code) (a_Code >= 300 && a_Code < 400)
|
---|
138 | /** Checks whether a HTTP status indicates a client error or not. */
|
---|
139 | #define RTHTTPSTATUS_IS_CLIENTERROR(a_Code) (a_Code >= 400 && a_Code < 500)
|
---|
140 | /** Checks whether a HTTP status indicates a server error or not. */
|
---|
141 | #define RTHTTPSTATUS_IS_SERVERERROR(a_Code) (a_Code >= 500 && a_Code < 600)
|
---|
142 | /** Checks whether a HTTP status indicates an error or not. */
|
---|
143 | #define RTHTTPSTATUS_IS_ERROR(a_Code) (a_Code >= 400)
|
---|
144 |
|
---|
145 | /** Specifies a HTTP MIME type. */
|
---|
146 | typedef uint32_t RTHTTPMIMETYPE;
|
---|
147 |
|
---|
148 | #define RTHTTPMIMETYPE_TEXT_PLAIN "text/plain"
|
---|
149 | #define RTHTTPMIMETYPE_APPLICATION_OCTET_STREAM "application/octet-stream"
|
---|
150 |
|
---|
151 | /** Specifies HTTP version 1.1 as a string. */
|
---|
152 | #define RTHTTPVER_1_1_STR "HTTP/1.1"
|
---|
153 |
|
---|
154 | /** @todo the following three definitions may move the iprt/types.h later. */
|
---|
155 | /** HTTP header list handle. */
|
---|
156 | typedef R3PTRTYPE(struct RTHTTPHEADERLISTINTERNAL *) RTHTTPHEADERLIST;
|
---|
157 | /** Pointer to a HTTP header list handle. */
|
---|
158 | typedef RTHTTPHEADERLIST *PRTHTTPHEADERLIST;
|
---|
159 | /** Nil HTTP HTTP header list handle. */
|
---|
160 | #define NIL_RTHTTPHEADERLIST ((RTHTTPHEADERLIST)0)
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * HTTP header list entry.
|
---|
164 | */
|
---|
165 | typedef struct RTHTTPHEADERENTRY
|
---|
166 | {
|
---|
167 | /** The list node. */
|
---|
168 | RTLISTNODE Node;
|
---|
169 | /** The field name length. */
|
---|
170 | uint32_t cchName;
|
---|
171 | /** The value offset. */
|
---|
172 | uint32_t offValue;
|
---|
173 | /** The full header field. */
|
---|
174 | RT_FLEXIBLE_ARRAY_EXTENSION
|
---|
175 | RT_GCC_EXTENSION char szData[RT_FLEXIBLE_ARRAY];
|
---|
176 | } RTHTTPHEADERENTRY;
|
---|
177 | /** Pointer to a HTTP header. */
|
---|
178 | typedef RTHTTPHEADERENTRY *PRTHTTPHEADERENTRY;
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Structure for maintaining a HTTP body.
|
---|
182 | */
|
---|
183 | typedef struct RTHTTPBODY
|
---|
184 | {
|
---|
185 | /** Body to send, if any. Can be NULL. */
|
---|
186 | void *pvBody;
|
---|
187 | /** Body allocation size (in bytes). */
|
---|
188 | size_t cbBodyAlloc;
|
---|
189 | /** How much body data is being used (in bytes). */
|
---|
190 | size_t cbBodyUsed;
|
---|
191 | /** Current body data read/write offset (in bytes). */
|
---|
192 | size_t offBody;
|
---|
193 | } RTHTTPBODY;
|
---|
194 | /** Pointer to a HTTP body. */
|
---|
195 | typedef RTHTTPBODY *PRTHTTPBODY;
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Returns the name of the HTTP method.
|
---|
199 | * @returns Read only string.
|
---|
200 | * @param enmMethod The HTTP method to name.
|
---|
201 | */
|
---|
202 | RTR3DECL(const char *) RTHttpMethodToStr(RTHTTPMETHOD enmMethod);
|
---|
203 |
|
---|
204 | RTR3DECL(const char *) RTHttpStatusToStr(RTHTTPSTATUS enmSts);
|
---|
205 |
|
---|
206 | RTR3DECL(int) RTHttpHeaderListInit(PRTHTTPHEADERLIST hHdrList);
|
---|
207 |
|
---|
208 | RTR3DECL(void) RTHttpHeaderListDestroy(RTHTTPHEADERLIST hHdrList);
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Set custom raw headers.
|
---|
212 | *
|
---|
213 | * @returns IPRT status code.
|
---|
214 | * @param hHdrLst The HTTP header list handle.
|
---|
215 | * @param cHeaders Number of custom headers.
|
---|
216 | * @param papszHeaders Array of headers in form "foo: bar".
|
---|
217 | */
|
---|
218 | RTR3DECL(int) RTHttpHeaderListSet(RTHTTPHEADERLIST hHdrLst, size_t cHeaders, const char * const *papszHeaders);
|
---|
219 |
|
---|
220 | /** @name RTHTTPHEADERLISTADD_F_XXX - Flags for RTHttpHeaderListAddRaw and RTHttpHeaderListAdd
|
---|
221 | * @{ */
|
---|
222 | #define RTHTTPHEADERLISTADD_F_BACK UINT32_C(0) /**< Append the header. */
|
---|
223 | #define RTHTTPHEADERLISTADD_F_FRONT UINT32_C(1) /**< Prepend the header. */
|
---|
224 | /** @} */
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Adds a raw header.
|
---|
228 | *
|
---|
229 | * @returns IPRT status code.
|
---|
230 | * @param hHdrLst The HTTP header list handle.
|
---|
231 | * @param pszHeader Header string on the form "foo: bar".
|
---|
232 | * @param fFlags RTHTTPADDHDR_F_FRONT or RTHTTPADDHDR_F_BACK.
|
---|
233 | */
|
---|
234 | RTR3DECL(int) RTHttpHeaderListAddRaw(RTHTTPHEADERLIST hHdrLst, const char *pszHeader, uint32_t fFlags);
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Adds a header field and value.
|
---|
238 | *
|
---|
239 | * @returns IPRT status code.
|
---|
240 | * @param hHdrLst The HTTP header list handle.
|
---|
241 | * @param pszField The header field name.
|
---|
242 | * @param pszValue The header field value.
|
---|
243 | * @param cchValue The value length or RTSTR_MAX.
|
---|
244 | * @param fFlags Only RTHTTPADDHDR_F_FRONT or RTHTTPADDHDR_F_BACK,
|
---|
245 | * may be extended with encoding controlling flags if
|
---|
246 | * needed later.
|
---|
247 | */
|
---|
248 | RTR3DECL(int) RTHttpHeaderListAdd(RTHTTPHEADERLIST hHdrLst, const char *pszField, const char *pszValue, size_t cchValue, uint32_t fFlags);
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Gets a header previously added using RTHttpSetHeaders, RTHttpAppendRawHeader
|
---|
252 | * or RTHttpAppendHeader.
|
---|
253 | *
|
---|
254 | * @returns Pointer to the header value on if found, otherwise NULL.
|
---|
255 | * @param hHdrLst The HTTP header list handle.
|
---|
256 | * @param pszField The field name (no colon).
|
---|
257 | * @param cchField The length of the field name or RTSTR_MAX.
|
---|
258 | */
|
---|
259 | RTR3DECL(const char *) RTHttpHeaderListGet(RTHTTPHEADERLIST hHdrLst, const char *pszField, size_t cchField);
|
---|
260 |
|
---|
261 | /**
|
---|
262 | * Gets the number of headers specified by RTHttpAddHeader, RTHttpAddRawHeader or RTHttpSetHeaders.
|
---|
263 | *
|
---|
264 | * @returns Number of headers.
|
---|
265 | * @param hHdrLst The HTTP header list handle.
|
---|
266 | * @note This can be slow and is only really intended for test cases and debugging!
|
---|
267 | */
|
---|
268 | RTR3DECL(size_t) RTHttpHeaderListGetCount(RTHTTPHEADERLIST hHdrLst);
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Gets a header by ordinal.
|
---|
272 | *
|
---|
273 | * Can be used together with RTHttpGetHeaderCount by test case and debug code to
|
---|
274 | * iterate headers specified by RTHttpAddHeader, RTHttpAddRawHeader or RTHttpSetHeaders.
|
---|
275 | *
|
---|
276 | * @returns The header string ("field: value").
|
---|
277 | * @param hHdrLst The HTTP header list handle.
|
---|
278 | * @param iOrdinal The number of the header to get.
|
---|
279 | * @note This can be slow and is only really intended for test cases and debugging!
|
---|
280 | */
|
---|
281 | RTR3DECL(const char *) RTHttpHeaderListGetByOrdinal(RTHTTPHEADERLIST hHdrLst, size_t iOrdinal);
|
---|
282 |
|
---|
283 | RT_C_DECLS_END
|
---|
284 |
|
---|
285 | #endif /* !IPRT_INCLUDED_http_common_h */
|
---|
286 |
|
---|