NetworkPkg: Avoid memory allocation for each HTTP message exchange.
[mirror_edk2.git] / NetworkPkg / HttpDxe / HttpProto.h
1 /** @file
2 The header files of miscellaneous routines for HttpDxe driver.
3
4 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #ifndef __EFI_HTTP_PROTO_H__
16 #define __EFI_HTTP_PROTO_H__
17
18 #define DEF_BUF_LEN 2048
19
20 #define HTTP_SERVICE_SIGNATURE SIGNATURE_32('H', 't', 't', 'S')
21
22 #define HTTP_SERVICE_FROM_PROTOCOL(a) \
23 CR ( \
24 (a), \
25 HTTP_SERVICE, \
26 ServiceBinding, \
27 HTTP_SERVICE_SIGNATURE \
28 )
29
30 //
31 // The state of HTTP protocol. It starts from UNCONFIGED.
32 //
33 #define HTTP_STATE_UNCONFIGED 0
34 #define HTTP_STATE_HTTP_CONFIGED 1
35 #define HTTP_STATE_TCP_CONFIGED 2
36 #define HTTP_STATE_TCP_UNCONFIGED 3
37 #define HTTP_STATE_TCP_CONNECTED 4
38 #define HTTP_STATE_TCP_CLOSED 5
39
40 //
41 // TCP configured data.
42 //
43 #define HTTP_TOS_DEAULT 8
44 #define HTTP_TTL_DEAULT 255
45 #define HTTP_BUFFER_SIZE_DEAULT 65535
46 #define HTTP_MAX_SYN_BACK_LOG 5
47 #define HTTP_CONNECTION_TIMEOUT 60
48 #define HTTP_DATA_RETRIES 12
49 #define HTTP_FIN_TIMEOUT 2
50 #define HTTP_KEEP_ALIVE_PROBES 6
51 #define HTTP_KEEP_ALIVE_TIME 7200
52 #define HTTP_KEEP_ALIVE_INTERVAL 30
53
54 #define HTTP_URL_BUFFER_LEN 4096
55
56 typedef struct _HTTP_SERVICE {
57 UINT32 Signature;
58 EFI_SERVICE_BINDING_PROTOCOL ServiceBinding;
59 EFI_HANDLE ImageHandle;
60 EFI_HANDLE ControllerHandle;
61 LIST_ENTRY ChildrenList;
62 UINTN ChildrenNumber;
63 EFI_HANDLE TcpChildHandle;
64 INTN State;
65 } HTTP_SERVICE;
66
67 typedef struct {
68 EFI_TCP4_IO_TOKEN TxToken;
69 EFI_TCP4_TRANSMIT_DATA TxData;
70 BOOLEAN IsTxDone;
71 EFI_TCP4_IO_TOKEN RxToken;
72 EFI_TCP4_RECEIVE_DATA RxData;
73 BOOLEAN IsRxDone;
74 UINTN BodyLen;
75 EFI_HTTP_METHOD Method;
76 } HTTP_TCP_TOKEN_WRAP;
77
78 typedef struct _HTTP_PROTOCOL {
79 UINT32 Signature;
80 EFI_HTTP_PROTOCOL Http;
81 EFI_HANDLE Handle;
82 HTTP_SERVICE *Service;
83 LIST_ENTRY Link; // Link to all HTTP instance from the service.
84 BOOLEAN InDestroy;
85 INTN State;
86
87 EFI_HANDLE TcpChildHandle;
88 EFI_TCP4_PROTOCOL *Tcp4;
89 EFI_TCP4_CONFIG_DATA Tcp4CfgData;
90 EFI_TCP4_OPTION Tcp4Option;
91
92 EFI_TCP4_CONNECTION_TOKEN ConnToken;
93 BOOLEAN IsConnDone;
94 EFI_TCP4_CLOSE_TOKEN CloseToken;
95 BOOLEAN IsCloseDone;
96
97 CHAR8 *RemoteHost;
98 UINT16 RemotePort;
99 EFI_IPv4_ADDRESS RemoteAddr;
100 //
101 // RxToken used for receiving HTTP header.
102 //
103 EFI_TCP4_IO_TOKEN RxToken;
104 EFI_TCP4_RECEIVE_DATA RxData;
105 BOOLEAN IsRxDone;
106
107 CHAR8 *CacheBody;
108 CHAR8 *NextMsg;
109 UINTN CacheLen;
110 UINTN CacheOffset;
111
112 //
113 // HTTP message-body parser.
114 //
115 VOID *MsgParser;
116
117 EFI_HTTP_VERSION HttpVersion;
118 UINT32 TimeOutMillisec;
119 BOOLEAN LocalAddressIsIPv6;
120
121 EFI_HTTPv4_ACCESS_POINT IPv4Node;
122
123 NET_MAP TxTokens;
124 NET_MAP RxTokens;
125
126 CHAR8 *Url;
127 } HTTP_PROTOCOL;
128
129 typedef struct {
130 EFI_HTTP_TOKEN *HttpToken;
131 HTTP_PROTOCOL *HttpInstance;
132 HTTP_TCP_TOKEN_WRAP TcpWrap;
133 } HTTP_TOKEN_WRAP;
134
135
136 #define HTTP_PROTOCOL_SIGNATURE SIGNATURE_32('H', 't', 't', 'P')
137
138 #define HTTP_INSTANCE_FROM_PROTOCOL(a) \
139 CR ( \
140 (a), \
141 HTTP_PROTOCOL, \
142 Http, \
143 HTTP_PROTOCOL_SIGNATURE \
144 )
145
146 /**
147 The common notify function used in HTTP driver.
148
149 @param[in] Event The event signaled.
150 @param[in] Context The context.
151
152 **/
153 VOID
154 EFIAPI
155 HttpCommonNotify (
156 IN EFI_EVENT Event,
157 IN VOID *Context
158 );
159
160 /**
161 Create events for the TCP4 connection token and TCP4 close token.
162
163 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
164
165 @retval EFI_SUCCESS The events are created successfully.
166 @retval others Other error as indicated.
167
168 **/
169 EFI_STATUS
170 HttpCreateTcp4ConnCloseEvent (
171 IN HTTP_PROTOCOL *HttpInstance
172 );
173
174 /**
175 Close events in the TCP4 connection token and TCP4 close token.
176
177 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
178
179 **/
180 VOID
181 HttpCloseTcp4ConnCloseEvent (
182 IN HTTP_PROTOCOL *HttpInstance
183 );
184
185 /**
186 Create event for the TCP4 transmit token.
187
188 @param[in] Wrap Point to HTTP token's wrap data.
189
190 @retval EFI_SUCCESS The events is created successfully.
191 @retval others Other error as indicated.
192
193 **/
194 EFI_STATUS
195 HttpCreateTcp4TxEvent (
196 IN HTTP_TOKEN_WRAP *Wrap
197 );
198
199 /**
200 Create event for the TCP4 receive token which is used to receive HTTP header.
201
202 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
203
204 @retval EFI_SUCCESS The events is created successfully.
205 @retval others Other error as indicated.
206
207 **/
208 EFI_STATUS
209 HttpCreateTcp4RxEventForHeader (
210 IN HTTP_PROTOCOL *HttpInstance
211 );
212
213 /**
214 Create event for the TCP4 receive token which is used to receive HTTP body.
215
216 @param[in] Wrap Point to HTTP token's wrap data.
217
218 @retval EFI_SUCCESS The events is created successfully.
219 @retval others Other error as indicated.
220
221 **/
222 EFI_STATUS
223 HttpCreateTcp4RxEvent (
224 IN HTTP_TOKEN_WRAP *Wrap
225 );
226
227 /**
228 Intiialize the HTTP_PROTOCOL structure to the unconfigured state.
229
230 @param[in] HttpSb The HTTP service private instance.
231 @param[in, out] HttpInstance Pointer to HTTP_PROTOCOL structure.
232
233 @retval EFI_SUCCESS HTTP_PROTOCOL structure is initialized successfully.
234 @retval Others Other error as indicated.
235
236 **/
237 EFI_STATUS
238 HttpInitProtocol (
239 IN HTTP_SERVICE *HttpSb,
240 IN OUT HTTP_PROTOCOL *HttpInstance
241 );
242
243 /**
244 Clean up the HTTP child, release all the resources used by it.
245
246 @param[in] HttpInstance The HTTP child to clean up.
247
248 **/
249 VOID
250 HttpCleanProtocol (
251 IN HTTP_PROTOCOL *HttpInstance
252 );
253
254 /**
255 Establish TCP connection with HTTP server.
256
257 @param[in] HttpInstance The HTTP instance private data.
258
259 @retval EFI_SUCCESS The TCP connection is established.
260 @retval Others Other error as indicated.
261
262 **/
263 EFI_STATUS
264 HttpCreateConnection (
265 IN HTTP_PROTOCOL *HttpInstance
266 );
267
268 /**
269 Close existing TCP connection.
270
271 @param[in] HttpInstance The HTTP instance private data.
272
273 @retval EFI_SUCCESS The TCP connection is closed.
274 @retval Others Other error as indicated.
275
276 **/
277 EFI_STATUS
278 HttpCloseConnection (
279 IN HTTP_PROTOCOL *HttpInstance
280 );
281
282 /**
283 Configure TCP4 protocol child.
284
285 @param[in] HttpInstance The HTTP instance private data.
286 @param[in] Wrap The HTTP token's wrap data.
287
288 @retval EFI_SUCCESS The TCP4 protocol child is configured.
289 @retval Others Other error as indicated.
290
291 **/
292 EFI_STATUS
293 HttpConfigureTcp4 (
294 IN HTTP_PROTOCOL *HttpInstance,
295 IN HTTP_TOKEN_WRAP *Wrap
296 );
297
298 /**
299 Check existing TCP connection, if in error state, receover TCP4 connection.
300
301 @param[in] HttpInstance The HTTP instance private data.
302
303 @retval EFI_SUCCESS The TCP connection is established.
304 @retval EFI_NOT_READY TCP4 protocol child is not created or configured.
305 @retval Others Other error as indicated.
306
307 **/
308 EFI_STATUS
309 HttpConnectTcp4 (
310 IN HTTP_PROTOCOL *HttpInstance
311 );
312
313 /**
314 Send the HTTP message through TCP4.
315
316 @param[in] HttpInstance The HTTP instance private data.
317 @param[in] Wrap The HTTP token's wrap data.
318 @param[in] TxString Buffer containing the HTTP message string.
319 @param[in] TxStringLen Length of the HTTP message string in bytes.
320
321 @retval EFI_SUCCESS The HTTP message is queued into TCP transmit queue.
322 @retval Others Other error as indicated.
323
324 **/
325 EFI_STATUS
326 HttpTransmitTcp4 (
327 IN HTTP_PROTOCOL *HttpInstance,
328 IN HTTP_TOKEN_WRAP *Wrap,
329 IN UINT8 *TxString,
330 IN UINTN TxStringLen
331 );
332
333 /**
334 Translate the status code in HTTP message to EFI_HTTP_STATUS_CODE defined
335 in UEFI 2.5 specification.
336
337 @param[in] StatusCode The status code value in HTTP message.
338
339 @return Value defined in EFI_HTTP_STATUS_CODE .
340
341 **/
342 EFI_HTTP_STATUS_CODE
343 HttpMappingToStatusCode (
344 IN UINTN StatusCode
345 );
346
347 /**
348 Check whether the user's token or event has already
349 been enqueue on HTTP TxToken or RxToken list.
350
351 @param[in] Map The container of either user's transmit or receive
352 token.
353 @param[in] Item Current item to check against.
354 @param[in] Context The Token to check againist.
355
356 @retval EFI_ACCESS_DENIED The token or event has already been enqueued in IP
357 @retval EFI_SUCCESS The current item isn't the same token/event as the
358 context.
359
360 **/
361 EFI_STATUS
362 EFIAPI
363 HttpTokenExist (
364 IN NET_MAP *Map,
365 IN NET_MAP_ITEM *Item,
366 IN VOID *Context
367 );
368
369 /**
370 Check whether the HTTP message associated with TxToken is already sent out.
371
372 @param[in] Map The container of TxToken.
373 @param[in] Item Current item to check against.
374 @param[in] Context The Token to check againist.
375
376 @retval EFI_NOT_READY The HTTP message is still queued in the list.
377 @retval EFI_SUCCESS The HTTP message has been sent out.
378
379 **/
380 EFI_STATUS
381 EFIAPI
382 HttpTcpNotReady (
383 IN NET_MAP *Map,
384 IN NET_MAP_ITEM *Item,
385 IN VOID *Context
386 );
387
388 /**
389 Transmit the HTTP mssage by processing the associated HTTP token.
390
391 @param[in] Map The container of TxToken.
392 @param[in] Item Current item to check against.
393 @param[in] Context The Token to check againist.
394
395 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
396 @retval EFI_SUCCESS The HTTP message is queued into TCP transmit
397 queue.
398
399 **/
400 EFI_STATUS
401 EFIAPI
402 HttpTcpTransmit (
403 IN NET_MAP *Map,
404 IN NET_MAP_ITEM *Item,
405 IN VOID *Context
406 );
407
408 /**
409 Receive the HTTP response by processing the associated HTTP token.
410
411 @param[in] Map The container of RxToken.
412 @param[in] Item Current item to check against.
413 @param[in] Context The Token to check againist.
414
415 @retval EFI_SUCCESS The HTTP response is queued into TCP receive
416 queue.
417 @retval Others Other error as indicated.
418
419 **/
420 EFI_STATUS
421 EFIAPI
422 HttpTcpReceive (
423 IN NET_MAP *Map,
424 IN NET_MAP_ITEM *Item,
425 IN VOID *Context
426 );
427
428 /**
429 Generate HTTP request string.
430
431 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.
432 @param[in] Message Pointer to storage containing HTTP message data.
433 @param[in] Url The URL of a remote host.
434
435 @return Pointer to the created HTTP request string.
436 @return NULL if any error occured.
437
438 **/
439 CHAR8 *
440 HttpGenRequestString (
441 IN HTTP_PROTOCOL *HttpInstance,
442 IN EFI_HTTP_MESSAGE *Message,
443 IN CHAR8 *Url
444 );
445
446 /**
447 The work function of EfiHttpResponse().
448
449 @param[in] Wrap Pointer to HTTP token's wrap data.
450
451 @retval EFI_SUCCESS Allocation succeeded.
452 @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.
453 @retval EFI_NOT_READY Can't find a corresponding TxToken.
454
455 **/
456 EFI_STATUS
457 HttpResponseWorker (
458 IN HTTP_TOKEN_WRAP *Wrap
459 );
460
461 #endif