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