VirtualBox

source: vbox/trunk/include/iprt/http-common.h@ 87012

Last change on this file since 87012 was 87010, checked in by vboxsync, 4 years ago

Shared Clipboard/Transfers: Initial commit for HTTP server. Work in progress [SCM fixes]. bugref:9874

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.8 KB
Line 
1/* $Id: http-common.h 87010 2020-11-27 17:25:54Z 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
36RT_C_DECLS_BEGIN
37
38/** HTTP methods. */
39typedef 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 RTHTTPMETHOD_END,
51 RTHTTPMETHOD_32BIT_HACK = 0x7fffffff
52} RTHTTPMETHOD;
53
54/** HTTP status codes. */
55typedef enum RTHTTPSTATUS
56{
57 RTHTTPSTATUS_INTERNAL_NOT_SET = 0,
58 /**
59 * 2xx - Success / information codes.
60 */
61 RTHTTPSTATUS_OK = 200,
62 RTHTTPSTATUS_CREATED = 201,
63 RTHTTPSTATUS_ACCEPTED = 202,
64 RTHTTPSTATUS_NONAUTHORITATIVEINFORMATION = 203,
65 RTHTTPSTATUS_NOCONTENT = 204,
66 RTHTTPSTATUS_RESETCONTENT = 205,
67 RTHTTPSTATUS_PARTIALCONTENT = 206,
68 RTHTTPSTATUS_MULTISTATUS = 207,
69 RTHTTPSTATUS_ALREADYREPORTED = 208,
70 RTHTTPSTATUS_IMUSED = 226,
71 /**
72 * 4xx - Client error codes.
73 */
74 RTHTTPSTATUS_BADREQUEST = 400,
75 RTHTTPSTATUS_UNAUTHORIZED = 401,
76 RTHTTPSTATUS_PAYMENTREQUIRED = 402,
77 RTHTTPSTATUS_FORBIDDEN = 403,
78 RTHTTPSTATUS_NOTFOUND = 404,
79 RTHTTPSTATUS_METHODNOTALLOWED = 405,
80 RTHTTPSTATUS_NOTACCEPTABLE = 406,
81 RTHTTPSTATUS_PROXYAUTHENTICATIONREQUIRED = 407,
82 RTHTTPSTATUS_REQUESTTIMEOUT = 408,
83 RTHTTPSTATUS_CONFLICT = 409,
84 RTHTTPSTATUS_GONE = 410,
85 RTHTTPSTATUS_LENGTHREQUIRED = 411,
86 RTHTTPSTATUS_PRECONDITIONFAILED = 412,
87 RTHTTPSTATUS_PAYLOADTOOLARGE = 413,
88 RTHTTPSTATUS_URITOOLONG = 414,
89 RTHTTPSTATUS_UNSUPPORTEDMEDIATYPE = 415,
90 RTHTTPSTATUS_RANGENOTSATISFIABLE = 416,
91 RTHTTPSTATUS_EXPECTATIONFAILED = 417,
92 RTHTTPSTATUS_IMATEAPOT = 418,
93 RTHTTPSTATUS_UNPROCESSABLEENTITY = 422,
94 RTHTTPSTATUS_LOCKED = 423,
95 RTHTTPSTATUS_FAILEDDEPENDENCY = 424,
96 RTHTTPSTATUS_UPGRADEREQUIRED = 426,
97 RTHTTPSTATUS_PRECONDITIONREQUIRED = 428,
98 RTHTTPSTATUS_TOOMANYREQUESTS = 429,
99 RTHTTPSTATUS_REQUESTHEADERFIELDSTOOLARGE = 431,
100 RTHTTPSTATUS_UNAVAILABLEFORLEGALREASONS = 451,
101 /**
102 * 5xx - Server error codes.
103 */
104 RTHTTPSTATUS_INTERNALSERVERERROR = 500,
105 RTHTTPSTATUS_NOTIMPLEMENTED = 501,
106 RTHTTPSTATUS_BADGATEWAY = 502,
107 RTHTTPSTATUS_SERVICEUNAVAILABLE = 503,
108 RTHTTPSTATUS_GATEWAYTIMEOUT = 504,
109 RTHTTPSTATUS_HTTPVERSIONNOTSUPPORTED = 505,
110 RTHTTPSTATUS_VARIANTALSONEGOTIATES = 506,
111 RTHTTPSTATUS_INSUFFICIENTSTORAGE = 507,
112 RTHTTPSTATUS_LOOPDETECTED = 508,
113 RTHTTPSTATUS_NOTEXTENDED = 510,
114 RTHTTPSTATUS_NETWORKAUTHENTICATIONREQUIRED = 511,
115
116 RTHTTPSTATUS_32BIT_HACK = 0x7fffffff
117} RTHTTPSTATUS;
118
119/** Checks whether a HTTP status is of type "informational" or not. */
120#define RTHTTPSTATUS_IS_INFO(a_Code) (a_Code >= 100 && a_Code < 200)
121/** Checks whether a HTTP status indicates success or not. */
122#define RTHTTPSTATUS_IS_OK(a_Code) (a_Code >= 200 && a_Code < 300)
123/** Checks whether a HTTP status indicates a redirection or not. */
124#define RTHTTPSTATUS_IS_REDIRECT(a_Code) (a_Code >= 300 && a_Code < 400)
125/** Checks whether a HTTP status indicates a client error or not. */
126#define RTHTTPSTATUS_IS_CLIENTERROR(a_Code) (a_Code >= 400 && a_Code < 500)
127/** Checks whether a HTTP status indicates a server error or not. */
128#define RTHTTPSTATUS_IS_SERVERERROR(a_Code) (a_Code >= 500 && a_Code < 600)
129/** Checks whether a HTTP status indicates an error or not. */
130#define RTHTTPSTATUS_IS_ERROR(a_Code) (a_Code >= 400)
131
132/** Specifies a HTTP MIME type. */
133typedef uint32_t RTHTTPMIMETYPE;
134
135#define RTHTTPMIMETYPE_TEXT_PLAIN "text/plain"
136#define RTHTTPMIMETYPE_APPLICATION_OCTET_STREAM "application/octet-stream"
137
138/** Specifies HTTP version 1.1 as a string. */
139#define RTHTTPVER_1_1_STR "HTTP/1.1"
140
141/** @todo the following three definitions may move the iprt/types.h later. */
142/** HTTP header list handle. */
143typedef R3PTRTYPE(struct RTHTTPHEADERLISTINTERNAL *) RTHTTPHEADERLIST;
144/** Pointer to a HTTP header list handle. */
145typedef RTHTTPHEADERLIST *PRTHTTPHEADERLIST;
146/** Nil HTTP HTTP header list handle. */
147#define NIL_RTHTTPHEADERLIST ((RTHTTPHEADERLIST)0)
148
149/**
150 * HTTP header list entry.
151 */
152typedef struct RTHTTPHEADERENTRY
153{
154 /** The list node. */
155 RTLISTNODE Node;
156 /** The field name length. */
157 uint32_t cchName;
158 /** The value offset. */
159 uint32_t offValue;
160 /** The full header field. */
161 RT_FLEXIBLE_ARRAY_EXTENSION
162 RT_GCC_EXTENSION char szData[RT_FLEXIBLE_ARRAY];
163} RTHTTPHEADERENTRY;
164/** Pointer to a HTTP header. */
165typedef RTHTTPHEADERENTRY *PRTHTTPHEADERENTRY;
166
167/**
168 * Returns the name of the HTTP method.
169 * @returns Read only string.
170 * @param enmMethod The HTTP method to name.
171 */
172RTR3DECL(const char *) RTHttpMethodToStr(RTHTTPMETHOD enmMethod);
173
174RTR3DECL(const char *) RTHttpStatusToStr(RTHTTPSTATUS enmSts);
175
176RTR3DECL(int) RTHttpHeaderListInit(PRTHTTPHEADERLIST hHdrList);
177
178RTR3DECL(void) RTHttpHeaderListDestroy(RTHTTPHEADERLIST hHdrList);
179
180/**
181 * Set custom raw headers.
182 *
183 * @returns IPRT status code.
184 * @param hHdrLst The HTTP header list handle.
185 * @param cHeaders Number of custom headers.
186 * @param papszHeaders Array of headers in form "foo: bar".
187 */
188RTR3DECL(int) RTHttpHeaderListSet(RTHTTPHEADERLIST hHdrLst, size_t cHeaders, const char * const *papszHeaders);
189
190/** @name RTHTTPHEADERLISTADD_F_XXX - Flags for RTHttpHeaderListAddRaw and RTHttpHeaderListAdd
191 * @{ */
192#define RTHTTPHEADERLISTADD_F_BACK UINT32_C(0) /**< Append the header. */
193#define RTHTTPHEADERLISTADD_F_FRONT UINT32_C(1) /**< Prepend the header. */
194/** @} */
195
196/**
197 * Adds a raw header.
198 *
199 * @returns IPRT status code.
200 * @param hHdrLst The HTTP header list handle.
201 * @param pszHeader Header string on the form "foo: bar".
202 * @param fFlags RTHTTPADDHDR_F_FRONT or RTHTTPADDHDR_F_BACK.
203 */
204RTR3DECL(int) RTHttpHeaderListAddRaw(RTHTTPHEADERLIST hHdrLst, const char *pszHeader, uint32_t fFlags);
205
206/**
207 * Adds a header field and value.
208 *
209 * @returns IPRT status code.
210 * @param hHdrLst The HTTP header list handle.
211 * @param pszField The header field name.
212 * @param pszValue The header field value.
213 * @param cchValue The value length or RTSTR_MAX.
214 * @param fFlags Only RTHTTPADDHDR_F_FRONT or RTHTTPADDHDR_F_BACK,
215 * may be extended with encoding controlling flags if
216 * needed later.
217 */
218RTR3DECL(int) RTHttpHeaderListAdd(RTHTTPHEADERLIST hHdrLst, const char *pszField, const char *pszValue, size_t cchValue, uint32_t fFlags);
219
220/**
221 * Gets a header previously added using RTHttpSetHeaders, RTHttpAppendRawHeader
222 * or RTHttpAppendHeader.
223 *
224 * @returns Pointer to the header value on if found, otherwise NULL.
225 * @param hHdrLst The HTTP header list handle.
226 * @param pszField The field name (no colon).
227 * @param cchField The length of the field name or RTSTR_MAX.
228 */
229RTR3DECL(const char *) RTHttpHeaderListGet(RTHTTPHEADERLIST hHdrLst, const char *pszField, size_t cchField);
230
231/**
232 * Gets the number of headers specified by RTHttpAddHeader, RTHttpAddRawHeader or RTHttpSetHeaders.
233 *
234 * @returns Number of headers.
235 * @param hHdrLst The HTTP header list handle.
236 * @note This can be slow and is only really intended for test cases and debugging!
237 */
238RTR3DECL(size_t) RTHttpHeaderListGetCount(RTHTTPHEADERLIST hHdrLst);
239
240/**
241 * Gets a header by ordinal.
242 *
243 * Can be used together with RTHttpGetHeaderCount by test case and debug code to
244 * iterate headers specified by RTHttpAddHeader, RTHttpAddRawHeader or RTHttpSetHeaders.
245 *
246 * @returns The header string ("field: value").
247 * @param hHdrLst The HTTP header list handle.
248 * @param iOrdinal The number of the header to get.
249 * @note This can be slow and is only really intended for test cases and debugging!
250 */
251RTR3DECL(const char *) RTHttpHeaderListGetByOrdinal(RTHTTPHEADERLIST hHdrLst, size_t iOrdinal);
252
253RT_C_DECLS_END
254
255#endif /* !IPRT_INCLUDED_http_common_h */
256
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette