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