]>
Commit | Line | Data |
---|---|---|
47f51a06 YT |
1 | /** @file\r |
2 | Implementation of EFI_HTTP_PROTOCOL protocol interfaces.\r | |
3 | \r | |
221463c2 | 4 | Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>\r |
f58554fc | 5 | (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>\r |
47f51a06 YT |
6 | \r |
7 | This program and the accompanying materials\r | |
8 | are licensed and made available under the terms and conditions of the BSD License\r | |
9 | which accompanies this distribution. The full text of the license may be found at\r | |
10 | http://opensource.org/licenses/bsd-license.php.\r | |
11 | \r | |
12 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
13 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
14 | \r | |
15 | **/\r | |
16 | \r | |
17 | #include "HttpDriver.h"\r | |
18 | \r | |
19 | EFI_HTTP_PROTOCOL mEfiHttpTemplate = {\r | |
20 | EfiHttpGetModeData,\r | |
21 | EfiHttpConfigure,\r | |
22 | EfiHttpRequest,\r | |
23 | EfiHttpCancel,\r | |
24 | EfiHttpResponse,\r | |
25 | EfiHttpPoll\r | |
26 | };\r | |
27 | \r | |
28 | /**\r | |
29 | Returns the operational parameters for the current HTTP child instance.\r | |
30 | \r | |
31 | The GetModeData() function is used to read the current mode data (operational\r | |
32 | parameters) for this HTTP protocol instance.\r | |
33 | \r | |
34 | @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r | |
35 | @param[out] HttpConfigData Point to buffer for operational parameters of this\r | |
de15f8b6 JW |
36 | HTTP instance. It is the responsibility of the caller \r |
37 | to allocate the memory for HttpConfigData and \r | |
38 | HttpConfigData->AccessPoint.IPv6Node/IPv4Node. In fact, \r | |
39 | it is recommended to allocate sufficient memory to record \r | |
40 | IPv6Node since it is big enough for all possibilities. \r | |
47f51a06 YT |
41 | \r |
42 | @retval EFI_SUCCESS Operation succeeded.\r | |
43 | @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r | |
44 | This is NULL.\r | |
45 | HttpConfigData is NULL.\r | |
de15f8b6 JW |
46 | HttpConfigData->AccessPoint.IPv4Node or \r |
47 | HttpConfigData->AccessPoint.IPv6Node is NULL.\r | |
f0ab5a81 | 48 | @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.\r |
47f51a06 YT |
49 | \r |
50 | **/\r | |
51 | EFI_STATUS\r | |
52 | EFIAPI\r | |
53 | EfiHttpGetModeData (\r | |
54 | IN EFI_HTTP_PROTOCOL *This,\r | |
55 | OUT EFI_HTTP_CONFIG_DATA *HttpConfigData\r | |
56 | )\r | |
57 | {\r | |
58 | HTTP_PROTOCOL *HttpInstance;\r | |
59 | \r | |
f0ab5a81 ZL |
60 | //\r |
61 | // Check input parameters.\r | |
62 | //\r | |
47f51a06 YT |
63 | if ((This == NULL) || (HttpConfigData == NULL)) {\r |
64 | return EFI_INVALID_PARAMETER;\r | |
65 | }\r | |
f0ab5a81 | 66 | \r |
47f51a06 YT |
67 | HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r |
68 | ASSERT (HttpInstance != NULL);\r | |
f0ab5a81 | 69 | \r |
de15f8b6 JW |
70 | if ((HttpConfigData->AccessPoint.IPv6Node == NULL) ||\r |
71 | (HttpConfigData->AccessPoint.IPv4Node == NULL)) {\r | |
f0ab5a81 ZL |
72 | return EFI_INVALID_PARAMETER;\r |
73 | }\r | |
74 | \r | |
47f51a06 YT |
75 | if (HttpInstance->State < HTTP_STATE_HTTP_CONFIGED) {\r |
76 | return EFI_NOT_STARTED;\r | |
77 | }\r | |
78 | \r | |
47f51a06 YT |
79 | HttpConfigData->HttpVersion = HttpInstance->HttpVersion;\r |
80 | HttpConfigData->TimeOutMillisec = HttpInstance->TimeOutMillisec;\r | |
81 | HttpConfigData->LocalAddressIsIPv6 = HttpInstance->LocalAddressIsIPv6;\r | |
82 | \r | |
b659408b | 83 | if (HttpInstance->LocalAddressIsIPv6) {\r |
b659408b | 84 | CopyMem (\r |
f0ab5a81 | 85 | HttpConfigData->AccessPoint.IPv6Node,\r |
b659408b ZL |
86 | &HttpInstance->Ipv6Node,\r |
87 | sizeof (HttpInstance->Ipv6Node)\r | |
47f51a06 | 88 | );\r |
b659408b | 89 | } else {\r |
b659408b | 90 | CopyMem (\r |
f0ab5a81 | 91 | HttpConfigData->AccessPoint.IPv4Node,\r |
b659408b ZL |
92 | &HttpInstance->IPv4Node,\r |
93 | sizeof (HttpInstance->IPv4Node)\r | |
94 | );\r | |
b659408b | 95 | }\r |
47f51a06 YT |
96 | \r |
97 | return EFI_SUCCESS;\r | |
98 | }\r | |
99 | \r | |
100 | /**\r | |
101 | Initialize or brutally reset the operational parameters for this EFI HTTP instance.\r | |
102 | \r | |
103 | The Configure() function does the following:\r | |
104 | When HttpConfigData is not NULL Initialize this EFI HTTP instance by configuring\r | |
105 | timeout, local address, port, etc.\r | |
106 | When HttpConfigData is NULL, reset this EFI HTTP instance by closing all active\r | |
107 | connections with remote hosts, canceling all asynchronous tokens, and flush request\r | |
108 | and response buffers without informing the appropriate hosts.\r | |
109 | \r | |
f0ab5a81 ZL |
110 | No other EFI HTTP function can be executed by this instance until the Configure()\r |
111 | function is executed and returns successfully.\r | |
47f51a06 YT |
112 | \r |
113 | @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r | |
114 | @param[in] HttpConfigData Pointer to the configure data to configure the instance.\r | |
115 | \r | |
116 | @retval EFI_SUCCESS Operation succeeded.\r | |
117 | @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r | |
118 | This is NULL.\r | |
119 | HttpConfigData->LocalAddressIsIPv6 is FALSE and\r | |
de15f8b6 | 120 | HttpConfigData->AccessPoint.IPv4Node is NULL.\r |
47f51a06 | 121 | HttpConfigData->LocalAddressIsIPv6 is TRUE and\r |
de15f8b6 | 122 | HttpConfigData->AccessPoint.IPv6Node is NULL.\r |
47f51a06 YT |
123 | @retval EFI_ALREADY_STARTED Reinitialize this HTTP instance without calling\r |
124 | Configure() with NULL to reset it.\r | |
125 | @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r | |
126 | @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when\r | |
127 | executing Configure().\r | |
128 | @retval EFI_UNSUPPORTED One or more options in HttpConfigData are not supported\r | |
129 | in the implementation.\r | |
130 | **/\r | |
131 | EFI_STATUS\r | |
132 | EFIAPI\r | |
133 | EfiHttpConfigure (\r | |
134 | IN EFI_HTTP_PROTOCOL *This,\r | |
30c9eaea | 135 | IN EFI_HTTP_CONFIG_DATA *HttpConfigData OPTIONAL\r |
47f51a06 YT |
136 | ) \r |
137 | {\r | |
138 | HTTP_PROTOCOL *HttpInstance;\r | |
139 | EFI_STATUS Status;\r | |
b659408b ZL |
140 | \r |
141 | //\r | |
142 | // Check input parameters.\r | |
143 | //\r | |
f0ab5a81 | 144 | if (This == NULL ||\r |
6be1193f JW |
145 | (HttpConfigData != NULL && \r |
146 | ((HttpConfigData->LocalAddressIsIPv6 && HttpConfigData->AccessPoint.IPv6Node == NULL) ||\r | |
147 | (!HttpConfigData->LocalAddressIsIPv6 && HttpConfigData->AccessPoint.IPv4Node == NULL)))) {\r | |
47f51a06 YT |
148 | return EFI_INVALID_PARAMETER;\r |
149 | }\r | |
150 | \r | |
151 | HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r | |
152 | ASSERT (HttpInstance != NULL && HttpInstance->Service != NULL);\r | |
153 | \r | |
154 | if (HttpConfigData != NULL) {\r | |
b659408b | 155 | \r |
79de8c79 JW |
156 | if (HttpConfigData->HttpVersion >= HttpVersionUnsupported) {\r |
157 | return EFI_UNSUPPORTED;\r | |
158 | }\r | |
159 | \r | |
47f51a06 YT |
160 | //\r |
161 | // Now configure this HTTP instance.\r | |
162 | //\r | |
163 | if (HttpInstance->State != HTTP_STATE_UNCONFIGED) {\r | |
164 | return EFI_ALREADY_STARTED;\r | |
165 | }\r | |
166 | \r | |
167 | HttpInstance->HttpVersion = HttpConfigData->HttpVersion;\r | |
168 | HttpInstance->TimeOutMillisec = HttpConfigData->TimeOutMillisec;\r | |
169 | HttpInstance->LocalAddressIsIPv6 = HttpConfigData->LocalAddressIsIPv6;\r | |
b659408b ZL |
170 | \r |
171 | if (HttpConfigData->LocalAddressIsIPv6) { \r | |
172 | CopyMem (\r | |
173 | &HttpInstance->Ipv6Node,\r | |
174 | HttpConfigData->AccessPoint.IPv6Node,\r | |
175 | sizeof (HttpInstance->Ipv6Node)\r | |
176 | );\r | |
47f51a06 YT |
177 | } else {\r |
178 | CopyMem (\r | |
179 | &HttpInstance->IPv4Node,\r | |
180 | HttpConfigData->AccessPoint.IPv4Node,\r | |
181 | sizeof (HttpInstance->IPv4Node)\r | |
182 | );\r | |
47f51a06 | 183 | }\r |
b347a22a | 184 | \r |
b659408b ZL |
185 | //\r |
186 | // Creat Tcp child\r | |
187 | //\r | |
188 | Status = HttpInitProtocol (HttpInstance, HttpInstance->LocalAddressIsIPv6);\r | |
189 | if (EFI_ERROR (Status)) {\r | |
190 | return Status;\r | |
191 | }\r | |
192 | \r | |
193 | HttpInstance->State = HTTP_STATE_HTTP_CONFIGED;\r | |
194 | return EFI_SUCCESS;\r | |
47f51a06 YT |
195 | \r |
196 | } else {\r | |
b659408b ZL |
197 | //\r |
198 | // Reset all the resources related to HttpInsance.\r | |
199 | //\r | |
200 | HttpCleanProtocol (HttpInstance);\r | |
201 | HttpInstance->State = HTTP_STATE_UNCONFIGED;\r | |
202 | return EFI_SUCCESS;\r | |
47f51a06 YT |
203 | }\r |
204 | }\r | |
205 | \r | |
206 | \r | |
207 | /**\r | |
208 | The Request() function queues an HTTP request to this HTTP instance.\r | |
209 | \r | |
210 | Similar to Transmit() function in the EFI TCP driver. When the HTTP request is sent\r | |
211 | successfully, or if there is an error, Status in token will be updated and Event will\r | |
212 | be signaled.\r | |
213 | \r | |
214 | @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r | |
215 | @param[in] Token Pointer to storage containing HTTP request token.\r | |
216 | \r | |
217 | @retval EFI_SUCCESS Outgoing data was processed.\r | |
218 | @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.\r | |
219 | @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r | |
220 | @retval EFI_TIMEOUT Data was dropped out of the transmit or receive queue.\r | |
221 | @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.\r | |
222 | @retval EFI_UNSUPPORTED The HTTP method is not supported in current\r | |
223 | implementation.\r | |
224 | @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r | |
225 | This is NULL.\r | |
f0ab5a81 | 226 | Token is NULL.\r |
47f51a06 YT |
227 | Token->Message is NULL.\r |
228 | Token->Message->Body is not NULL,\r | |
229 | Token->Message->BodyLength is non-zero, and\r | |
230 | Token->Message->Data is NULL, but a previous call to\r | |
231 | Request()has not been completed successfully.\r | |
232 | **/\r | |
233 | EFI_STATUS\r | |
234 | EFIAPI\r | |
235 | EfiHttpRequest (\r | |
236 | IN EFI_HTTP_PROTOCOL *This,\r | |
237 | IN EFI_HTTP_TOKEN *Token\r | |
238 | )\r | |
239 | {\r | |
240 | EFI_HTTP_MESSAGE *HttpMsg;\r | |
241 | EFI_HTTP_REQUEST_DATA *Request;\r | |
242 | VOID *UrlParser;\r | |
243 | EFI_STATUS Status;\r | |
244 | CHAR8 *HostName;\r | |
b9679cd7 | 245 | UINTN HostNameSize;\r |
47f51a06 YT |
246 | UINT16 RemotePort;\r |
247 | HTTP_PROTOCOL *HttpInstance;\r | |
248 | BOOLEAN Configure;\r | |
249 | BOOLEAN ReConfigure;\r | |
dac45de3 | 250 | BOOLEAN TlsConfigure;\r |
19c25725 | 251 | CHAR8 *RequestMsg;\r |
47f51a06 | 252 | CHAR8 *Url;\r |
51b0450e | 253 | UINTN UrlLen;\r |
47f51a06 YT |
254 | CHAR16 *HostNameStr;\r |
255 | HTTP_TOKEN_WRAP *Wrap;\r | |
b199d941 | 256 | CHAR8 *FileUrl;\r |
19c25725 | 257 | UINTN RequestMsgSize;\r |
7cf59c85 | 258 | EFI_HANDLE ImageHandle;\r |
d8293d31 NH |
259 | \r |
260 | //\r | |
261 | // Initializations\r | |
262 | //\r | |
263 | Url = NULL;\r | |
5646819f | 264 | UrlParser = NULL;\r |
6be1193f | 265 | RemotePort = 0;\r |
d8293d31 NH |
266 | HostName = NULL;\r |
267 | RequestMsg = NULL;\r | |
268 | HostNameStr = NULL;\r | |
269 | Wrap = NULL;\r | |
270 | FileUrl = NULL;\r | |
dac45de3 | 271 | TlsConfigure = FALSE;\r |
d8293d31 | 272 | \r |
47f51a06 YT |
273 | if ((This == NULL) || (Token == NULL)) {\r |
274 | return EFI_INVALID_PARAMETER;\r | |
275 | }\r | |
276 | \r | |
277 | HttpMsg = Token->Message;\r | |
d8293d31 | 278 | if (HttpMsg == NULL) {\r |
47f51a06 YT |
279 | return EFI_INVALID_PARAMETER;\r |
280 | }\r | |
281 | \r | |
47f51a06 | 282 | Request = HttpMsg->Data.Request;\r |
47f51a06 YT |
283 | \r |
284 | //\r | |
97c567ef | 285 | // Only support GET, HEAD, PATCH, PUT and POST method in current implementation.\r |
47f51a06 | 286 | //\r |
d8293d31 | 287 | if ((Request != NULL) && (Request->Method != HttpMethodGet) &&\r |
97c567ef JW |
288 | (Request->Method != HttpMethodHead) && (Request->Method != HttpMethodPut) && \r |
289 | (Request->Method != HttpMethodPost) && (Request->Method != HttpMethodPatch)) {\r | |
47f51a06 YT |
290 | return EFI_UNSUPPORTED;\r |
291 | }\r | |
292 | \r | |
293 | HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r | |
294 | ASSERT (HttpInstance != NULL);\r | |
295 | \r | |
d8293d31 NH |
296 | //\r |
297 | // Capture the method into HttpInstance.\r | |
298 | //\r | |
299 | if (Request != NULL) {\r | |
300 | HttpInstance->Method = Request->Method;\r | |
301 | }\r | |
302 | \r | |
47f51a06 YT |
303 | if (HttpInstance->State < HTTP_STATE_HTTP_CONFIGED) {\r |
304 | return EFI_NOT_STARTED;\r | |
305 | }\r | |
306 | \r | |
d8293d31 NH |
307 | if (Request == NULL) {\r |
308 | //\r | |
97c567ef | 309 | // Request would be NULL only for PUT/POST/PATCH operation (in the current implementation)\r |
d8293d31 | 310 | //\r |
97c567ef JW |
311 | if ((HttpInstance->Method != HttpMethodPut) && \r |
312 | (HttpInstance->Method != HttpMethodPost) && \r | |
313 | (HttpInstance->Method != HttpMethodPatch)) {\r | |
d8293d31 NH |
314 | return EFI_INVALID_PARAMETER;\r |
315 | }\r | |
47f51a06 | 316 | \r |
d8293d31 | 317 | //\r |
97c567ef | 318 | // For PUT/POST/PATCH, we need to have the TCP already configured. Bail out if it is not!\r |
d8293d31 NH |
319 | //\r |
320 | if (HttpInstance->State < HTTP_STATE_TCP_CONFIGED) {\r | |
321 | return EFI_INVALID_PARAMETER;\r | |
322 | }\r | |
47f51a06 | 323 | \r |
d8293d31 NH |
324 | //\r |
325 | // We need to have the Message Body for sending the HTTP message across in these cases.\r | |
326 | //\r | |
327 | if (HttpMsg->Body == NULL || HttpMsg->BodyLength == 0) {\r | |
328 | return EFI_INVALID_PARAMETER;\r | |
51b0450e | 329 | }\r |
b659408b | 330 | \r |
d8293d31 NH |
331 | //\r |
332 | // Use existing TCP instance to transmit the packet.\r | |
333 | //\r | |
334 | Configure = FALSE;\r | |
335 | ReConfigure = FALSE;\r | |
336 | } else {\r | |
337 | //\r | |
338 | // Check whether the token already existed.\r | |
339 | //\r | |
340 | if (EFI_ERROR (NetMapIterate (&HttpInstance->TxTokens, HttpTokenExist, Token))) {\r | |
341 | return EFI_ACCESS_DENIED;\r | |
342 | }\r | |
47f51a06 | 343 | \r |
d8293d31 NH |
344 | //\r |
345 | // Parse the URI of the remote host.\r | |
346 | //\r | |
347 | Url = HttpInstance->Url;\r | |
348 | UrlLen = StrLen (Request->Url) + 1;\r | |
349 | if (UrlLen > HTTP_URL_BUFFER_LEN) {\r | |
350 | Url = AllocateZeroPool (UrlLen);\r | |
351 | if (Url == NULL) {\r | |
352 | return EFI_OUT_OF_RESOURCES;\r | |
353 | }\r | |
354 | FreePool (HttpInstance->Url);\r | |
355 | HttpInstance->Url = Url;\r | |
356 | }\r | |
47f51a06 | 357 | \r |
47f51a06 | 358 | \r |
b9679cd7 | 359 | UnicodeStrToAsciiStrS (Request->Url, Url, UrlLen);\r |
dac45de3 JW |
360 | \r |
361 | //\r | |
362 | // From the information in Url, the HTTP instance will \r | |
363 | // be able to determine whether to use http or https.\r | |
364 | //\r | |
365 | HttpInstance->UseHttps = IsHttpsUrl (Url);\r | |
366 | \r | |
221463c2 JW |
367 | //\r |
368 | // HTTP is disabled, return directly if the URI is not HTTPS.\r | |
369 | //\r | |
370 | if (!PcdGetBool (PcdAllowHttpConnections) && !(HttpInstance->UseHttps)) {\r | |
371 | \r | |
372 | DEBUG ((EFI_D_ERROR, "EfiHttpRequest: HTTP is disabled.\n"));\r | |
373 | \r | |
374 | return EFI_ACCESS_DENIED;\r | |
375 | }\r | |
376 | \r | |
dac45de3 JW |
377 | //\r |
378 | // Check whether we need to create Tls child and open the TLS protocol.\r | |
379 | //\r | |
380 | if (HttpInstance->UseHttps && HttpInstance->TlsChildHandle == NULL) {\r | |
381 | //\r | |
382 | // Use TlsSb to create Tls child and open the TLS protocol.\r | |
383 | //\r | |
7cf59c85 ZL |
384 | if (HttpInstance->LocalAddressIsIPv6) {\r |
385 | ImageHandle = HttpInstance->Service->Ip6DriverBindingHandle;\r | |
386 | } else {\r | |
387 | ImageHandle = HttpInstance->Service->Ip4DriverBindingHandle;\r | |
388 | }\r | |
389 | \r | |
dac45de3 | 390 | HttpInstance->TlsChildHandle = TlsCreateChild (\r |
7cf59c85 | 391 | ImageHandle,\r |
45ea8a0c | 392 | &(HttpInstance->TlsSb),\r |
dac45de3 JW |
393 | &(HttpInstance->Tls),\r |
394 | &(HttpInstance->TlsConfiguration)\r | |
395 | );\r | |
396 | if (HttpInstance->TlsChildHandle == NULL) {\r | |
397 | return EFI_DEVICE_ERROR;\r | |
398 | }\r | |
399 | \r | |
400 | TlsConfigure = TRUE;\r | |
401 | }\r | |
402 | \r | |
d8293d31 NH |
403 | UrlParser = NULL;\r |
404 | Status = HttpParseUrl (Url, (UINT32) AsciiStrLen (Url), FALSE, &UrlParser);\r | |
405 | if (EFI_ERROR (Status)) {\r | |
406 | goto Error1;\r | |
407 | }\r | |
47f51a06 | 408 | \r |
d8293d31 NH |
409 | HostName = NULL;\r |
410 | Status = HttpUrlGetHostName (Url, UrlParser, &HostName);\r | |
411 | if (EFI_ERROR (Status)) {\r | |
412 | goto Error1;\r | |
413 | }\r | |
414 | \r | |
415 | Status = HttpUrlGetPort (Url, UrlParser, &RemotePort);\r | |
416 | if (EFI_ERROR (Status)) {\r | |
dac45de3 JW |
417 | if (HttpInstance->UseHttps) {\r |
418 | RemotePort = HTTPS_DEFAULT_PORT;\r | |
419 | } else {\r | |
420 | RemotePort = HTTP_DEFAULT_PORT;\r | |
421 | }\r | |
d8293d31 | 422 | }\r |
47f51a06 | 423 | //\r |
d8293d31 NH |
424 | // If Configure is TRUE, it indicates the first time to call Request();\r |
425 | // If ReConfigure is TRUE, it indicates the request URL is not same\r | |
426 | // with the previous call to Request();\r | |
47f51a06 | 427 | //\r |
d8293d31 NH |
428 | Configure = TRUE;\r |
429 | ReConfigure = TRUE;\r | |
430 | \r | |
431 | if (HttpInstance->RemoteHost == NULL) {\r | |
47f51a06 | 432 | //\r |
d8293d31 | 433 | // Request() is called the first time.\r |
47f51a06 | 434 | //\r |
d8293d31 NH |
435 | ReConfigure = FALSE;\r |
436 | } else {\r | |
437 | if ((HttpInstance->RemotePort == RemotePort) &&\r | |
dac45de3 JW |
438 | (AsciiStrCmp (HttpInstance->RemoteHost, HostName) == 0) && \r |
439 | (!HttpInstance->UseHttps || (HttpInstance->UseHttps && \r | |
440 | !TlsConfigure && \r | |
441 | HttpInstance->TlsSessionState == EfiTlsSessionDataTransferring))) {\r | |
47f51a06 | 442 | //\r |
d8293d31 | 443 | // Host Name and port number of the request URL are the same with previous call to Request().\r |
dac45de3 | 444 | // If Https protocol used, the corresponding SessionState is EfiTlsSessionDataTransferring.\r |
d8293d31 | 445 | // Check whether previous TCP packet sent out.\r |
47f51a06 | 446 | //\r |
47f51a06 | 447 | \r |
d8293d31 NH |
448 | if (EFI_ERROR (NetMapIterate (&HttpInstance->TxTokens, HttpTcpNotReady, NULL))) {\r |
449 | //\r | |
450 | // Wrap the HTTP token in HTTP_TOKEN_WRAP\r | |
451 | //\r | |
452 | Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));\r | |
453 | if (Wrap == NULL) {\r | |
454 | Status = EFI_OUT_OF_RESOURCES;\r | |
455 | goto Error1;\r | |
456 | }\r | |
457 | \r | |
458 | Wrap->HttpToken = Token;\r | |
459 | Wrap->HttpInstance = HttpInstance;\r | |
460 | \r | |
461 | Status = HttpCreateTcpTxEvent (Wrap);\r | |
462 | if (EFI_ERROR (Status)) {\r | |
463 | goto Error1;\r | |
464 | }\r | |
465 | \r | |
466 | Status = NetMapInsertTail (&HttpInstance->TxTokens, Token, Wrap);\r | |
467 | if (EFI_ERROR (Status)) {\r | |
468 | goto Error1;\r | |
469 | }\r | |
470 | \r | |
471 | Wrap->TcpWrap.Method = Request->Method;\r | |
472 | \r | |
473 | FreePool (HostName);\r | |
474 | \r | |
c0a0a5a5 JW |
475 | HttpUrlFreeParser (UrlParser);\r |
476 | \r | |
d8293d31 NH |
477 | //\r |
478 | // Queue the HTTP token and return.\r | |
479 | //\r | |
480 | return EFI_SUCCESS;\r | |
481 | } else {\r | |
482 | //\r | |
483 | // Use existing TCP instance to transmit the packet.\r | |
484 | //\r | |
485 | Configure = FALSE;\r | |
486 | ReConfigure = FALSE;\r | |
47f51a06 | 487 | }\r |
47f51a06 YT |
488 | } else {\r |
489 | //\r | |
d8293d31 | 490 | // Need close existing TCP instance and create a new TCP instance for data transmit.\r |
47f51a06 | 491 | //\r |
d8293d31 NH |
492 | if (HttpInstance->RemoteHost != NULL) {\r |
493 | FreePool (HttpInstance->RemoteHost);\r | |
494 | HttpInstance->RemoteHost = NULL;\r | |
495 | HttpInstance->RemotePort = 0;\r | |
496 | }\r | |
47f51a06 YT |
497 | }\r |
498 | }\r | |
499 | } \r | |
500 | \r | |
501 | if (Configure) {\r | |
502 | //\r | |
b659408b | 503 | // Parse Url for IPv4 or IPv6 address, if failed, perform DNS resolution.\r |
47f51a06 | 504 | //\r |
b659408b ZL |
505 | if (!HttpInstance->LocalAddressIsIPv6) {\r |
506 | Status = NetLibAsciiStrToIp4 (HostName, &HttpInstance->RemoteAddr);\r | |
507 | } else {\r | |
ac458853 | 508 | Status = HttpUrlGetIp6 (Url, UrlParser, &HttpInstance->RemoteIpv6Addr);\r |
b659408b ZL |
509 | }\r |
510 | \r | |
47f51a06 | 511 | if (EFI_ERROR (Status)) {\r |
b9679cd7 SZ |
512 | HostNameSize = AsciiStrSize (HostName);\r |
513 | HostNameStr = AllocateZeroPool (HostNameSize * sizeof (CHAR16));\r | |
47f51a06 YT |
514 | if (HostNameStr == NULL) {\r |
515 | Status = EFI_OUT_OF_RESOURCES;\r | |
516 | goto Error1;\r | |
517 | }\r | |
b659408b | 518 | \r |
b9679cd7 | 519 | AsciiStrToUnicodeStrS (HostName, HostNameStr, HostNameSize);\r |
b659408b ZL |
520 | if (!HttpInstance->LocalAddressIsIPv6) {\r |
521 | Status = HttpDns4 (HttpInstance, HostNameStr, &HttpInstance->RemoteAddr);\r | |
522 | } else {\r | |
523 | Status = HttpDns6 (HttpInstance, HostNameStr, &HttpInstance->RemoteIpv6Addr);\r | |
524 | }\r | |
525 | \r | |
47f51a06 YT |
526 | FreePool (HostNameStr);\r |
527 | if (EFI_ERROR (Status)) {\r | |
fca04738 | 528 | DEBUG ((EFI_D_ERROR, "Error: Could not retrieve the host address from DNS server.\n"));\r |
47f51a06 YT |
529 | goto Error1;\r |
530 | }\r | |
531 | }\r | |
532 | \r | |
533 | //\r | |
534 | // Save the RemotePort and RemoteHost.\r | |
535 | //\r | |
536 | ASSERT (HttpInstance->RemoteHost == NULL);\r | |
537 | HttpInstance->RemotePort = RemotePort;\r | |
538 | HttpInstance->RemoteHost = HostName;\r | |
539 | HostName = NULL;\r | |
540 | }\r | |
541 | \r | |
542 | if (ReConfigure) {\r | |
543 | //\r | |
544 | // The request URL is different from previous calls to Request(), close existing TCP instance.\r | |
545 | //\r | |
a2e61982 ZL |
546 | if (!HttpInstance->LocalAddressIsIPv6) {\r |
547 | ASSERT (HttpInstance->Tcp4 != NULL);\r | |
548 | } else {\r | |
549 | ASSERT (HttpInstance->Tcp6 != NULL);\r | |
550 | }\r | |
dac45de3 JW |
551 | \r |
552 | if (HttpInstance->UseHttps && !TlsConfigure) {\r | |
553 | Status = TlsCloseSession (HttpInstance);\r | |
554 | if (EFI_ERROR (Status)) {\r | |
555 | goto Error1;\r | |
556 | }\r | |
557 | \r | |
558 | TlsCloseTxRxEvent (HttpInstance);\r | |
559 | }\r | |
560 | \r | |
47f51a06 YT |
561 | HttpCloseConnection (HttpInstance);\r |
562 | EfiHttpCancel (This, NULL);\r | |
563 | }\r | |
564 | \r | |
565 | //\r | |
566 | // Wrap the HTTP token in HTTP_TOKEN_WRAP\r | |
567 | //\r | |
568 | Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));\r | |
569 | if (Wrap == NULL) {\r | |
570 | Status = EFI_OUT_OF_RESOURCES;\r | |
571 | goto Error1;\r | |
572 | }\r | |
573 | \r | |
574 | Wrap->HttpToken = Token;\r | |
575 | Wrap->HttpInstance = HttpInstance;\r | |
d8293d31 NH |
576 | if (Request != NULL) {\r |
577 | Wrap->TcpWrap.Method = Request->Method;\r | |
578 | }\r | |
dac45de3 JW |
579 | \r |
580 | Status = HttpInitSession (\r | |
581 | HttpInstance, \r | |
582 | Wrap, \r | |
583 | Configure || ReConfigure, \r | |
584 | TlsConfigure\r | |
585 | );\r | |
a2e61982 ZL |
586 | if (EFI_ERROR (Status)) {\r |
587 | goto Error2;\r | |
dac45de3 | 588 | }\r |
b659408b | 589 | \r |
dac45de3 | 590 | if (!Configure && !ReConfigure && !TlsConfigure) {\r |
47f51a06 YT |
591 | //\r |
592 | // For the new HTTP token, create TX TCP token events. \r | |
593 | //\r | |
b659408b | 594 | Status = HttpCreateTcpTxEvent (Wrap);\r |
47f51a06 YT |
595 | if (EFI_ERROR (Status)) {\r |
596 | goto Error1;\r | |
597 | }\r | |
598 | }\r | |
a2e61982 | 599 | \r |
47f51a06 YT |
600 | //\r |
601 | // Create request message.\r | |
602 | //\r | |
b199d941 | 603 | FileUrl = Url;\r |
d8293d31 | 604 | if (Url != NULL && *FileUrl != '/') {\r |
b199d941 GCPL |
605 | //\r |
606 | // Convert the absolute-URI to the absolute-path\r | |
607 | //\r | |
608 | while (*FileUrl != ':') {\r | |
609 | FileUrl++;\r | |
610 | }\r | |
611 | if ((*(FileUrl+1) == '/') && (*(FileUrl+2) == '/')) {\r | |
612 | FileUrl += 3;\r | |
613 | while (*FileUrl != '/') {\r | |
614 | FileUrl++;\r | |
615 | }\r | |
616 | } else {\r | |
617 | Status = EFI_INVALID_PARAMETER;\r | |
618 | goto Error3;\r | |
619 | }\r | |
620 | }\r | |
f58554fc | 621 | \r |
19c25725 | 622 | Status = HttpGenRequestMessage (HttpMsg, FileUrl, &RequestMsg, &RequestMsgSize);\r |
f58554fc | 623 | \r |
63f1d6a4 | 624 | if (EFI_ERROR (Status) || NULL == RequestMsg) {\r |
47f51a06 YT |
625 | goto Error3;\r |
626 | }\r | |
627 | \r | |
63f1d6a4 JW |
628 | ASSERT (RequestMsg != NULL);\r |
629 | \r | |
d8293d31 NH |
630 | //\r |
631 | // Every request we insert a TxToken and a response call would remove the TxToken.\r | |
97c567ef | 632 | // In cases of PUT/POST/PATCH, after an initial request-response pair, we would do a\r |
d8293d31 NH |
633 | // continuous request without a response call. So, in such cases, where Request\r |
634 | // structure is NULL, we would not insert a TxToken.\r | |
635 | //\r | |
636 | if (Request != NULL) {\r | |
637 | Status = NetMapInsertTail (&HttpInstance->TxTokens, Token, Wrap);\r | |
638 | if (EFI_ERROR (Status)) {\r | |
639 | goto Error4;\r | |
640 | }\r | |
47f51a06 YT |
641 | }\r |
642 | \r | |
47f51a06 YT |
643 | //\r |
644 | // Transmit the request message.\r | |
645 | //\r | |
b659408b | 646 | Status = HttpTransmitTcp (\r |
47f51a06 YT |
647 | HttpInstance,\r |
648 | Wrap,\r | |
19c25725 NH |
649 | (UINT8*) RequestMsg,\r |
650 | RequestMsgSize\r | |
47f51a06 YT |
651 | );\r |
652 | if (EFI_ERROR (Status)) {\r | |
653 | goto Error5; \r | |
654 | }\r | |
655 | \r | |
49c9f74c | 656 | DispatchDpc ();\r |
b659408b | 657 | \r |
cdf8c32e NH |
658 | if (HostName != NULL) {\r |
659 | FreePool (HostName);\r | |
660 | }\r | |
c0a0a5a5 JW |
661 | \r |
662 | if (UrlParser != NULL) {\r | |
663 | HttpUrlFreeParser (UrlParser);\r | |
664 | }\r | |
b659408b | 665 | \r |
47f51a06 YT |
666 | return EFI_SUCCESS;\r |
667 | \r | |
668 | Error5:\r | |
30526a51 JW |
669 | //\r |
670 | // We would have inserted a TxToken only if Request structure is not NULL.\r | |
671 | // Hence check before we do a remove in this error case.\r | |
672 | //\r | |
673 | if (Request != NULL) {\r | |
674 | NetMapRemoveTail (&HttpInstance->TxTokens, NULL);\r | |
675 | }\r | |
47f51a06 YT |
676 | \r |
677 | Error4:\r | |
19c25725 NH |
678 | if (RequestMsg != NULL) {\r |
679 | FreePool (RequestMsg);\r | |
47f51a06 YT |
680 | } \r |
681 | \r | |
682 | Error3:\r | |
dac45de3 JW |
683 | if (HttpInstance->UseHttps) {\r |
684 | TlsCloseSession (HttpInstance);\r | |
685 | TlsCloseTxRxEvent (HttpInstance);\r | |
686 | }\r | |
47f51a06 | 687 | \r |
47f51a06 | 688 | Error2:\r |
dac45de3 JW |
689 | HttpCloseConnection (HttpInstance);\r |
690 | \r | |
b659408b ZL |
691 | HttpCloseTcpConnCloseEvent (HttpInstance);\r |
692 | if (NULL != Wrap->TcpWrap.Tx4Token.CompletionToken.Event) {\r | |
693 | gBS->CloseEvent (Wrap->TcpWrap.Tx4Token.CompletionToken.Event);\r | |
694 | Wrap->TcpWrap.Tx4Token.CompletionToken.Event = NULL;\r | |
695 | }\r | |
696 | if (NULL != Wrap->TcpWrap.Tx6Token.CompletionToken.Event) {\r | |
697 | gBS->CloseEvent (Wrap->TcpWrap.Tx6Token.CompletionToken.Event);\r | |
698 | Wrap->TcpWrap.Tx6Token.CompletionToken.Event = NULL;\r | |
47f51a06 YT |
699 | }\r |
700 | \r | |
701 | Error1:\r | |
47f51a06 YT |
702 | if (HostName != NULL) {\r |
703 | FreePool (HostName);\r | |
704 | }\r | |
705 | if (Wrap != NULL) {\r | |
706 | FreePool (Wrap);\r | |
707 | }\r | |
c0a0a5a5 | 708 | if (UrlParser != NULL) {\r |
47f51a06 YT |
709 | HttpUrlFreeParser (UrlParser);\r |
710 | }\r | |
711 | \r | |
712 | return Status;\r | |
713 | \r | |
714 | }\r | |
715 | \r | |
716 | /**\r | |
b659408b | 717 | Cancel a user's Token. \r |
47f51a06 YT |
718 | \r |
719 | @param[in] Map The HTTP instance's token queue.\r | |
720 | @param[in] Item Object container for one HTTP token and token's wrap.\r | |
721 | @param[in] Context The user's token to cancel.\r | |
722 | \r | |
723 | @retval EFI_SUCCESS Continue to check the next Item.\r | |
724 | @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.\r | |
725 | \r | |
726 | **/\r | |
727 | EFI_STATUS\r | |
728 | EFIAPI\r | |
729 | HttpCancelTokens (\r | |
730 | IN NET_MAP *Map,\r | |
731 | IN NET_MAP_ITEM *Item,\r | |
732 | IN VOID *Context\r | |
733 | )\r | |
734 | {\r | |
47f51a06 YT |
735 | EFI_HTTP_TOKEN *Token;\r |
736 | HTTP_TOKEN_WRAP *Wrap;\r | |
b659408b | 737 | HTTP_PROTOCOL *HttpInstance;\r |
47f51a06 YT |
738 | \r |
739 | Token = (EFI_HTTP_TOKEN *) Context;\r | |
740 | \r | |
741 | //\r | |
742 | // Return EFI_SUCCESS to check the next item in the map if\r | |
743 | // this one doesn't match.\r | |
744 | //\r | |
745 | if ((Token != NULL) && (Token != Item->Key)) {\r | |
746 | return EFI_SUCCESS;\r | |
747 | }\r | |
748 | \r | |
749 | Wrap = (HTTP_TOKEN_WRAP *) Item->Value;\r | |
750 | ASSERT (Wrap != NULL);\r | |
b659408b | 751 | HttpInstance = Wrap->HttpInstance;\r |
47f51a06 | 752 | \r |
b659408b | 753 | if (!HttpInstance->LocalAddressIsIPv6) {\r |
b659408b | 754 | if (Wrap->TcpWrap.Rx4Token.CompletionToken.Event != NULL) {\r |
30526a51 JW |
755 | //\r |
756 | // Cancle the Token before close its Event.\r | |
757 | //\r | |
758 | HttpInstance->Tcp4->Cancel (HttpInstance->Tcp4, &Wrap->TcpWrap.Rx4Token.CompletionToken);\r | |
47f51a06 | 759 | \r |
30526a51 JW |
760 | //\r |
761 | // Dispatch the DPC queued by the NotifyFunction of the canceled token's events.\r | |
762 | //\r | |
763 | DispatchDpc ();\r | |
b659408b | 764 | }\r |
30526a51 | 765 | } else {\r |
b659408b | 766 | if (Wrap->TcpWrap.Rx6Token.CompletionToken.Event != NULL) {\r |
30526a51 JW |
767 | //\r |
768 | // Cancle the Token before close its Event.\r | |
769 | //\r | |
770 | HttpInstance->Tcp6->Cancel (HttpInstance->Tcp6, &Wrap->TcpWrap.Rx6Token.CompletionToken);\r | |
47f51a06 | 771 | \r |
30526a51 JW |
772 | //\r |
773 | // Dispatch the DPC queued by the NotifyFunction of the canceled token's events.\r | |
774 | //\r | |
775 | DispatchDpc ();\r | |
b659408b | 776 | }\r |
47f51a06 YT |
777 | }\r |
778 | \r | |
47f51a06 YT |
779 | //\r |
780 | // If only one item is to be cancel, return EFI_ABORTED to stop\r | |
781 | // iterating the map any more.\r | |
782 | //\r | |
783 | if (Token != NULL) {\r | |
784 | return EFI_ABORTED;\r | |
785 | }\r | |
786 | \r | |
787 | return EFI_SUCCESS; \r | |
788 | }\r | |
789 | \r | |
790 | /**\r | |
791 | Cancel the user's receive/transmit request. It is the worker function of\r | |
792 | EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the\r | |
793 | token.\r | |
794 | \r | |
795 | @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.\r | |
796 | @param[in] Token The token to cancel. If NULL, all token will be\r | |
797 | cancelled.\r | |
798 | \r | |
799 | @retval EFI_SUCCESS The token is cancelled.\r | |
800 | @retval EFI_NOT_FOUND The asynchronous request or response token is not found. \r | |
801 | @retval Others Other error as indicated.\r | |
802 | \r | |
803 | **/\r | |
804 | EFI_STATUS\r | |
805 | HttpCancel (\r | |
806 | IN HTTP_PROTOCOL *HttpInstance,\r | |
807 | IN EFI_HTTP_TOKEN *Token\r | |
808 | )\r | |
809 | {\r | |
810 | EFI_STATUS Status;\r | |
811 | \r | |
812 | //\r | |
813 | // First check the tokens queued by EfiHttpRequest().\r | |
814 | //\r | |
815 | Status = NetMapIterate (&HttpInstance->TxTokens, HttpCancelTokens, Token);\r | |
816 | if (EFI_ERROR (Status)) {\r | |
817 | if (Token != NULL) {\r | |
818 | if (Status == EFI_ABORTED) {\r | |
819 | return EFI_SUCCESS;\r | |
820 | } \r | |
821 | } else {\r | |
822 | return Status;\r | |
823 | }\r | |
824 | }\r | |
825 | \r | |
dac45de3 JW |
826 | if (!HttpInstance->UseHttps) {\r |
827 | //\r | |
828 | // Then check the tokens queued by EfiHttpResponse(), except for Https.\r | |
829 | //\r | |
830 | Status = NetMapIterate (&HttpInstance->RxTokens, HttpCancelTokens, Token);\r | |
831 | if (EFI_ERROR (Status)) {\r | |
832 | if (Token != NULL) {\r | |
833 | if (Status == EFI_ABORTED) {\r | |
834 | return EFI_SUCCESS;\r | |
835 | } else {\r | |
836 | return EFI_NOT_FOUND;\r | |
837 | }\r | |
47f51a06 | 838 | } else {\r |
dac45de3 | 839 | return Status;\r |
47f51a06 | 840 | }\r |
dac45de3 JW |
841 | }\r |
842 | } else {\r | |
843 | if (!HttpInstance->LocalAddressIsIPv6) {\r | |
844 | HttpInstance->Tcp4->Cancel (HttpInstance->Tcp4, &HttpInstance->Tcp4TlsRxToken.CompletionToken);\r | |
47f51a06 | 845 | } else {\r |
dac45de3 | 846 | HttpInstance->Tcp6->Cancel (HttpInstance->Tcp6, &HttpInstance->Tcp6TlsRxToken.CompletionToken);\r |
47f51a06 YT |
847 | }\r |
848 | }\r | |
dac45de3 | 849 | \r |
47f51a06 YT |
850 | return EFI_SUCCESS;\r |
851 | }\r | |
852 | \r | |
853 | \r | |
854 | /**\r | |
855 | Abort an asynchronous HTTP request or response token.\r | |
856 | \r | |
857 | The Cancel() function aborts a pending HTTP request or response transaction. If\r | |
858 | Token is not NULL and the token is in transmit or receive queues when it is being\r | |
859 | cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will\r | |
860 | be signaled. If the token is not in one of the queues, which usually means that the\r | |
861 | asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,\r | |
862 | all asynchronous tokens issued by Request() or Response() will be aborted.\r | |
863 | \r | |
864 | @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r | |
865 | @param[in] Token Point to storage containing HTTP request or response\r | |
866 | token.\r | |
867 | \r | |
868 | @retval EFI_SUCCESS Request and Response queues are successfully flushed.\r | |
869 | @retval EFI_INVALID_PARAMETER This is NULL.\r | |
870 | @retval EFI_NOT_STARTED This instance hasn't been configured.\r | |
47f51a06 YT |
871 | @retval EFI_NOT_FOUND The asynchronous request or response token is not\r |
872 | found.\r | |
873 | @retval EFI_UNSUPPORTED The implementation does not support this function.\r | |
874 | \r | |
875 | **/\r | |
876 | EFI_STATUS\r | |
877 | EFIAPI\r | |
878 | EfiHttpCancel (\r | |
879 | IN EFI_HTTP_PROTOCOL *This,\r | |
880 | IN EFI_HTTP_TOKEN *Token\r | |
881 | )\r | |
882 | {\r | |
883 | HTTP_PROTOCOL *HttpInstance;\r | |
884 | \r | |
885 | if (This == NULL) {\r | |
886 | return EFI_INVALID_PARAMETER;\r | |
887 | }\r | |
888 | \r | |
889 | HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r | |
890 | ASSERT (HttpInstance != NULL);\r | |
891 | \r | |
892 | if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {\r | |
893 | return EFI_NOT_STARTED;\r | |
894 | }\r | |
895 | \r | |
896 | return HttpCancel (HttpInstance, Token);\r | |
897 | \r | |
898 | }\r | |
899 | \r | |
900 | /**\r | |
901 | A callback function to intercept events during message parser.\r | |
902 | \r | |
903 | This function will be invoked during HttpParseMessageBody() with various events type. An error\r | |
904 | return status of the callback function will cause the HttpParseMessageBody() aborted.\r | |
905 | \r | |
906 | @param[in] EventType Event type of this callback call.\r | |
907 | @param[in] Data A pointer to data buffer.\r | |
908 | @param[in] Length Length in bytes of the Data.\r | |
909 | @param[in] Context Callback context set by HttpInitMsgParser().\r | |
910 | \r | |
911 | @retval EFI_SUCCESS Continue to parser the message body.\r | |
912 | \r | |
913 | **/\r | |
914 | EFI_STATUS\r | |
915 | EFIAPI\r | |
916 | HttpBodyParserCallback (\r | |
917 | IN HTTP_BODY_PARSE_EVENT EventType,\r | |
918 | IN CHAR8 *Data,\r | |
919 | IN UINTN Length,\r | |
920 | IN VOID *Context\r | |
921 | )\r | |
922 | {\r | |
923 | HTTP_TOKEN_WRAP *Wrap;\r | |
5ba9f065 ZL |
924 | UINTN BodyLength;\r |
925 | CHAR8 *Body;\r | |
47f51a06 YT |
926 | \r |
927 | if (EventType != BodyParseEventOnComplete) {\r | |
928 | return EFI_SUCCESS;\r | |
929 | }\r | |
930 | \r | |
931 | if (Data == NULL || Length != 0 || Context == NULL) {\r | |
932 | return EFI_SUCCESS;\r | |
933 | }\r | |
934 | \r | |
935 | Wrap = (HTTP_TOKEN_WRAP *) Context;\r | |
5ba9f065 ZL |
936 | Body = Wrap->HttpToken->Message->Body;\r |
937 | BodyLength = Wrap->HttpToken->Message->BodyLength;\r | |
938 | if (Data < Body + BodyLength) {\r | |
939 | Wrap->HttpInstance->NextMsg = Data;\r | |
940 | } else {\r | |
941 | Wrap->HttpInstance->NextMsg = NULL;\r | |
942 | }\r | |
943 | \r | |
47f51a06 YT |
944 | \r |
945 | //\r | |
b659408b | 946 | // Free Tx4Token or Tx6Token since already received corrsponding HTTP response.\r |
47f51a06 YT |
947 | //\r |
948 | FreePool (Wrap);\r | |
949 | \r | |
950 | return EFI_SUCCESS;\r | |
951 | }\r | |
952 | \r | |
953 | /**\r | |
954 | The work function of EfiHttpResponse().\r | |
955 | \r | |
956 | @param[in] Wrap Pointer to HTTP token's wrap data.\r | |
957 | \r | |
958 | @retval EFI_SUCCESS Allocation succeeded.\r | |
959 | @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.\r | |
b659408b | 960 | @retval EFI_NOT_READY Can't find a corresponding Tx4Token/Tx6Token or \r |
5ca29abe | 961 | the EFI_HTTP_UTILITIES_PROTOCOL is not available.\r |
47f51a06 YT |
962 | \r |
963 | **/\r | |
964 | EFI_STATUS\r | |
965 | HttpResponseWorker (\r | |
966 | IN HTTP_TOKEN_WRAP *Wrap\r | |
967 | )\r | |
968 | {\r | |
969 | EFI_STATUS Status;\r | |
970 | EFI_HTTP_MESSAGE *HttpMsg;\r | |
47f51a06 YT |
971 | CHAR8 *EndofHeader;\r |
972 | CHAR8 *HttpHeaders;\r | |
973 | UINTN SizeofHeaders;\r | |
47f51a06 YT |
974 | UINTN BufferSize;\r |
975 | UINTN StatusCode;\r | |
976 | CHAR8 *Tmp;\r | |
977 | CHAR8 *HeaderTmp;\r | |
978 | CHAR8 *StatusCodeStr;\r | |
979 | UINTN BodyLen;\r | |
980 | HTTP_PROTOCOL *HttpInstance;\r | |
981 | EFI_HTTP_TOKEN *Token;\r | |
982 | NET_MAP_ITEM *Item;\r | |
983 | HTTP_TOKEN_WRAP *ValueInItem;\r | |
984 | UINTN HdrLen;\r | |
dac45de3 | 985 | NET_FRAGMENT Fragment;\r |
47f51a06 | 986 | \r |
3fd7bd08 | 987 | if (Wrap == NULL || Wrap->HttpInstance == NULL) {\r |
988 | return EFI_INVALID_PARAMETER;\r | |
989 | }\r | |
990 | \r | |
47f51a06 YT |
991 | HttpInstance = Wrap->HttpInstance;\r |
992 | Token = Wrap->HttpToken;\r | |
47f51a06 YT |
993 | HttpMsg = Token->Message;\r |
994 | \r | |
b659408b ZL |
995 | HttpInstance->EndofHeader = NULL;\r |
996 | HttpInstance->HttpHeaders = NULL;\r | |
997 | HttpMsg->Headers = NULL;\r | |
998 | HttpHeaders = NULL;\r | |
999 | SizeofHeaders = 0;\r | |
1000 | BufferSize = 0;\r | |
1001 | EndofHeader = NULL;\r | |
6be1193f | 1002 | ValueInItem = NULL;\r |
dac45de3 JW |
1003 | Fragment.Len = 0;\r |
1004 | Fragment.Bulk = NULL;\r | |
47f51a06 YT |
1005 | \r |
1006 | if (HttpMsg->Data.Response != NULL) {\r | |
47f51a06 YT |
1007 | //\r |
1008 | // Check whether we have cached header from previous call.\r | |
1009 | //\r | |
1010 | if ((HttpInstance->CacheBody != NULL) && (HttpInstance->NextMsg != NULL)) {\r | |
1011 | //\r | |
1012 | // The data is stored at [NextMsg, CacheBody + CacheLen].\r | |
1013 | //\r | |
1014 | HdrLen = HttpInstance->CacheBody + HttpInstance->CacheLen - HttpInstance->NextMsg;\r | |
1015 | HttpHeaders = AllocateZeroPool (HdrLen);\r | |
1016 | if (HttpHeaders == NULL) {\r | |
1017 | Status = EFI_OUT_OF_RESOURCES;\r | |
1018 | goto Error;\r | |
1019 | }\r | |
1020 | \r | |
1021 | CopyMem (HttpHeaders, HttpInstance->NextMsg, HdrLen);\r | |
1022 | FreePool (HttpInstance->CacheBody);\r | |
1023 | HttpInstance->CacheBody = NULL;\r | |
1024 | HttpInstance->NextMsg = NULL;\r | |
1025 | HttpInstance->CacheOffset = 0;\r | |
1026 | SizeofHeaders = HdrLen;\r | |
1027 | BufferSize = HttpInstance->CacheLen;\r | |
1028 | \r | |
1029 | //\r | |
1030 | // Check whether we cached the whole HTTP headers.\r | |
1031 | //\r | |
1032 | EndofHeader = AsciiStrStr (HttpHeaders, HTTP_END_OF_HDR_STR); \r | |
b659408b | 1033 | } \r |
47f51a06 | 1034 | \r |
b659408b ZL |
1035 | HttpInstance->EndofHeader = &EndofHeader;\r |
1036 | HttpInstance->HttpHeaders = &HttpHeaders;\r | |
47f51a06 | 1037 | \r |
b347a22a JW |
1038 | \r |
1039 | if (HttpInstance->TimeoutEvent == NULL) {\r | |
1040 | //\r | |
1041 | // Create TimeoutEvent for response\r | |
1042 | //\r | |
1043 | Status = gBS->CreateEvent (\r | |
1044 | EVT_TIMER,\r | |
1045 | TPL_CALLBACK,\r | |
1046 | NULL,\r | |
1047 | NULL,\r | |
1048 | &HttpInstance->TimeoutEvent\r | |
1049 | );\r | |
1050 | if (EFI_ERROR (Status)) {\r | |
1051 | goto Error;\r | |
1052 | }\r | |
1053 | }\r | |
1054 | \r | |
1055 | //\r | |
1056 | // Start the timer, and wait Timeout seconds to receive the header packet.\r | |
1057 | //\r | |
1058 | Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, HTTP_RESPONSE_TIMEOUT * TICKS_PER_SECOND);\r | |
1059 | if (EFI_ERROR (Status)) {\r | |
1060 | goto Error;\r | |
1061 | }\r | |
1062 | \r | |
1063 | Status = HttpTcpReceiveHeader (HttpInstance, &SizeofHeaders, &BufferSize, HttpInstance->TimeoutEvent);\r | |
1064 | \r | |
1065 | gBS->SetTimer (HttpInstance->TimeoutEvent, TimerCancel, 0);\r | |
1066 | \r | |
b659408b ZL |
1067 | if (EFI_ERROR (Status)) {\r |
1068 | goto Error;\r | |
1069 | }\r | |
47f51a06 | 1070 | \r |
1b96428d ZL |
1071 | ASSERT (HttpHeaders != NULL);\r |
1072 | \r | |
47f51a06 YT |
1073 | //\r |
1074 | // Cache the part of body.\r | |
1075 | //\r | |
1076 | BodyLen = BufferSize - (EndofHeader - HttpHeaders);\r | |
1077 | if (BodyLen > 0) {\r | |
1078 | if (HttpInstance->CacheBody != NULL) {\r | |
1079 | FreePool (HttpInstance->CacheBody);\r | |
1080 | }\r | |
1081 | \r | |
1082 | HttpInstance->CacheBody = AllocateZeroPool (BodyLen);\r | |
1083 | if (HttpInstance->CacheBody == NULL) {\r | |
1084 | Status = EFI_OUT_OF_RESOURCES;\r | |
1085 | goto Error;\r | |
1086 | }\r | |
1087 | \r | |
1088 | CopyMem (HttpInstance->CacheBody, EndofHeader, BodyLen);\r | |
1089 | HttpInstance->CacheLen = BodyLen;\r | |
1090 | }\r | |
1091 | \r | |
47f51a06 YT |
1092 | //\r |
1093 | // Search for Status Code.\r | |
1094 | //\r | |
1095 | StatusCodeStr = HttpHeaders + AsciiStrLen (HTTP_VERSION_STR) + 1;\r | |
1096 | if (StatusCodeStr == NULL) {\r | |
30526a51 | 1097 | Status = EFI_NOT_READY;\r |
47f51a06 YT |
1098 | goto Error;\r |
1099 | }\r | |
1100 | \r | |
1101 | StatusCode = AsciiStrDecimalToUintn (StatusCodeStr);\r | |
1102 | \r | |
1103 | //\r | |
1104 | // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".\r | |
1105 | //\r | |
1106 | Tmp = AsciiStrStr (HttpHeaders, HTTP_CRLF_STR);\r | |
1107 | if (Tmp == NULL) {\r | |
30526a51 | 1108 | Status = EFI_NOT_READY;\r |
47f51a06 YT |
1109 | goto Error;\r |
1110 | }\r | |
1111 | \r | |
47f51a06 | 1112 | //\r |
d8293d31 NH |
1113 | // We could have response with just a HTTP message and no headers. For Example,\r |
1114 | // "100 Continue". In such cases, we would not want to unnecessarily call a Parse\r | |
1115 | // method. A "\r\n" following Tmp string again would indicate an end. Compare and\r | |
1116 | // set SizeofHeaders to 0.\r | |
47f51a06 | 1117 | //\r |
d8293d31 NH |
1118 | Tmp = Tmp + AsciiStrLen (HTTP_CRLF_STR);\r |
1119 | if (CompareMem (Tmp, HTTP_CRLF_STR, AsciiStrLen (HTTP_CRLF_STR)) == 0) {\r | |
1120 | SizeofHeaders = 0;\r | |
1121 | } else {\r | |
1122 | SizeofHeaders = SizeofHeaders - (Tmp - HttpHeaders);\r | |
47f51a06 YT |
1123 | }\r |
1124 | \r | |
47f51a06 | 1125 | HttpMsg->Data.Response->StatusCode = HttpMappingToStatusCode (StatusCode);\r |
072289f4 | 1126 | HttpInstance->StatusCode = StatusCode;\r |
d8293d31 | 1127 | \r |
47f51a06 YT |
1128 | Status = EFI_NOT_READY;\r |
1129 | ValueInItem = NULL;\r | |
47f51a06 YT |
1130 | \r |
1131 | //\r | |
97c567ef | 1132 | // In cases of PUT/POST/PATCH, after an initial request-response pair, we would do a\r |
d8293d31 NH |
1133 | // continuous request without a response call. So, we would not do an insert of\r |
1134 | // TxToken. After we have sent the complete file, we will call a response to get\r | |
1135 | // a final response from server. In such a case, we would not have any TxTokens.\r | |
1136 | // Hence, check that case before doing a NetMapRemoveHead.\r | |
47f51a06 | 1137 | //\r |
d8293d31 NH |
1138 | if (!NetMapIsEmpty (&HttpInstance->TxTokens)) {\r |
1139 | NetMapRemoveHead (&HttpInstance->TxTokens, (VOID**) &ValueInItem);\r | |
1140 | if (ValueInItem == NULL) {\r | |
1141 | goto Error;\r | |
1142 | }\r | |
47f51a06 | 1143 | \r |
d8293d31 NH |
1144 | //\r |
1145 | // The first Tx Token not transmitted yet, insert back and return error.\r | |
1146 | //\r | |
1147 | if (!ValueInItem->TcpWrap.IsTxDone) {\r | |
1148 | goto Error2;\r | |
1149 | }\r | |
47f51a06 YT |
1150 | }\r |
1151 | \r | |
d8293d31 NH |
1152 | if (SizeofHeaders != 0) {\r |
1153 | HeaderTmp = AllocateZeroPool (SizeofHeaders);\r | |
1154 | if (HeaderTmp == NULL) {\r | |
30526a51 | 1155 | Status = EFI_OUT_OF_RESOURCES;\r |
5646819f | 1156 | goto Error2;\r |
d8293d31 NH |
1157 | }\r |
1158 | \r | |
1159 | CopyMem (HeaderTmp, Tmp, SizeofHeaders);\r | |
1160 | FreePool (HttpHeaders);\r | |
1161 | HttpHeaders = HeaderTmp;\r | |
1162 | \r | |
1163 | //\r | |
1164 | // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available.\r | |
1165 | //\r | |
1166 | if (mHttpUtilities == NULL) {\r | |
1167 | Status = EFI_NOT_READY;\r | |
5646819f | 1168 | goto Error2;\r |
d8293d31 NH |
1169 | }\r |
1170 | \r | |
1171 | //\r | |
1172 | // Parse the HTTP header into array of key/value pairs.\r | |
1173 | //\r | |
1174 | Status = mHttpUtilities->Parse (\r | |
1175 | mHttpUtilities,\r | |
1176 | HttpHeaders,\r | |
1177 | SizeofHeaders,\r | |
1178 | &HttpMsg->Headers,\r | |
1179 | &HttpMsg->HeaderCount\r | |
1180 | );\r | |
1181 | if (EFI_ERROR (Status)) {\r | |
5646819f | 1182 | goto Error2;\r |
d8293d31 NH |
1183 | }\r |
1184 | \r | |
1185 | FreePool (HttpHeaders);\r | |
1186 | HttpHeaders = NULL;\r | |
1187 | \r | |
1188 | \r | |
1189 | //\r | |
1190 | // Init message-body parser by header information.\r | |
1191 | //\r | |
1192 | Status = HttpInitMsgParser (\r | |
1193 | HttpInstance->Method,\r | |
1194 | HttpMsg->Data.Response->StatusCode,\r | |
1195 | HttpMsg->HeaderCount,\r | |
1196 | HttpMsg->Headers,\r | |
1197 | HttpBodyParserCallback,\r | |
1198 | (VOID *) ValueInItem,\r | |
1199 | &HttpInstance->MsgParser\r | |
1200 | );\r | |
47f51a06 YT |
1201 | if (EFI_ERROR (Status)) {\r |
1202 | goto Error2;\r | |
1203 | }\r | |
1204 | \r | |
d8293d31 NH |
1205 | //\r |
1206 | // Check whether we received a complete HTTP message.\r | |
1207 | //\r | |
1208 | if (HttpInstance->CacheBody != NULL) {\r | |
1209 | Status = HttpParseMessageBody (HttpInstance->MsgParser, HttpInstance->CacheLen, HttpInstance->CacheBody);\r | |
1210 | if (EFI_ERROR (Status)) {\r | |
1211 | goto Error2;\r | |
1212 | }\r | |
1213 | \r | |
1214 | if (HttpIsMessageComplete (HttpInstance->MsgParser)) {\r | |
1215 | //\r | |
1216 | // Free the MsgParse since we already have a full HTTP message.\r | |
1217 | //\r | |
1218 | HttpFreeMsgParser (HttpInstance->MsgParser);\r | |
1219 | HttpInstance->MsgParser = NULL;\r | |
1220 | }\r | |
47f51a06 YT |
1221 | }\r |
1222 | }\r | |
1223 | \r | |
d8293d31 | 1224 | if ((HttpMsg->Body == NULL) || (HttpMsg->BodyLength == 0)) {\r |
47f51a06 YT |
1225 | Status = EFI_SUCCESS;\r |
1226 | goto Exit;\r | |
1227 | }\r | |
d8293d31 | 1228 | }\r |
47f51a06 YT |
1229 | \r |
1230 | //\r | |
1231 | // Receive the response body.\r | |
1232 | //\r | |
1233 | BodyLen = 0;\r | |
1234 | \r | |
1235 | //\r | |
1236 | // First check whether we cached some data.\r | |
1237 | //\r | |
1238 | if (HttpInstance->CacheBody != NULL) {\r | |
1239 | //\r | |
1240 | // Calculate the length of the cached data.\r | |
1241 | //\r | |
1242 | if (HttpInstance->NextMsg != NULL) {\r | |
1243 | //\r | |
1244 | // We have a cached HTTP message which includes a part of HTTP header of next message.\r | |
1245 | //\r | |
1246 | BodyLen = HttpInstance->NextMsg - (HttpInstance->CacheBody + HttpInstance->CacheOffset); \r | |
1247 | } else {\r | |
1248 | BodyLen = HttpInstance->CacheLen - HttpInstance->CacheOffset;\r | |
1249 | }\r | |
1250 | \r | |
1251 | if (BodyLen > 0) {\r | |
1252 | //\r | |
1253 | // We have some cached data. Just copy the data and return.\r | |
1254 | //\r | |
1255 | if (HttpMsg->BodyLength < BodyLen) {\r | |
1256 | CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, HttpMsg->BodyLength);\r | |
1257 | HttpInstance->CacheOffset = HttpInstance->CacheOffset + HttpMsg->BodyLength;\r | |
1258 | } else {\r | |
1259 | //\r | |
1260 | // Copy all cached data out.\r | |
1261 | //\r | |
1262 | CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, BodyLen);\r | |
1263 | HttpInstance->CacheOffset = BodyLen + HttpInstance->CacheOffset;\r | |
1264 | HttpMsg->BodyLength = BodyLen;\r | |
1265 | \r | |
1266 | if (HttpInstance->NextMsg == NULL) {\r | |
1267 | //\r | |
1268 | // There is no HTTP header of next message. Just free the cache buffer.\r | |
1269 | //\r | |
1270 | FreePool (HttpInstance->CacheBody);\r | |
1271 | HttpInstance->CacheBody = NULL;\r | |
1272 | HttpInstance->NextMsg = NULL;\r | |
1273 | HttpInstance->CacheOffset = 0;\r | |
1274 | }\r | |
1275 | }\r | |
1276 | //\r | |
1277 | // Return since we aready received required data.\r | |
1278 | //\r | |
1279 | Status = EFI_SUCCESS;\r | |
1280 | goto Exit;\r | |
1281 | } \r | |
1282 | \r | |
1283 | if (BodyLen == 0 && HttpInstance->MsgParser == NULL) {\r | |
1284 | //\r | |
1285 | // We received a complete HTTP message, and we don't have more data to return to caller.\r | |
1286 | //\r | |
1287 | HttpMsg->BodyLength = 0;\r | |
1288 | Status = EFI_SUCCESS;\r | |
1289 | goto Exit; \r | |
1290 | } \r | |
1291 | }\r | |
1292 | \r | |
1293 | ASSERT (HttpInstance->MsgParser != NULL);\r | |
1294 | \r | |
1295 | //\r | |
1296 | // We still need receive more data when there is no cache data and MsgParser is not NULL;\r | |
1297 | //\r | |
dac45de3 JW |
1298 | if (!HttpInstance->UseHttps) {\r |
1299 | Status = HttpTcpReceiveBody (Wrap, HttpMsg);\r | |
1300 | \r | |
1301 | if (EFI_ERROR (Status)) {\r | |
1302 | goto Error2;\r | |
1303 | }\r | |
1304 | \r | |
1305 | } else {\r | |
1306 | if (HttpInstance->TimeoutEvent == NULL) {\r | |
1307 | //\r | |
1308 | // Create TimeoutEvent for response\r | |
1309 | //\r | |
1310 | Status = gBS->CreateEvent (\r | |
1311 | EVT_TIMER,\r | |
1312 | TPL_CALLBACK,\r | |
1313 | NULL,\r | |
1314 | NULL,\r | |
1315 | &HttpInstance->TimeoutEvent\r | |
1316 | );\r | |
1317 | if (EFI_ERROR (Status)) {\r | |
1318 | goto Error2;\r | |
1319 | }\r | |
1320 | }\r | |
1321 | \r | |
1322 | //\r | |
1323 | // Start the timer, and wait Timeout seconds to receive the body packet.\r | |
1324 | //\r | |
1325 | Status = gBS->SetTimer (HttpInstance->TimeoutEvent, TimerRelative, HTTP_RESPONSE_TIMEOUT * TICKS_PER_SECOND);\r | |
1326 | if (EFI_ERROR (Status)) {\r | |
1327 | goto Error2;\r | |
1328 | }\r | |
1329 | \r | |
1330 | Status = HttpsReceive (HttpInstance, &Fragment, HttpInstance->TimeoutEvent);\r | |
1331 | \r | |
1332 | gBS->SetTimer (HttpInstance->TimeoutEvent, TimerCancel, 0);\r | |
1333 | \r | |
1334 | if (EFI_ERROR (Status)) {\r | |
1335 | goto Error2;\r | |
1336 | }\r | |
1337 | \r | |
1338 | //\r | |
1339 | // Check whether we receive a complete HTTP message.\r | |
1340 | //\r | |
1341 | Status = HttpParseMessageBody (\r | |
1342 | HttpInstance->MsgParser,\r | |
1343 | (UINTN) Fragment.Len,\r | |
1344 | (CHAR8 *) Fragment.Bulk\r | |
1345 | );\r | |
1346 | if (EFI_ERROR (Status)) {\r | |
1347 | goto Error2;\r | |
1348 | }\r | |
1349 | \r | |
1350 | if (HttpIsMessageComplete (HttpInstance->MsgParser)) {\r | |
1351 | //\r | |
1352 | // Free the MsgParse since we already have a full HTTP message.\r | |
1353 | //\r | |
1354 | HttpFreeMsgParser (HttpInstance->MsgParser);\r | |
1355 | HttpInstance->MsgParser = NULL;\r | |
1356 | }\r | |
1357 | \r | |
1358 | //\r | |
1359 | // We receive part of header of next HTTP msg.\r | |
1360 | //\r | |
1361 | if (HttpInstance->NextMsg != NULL) {\r | |
cb7dbc13 | 1362 | HttpMsg->BodyLength = MIN ((UINTN) HttpInstance->NextMsg - (UINTN) Fragment.Bulk, HttpMsg->BodyLength);\r |
dac45de3 JW |
1363 | CopyMem (HttpMsg->Body, Fragment.Bulk, HttpMsg->BodyLength);\r |
1364 | \r | |
1365 | HttpInstance->CacheLen = Fragment.Len - HttpMsg->BodyLength;\r | |
1366 | if (HttpInstance->CacheLen != 0) {\r | |
1367 | if (HttpInstance->CacheBody != NULL) {\r | |
1368 | FreePool (HttpInstance->CacheBody);\r | |
1369 | }\r | |
1370 | \r | |
1371 | HttpInstance->CacheBody = AllocateZeroPool (HttpInstance->CacheLen);\r | |
1372 | if (HttpInstance->CacheBody == NULL) {\r | |
1373 | Status = EFI_OUT_OF_RESOURCES;\r | |
1374 | goto Error2;\r | |
1375 | }\r | |
1376 | \r | |
1377 | CopyMem (HttpInstance->CacheBody, Fragment.Bulk + HttpMsg->BodyLength, HttpInstance->CacheLen);\r | |
1378 | HttpInstance->CacheOffset = 0;\r | |
1379 | \r | |
cb7dbc13 | 1380 | HttpInstance->NextMsg = HttpInstance->CacheBody + ((UINTN) HttpInstance->NextMsg - (UINTN) (Fragment.Bulk + HttpMsg->BodyLength));\r |
dac45de3 JW |
1381 | }\r |
1382 | } else {\r | |
1383 | HttpMsg->BodyLength = MIN (Fragment.Len, (UINT32) HttpMsg->BodyLength);\r | |
1384 | CopyMem (HttpMsg->Body, Fragment.Bulk, HttpMsg->BodyLength);\r | |
1385 | HttpInstance->CacheLen = Fragment.Len - HttpMsg->BodyLength;\r | |
1386 | if (HttpInstance->CacheLen != 0) {\r | |
1387 | if (HttpInstance->CacheBody != NULL) {\r | |
1388 | FreePool (HttpInstance->CacheBody);\r | |
1389 | }\r | |
1390 | \r | |
1391 | HttpInstance->CacheBody = AllocateZeroPool (HttpInstance->CacheLen);\r | |
1392 | if (HttpInstance->CacheBody == NULL) {\r | |
1393 | Status = EFI_OUT_OF_RESOURCES;\r | |
1394 | goto Error2;\r | |
1395 | }\r | |
1396 | \r | |
1397 | CopyMem (HttpInstance->CacheBody, Fragment.Bulk + HttpMsg->BodyLength, HttpInstance->CacheLen);\r | |
1398 | HttpInstance->CacheOffset = 0;\r | |
1399 | }\r | |
1400 | }\r | |
1401 | \r | |
1402 | if (Fragment.Bulk != NULL) {\r | |
1403 | FreePool (Fragment.Bulk);\r | |
1404 | Fragment.Bulk = NULL;\r | |
1405 | }\r | |
1406 | \r | |
1407 | goto Exit;\r | |
47f51a06 YT |
1408 | }\r |
1409 | \r | |
1410 | return Status;\r | |
1411 | \r | |
1412 | Exit:\r | |
1413 | Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken);\r | |
1414 | if (Item != NULL) {\r | |
1415 | NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL);\r | |
1416 | }\r | |
072289f4 ZL |
1417 | \r |
1418 | if (HttpInstance->StatusCode >= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE) {\r | |
1419 | Token->Status = EFI_HTTP_ERROR;\r | |
1420 | } else {\r | |
1421 | Token->Status = Status;\r | |
1422 | }\r | |
1423 | \r | |
47f51a06 | 1424 | gBS->SignalEvent (Token->Event);\r |
b659408b | 1425 | HttpCloseTcpRxEvent (Wrap);\r |
47f51a06 YT |
1426 | FreePool (Wrap);\r |
1427 | return Status;\r | |
1428 | \r | |
1429 | Error2:\r | |
5646819f FS |
1430 | if (ValueInItem != NULL) {\r |
1431 | NetMapInsertHead (&HttpInstance->TxTokens, ValueInItem->HttpToken, ValueInItem);\r | |
1432 | }\r | |
47f51a06 YT |
1433 | \r |
1434 | Error:\r | |
30526a51 JW |
1435 | Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken);\r |
1436 | if (Item != NULL) {\r | |
1437 | NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL);\r | |
1438 | }\r | |
dac45de3 JW |
1439 | \r |
1440 | if (!HttpInstance->UseHttps) {\r | |
1441 | HttpTcpTokenCleanup (Wrap);\r | |
1442 | } else {\r | |
1443 | FreePool (Wrap);\r | |
1444 | }\r | |
47f51a06 YT |
1445 | \r |
1446 | if (HttpHeaders != NULL) {\r | |
1447 | FreePool (HttpHeaders);\r | |
dac45de3 JW |
1448 | HttpHeaders = NULL;\r |
1449 | }\r | |
1450 | \r | |
1451 | if (Fragment.Bulk != NULL) {\r | |
1452 | FreePool (Fragment.Bulk);\r | |
1453 | Fragment.Bulk = NULL;\r | |
47f51a06 YT |
1454 | }\r |
1455 | \r | |
1456 | if (HttpMsg->Headers != NULL) {\r | |
1457 | FreePool (HttpMsg->Headers);\r | |
dac45de3 | 1458 | HttpMsg->Headers = NULL;\r |
47f51a06 YT |
1459 | }\r |
1460 | \r | |
1461 | if (HttpInstance->CacheBody != NULL) {\r | |
1462 | FreePool (HttpInstance->CacheBody);\r | |
1463 | HttpInstance->CacheBody = NULL;\r | |
1464 | }\r | |
1465 | \r | |
072289f4 ZL |
1466 | if (HttpInstance->StatusCode >= HTTP_ERROR_OR_NOT_SUPPORT_STATUS_CODE) {\r |
1467 | Token->Status = EFI_HTTP_ERROR;\r | |
1468 | } else {\r | |
1469 | Token->Status = Status;\r | |
1470 | }\r | |
1471 | \r | |
47f51a06 YT |
1472 | gBS->SignalEvent (Token->Event);\r |
1473 | \r | |
1474 | return Status; \r | |
1475 | \r | |
1476 | }\r | |
1477 | \r | |
1478 | \r | |
1479 | /**\r | |
1480 | The Response() function queues an HTTP response to this HTTP instance, similar to\r | |
f0ab5a81 | 1481 | Receive() function in the EFI TCP driver. When the HTTP response is received successfully,\r |
47f51a06 YT |
1482 | or if there is an error, Status in token will be updated and Event will be signaled.\r |
1483 | \r | |
1484 | The HTTP driver will queue a receive token to the underlying TCP instance. When data\r | |
1485 | is received in the underlying TCP instance, the data will be parsed and Token will\r | |
1486 | be populated with the response data. If the data received from the remote host\r | |
1487 | contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting\r | |
1488 | (asynchronously) for more data to be sent from the remote host before signaling\r | |
1489 | Event in Token.\r | |
1490 | \r | |
1491 | It is the responsibility of the caller to allocate a buffer for Body and specify the\r | |
1492 | size in BodyLength. If the remote host provides a response that contains a content\r | |
1493 | body, up to BodyLength bytes will be copied from the receive buffer into Body and\r | |
1494 | BodyLength will be updated with the amount of bytes received and copied to Body. This\r | |
1495 | allows the client to download a large file in chunks instead of into one contiguous\r | |
1496 | block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is\r | |
1497 | non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive\r | |
1498 | token to underlying TCP instance. If data arrives in the receive buffer, up to\r | |
1499 | BodyLength bytes of data will be copied to Body. The HTTP driver will then update\r | |
1500 | BodyLength with the amount of bytes received and copied to Body.\r | |
1501 | \r | |
1502 | If the HTTP driver does not have an open underlying TCP connection with the host\r | |
1503 | specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is\r | |
1504 | consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain\r | |
1505 | an open TCP connection between client and host.\r | |
1506 | \r | |
1507 | @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r | |
1508 | @param[in] Token Pointer to storage containing HTTP response token.\r | |
1509 | \r | |
1510 | @retval EFI_SUCCESS Allocation succeeded.\r | |
1511 | @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been\r | |
1512 | initialized.\r | |
1513 | @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r | |
1514 | This is NULL.\r | |
1515 | Token is NULL.\r | |
1516 | Token->Message->Headers is NULL.\r | |
1517 | Token->Message is NULL.\r | |
1518 | Token->Message->Body is not NULL,\r | |
1519 | Token->Message->BodyLength is non-zero, and\r | |
1520 | Token->Message->Data is NULL, but a previous call to\r | |
1521 | Response() has not been completed successfully.\r | |
1522 | @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.\r | |
1523 | @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host\r | |
1524 | specified by response URL.\r | |
1525 | **/\r | |
1526 | EFI_STATUS\r | |
1527 | EFIAPI\r | |
1528 | EfiHttpResponse (\r | |
1529 | IN EFI_HTTP_PROTOCOL *This,\r | |
1530 | IN EFI_HTTP_TOKEN *Token\r | |
1531 | )\r | |
1532 | {\r | |
1533 | EFI_STATUS Status;\r | |
1534 | EFI_HTTP_MESSAGE *HttpMsg;\r | |
1535 | HTTP_PROTOCOL *HttpInstance;\r | |
1536 | HTTP_TOKEN_WRAP *Wrap;\r | |
1537 | \r | |
1538 | if ((This == NULL) || (Token == NULL)) {\r | |
1539 | return EFI_INVALID_PARAMETER;\r | |
1540 | }\r | |
1541 | \r | |
1542 | HttpMsg = Token->Message;\r | |
1543 | if (HttpMsg == NULL) {\r | |
1544 | return EFI_INVALID_PARAMETER;\r | |
1545 | }\r | |
1546 | \r | |
1547 | HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r | |
1548 | ASSERT (HttpInstance != NULL);\r | |
1549 | \r | |
1550 | if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {\r | |
1551 | return EFI_NOT_STARTED;\r | |
1552 | }\r | |
1553 | \r | |
47f51a06 YT |
1554 | //\r |
1555 | // Check whether the token already existed.\r | |
1556 | //\r | |
1557 | if (EFI_ERROR (NetMapIterate (&HttpInstance->RxTokens, HttpTokenExist, Token))) {\r | |
1558 | return EFI_ACCESS_DENIED; \r | |
1559 | }\r | |
1560 | \r | |
1561 | Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));\r | |
1562 | if (Wrap == NULL) {\r | |
1563 | return EFI_OUT_OF_RESOURCES;\r | |
1564 | }\r | |
1565 | \r | |
1566 | Wrap->HttpInstance = HttpInstance;\r | |
1567 | Wrap->HttpToken = Token;\r | |
1568 | \r | |
dac45de3 JW |
1569 | //\r |
1570 | // Notes: For Https, receive token wrapped in HTTP_TOKEN_WRAP is not used to \r | |
1571 | // receive the https response. A special TlsRxToken is used for receiving TLS \r | |
1572 | // related messages. It should be a blocking response.\r | |
1573 | //\r | |
1574 | if (!HttpInstance->UseHttps) {\r | |
1575 | Status = HttpCreateTcpRxEvent (Wrap);\r | |
1576 | if (EFI_ERROR (Status)) {\r | |
1577 | goto Error;\r | |
1578 | }\r | |
47f51a06 YT |
1579 | }\r |
1580 | \r | |
1581 | Status = NetMapInsertTail (&HttpInstance->RxTokens, Token, Wrap);\r | |
1582 | if (EFI_ERROR (Status)) {\r | |
1583 | goto Error;\r | |
1584 | }\r | |
1585 | \r | |
1586 | //\r | |
1587 | // If already have pending RxTokens, return directly.\r | |
1588 | //\r | |
1589 | if (NetMapGetCount (&HttpInstance->RxTokens) > 1) {\r | |
1590 | return EFI_SUCCESS;\r | |
1591 | }\r | |
1592 | \r | |
1593 | return HttpResponseWorker (Wrap);\r | |
1594 | \r | |
1595 | Error:\r | |
1596 | if (Wrap != NULL) {\r | |
b659408b ZL |
1597 | if (Wrap->TcpWrap.Rx4Token.CompletionToken.Event != NULL) {\r |
1598 | gBS->CloseEvent (Wrap->TcpWrap.Rx4Token.CompletionToken.Event);\r | |
1599 | }\r | |
1600 | \r | |
1601 | if (Wrap->TcpWrap.Rx6Token.CompletionToken.Event != NULL) {\r | |
1602 | gBS->CloseEvent (Wrap->TcpWrap.Rx6Token.CompletionToken.Event);\r | |
47f51a06 YT |
1603 | }\r |
1604 | FreePool (Wrap);\r | |
1605 | } \r | |
1606 | \r | |
1607 | return Status; \r | |
1608 | }\r | |
1609 | \r | |
1610 | /**\r | |
1611 | The Poll() function can be used by network drivers and applications to increase the\r | |
1612 | rate that data packets are moved between the communication devices and the transmit\r | |
1613 | and receive queues.\r | |
1614 | \r | |
1615 | In some systems, the periodic timer event in the managed network driver may not poll\r | |
1616 | the underlying communications device fast enough to transmit and/or receive all data\r | |
1617 | packets without missing incoming packets or dropping outgoing packets. Drivers and\r | |
1618 | applications that are experiencing packet loss should try calling the Poll() function\r | |
1619 | more often.\r | |
1620 | \r | |
1621 | @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r | |
1622 | \r | |
1623 | @retval EFI_SUCCESS Incoming or outgoing data was processed.\r | |
1624 | @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r | |
1625 | @retval EFI_INVALID_PARAMETER This is NULL.\r | |
1626 | @retval EFI_NOT_READY No incoming or outgoing data is processed.\r | |
1627 | @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.\r | |
1628 | \r | |
1629 | **/\r | |
1630 | EFI_STATUS\r | |
1631 | EFIAPI\r | |
1632 | EfiHttpPoll (\r | |
1633 | IN EFI_HTTP_PROTOCOL *This\r | |
1634 | )\r | |
1635 | {\r | |
49c9f74c | 1636 | EFI_STATUS Status;\r |
b659408b | 1637 | HTTP_PROTOCOL *HttpInstance;\r |
47f51a06 YT |
1638 | \r |
1639 | if (This == NULL) {\r | |
1640 | return EFI_INVALID_PARAMETER;\r | |
1641 | }\r | |
1642 | \r | |
1643 | HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r | |
1644 | ASSERT (HttpInstance != NULL);\r | |
1645 | \r | |
1b96428d | 1646 | if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {\r |
47f51a06 YT |
1647 | return EFI_NOT_STARTED;\r |
1648 | }\r | |
b659408b ZL |
1649 | \r |
1650 | if (HttpInstance->LocalAddressIsIPv6) {\r | |
1b96428d ZL |
1651 | if (HttpInstance->Tcp6 == NULL) {\r |
1652 | return EFI_NOT_STARTED;\r | |
1653 | }\r | |
b659408b ZL |
1654 | Status = HttpInstance->Tcp6->Poll (HttpInstance->Tcp6);\r |
1655 | } else {\r | |
1b96428d ZL |
1656 | if (HttpInstance->Tcp4 == NULL) {\r |
1657 | return EFI_NOT_STARTED;\r | |
1658 | }\r | |
b659408b ZL |
1659 | Status = HttpInstance->Tcp4->Poll (HttpInstance->Tcp4);\r |
1660 | }\r | |
1661 | \r | |
49c9f74c | 1662 | DispatchDpc ();\r |
b659408b | 1663 | \r |
49c9f74c | 1664 | return Status;\r |
47f51a06 | 1665 | }\r |