1 | /* $Id: http-server.h 87032 2020-12-02 16:33:29Z 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 | char *pszUrl;
|
---|
58 | RTHTTPMETHOD enmMethod;
|
---|
59 | RTHTTPHEADERLIST hHdrLst;
|
---|
60 | void *pvBody;
|
---|
61 | size_t cbBodyAlloc;
|
---|
62 | size_t cbBodyUsed;
|
---|
63 | } RTHTTPSERVERREQ;
|
---|
64 | /** Pointer to a HTTP client request. */
|
---|
65 | typedef RTHTTPSERVERREQ *PRTHTTPSERVERREQ;
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Structure for maintaining a HTTP server response.
|
---|
69 | */
|
---|
70 | typedef struct RTHTTPSERVERRESP
|
---|
71 | {
|
---|
72 | RTHTTPSTATUS enmSts;
|
---|
73 | RTHTTPHEADERLIST hHdrLst;
|
---|
74 | void *pvBody;
|
---|
75 | size_t cbBodyAlloc;
|
---|
76 | size_t cbBodyUsed;
|
---|
77 | } RTHTTPSERVERRESP;
|
---|
78 | /** Pointer to a HTTP server response. */
|
---|
79 | typedef RTHTTPSERVERRESP *PRTHTTPSERVERRESP;
|
---|
80 |
|
---|
81 | RTR3DECL(int) RTHttpServerResponseInitEx(PRTHTTPSERVERRESP pResp, size_t cbBody);
|
---|
82 | RTR3DECL(int) RTHttpServerResponseInit(PRTHTTPSERVERRESP pResp);
|
---|
83 | RTR3DECL(void) RTHttpServerResponseDestroy(PRTHTTPSERVERRESP pResp);
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Structure for maintaining a HTTP server client state.
|
---|
87 | *
|
---|
88 | * Note: The HTTP protocol itself is stateless, but we want to have to possibility to store
|
---|
89 | * some state stuff here nevertheless.
|
---|
90 | */
|
---|
91 | typedef struct RTHTTPSERVERCLIENTSTATE
|
---|
92 | {
|
---|
93 | uint32_t fUnused;
|
---|
94 | } RTHTTPSERVERCLIENTSTATE;
|
---|
95 | /** Pointer to a FTP server client state. */
|
---|
96 | typedef RTHTTPSERVERCLIENTSTATE *PRTHTTPSERVERCLIENTSTATE;
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Structure for storing HTTP server callback data.
|
---|
100 | */
|
---|
101 | typedef struct RTHTTPCALLBACKDATA
|
---|
102 | {
|
---|
103 | /** Pointer to the client state. */
|
---|
104 | PRTHTTPSERVERCLIENTSTATE pClient;
|
---|
105 | /** Saved user pointer. */
|
---|
106 | void *pvUser;
|
---|
107 | /** Size (in bytes) of data at user pointer. */
|
---|
108 | size_t cbUser;
|
---|
109 | } RTHTTPCALLBACKDATA;
|
---|
110 | /** Pointer to HTTP server callback data. */
|
---|
111 | typedef RTHTTPCALLBACKDATA *PRTHTTPCALLBACKDATA;
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Function callback table for the HTTP server implementation.
|
---|
115 | *
|
---|
116 | * All callbacks are optional and therefore can be NULL.
|
---|
117 | */
|
---|
118 | typedef struct RTHTTPSERVERCALLBACKS
|
---|
119 | {
|
---|
120 | /**
|
---|
121 | * Called before a given URL will be retrieved by the GET method.
|
---|
122 | *
|
---|
123 | * Note: High level function, not being called when pfnOnGetRequest is implemented.
|
---|
124 | *
|
---|
125 | * @returns VBox status code.
|
---|
126 | * @param pData Pointer to HTTP callback data.
|
---|
127 | * @param pReq Pointer to request to handle.
|
---|
128 | * @param ppvHandle Where to return the pointer to the opaque handle used for object identification.
|
---|
129 | */
|
---|
130 | DECLCALLBACKMEMBER(int, pfnOpen,(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq, void **ppvHandle));
|
---|
131 | /**
|
---|
132 | * Called when a given URL will be retrieved by the GET method.
|
---|
133 | *
|
---|
134 | * Note: High level function, not being called when pfnOnGetRequest is implemented.
|
---|
135 | * Note2: Can be called multiple times, based on the body size to send.
|
---|
136 | *
|
---|
137 | * @returns VBox status code.
|
---|
138 | * @param pData Pointer to HTTP callback data.
|
---|
139 | * @param pvHandle Opaque handle for object identification.
|
---|
140 | * @param pvBuf Pointer to buffer where to store the read data.
|
---|
141 | * @param cbBuf Size (in bytes) of the buffer where to store the read data.
|
---|
142 | * @param pcbRead Where to return the amount (in bytes) of read data. Optional and can be NULL.
|
---|
143 | */
|
---|
144 | DECLCALLBACKMEMBER(int, pfnRead,(PRTHTTPCALLBACKDATA pData, void *pvHandle, void *pvBuf, size_t cbBuf, size_t *pcbRead));
|
---|
145 | /**
|
---|
146 | * Called when a given URL is done retrieving by the GET method.
|
---|
147 | *
|
---|
148 | * Note: High level function, not being called when pfnOnGetRequest is implemented.
|
---|
149 | *
|
---|
150 | * @returns VBox status code.
|
---|
151 | * @param pData Pointer to HTTP callback data.
|
---|
152 | * @param pszUrl URL to handle.
|
---|
153 | * @param pvHandle Opaque handle for object identification.
|
---|
154 | */
|
---|
155 | DECLCALLBACKMEMBER(int, pfnClose,(PRTHTTPCALLBACKDATA pData, void *pvHandle));
|
---|
156 | /**
|
---|
157 | * Queries information about a given URL.
|
---|
158 | *
|
---|
159 | * Will be called with GET or HEAD request.
|
---|
160 | *
|
---|
161 | * @returns VBox status code.
|
---|
162 | * @param pData Pointer to HTTP callback data.
|
---|
163 | * @param pReq Pointer to request to handle.
|
---|
164 | * @param pObjInfo Where to store the queried file information on success.
|
---|
165 | * @param ppszMIMEHint Where to return an allocated MIME type hint on success.
|
---|
166 | * Must be free'd by the caller using RTStrFree().
|
---|
167 | */
|
---|
168 | DECLCALLBACKMEMBER(int, pfnQueryInfo,(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq, PRTFSOBJINFO pObjInfo, char **ppszMIMEHint));
|
---|
169 | /**
|
---|
170 | * Low-level handler for a GET method request.
|
---|
171 | *
|
---|
172 | * @returns VBox status code.
|
---|
173 | * @param pData Pointer to HTTP callback data.
|
---|
174 | * @param pReq Pointer to request to handle.
|
---|
175 | */
|
---|
176 | DECLCALLBACKMEMBER(int, pfnOnGetRequest,(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq));
|
---|
177 | /**
|
---|
178 | * Low-level handler for a HEAD method request.
|
---|
179 | *
|
---|
180 | * @returns VBox status code.
|
---|
181 | * @param pData Pointer to HTTP callback data.
|
---|
182 | * @param pReq Pointer to request to handle.
|
---|
183 | */
|
---|
184 | DECLCALLBACKMEMBER(int, pfnOnHeadRequest,(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq));
|
---|
185 | /**
|
---|
186 | * Called before the HTTP server will be destroyed.
|
---|
187 | *
|
---|
188 | * @returns VBox status code.
|
---|
189 | * @param pData Pointer to HTTP callback data.
|
---|
190 | */
|
---|
191 | DECLCALLBACKMEMBER(int, pfnDestroy,(PRTHTTPCALLBACKDATA pData));
|
---|
192 | } RTHTTPSERVERCALLBACKS;
|
---|
193 | /** Pointer to a HTTP server callback data table. */
|
---|
194 | typedef RTHTTPSERVERCALLBACKS *PRTHTTPSERVERCALLBACKS;
|
---|
195 |
|
---|
196 | /** Maximum length (in bytes) a single client request can have. */
|
---|
197 | #define RTHTTPSERVER_MAX_REQ_LEN _8K
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Creates a HTTP server instance.
|
---|
201 | *
|
---|
202 | * @returns IPRT status code.
|
---|
203 | * @param phHttpServer Where to store the HTTP server handle.
|
---|
204 | * @param pcszAddress The address for creating a listening socket.
|
---|
205 | * If NULL or empty string the server is bound to all interfaces.
|
---|
206 | * @param uPort The port for creating a listening socket.
|
---|
207 | * @param pCallbacks Callback table to use.
|
---|
208 | * @param pvUser Pointer to user-specific data. Optional.
|
---|
209 | * @param cbUser Size of user-specific data. Optional.
|
---|
210 | */
|
---|
211 | RTR3DECL(int) RTHttpServerCreate(PRTHTTPSERVER phHttpServer, const char *pcszAddress, uint16_t uPort,
|
---|
212 | PRTHTTPSERVERCALLBACKS pCallbacks, void *pvUser, size_t cbUser);
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Destroys a HTTP server instance.
|
---|
216 | *
|
---|
217 | * @returns IPRT status code.
|
---|
218 | * @param hHttpServer Handle to the HTTP server handle.
|
---|
219 | */
|
---|
220 | RTR3DECL(int) RTHttpServerDestroy(RTHTTPSERVER hHttpServer);
|
---|
221 |
|
---|
222 | /** @} */
|
---|
223 | RT_C_DECLS_END
|
---|
224 |
|
---|
225 | #endif /* !IPRT_INCLUDED_http_server_h */
|
---|
226 |
|
---|