1 | /* $Id: http-server.h 87042 2020-12-04 12:10:36Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Header file for HTTP server implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 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_server_h
|
---|
28 | #define IPRT_INCLUDED_http_server_h
|
---|
29 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
30 | # pragma once
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #include <iprt/http-common.h>
|
---|
34 | #include <iprt/types.h>
|
---|
35 | #include <iprt/fs.h>
|
---|
36 |
|
---|
37 | RT_C_DECLS_BEGIN
|
---|
38 |
|
---|
39 | /** @defgroup grp_rt_httpserver RTHttpServer - HTTP server implementation.
|
---|
40 | * @ingroup grp_rt
|
---|
41 | * @{
|
---|
42 | */
|
---|
43 |
|
---|
44 | /** @todo the following three definitions may move the iprt/types.h later. */
|
---|
45 | /** HTTP server handle. */
|
---|
46 | typedef R3PTRTYPE(struct RTHTTPSERVERINTERNAL *) RTHTTPSERVER;
|
---|
47 | /** Pointer to a HTTP server handle. */
|
---|
48 | typedef RTHTTPSERVER *PRTHTTPSERVER;
|
---|
49 | /** Nil HTTP client handle. */
|
---|
50 | #define NIL_RTHTTPSERVER ((RTHTTPSERVER)0)
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Structure for maintaining a HTTP client request.
|
---|
54 | */
|
---|
55 | typedef struct RTHTTPSERVERREQ
|
---|
56 | {
|
---|
57 | /** Request URL. */
|
---|
58 | char *pszUrl;
|
---|
59 | /** Request method. */
|
---|
60 | RTHTTPMETHOD enmMethod;
|
---|
61 | /** Request header list. */
|
---|
62 | RTHTTPHEADERLIST hHdrLst;
|
---|
63 | /** Request body data. */
|
---|
64 | RTHTTPBODY Body;
|
---|
65 | } RTHTTPSERVERREQ;
|
---|
66 | /** Pointer to a HTTP client request. */
|
---|
67 | typedef RTHTTPSERVERREQ *PRTHTTPSERVERREQ;
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Structure for maintaining a HTTP server response.
|
---|
71 | */
|
---|
72 | typedef struct RTHTTPSERVERRESP
|
---|
73 | {
|
---|
74 | /** HTTP status to send. */
|
---|
75 | RTHTTPSTATUS enmSts;
|
---|
76 | /** List of headers to send. */
|
---|
77 | RTHTTPHEADERLIST hHdrLst;
|
---|
78 | /** Body data to send. */
|
---|
79 | RTHTTPBODY Body;
|
---|
80 | } RTHTTPSERVERRESP;
|
---|
81 | /** Pointer to a HTTP server response. */
|
---|
82 | typedef RTHTTPSERVERRESP *PRTHTTPSERVERRESP;
|
---|
83 |
|
---|
84 | RTR3DECL(int) RTHttpServerResponseInitEx(PRTHTTPSERVERRESP pResp, size_t cbBody);
|
---|
85 | RTR3DECL(int) RTHttpServerResponseInit(PRTHTTPSERVERRESP pResp);
|
---|
86 | RTR3DECL(void) RTHttpServerResponseDestroy(PRTHTTPSERVERRESP pResp);
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Structure for maintaining a HTTP server client state.
|
---|
90 | *
|
---|
91 | * Note: The HTTP protocol itself is stateless, but we want to have to possibility to store
|
---|
92 | * some state stuff here nevertheless.
|
---|
93 | */
|
---|
94 | typedef struct RTHTTPSERVERCLIENTSTATE
|
---|
95 | {
|
---|
96 | /** If non-zero, the time (in ms) to keep a client connection alive.
|
---|
97 | * Requested via client header, but set and controlled by the server in the end. */
|
---|
98 | RTMSINTERVAL msKeepAlive;
|
---|
99 | } RTHTTPSERVERCLIENTSTATE;
|
---|
100 | /** Pointer to a FTP server client state. */
|
---|
101 | typedef RTHTTPSERVERCLIENTSTATE *PRTHTTPSERVERCLIENTSTATE;
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Structure for storing HTTP server callback data.
|
---|
105 | */
|
---|
106 | typedef struct RTHTTPCALLBACKDATA
|
---|
107 | {
|
---|
108 | /** Pointer to the client state. */
|
---|
109 | PRTHTTPSERVERCLIENTSTATE pClient;
|
---|
110 | /** Saved user pointer. */
|
---|
111 | void *pvUser;
|
---|
112 | /** Size (in bytes) of data at user pointer. */
|
---|
113 | size_t cbUser;
|
---|
114 | } RTHTTPCALLBACKDATA;
|
---|
115 | /** Pointer to HTTP server callback data. */
|
---|
116 | typedef RTHTTPCALLBACKDATA *PRTHTTPCALLBACKDATA;
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Function callback table for the HTTP server implementation.
|
---|
120 | *
|
---|
121 | * All callbacks are optional and therefore can be NULL.
|
---|
122 | */
|
---|
123 | typedef struct RTHTTPSERVERCALLBACKS
|
---|
124 | {
|
---|
125 | /**
|
---|
126 | * Called before a given URL will be retrieved by the GET method.
|
---|
127 | *
|
---|
128 | * Note: High level function, not being called when pfnOnGetRequest is implemented.
|
---|
129 | *
|
---|
130 | * @returns VBox status code.
|
---|
131 | * @param pData Pointer to HTTP callback data.
|
---|
132 | * @param pReq Pointer to request to handle.
|
---|
133 | * @param ppvHandle Where to return the pointer to the opaque handle used for object identification.
|
---|
134 | */
|
---|
135 | DECLCALLBACKMEMBER(int, pfnOpen,(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq, void **ppvHandle));
|
---|
136 | /**
|
---|
137 | * Called when a given URL will be retrieved by the GET method.
|
---|
138 | *
|
---|
139 | * Note: High level function, not being called when pfnOnGetRequest is implemented.
|
---|
140 | * Note2: Can be called multiple times, based on the body size to send.
|
---|
141 | *
|
---|
142 | * @returns VBox status code.
|
---|
143 | * @param pData Pointer to HTTP callback data.
|
---|
144 | * @param pvHandle Opaque handle for object identification.
|
---|
145 | * @param pvBuf Pointer to buffer where to store the read data.
|
---|
146 | * @param cbBuf Size (in bytes) of the buffer where to store the read data.
|
---|
147 | * @param pcbRead Where to return the amount (in bytes) of read data. Optional and can be NULL.
|
---|
148 | */
|
---|
149 | DECLCALLBACKMEMBER(int, pfnRead,(PRTHTTPCALLBACKDATA pData, void *pvHandle, void *pvBuf, size_t cbBuf, size_t *pcbRead));
|
---|
150 | /**
|
---|
151 | * Called when a given URL is done retrieving by the GET method.
|
---|
152 | *
|
---|
153 | * Note: High level function, not being called when pfnOnGetRequest is implemented.
|
---|
154 | *
|
---|
155 | * @returns VBox status code.
|
---|
156 | * @param pData Pointer to HTTP callback data.
|
---|
157 | * @param pszUrl URL to handle.
|
---|
158 | * @param pvHandle Opaque handle for object identification.
|
---|
159 | */
|
---|
160 | DECLCALLBACKMEMBER(int, pfnClose,(PRTHTTPCALLBACKDATA pData, void *pvHandle));
|
---|
161 | /**
|
---|
162 | * Queries information about a given URL.
|
---|
163 | *
|
---|
164 | * Will be called with GET or HEAD request.
|
---|
165 | *
|
---|
166 | * @returns VBox status code.
|
---|
167 | * @param pData Pointer to HTTP callback data.
|
---|
168 | * @param pReq Pointer to request to handle.
|
---|
169 | * @param pObjInfo Where to store the queried file information on success.
|
---|
170 | * @param ppszMIMEHint Where to return an allocated MIME type hint on success.
|
---|
171 | * Must be free'd by the caller using RTStrFree().
|
---|
172 | */
|
---|
173 | DECLCALLBACKMEMBER(int, pfnQueryInfo,(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq, PRTFSOBJINFO pObjInfo, char **ppszMIMEHint));
|
---|
174 | /**
|
---|
175 | * Low-level handler for a GET method request.
|
---|
176 | *
|
---|
177 | * @returns VBox status code.
|
---|
178 | * @param pData Pointer to HTTP callback data.
|
---|
179 | * @param pReq Pointer to request to handle.
|
---|
180 | */
|
---|
181 | DECLCALLBACKMEMBER(int, pfnOnGetRequest,(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq));
|
---|
182 | /**
|
---|
183 | * Low-level handler for a HEAD method request.
|
---|
184 | *
|
---|
185 | * @returns VBox status code.
|
---|
186 | * @param pData Pointer to HTTP callback data.
|
---|
187 | * @param pReq Pointer to request to handle.
|
---|
188 | */
|
---|
189 | DECLCALLBACKMEMBER(int, pfnOnHeadRequest,(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq));
|
---|
190 | /**
|
---|
191 | * Called before the HTTP server will be destroyed.
|
---|
192 | *
|
---|
193 | * @returns VBox status code.
|
---|
194 | * @param pData Pointer to HTTP callback data.
|
---|
195 | */
|
---|
196 | DECLCALLBACKMEMBER(int, pfnDestroy,(PRTHTTPCALLBACKDATA pData));
|
---|
197 | } RTHTTPSERVERCALLBACKS;
|
---|
198 | /** Pointer to a HTTP server callback data table. */
|
---|
199 | typedef RTHTTPSERVERCALLBACKS *PRTHTTPSERVERCALLBACKS;
|
---|
200 |
|
---|
201 | /** Maximum length (in bytes) a single client request can have. */
|
---|
202 | #define RTHTTPSERVER_MAX_REQ_LEN _8K
|
---|
203 | /** EOL string according to the HTTP 1.1 specs.
|
---|
204 | * See https://tools.ietf.org/html/rfc2616#section-2.2 */
|
---|
205 | #define RTHTTPSERVER_HTTP11_EOL_STR "\r\n"
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Creates a HTTP server instance.
|
---|
209 | *
|
---|
210 | * @returns IPRT status code.
|
---|
211 | * @param phHttpServer Where to store the HTTP server handle.
|
---|
212 | * @param pcszAddress The address for creating a listening socket.
|
---|
213 | * If NULL or empty string the server is bound to all interfaces.
|
---|
214 | * @param uPort The port for creating a listening socket.
|
---|
215 | * @param pCallbacks Callback table to use.
|
---|
216 | * @param pvUser Pointer to user-specific data. Optional.
|
---|
217 | * @param cbUser Size of user-specific data. Optional.
|
---|
218 | */
|
---|
219 | RTR3DECL(int) RTHttpServerCreate(PRTHTTPSERVER phHttpServer, const char *pcszAddress, uint16_t uPort,
|
---|
220 | PRTHTTPSERVERCALLBACKS pCallbacks, void *pvUser, size_t cbUser);
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * Destroys a HTTP server instance.
|
---|
224 | *
|
---|
225 | * @returns IPRT status code.
|
---|
226 | * @param hHttpServer Handle to the HTTP server handle.
|
---|
227 | */
|
---|
228 | RTR3DECL(int) RTHttpServerDestroy(RTHTTPSERVER hHttpServer);
|
---|
229 |
|
---|
230 | /** @} */
|
---|
231 | RT_C_DECLS_END
|
---|
232 |
|
---|
233 | #endif /* !IPRT_INCLUDED_http_server_h */
|
---|
234 |
|
---|