]>
Commit | Line | Data |
---|---|---|
47f51a06 YT |
1 | /** @file\r |
2 | Implementation of EFI_HTTP_PROTOCOL protocol interfaces.\r | |
3 | \r | |
4 | Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r | |
c5a693ce | 5 | (C) Copyright 2015 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 | |
36 | HTTP instance.\r | |
37 | \r | |
38 | @retval EFI_SUCCESS Operation succeeded.\r | |
39 | @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r | |
40 | This is NULL.\r | |
41 | HttpConfigData is NULL.\r | |
42 | HttpConfigData->AccessPoint is NULL.\r | |
43 | @retval EFI_NOT_STARTED The HTTP instance is not configured.\r | |
44 | \r | |
45 | **/\r | |
46 | EFI_STATUS\r | |
47 | EFIAPI\r | |
48 | EfiHttpGetModeData (\r | |
49 | IN EFI_HTTP_PROTOCOL *This,\r | |
50 | OUT EFI_HTTP_CONFIG_DATA *HttpConfigData\r | |
51 | )\r | |
52 | {\r | |
53 | HTTP_PROTOCOL *HttpInstance;\r | |
b659408b ZL |
54 | EFI_HTTPv4_ACCESS_POINT *Http4AccessPoint;\r |
55 | EFI_HTTPv6_ACCESS_POINT *Http6AccessPoint;\r | |
47f51a06 YT |
56 | \r |
57 | if ((This == NULL) || (HttpConfigData == NULL)) {\r | |
58 | return EFI_INVALID_PARAMETER;\r | |
59 | }\r | |
60 | \r | |
61 | HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r | |
62 | ASSERT (HttpInstance != NULL);\r | |
b659408b | 63 | \r |
47f51a06 YT |
64 | if (HttpInstance->State < HTTP_STATE_HTTP_CONFIGED) {\r |
65 | return EFI_NOT_STARTED;\r | |
66 | }\r | |
67 | \r | |
47f51a06 YT |
68 | HttpConfigData->HttpVersion = HttpInstance->HttpVersion;\r |
69 | HttpConfigData->TimeOutMillisec = HttpInstance->TimeOutMillisec;\r | |
70 | HttpConfigData->LocalAddressIsIPv6 = HttpInstance->LocalAddressIsIPv6;\r | |
71 | \r | |
b659408b ZL |
72 | if (HttpInstance->LocalAddressIsIPv6) {\r |
73 | Http6AccessPoint = AllocateZeroPool (sizeof (EFI_HTTPv6_ACCESS_POINT));\r | |
74 | CopyMem (\r | |
75 | Http6AccessPoint,\r | |
76 | &HttpInstance->Ipv6Node,\r | |
77 | sizeof (HttpInstance->Ipv6Node)\r | |
47f51a06 | 78 | );\r |
b659408b ZL |
79 | HttpConfigData->AccessPoint.IPv6Node = Http6AccessPoint;\r |
80 | } else {\r | |
81 | Http4AccessPoint = AllocateZeroPool (sizeof (EFI_HTTPv4_ACCESS_POINT));\r | |
82 | CopyMem (\r | |
83 | Http4AccessPoint,\r | |
84 | &HttpInstance->IPv4Node,\r | |
85 | sizeof (HttpInstance->IPv4Node)\r | |
86 | );\r | |
87 | HttpConfigData->AccessPoint.IPv4Node = Http4AccessPoint;\r | |
88 | }\r | |
47f51a06 YT |
89 | \r |
90 | return EFI_SUCCESS;\r | |
91 | }\r | |
92 | \r | |
93 | /**\r | |
94 | Initialize or brutally reset the operational parameters for this EFI HTTP instance.\r | |
95 | \r | |
96 | The Configure() function does the following:\r | |
97 | When HttpConfigData is not NULL Initialize this EFI HTTP instance by configuring\r | |
98 | timeout, local address, port, etc.\r | |
99 | When HttpConfigData is NULL, reset this EFI HTTP instance by closing all active\r | |
100 | connections with remote hosts, canceling all asynchronous tokens, and flush request\r | |
101 | and response buffers without informing the appropriate hosts.\r | |
102 | \r | |
103 | Except for GetModeData() and Configure(), No other EFI HTTP function can be executed\r | |
104 | by this instance until the Configure() function is executed and returns successfully.\r | |
105 | \r | |
106 | @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r | |
107 | @param[in] HttpConfigData Pointer to the configure data to configure the instance.\r | |
108 | \r | |
109 | @retval EFI_SUCCESS Operation succeeded.\r | |
110 | @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r | |
111 | This is NULL.\r | |
112 | HttpConfigData->LocalAddressIsIPv6 is FALSE and\r | |
113 | HttpConfigData->IPv4Node is NULL.\r | |
114 | HttpConfigData->LocalAddressIsIPv6 is TRUE and\r | |
115 | HttpConfigData->IPv6Node is NULL.\r | |
116 | @retval EFI_ALREADY_STARTED Reinitialize this HTTP instance without calling\r | |
117 | Configure() with NULL to reset it.\r | |
118 | @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r | |
119 | @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources when\r | |
120 | executing Configure().\r | |
121 | @retval EFI_UNSUPPORTED One or more options in HttpConfigData are not supported\r | |
122 | in the implementation.\r | |
123 | **/\r | |
124 | EFI_STATUS\r | |
125 | EFIAPI\r | |
126 | EfiHttpConfigure (\r | |
127 | IN EFI_HTTP_PROTOCOL *This,\r | |
128 | IN EFI_HTTP_CONFIG_DATA *HttpConfigData\r | |
129 | ) \r | |
130 | {\r | |
131 | HTTP_PROTOCOL *HttpInstance;\r | |
132 | EFI_STATUS Status;\r | |
b659408b ZL |
133 | \r |
134 | //\r | |
135 | // Check input parameters.\r | |
136 | //\r | |
137 | if (This == NULL || \r | |
138 | (HttpConfigData != NULL && ((HttpConfigData->LocalAddressIsIPv6 && HttpConfigData->AccessPoint.IPv6Node == NULL) ||\r | |
139 | (!HttpConfigData->LocalAddressIsIPv6 && HttpConfigData->AccessPoint.IPv4Node == NULL)))) {\r | |
47f51a06 YT |
140 | return EFI_INVALID_PARAMETER;\r |
141 | }\r | |
142 | \r | |
143 | HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r | |
144 | ASSERT (HttpInstance != NULL && HttpInstance->Service != NULL);\r | |
145 | \r | |
146 | if (HttpConfigData != NULL) {\r | |
b659408b | 147 | \r |
47f51a06 YT |
148 | //\r |
149 | // Now configure this HTTP instance.\r | |
150 | //\r | |
151 | if (HttpInstance->State != HTTP_STATE_UNCONFIGED) {\r | |
152 | return EFI_ALREADY_STARTED;\r | |
153 | }\r | |
154 | \r | |
155 | HttpInstance->HttpVersion = HttpConfigData->HttpVersion;\r | |
156 | HttpInstance->TimeOutMillisec = HttpConfigData->TimeOutMillisec;\r | |
157 | HttpInstance->LocalAddressIsIPv6 = HttpConfigData->LocalAddressIsIPv6;\r | |
b659408b ZL |
158 | \r |
159 | if (HttpConfigData->LocalAddressIsIPv6) { \r | |
160 | CopyMem (\r | |
161 | &HttpInstance->Ipv6Node,\r | |
162 | HttpConfigData->AccessPoint.IPv6Node,\r | |
163 | sizeof (HttpInstance->Ipv6Node)\r | |
164 | );\r | |
47f51a06 YT |
165 | } else {\r |
166 | CopyMem (\r | |
167 | &HttpInstance->IPv4Node,\r | |
168 | HttpConfigData->AccessPoint.IPv4Node,\r | |
169 | sizeof (HttpInstance->IPv4Node)\r | |
170 | );\r | |
47f51a06 | 171 | }\r |
b659408b ZL |
172 | //\r |
173 | // Creat Tcp child\r | |
174 | //\r | |
175 | Status = HttpInitProtocol (HttpInstance, HttpInstance->LocalAddressIsIPv6);\r | |
176 | if (EFI_ERROR (Status)) {\r | |
177 | return Status;\r | |
178 | }\r | |
179 | \r | |
180 | HttpInstance->State = HTTP_STATE_HTTP_CONFIGED;\r | |
181 | return EFI_SUCCESS;\r | |
47f51a06 YT |
182 | \r |
183 | } else {\r | |
b659408b ZL |
184 | //\r |
185 | // Reset all the resources related to HttpInsance.\r | |
186 | //\r | |
187 | HttpCleanProtocol (HttpInstance);\r | |
188 | HttpInstance->State = HTTP_STATE_UNCONFIGED;\r | |
189 | return EFI_SUCCESS;\r | |
47f51a06 YT |
190 | }\r |
191 | }\r | |
192 | \r | |
193 | \r | |
194 | /**\r | |
195 | The Request() function queues an HTTP request to this HTTP instance.\r | |
196 | \r | |
197 | Similar to Transmit() function in the EFI TCP driver. When the HTTP request is sent\r | |
198 | successfully, or if there is an error, Status in token will be updated and Event will\r | |
199 | be signaled.\r | |
200 | \r | |
201 | @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r | |
202 | @param[in] Token Pointer to storage containing HTTP request token.\r | |
203 | \r | |
204 | @retval EFI_SUCCESS Outgoing data was processed.\r | |
205 | @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.\r | |
206 | @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r | |
207 | @retval EFI_TIMEOUT Data was dropped out of the transmit or receive queue.\r | |
208 | @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.\r | |
209 | @retval EFI_UNSUPPORTED The HTTP method is not supported in current\r | |
210 | implementation.\r | |
211 | @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r | |
212 | This is NULL.\r | |
213 | Token->Message is NULL.\r | |
214 | Token->Message->Body is not NULL,\r | |
215 | Token->Message->BodyLength is non-zero, and\r | |
216 | Token->Message->Data is NULL, but a previous call to\r | |
217 | Request()has not been completed successfully.\r | |
218 | **/\r | |
219 | EFI_STATUS\r | |
220 | EFIAPI\r | |
221 | EfiHttpRequest (\r | |
222 | IN EFI_HTTP_PROTOCOL *This,\r | |
223 | IN EFI_HTTP_TOKEN *Token\r | |
224 | )\r | |
225 | {\r | |
226 | EFI_HTTP_MESSAGE *HttpMsg;\r | |
227 | EFI_HTTP_REQUEST_DATA *Request;\r | |
228 | VOID *UrlParser;\r | |
229 | EFI_STATUS Status;\r | |
230 | CHAR8 *HostName;\r | |
231 | UINT16 RemotePort;\r | |
232 | HTTP_PROTOCOL *HttpInstance;\r | |
233 | BOOLEAN Configure;\r | |
234 | BOOLEAN ReConfigure;\r | |
235 | CHAR8 *RequestStr;\r | |
236 | CHAR8 *Url;\r | |
51b0450e | 237 | UINTN UrlLen;\r |
47f51a06 YT |
238 | CHAR16 *HostNameStr;\r |
239 | HTTP_TOKEN_WRAP *Wrap;\r | |
b199d941 GCPL |
240 | CHAR8 *FileUrl;\r |
241 | \r | |
47f51a06 YT |
242 | if ((This == NULL) || (Token == NULL)) {\r |
243 | return EFI_INVALID_PARAMETER;\r | |
244 | }\r | |
245 | \r | |
246 | HttpMsg = Token->Message;\r | |
247 | if ((HttpMsg == NULL) || (HttpMsg->Headers == NULL)) {\r | |
248 | return EFI_INVALID_PARAMETER;\r | |
249 | }\r | |
250 | \r | |
251 | //\r | |
252 | // Current implementation does not support POST/PUT method.\r | |
253 | // If future version supports these two methods, Request could be NULL for a special case that to send large amounts\r | |
254 | // of data. For this case, the implementation need check whether previous call to Request() has been completed or not.\r | |
255 | // \r | |
256 | //\r | |
257 | Request = HttpMsg->Data.Request;\r | |
258 | if ((Request == NULL) || (Request->Url == NULL)) {\r | |
259 | return EFI_INVALID_PARAMETER;\r | |
260 | }\r | |
261 | \r | |
262 | //\r | |
263 | // Only support GET and HEAD method in current implementation.\r | |
264 | //\r | |
265 | if ((Request->Method != HttpMethodGet) && (Request->Method != HttpMethodHead)) {\r | |
266 | return EFI_UNSUPPORTED;\r | |
267 | }\r | |
268 | \r | |
269 | HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r | |
270 | ASSERT (HttpInstance != NULL);\r | |
271 | \r | |
272 | if (HttpInstance->State < HTTP_STATE_HTTP_CONFIGED) {\r | |
273 | return EFI_NOT_STARTED;\r | |
274 | }\r | |
275 | \r | |
47f51a06 YT |
276 | //\r |
277 | // Check whether the token already existed.\r | |
278 | //\r | |
279 | if (EFI_ERROR (NetMapIterate (&HttpInstance->TxTokens, HttpTokenExist, Token))) {\r | |
280 | return EFI_ACCESS_DENIED; \r | |
281 | } \r | |
282 | \r | |
47f51a06 YT |
283 | HostName = NULL;\r |
284 | Wrap = NULL;\r | |
285 | HostNameStr = NULL;\r | |
47f51a06 YT |
286 | \r |
287 | //\r | |
288 | // Parse the URI of the remote host.\r | |
289 | //\r | |
7ef0690f | 290 | Url = HttpInstance->Url;\r |
51b0450e FS |
291 | UrlLen = StrLen (Request->Url) + 1;\r |
292 | if (UrlLen > HTTP_URL_BUFFER_LEN) {\r | |
293 | Url = AllocateZeroPool (UrlLen);\r | |
294 | if (Url == NULL) {\r | |
295 | return EFI_OUT_OF_RESOURCES;\r | |
296 | }\r | |
297 | FreePool (HttpInstance->Url);\r | |
298 | HttpInstance->Url = Url; \r | |
b659408b ZL |
299 | } \r |
300 | \r | |
47f51a06 YT |
301 | \r |
302 | UnicodeStrToAsciiStr (Request->Url, Url);\r | |
303 | UrlParser = NULL;\r | |
304 | Status = HttpParseUrl (Url, (UINT32) AsciiStrLen (Url), FALSE, &UrlParser);\r | |
305 | if (EFI_ERROR (Status)) {\r | |
306 | goto Error1;\r | |
307 | }\r | |
308 | \r | |
309 | RequestStr = NULL;\r | |
310 | HostName = NULL;\r | |
311 | Status = HttpUrlGetHostName (Url, UrlParser, &HostName);\r | |
312 | if (EFI_ERROR (Status)) {\r | |
313 | goto Error1;\r | |
314 | }\r | |
315 | \r | |
316 | Status = HttpUrlGetPort (Url, UrlParser, &RemotePort);\r | |
317 | if (EFI_ERROR (Status)) {\r | |
318 | RemotePort = HTTP_DEFAULT_PORT;\r | |
319 | }\r | |
a2e61982 ZL |
320 | //\r |
321 | // If Configure is TRUE, it indicates the first time to call Request();\r | |
322 | // If ReConfigure is TRUE, it indicates the request URL is not same\r | |
323 | // with the previous call to Request();\r | |
324 | //\r | |
47f51a06 YT |
325 | Configure = TRUE;\r |
326 | ReConfigure = TRUE; \r | |
327 | \r | |
a8706acb | 328 | if (HttpInstance->RemoteHost == NULL) {\r |
47f51a06 YT |
329 | //\r |
330 | // Request() is called the first time. \r | |
331 | //\r | |
332 | ReConfigure = FALSE;\r | |
333 | } else {\r | |
334 | if ((HttpInstance->RemotePort == RemotePort) &&\r | |
335 | (AsciiStrCmp (HttpInstance->RemoteHost, HostName) == 0)) {\r | |
336 | //\r | |
337 | // Host Name and port number of the request URL are the same with previous call to Request().\r | |
338 | // Check whether previous TCP packet sent out.\r | |
339 | //\r | |
340 | if (EFI_ERROR (NetMapIterate (&HttpInstance->TxTokens, HttpTcpNotReady, NULL))) {\r | |
341 | //\r | |
342 | // Wrap the HTTP token in HTTP_TOKEN_WRAP\r | |
343 | //\r | |
344 | Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));\r | |
345 | if (Wrap == NULL) {\r | |
346 | Status = EFI_OUT_OF_RESOURCES;\r | |
347 | goto Error1;\r | |
348 | }\r | |
349 | \r | |
350 | Wrap->HttpToken = Token;\r | |
351 | Wrap->HttpInstance = HttpInstance;\r | |
352 | \r | |
b659408b | 353 | Status = HttpCreateTcpTxEvent (Wrap);\r |
47f51a06 YT |
354 | if (EFI_ERROR (Status)) {\r |
355 | goto Error1;\r | |
356 | }\r | |
357 | \r | |
358 | Status = NetMapInsertTail (&HttpInstance->TxTokens, Token, Wrap);\r | |
359 | if (EFI_ERROR (Status)) {\r | |
360 | goto Error1;\r | |
361 | }\r | |
362 | \r | |
363 | Wrap->TcpWrap.Method = Request->Method;\r | |
364 | \r | |
47f51a06 YT |
365 | FreePool (HostName);\r |
366 | \r | |
367 | //\r | |
368 | // Queue the HTTP token and return.\r | |
369 | //\r | |
370 | return EFI_SUCCESS;\r | |
371 | } else {\r | |
372 | //\r | |
373 | // Use existing TCP instance to transmit the packet.\r | |
374 | //\r | |
375 | Configure = FALSE;\r | |
376 | ReConfigure = FALSE;\r | |
377 | }\r | |
378 | } else {\r | |
379 | //\r | |
380 | // Need close existing TCP instance and create a new TCP instance for data transmit.\r | |
381 | //\r | |
382 | if (HttpInstance->RemoteHost != NULL) {\r | |
383 | FreePool (HttpInstance->RemoteHost);\r | |
384 | HttpInstance->RemoteHost = NULL;\r | |
a8706acb | 385 | HttpInstance->RemotePort = 0;\r |
47f51a06 YT |
386 | }\r |
387 | }\r | |
388 | } \r | |
389 | \r | |
390 | if (Configure) {\r | |
391 | //\r | |
b659408b | 392 | // Parse Url for IPv4 or IPv6 address, if failed, perform DNS resolution.\r |
47f51a06 | 393 | //\r |
b659408b ZL |
394 | if (!HttpInstance->LocalAddressIsIPv6) {\r |
395 | Status = NetLibAsciiStrToIp4 (HostName, &HttpInstance->RemoteAddr);\r | |
396 | } else {\r | |
ac458853 | 397 | Status = HttpUrlGetIp6 (Url, UrlParser, &HttpInstance->RemoteIpv6Addr);\r |
b659408b ZL |
398 | }\r |
399 | \r | |
47f51a06 | 400 | if (EFI_ERROR (Status)) {\r |
b659408b | 401 | HostNameStr = AllocateZeroPool ((AsciiStrLen (HostName) + 1) * sizeof (CHAR16));\r |
47f51a06 YT |
402 | if (HostNameStr == NULL) {\r |
403 | Status = EFI_OUT_OF_RESOURCES;\r | |
404 | goto Error1;\r | |
405 | }\r | |
b659408b | 406 | \r |
47f51a06 | 407 | AsciiStrToUnicodeStr (HostName, HostNameStr);\r |
b659408b ZL |
408 | if (!HttpInstance->LocalAddressIsIPv6) {\r |
409 | Status = HttpDns4 (HttpInstance, HostNameStr, &HttpInstance->RemoteAddr);\r | |
410 | } else {\r | |
411 | Status = HttpDns6 (HttpInstance, HostNameStr, &HttpInstance->RemoteIpv6Addr);\r | |
412 | }\r | |
413 | \r | |
47f51a06 YT |
414 | FreePool (HostNameStr);\r |
415 | if (EFI_ERROR (Status)) {\r | |
416 | goto Error1;\r | |
417 | }\r | |
418 | }\r | |
419 | \r | |
b659408b | 420 | \r |
47f51a06 YT |
421 | //\r |
422 | // Save the RemotePort and RemoteHost.\r | |
423 | //\r | |
424 | ASSERT (HttpInstance->RemoteHost == NULL);\r | |
425 | HttpInstance->RemotePort = RemotePort;\r | |
426 | HttpInstance->RemoteHost = HostName;\r | |
427 | HostName = NULL;\r | |
428 | }\r | |
429 | \r | |
430 | if (ReConfigure) {\r | |
431 | //\r | |
432 | // The request URL is different from previous calls to Request(), close existing TCP instance.\r | |
433 | //\r | |
a2e61982 ZL |
434 | if (!HttpInstance->LocalAddressIsIPv6) {\r |
435 | ASSERT (HttpInstance->Tcp4 != NULL);\r | |
436 | } else {\r | |
437 | ASSERT (HttpInstance->Tcp6 != NULL);\r | |
438 | }\r | |
47f51a06 YT |
439 | HttpCloseConnection (HttpInstance);\r |
440 | EfiHttpCancel (This, NULL);\r | |
441 | }\r | |
442 | \r | |
443 | //\r | |
444 | // Wrap the HTTP token in HTTP_TOKEN_WRAP\r | |
445 | //\r | |
446 | Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));\r | |
447 | if (Wrap == NULL) {\r | |
448 | Status = EFI_OUT_OF_RESOURCES;\r | |
449 | goto Error1;\r | |
450 | }\r | |
451 | \r | |
452 | Wrap->HttpToken = Token;\r | |
453 | Wrap->HttpInstance = HttpInstance;\r | |
454 | Wrap->TcpWrap.Method = Request->Method;\r | |
455 | \r | |
a2e61982 ZL |
456 | Status = HttpInitTcp (HttpInstance, Wrap, Configure);\r |
457 | if (EFI_ERROR (Status)) {\r | |
458 | goto Error2;\r | |
459 | } \r | |
b659408b | 460 | \r |
a2e61982 | 461 | if (!Configure) {\r |
47f51a06 YT |
462 | //\r |
463 | // For the new HTTP token, create TX TCP token events. \r | |
464 | //\r | |
b659408b | 465 | Status = HttpCreateTcpTxEvent (Wrap);\r |
47f51a06 YT |
466 | if (EFI_ERROR (Status)) {\r |
467 | goto Error1;\r | |
468 | }\r | |
469 | }\r | |
a2e61982 | 470 | \r |
47f51a06 YT |
471 | //\r |
472 | // Create request message.\r | |
473 | //\r | |
b199d941 GCPL |
474 | FileUrl = Url;\r |
475 | if (*FileUrl != '/') {\r | |
476 | //\r | |
477 | // Convert the absolute-URI to the absolute-path\r | |
478 | //\r | |
479 | while (*FileUrl != ':') {\r | |
480 | FileUrl++;\r | |
481 | }\r | |
482 | if ((*(FileUrl+1) == '/') && (*(FileUrl+2) == '/')) {\r | |
483 | FileUrl += 3;\r | |
484 | while (*FileUrl != '/') {\r | |
485 | FileUrl++;\r | |
486 | }\r | |
487 | } else {\r | |
488 | Status = EFI_INVALID_PARAMETER;\r | |
489 | goto Error3;\r | |
490 | }\r | |
491 | }\r | |
492 | RequestStr = HttpGenRequestString (HttpInstance, HttpMsg, FileUrl);\r | |
47f51a06 YT |
493 | if (RequestStr == NULL) {\r |
494 | Status = EFI_OUT_OF_RESOURCES;\r | |
495 | goto Error3;\r | |
496 | }\r | |
497 | \r | |
498 | Status = NetMapInsertTail (&HttpInstance->TxTokens, Token, Wrap);\r | |
499 | if (EFI_ERROR (Status)) {\r | |
500 | goto Error4;\r | |
501 | }\r | |
502 | \r | |
47f51a06 YT |
503 | //\r |
504 | // Transmit the request message.\r | |
505 | //\r | |
b659408b | 506 | Status = HttpTransmitTcp (\r |
47f51a06 YT |
507 | HttpInstance,\r |
508 | Wrap,\r | |
509 | (UINT8*) RequestStr,\r | |
510 | AsciiStrLen (RequestStr)\r | |
511 | );\r | |
512 | if (EFI_ERROR (Status)) {\r | |
513 | goto Error5; \r | |
514 | }\r | |
515 | \r | |
49c9f74c | 516 | DispatchDpc ();\r |
b659408b | 517 | \r |
cdf8c32e NH |
518 | if (HostName != NULL) {\r |
519 | FreePool (HostName);\r | |
520 | }\r | |
b659408b | 521 | \r |
47f51a06 YT |
522 | return EFI_SUCCESS;\r |
523 | \r | |
524 | Error5:\r | |
525 | NetMapRemoveTail (&HttpInstance->TxTokens, NULL);\r | |
526 | \r | |
527 | Error4:\r | |
528 | if (RequestStr != NULL) {\r | |
529 | FreePool (RequestStr);\r | |
530 | } \r | |
531 | \r | |
532 | Error3:\r | |
533 | HttpCloseConnection (HttpInstance);\r | |
534 | \r | |
47f51a06 | 535 | Error2:\r |
b659408b ZL |
536 | HttpCloseTcpConnCloseEvent (HttpInstance);\r |
537 | if (NULL != Wrap->TcpWrap.Tx4Token.CompletionToken.Event) {\r | |
538 | gBS->CloseEvent (Wrap->TcpWrap.Tx4Token.CompletionToken.Event);\r | |
539 | Wrap->TcpWrap.Tx4Token.CompletionToken.Event = NULL;\r | |
540 | }\r | |
541 | if (NULL != Wrap->TcpWrap.Tx6Token.CompletionToken.Event) {\r | |
542 | gBS->CloseEvent (Wrap->TcpWrap.Tx6Token.CompletionToken.Event);\r | |
543 | Wrap->TcpWrap.Tx6Token.CompletionToken.Event = NULL;\r | |
47f51a06 YT |
544 | }\r |
545 | \r | |
546 | Error1:\r | |
b659408b | 547 | \r |
47f51a06 YT |
548 | if (HostName != NULL) {\r |
549 | FreePool (HostName);\r | |
550 | }\r | |
551 | if (Wrap != NULL) {\r | |
552 | FreePool (Wrap);\r | |
553 | }\r | |
554 | if (UrlParser!= NULL) {\r | |
555 | HttpUrlFreeParser (UrlParser);\r | |
556 | }\r | |
557 | \r | |
558 | return Status;\r | |
559 | \r | |
560 | }\r | |
561 | \r | |
562 | /**\r | |
b659408b | 563 | Cancel a user's Token. \r |
47f51a06 YT |
564 | \r |
565 | @param[in] Map The HTTP instance's token queue.\r | |
566 | @param[in] Item Object container for one HTTP token and token's wrap.\r | |
567 | @param[in] Context The user's token to cancel.\r | |
568 | \r | |
569 | @retval EFI_SUCCESS Continue to check the next Item.\r | |
570 | @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.\r | |
571 | \r | |
572 | **/\r | |
573 | EFI_STATUS\r | |
574 | EFIAPI\r | |
575 | HttpCancelTokens (\r | |
576 | IN NET_MAP *Map,\r | |
577 | IN NET_MAP_ITEM *Item,\r | |
578 | IN VOID *Context\r | |
579 | )\r | |
580 | {\r | |
581 | \r | |
582 | EFI_HTTP_TOKEN *Token;\r | |
583 | HTTP_TOKEN_WRAP *Wrap;\r | |
b659408b | 584 | HTTP_PROTOCOL *HttpInstance;\r |
47f51a06 YT |
585 | \r |
586 | Token = (EFI_HTTP_TOKEN *) Context;\r | |
587 | \r | |
588 | //\r | |
589 | // Return EFI_SUCCESS to check the next item in the map if\r | |
590 | // this one doesn't match.\r | |
591 | //\r | |
592 | if ((Token != NULL) && (Token != Item->Key)) {\r | |
593 | return EFI_SUCCESS;\r | |
594 | }\r | |
595 | \r | |
596 | Wrap = (HTTP_TOKEN_WRAP *) Item->Value;\r | |
597 | ASSERT (Wrap != NULL);\r | |
b659408b | 598 | HttpInstance = Wrap->HttpInstance;\r |
47f51a06 YT |
599 | \r |
600 | //\r | |
601 | // Free resources.\r | |
602 | //\r | |
603 | NetMapRemoveItem (Map, Item, NULL); \r | |
604 | \r | |
b659408b ZL |
605 | if (!HttpInstance->LocalAddressIsIPv6) {\r |
606 | if (Wrap->TcpWrap.Tx4Token.CompletionToken.Event != NULL) {\r | |
607 | gBS->CloseEvent (Wrap->TcpWrap.Tx4Token.CompletionToken.Event);\r | |
608 | }\r | |
609 | \r | |
610 | if (Wrap->TcpWrap.Rx4Token.CompletionToken.Event != NULL) {\r | |
611 | gBS->CloseEvent (Wrap->TcpWrap.Rx4Token.CompletionToken.Event);\r | |
612 | }\r | |
613 | \r | |
614 | if (Wrap->TcpWrap.Rx4Token.Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) {\r | |
615 | FreePool (Wrap->TcpWrap.Rx4Token.Packet.RxData->FragmentTable[0].FragmentBuffer);\r | |
616 | }\r | |
47f51a06 | 617 | \r |
b659408b ZL |
618 | } else {\r |
619 | if (Wrap->TcpWrap.Tx6Token.CompletionToken.Event != NULL) {\r | |
620 | gBS->CloseEvent (Wrap->TcpWrap.Tx6Token.CompletionToken.Event);\r | |
621 | }\r | |
622 | \r | |
623 | if (Wrap->TcpWrap.Rx6Token.CompletionToken.Event != NULL) {\r | |
624 | gBS->CloseEvent (Wrap->TcpWrap.Rx6Token.CompletionToken.Event);\r | |
625 | }\r | |
47f51a06 | 626 | \r |
b659408b ZL |
627 | if (Wrap->TcpWrap.Rx6Token.Packet.RxData->FragmentTable[0].FragmentBuffer != NULL) {\r |
628 | FreePool (Wrap->TcpWrap.Rx6Token.Packet.RxData->FragmentTable[0].FragmentBuffer);\r | |
629 | }\r | |
47f51a06 YT |
630 | }\r |
631 | \r | |
b659408b | 632 | \r |
47f51a06 YT |
633 | FreePool (Wrap);\r |
634 | \r | |
635 | //\r | |
636 | // If only one item is to be cancel, return EFI_ABORTED to stop\r | |
637 | // iterating the map any more.\r | |
638 | //\r | |
639 | if (Token != NULL) {\r | |
640 | return EFI_ABORTED;\r | |
641 | }\r | |
642 | \r | |
643 | return EFI_SUCCESS; \r | |
644 | }\r | |
645 | \r | |
646 | /**\r | |
647 | Cancel the user's receive/transmit request. It is the worker function of\r | |
648 | EfiHttpCancel API. If a matching token is found, it will call HttpCancelTokens to cancel the\r | |
649 | token.\r | |
650 | \r | |
651 | @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.\r | |
652 | @param[in] Token The token to cancel. If NULL, all token will be\r | |
653 | cancelled.\r | |
654 | \r | |
655 | @retval EFI_SUCCESS The token is cancelled.\r | |
656 | @retval EFI_NOT_FOUND The asynchronous request or response token is not found. \r | |
657 | @retval Others Other error as indicated.\r | |
658 | \r | |
659 | **/\r | |
660 | EFI_STATUS\r | |
661 | HttpCancel (\r | |
662 | IN HTTP_PROTOCOL *HttpInstance,\r | |
663 | IN EFI_HTTP_TOKEN *Token\r | |
664 | )\r | |
665 | {\r | |
666 | EFI_STATUS Status;\r | |
667 | \r | |
668 | //\r | |
669 | // First check the tokens queued by EfiHttpRequest().\r | |
670 | //\r | |
671 | Status = NetMapIterate (&HttpInstance->TxTokens, HttpCancelTokens, Token);\r | |
672 | if (EFI_ERROR (Status)) {\r | |
673 | if (Token != NULL) {\r | |
674 | if (Status == EFI_ABORTED) {\r | |
675 | return EFI_SUCCESS;\r | |
676 | } \r | |
677 | } else {\r | |
678 | return Status;\r | |
679 | }\r | |
680 | }\r | |
681 | \r | |
682 | //\r | |
683 | // Then check the tokens queued by EfiHttpResponse().\r | |
684 | //\r | |
685 | Status = NetMapIterate (&HttpInstance->RxTokens, HttpCancelTokens, Token);\r | |
686 | if (EFI_ERROR (Status)) {\r | |
687 | if (Token != NULL) {\r | |
688 | if (Status == EFI_ABORTED) {\r | |
689 | return EFI_SUCCESS;\r | |
690 | } else {\r | |
691 | return EFI_NOT_FOUND;\r | |
692 | }\r | |
693 | } else {\r | |
694 | return Status;\r | |
695 | }\r | |
696 | }\r | |
697 | \r | |
698 | return EFI_SUCCESS;\r | |
699 | }\r | |
700 | \r | |
701 | \r | |
702 | /**\r | |
703 | Abort an asynchronous HTTP request or response token.\r | |
704 | \r | |
705 | The Cancel() function aborts a pending HTTP request or response transaction. If\r | |
706 | Token is not NULL and the token is in transmit or receive queues when it is being\r | |
707 | cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will\r | |
708 | be signaled. If the token is not in one of the queues, which usually means that the\r | |
709 | asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,\r | |
710 | all asynchronous tokens issued by Request() or Response() will be aborted.\r | |
711 | \r | |
712 | @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r | |
713 | @param[in] Token Point to storage containing HTTP request or response\r | |
714 | token.\r | |
715 | \r | |
716 | @retval EFI_SUCCESS Request and Response queues are successfully flushed.\r | |
717 | @retval EFI_INVALID_PARAMETER This is NULL.\r | |
718 | @retval EFI_NOT_STARTED This instance hasn't been configured.\r | |
719 | @retval EFI_NO_MAPPING When using the default address, configuration (DHCP,\r | |
720 | BOOTP, RARP, etc.) hasn't finished yet.\r | |
721 | @retval EFI_NOT_FOUND The asynchronous request or response token is not\r | |
722 | found.\r | |
723 | @retval EFI_UNSUPPORTED The implementation does not support this function.\r | |
724 | \r | |
725 | **/\r | |
726 | EFI_STATUS\r | |
727 | EFIAPI\r | |
728 | EfiHttpCancel (\r | |
729 | IN EFI_HTTP_PROTOCOL *This,\r | |
730 | IN EFI_HTTP_TOKEN *Token\r | |
731 | )\r | |
732 | {\r | |
733 | HTTP_PROTOCOL *HttpInstance;\r | |
734 | \r | |
735 | if (This == NULL) {\r | |
736 | return EFI_INVALID_PARAMETER;\r | |
737 | }\r | |
738 | \r | |
739 | HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r | |
740 | ASSERT (HttpInstance != NULL);\r | |
741 | \r | |
742 | if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {\r | |
743 | return EFI_NOT_STARTED;\r | |
744 | }\r | |
745 | \r | |
746 | return HttpCancel (HttpInstance, Token);\r | |
747 | \r | |
748 | }\r | |
749 | \r | |
750 | /**\r | |
751 | A callback function to intercept events during message parser.\r | |
752 | \r | |
753 | This function will be invoked during HttpParseMessageBody() with various events type. An error\r | |
754 | return status of the callback function will cause the HttpParseMessageBody() aborted.\r | |
755 | \r | |
756 | @param[in] EventType Event type of this callback call.\r | |
757 | @param[in] Data A pointer to data buffer.\r | |
758 | @param[in] Length Length in bytes of the Data.\r | |
759 | @param[in] Context Callback context set by HttpInitMsgParser().\r | |
760 | \r | |
761 | @retval EFI_SUCCESS Continue to parser the message body.\r | |
762 | \r | |
763 | **/\r | |
764 | EFI_STATUS\r | |
765 | EFIAPI\r | |
766 | HttpBodyParserCallback (\r | |
767 | IN HTTP_BODY_PARSE_EVENT EventType,\r | |
768 | IN CHAR8 *Data,\r | |
769 | IN UINTN Length,\r | |
770 | IN VOID *Context\r | |
771 | )\r | |
772 | {\r | |
773 | HTTP_TOKEN_WRAP *Wrap;\r | |
774 | \r | |
775 | if (EventType != BodyParseEventOnComplete) {\r | |
776 | return EFI_SUCCESS;\r | |
777 | }\r | |
778 | \r | |
779 | if (Data == NULL || Length != 0 || Context == NULL) {\r | |
780 | return EFI_SUCCESS;\r | |
781 | }\r | |
782 | \r | |
783 | Wrap = (HTTP_TOKEN_WRAP *) Context;\r | |
784 | Wrap->HttpInstance->NextMsg = Data;\r | |
785 | \r | |
786 | //\r | |
b659408b | 787 | // Free Tx4Token or Tx6Token since already received corrsponding HTTP response.\r |
47f51a06 YT |
788 | //\r |
789 | FreePool (Wrap);\r | |
790 | \r | |
791 | return EFI_SUCCESS;\r | |
792 | }\r | |
793 | \r | |
794 | /**\r | |
795 | The work function of EfiHttpResponse().\r | |
796 | \r | |
797 | @param[in] Wrap Pointer to HTTP token's wrap data.\r | |
798 | \r | |
799 | @retval EFI_SUCCESS Allocation succeeded.\r | |
800 | @retval EFI_OUT_OF_RESOURCES Failed to complete the opration due to lack of resources.\r | |
b659408b | 801 | @retval EFI_NOT_READY Can't find a corresponding Tx4Token/Tx6Token or \r |
5ca29abe | 802 | the EFI_HTTP_UTILITIES_PROTOCOL is not available.\r |
47f51a06 YT |
803 | \r |
804 | **/\r | |
805 | EFI_STATUS\r | |
806 | HttpResponseWorker (\r | |
807 | IN HTTP_TOKEN_WRAP *Wrap\r | |
808 | )\r | |
809 | {\r | |
810 | EFI_STATUS Status;\r | |
811 | EFI_HTTP_MESSAGE *HttpMsg;\r | |
47f51a06 YT |
812 | CHAR8 *EndofHeader;\r |
813 | CHAR8 *HttpHeaders;\r | |
814 | UINTN SizeofHeaders;\r | |
47f51a06 YT |
815 | UINTN BufferSize;\r |
816 | UINTN StatusCode;\r | |
817 | CHAR8 *Tmp;\r | |
818 | CHAR8 *HeaderTmp;\r | |
819 | CHAR8 *StatusCodeStr;\r | |
820 | UINTN BodyLen;\r | |
821 | HTTP_PROTOCOL *HttpInstance;\r | |
822 | EFI_HTTP_TOKEN *Token;\r | |
823 | NET_MAP_ITEM *Item;\r | |
824 | HTTP_TOKEN_WRAP *ValueInItem;\r | |
825 | UINTN HdrLen;\r | |
826 | \r | |
3fd7bd08 | 827 | if (Wrap == NULL || Wrap->HttpInstance == NULL) {\r |
828 | return EFI_INVALID_PARAMETER;\r | |
829 | }\r | |
830 | \r | |
47f51a06 YT |
831 | HttpInstance = Wrap->HttpInstance;\r |
832 | Token = Wrap->HttpToken;\r | |
47f51a06 YT |
833 | HttpMsg = Token->Message;\r |
834 | \r | |
b659408b ZL |
835 | HttpInstance->EndofHeader = NULL;\r |
836 | HttpInstance->HttpHeaders = NULL;\r | |
837 | HttpMsg->Headers = NULL;\r | |
838 | HttpHeaders = NULL;\r | |
839 | SizeofHeaders = 0;\r | |
840 | BufferSize = 0;\r | |
841 | EndofHeader = NULL;\r | |
47f51a06 YT |
842 | \r |
843 | if (HttpMsg->Data.Response != NULL) {\r | |
844 | //\r | |
845 | // Need receive the HTTP headers, prepare buffer.\r | |
846 | //\r | |
b659408b | 847 | Status = HttpCreateTcpRxEventForHeader (HttpInstance);\r |
47f51a06 YT |
848 | if (EFI_ERROR (Status)) {\r |
849 | goto Error;\r | |
850 | }\r | |
851 | \r | |
852 | //\r | |
853 | // Check whether we have cached header from previous call.\r | |
854 | //\r | |
855 | if ((HttpInstance->CacheBody != NULL) && (HttpInstance->NextMsg != NULL)) {\r | |
856 | //\r | |
857 | // The data is stored at [NextMsg, CacheBody + CacheLen].\r | |
858 | //\r | |
859 | HdrLen = HttpInstance->CacheBody + HttpInstance->CacheLen - HttpInstance->NextMsg;\r | |
860 | HttpHeaders = AllocateZeroPool (HdrLen);\r | |
861 | if (HttpHeaders == NULL) {\r | |
862 | Status = EFI_OUT_OF_RESOURCES;\r | |
863 | goto Error;\r | |
864 | }\r | |
865 | \r | |
866 | CopyMem (HttpHeaders, HttpInstance->NextMsg, HdrLen);\r | |
867 | FreePool (HttpInstance->CacheBody);\r | |
868 | HttpInstance->CacheBody = NULL;\r | |
869 | HttpInstance->NextMsg = NULL;\r | |
870 | HttpInstance->CacheOffset = 0;\r | |
871 | SizeofHeaders = HdrLen;\r | |
872 | BufferSize = HttpInstance->CacheLen;\r | |
873 | \r | |
874 | //\r | |
875 | // Check whether we cached the whole HTTP headers.\r | |
876 | //\r | |
877 | EndofHeader = AsciiStrStr (HttpHeaders, HTTP_END_OF_HDR_STR); \r | |
b659408b | 878 | } \r |
47f51a06 | 879 | \r |
b659408b ZL |
880 | HttpInstance->EndofHeader = &EndofHeader;\r |
881 | HttpInstance->HttpHeaders = &HttpHeaders;\r | |
47f51a06 | 882 | \r |
b659408b ZL |
883 | Status = HttpTcpReceiveHeader (HttpInstance, &SizeofHeaders, &BufferSize);\r |
884 | if (EFI_ERROR (Status)) {\r | |
885 | goto Error;\r | |
886 | }\r | |
47f51a06 YT |
887 | \r |
888 | //\r | |
889 | // Cache the part of body.\r | |
890 | //\r | |
891 | BodyLen = BufferSize - (EndofHeader - HttpHeaders);\r | |
892 | if (BodyLen > 0) {\r | |
893 | if (HttpInstance->CacheBody != NULL) {\r | |
894 | FreePool (HttpInstance->CacheBody);\r | |
895 | }\r | |
896 | \r | |
897 | HttpInstance->CacheBody = AllocateZeroPool (BodyLen);\r | |
898 | if (HttpInstance->CacheBody == NULL) {\r | |
899 | Status = EFI_OUT_OF_RESOURCES;\r | |
900 | goto Error;\r | |
901 | }\r | |
902 | \r | |
903 | CopyMem (HttpInstance->CacheBody, EndofHeader, BodyLen);\r | |
904 | HttpInstance->CacheLen = BodyLen;\r | |
905 | }\r | |
906 | \r | |
47f51a06 YT |
907 | //\r |
908 | // Search for Status Code.\r | |
909 | //\r | |
910 | StatusCodeStr = HttpHeaders + AsciiStrLen (HTTP_VERSION_STR) + 1;\r | |
911 | if (StatusCodeStr == NULL) {\r | |
912 | goto Error;\r | |
913 | }\r | |
914 | \r | |
915 | StatusCode = AsciiStrDecimalToUintn (StatusCodeStr);\r | |
916 | \r | |
917 | //\r | |
918 | // Remove the first line of HTTP message, e.g. "HTTP/1.1 200 OK\r\n".\r | |
919 | //\r | |
920 | Tmp = AsciiStrStr (HttpHeaders, HTTP_CRLF_STR);\r | |
921 | if (Tmp == NULL) {\r | |
922 | goto Error;\r | |
923 | }\r | |
924 | \r | |
925 | Tmp = Tmp + AsciiStrLen (HTTP_CRLF_STR);\r | |
926 | SizeofHeaders = SizeofHeaders - (Tmp - HttpHeaders);\r | |
927 | HeaderTmp = AllocateZeroPool (SizeofHeaders);\r | |
928 | if (HeaderTmp == NULL) {\r | |
929 | goto Error;\r | |
930 | }\r | |
931 | \r | |
932 | CopyMem (HeaderTmp, Tmp, SizeofHeaders);\r | |
933 | FreePool (HttpHeaders);\r | |
934 | HttpHeaders = HeaderTmp;\r | |
5ca29abe JW |
935 | \r |
936 | //\r | |
937 | // Check whether the EFI_HTTP_UTILITIES_PROTOCOL is available.\r | |
938 | //\r | |
939 | if (mHttpUtilities == NULL) {\r | |
940 | Status = EFI_NOT_READY;\r | |
941 | goto Error;\r | |
942 | }\r | |
943 | \r | |
47f51a06 YT |
944 | //\r |
945 | // Parse the HTTP header into array of key/value pairs.\r | |
946 | //\r | |
5ca29abe JW |
947 | Status = mHttpUtilities->Parse (\r |
948 | mHttpUtilities, \r | |
949 | HttpHeaders, \r | |
950 | SizeofHeaders, \r | |
951 | &HttpMsg->Headers, \r | |
952 | &HttpMsg->HeaderCount\r | |
953 | );\r | |
47f51a06 YT |
954 | if (EFI_ERROR (Status)) {\r |
955 | goto Error;\r | |
956 | }\r | |
957 | \r | |
958 | FreePool (HttpHeaders);\r | |
959 | HttpHeaders = NULL;\r | |
960 | \r | |
961 | HttpMsg->Data.Response->StatusCode = HttpMappingToStatusCode (StatusCode);\r | |
962 | \r | |
963 | //\r | |
964 | // Init message-body parser by header information. \r | |
965 | //\r | |
966 | Status = EFI_NOT_READY;\r | |
967 | ValueInItem = NULL;\r | |
968 | NetMapRemoveHead (&HttpInstance->TxTokens, (VOID**) &ValueInItem);\r | |
969 | if (ValueInItem == NULL) {\r | |
970 | goto Error;\r | |
971 | }\r | |
972 | \r | |
973 | //\r | |
b659408b | 974 | // The first Tx Token not transmitted yet, insert back and return error.\r |
47f51a06 YT |
975 | //\r |
976 | if (!ValueInItem->TcpWrap.IsTxDone) {\r | |
977 | goto Error2;\r | |
978 | }\r | |
979 | \r | |
980 | Status = HttpInitMsgParser (\r | |
981 | ValueInItem->TcpWrap.Method,\r | |
982 | HttpMsg->Data.Response->StatusCode,\r | |
983 | HttpMsg->HeaderCount,\r | |
984 | HttpMsg->Headers,\r | |
985 | HttpBodyParserCallback,\r | |
986 | (VOID *) ValueInItem,\r | |
987 | &HttpInstance->MsgParser\r | |
988 | );\r | |
989 | if (EFI_ERROR (Status)) { \r | |
990 | goto Error2;\r | |
991 | }\r | |
992 | \r | |
993 | //\r | |
994 | // Check whether we received a complete HTTP message.\r | |
995 | //\r | |
996 | if (HttpInstance->CacheBody != NULL) {\r | |
997 | Status = HttpParseMessageBody (HttpInstance->MsgParser, HttpInstance->CacheLen, HttpInstance->CacheBody);\r | |
998 | if (EFI_ERROR (Status)) {\r | |
999 | goto Error2;\r | |
1000 | }\r | |
1001 | \r | |
1002 | if (HttpIsMessageComplete (HttpInstance->MsgParser)) {\r | |
1003 | //\r | |
1004 | // Free the MsgParse since we already have a full HTTP message.\r | |
1005 | //\r | |
1006 | HttpFreeMsgParser (HttpInstance->MsgParser);\r | |
1007 | HttpInstance->MsgParser = NULL;\r | |
1008 | }\r | |
1009 | }\r | |
1010 | \r | |
1011 | if ((HttpMsg->Body == NULL) || (HttpMsg->BodyLength == 0)) { \r | |
1012 | Status = EFI_SUCCESS;\r | |
1013 | goto Exit;\r | |
1014 | }\r | |
1015 | } \r | |
1016 | \r | |
1017 | //\r | |
1018 | // Receive the response body.\r | |
1019 | //\r | |
1020 | BodyLen = 0;\r | |
1021 | \r | |
1022 | //\r | |
1023 | // First check whether we cached some data.\r | |
1024 | //\r | |
1025 | if (HttpInstance->CacheBody != NULL) {\r | |
1026 | //\r | |
1027 | // Calculate the length of the cached data.\r | |
1028 | //\r | |
1029 | if (HttpInstance->NextMsg != NULL) {\r | |
1030 | //\r | |
1031 | // We have a cached HTTP message which includes a part of HTTP header of next message.\r | |
1032 | //\r | |
1033 | BodyLen = HttpInstance->NextMsg - (HttpInstance->CacheBody + HttpInstance->CacheOffset); \r | |
1034 | } else {\r | |
1035 | BodyLen = HttpInstance->CacheLen - HttpInstance->CacheOffset;\r | |
1036 | }\r | |
1037 | \r | |
1038 | if (BodyLen > 0) {\r | |
1039 | //\r | |
1040 | // We have some cached data. Just copy the data and return.\r | |
1041 | //\r | |
1042 | if (HttpMsg->BodyLength < BodyLen) {\r | |
1043 | CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, HttpMsg->BodyLength);\r | |
1044 | HttpInstance->CacheOffset = HttpInstance->CacheOffset + HttpMsg->BodyLength;\r | |
1045 | } else {\r | |
1046 | //\r | |
1047 | // Copy all cached data out.\r | |
1048 | //\r | |
1049 | CopyMem (HttpMsg->Body, HttpInstance->CacheBody + HttpInstance->CacheOffset, BodyLen);\r | |
1050 | HttpInstance->CacheOffset = BodyLen + HttpInstance->CacheOffset;\r | |
1051 | HttpMsg->BodyLength = BodyLen;\r | |
1052 | \r | |
1053 | if (HttpInstance->NextMsg == NULL) {\r | |
1054 | //\r | |
1055 | // There is no HTTP header of next message. Just free the cache buffer.\r | |
1056 | //\r | |
1057 | FreePool (HttpInstance->CacheBody);\r | |
1058 | HttpInstance->CacheBody = NULL;\r | |
1059 | HttpInstance->NextMsg = NULL;\r | |
1060 | HttpInstance->CacheOffset = 0;\r | |
1061 | }\r | |
1062 | }\r | |
1063 | //\r | |
1064 | // Return since we aready received required data.\r | |
1065 | //\r | |
1066 | Status = EFI_SUCCESS;\r | |
1067 | goto Exit;\r | |
1068 | } \r | |
1069 | \r | |
1070 | if (BodyLen == 0 && HttpInstance->MsgParser == NULL) {\r | |
1071 | //\r | |
1072 | // We received a complete HTTP message, and we don't have more data to return to caller.\r | |
1073 | //\r | |
1074 | HttpMsg->BodyLength = 0;\r | |
1075 | Status = EFI_SUCCESS;\r | |
1076 | goto Exit; \r | |
1077 | } \r | |
1078 | }\r | |
1079 | \r | |
1080 | ASSERT (HttpInstance->MsgParser != NULL);\r | |
1081 | \r | |
1082 | //\r | |
1083 | // We still need receive more data when there is no cache data and MsgParser is not NULL;\r | |
1084 | //\r | |
b659408b | 1085 | Status = HttpTcpReceiveBody (Wrap, HttpMsg);\r |
47f51a06 | 1086 | if (EFI_ERROR (Status)) {\r |
47f51a06 YT |
1087 | goto Error;\r |
1088 | }\r | |
1089 | \r | |
1090 | return Status;\r | |
1091 | \r | |
1092 | Exit:\r | |
1093 | Item = NetMapFindKey (&Wrap->HttpInstance->RxTokens, Wrap->HttpToken);\r | |
1094 | if (Item != NULL) {\r | |
1095 | NetMapRemoveItem (&Wrap->HttpInstance->RxTokens, Item, NULL);\r | |
1096 | }\r | |
1097 | Token->Status = Status;\r | |
1098 | gBS->SignalEvent (Token->Event);\r | |
b659408b | 1099 | HttpCloseTcpRxEvent (Wrap);\r |
47f51a06 YT |
1100 | FreePool (Wrap);\r |
1101 | return Status;\r | |
1102 | \r | |
1103 | Error2:\r | |
1104 | NetMapInsertHead (&HttpInstance->TxTokens, ValueInItem->HttpToken, ValueInItem);\r | |
1105 | \r | |
1106 | Error:\r | |
b659408b | 1107 | HttpTcpTokenCleanup (Wrap);\r |
47f51a06 YT |
1108 | \r |
1109 | if (HttpHeaders != NULL) {\r | |
1110 | FreePool (HttpHeaders);\r | |
1111 | }\r | |
1112 | \r | |
1113 | if (HttpMsg->Headers != NULL) {\r | |
1114 | FreePool (HttpMsg->Headers);\r | |
1115 | }\r | |
1116 | \r | |
1117 | if (HttpInstance->CacheBody != NULL) {\r | |
1118 | FreePool (HttpInstance->CacheBody);\r | |
1119 | HttpInstance->CacheBody = NULL;\r | |
1120 | }\r | |
1121 | \r | |
1122 | Token->Status = Status;\r | |
1123 | gBS->SignalEvent (Token->Event);\r | |
1124 | \r | |
1125 | return Status; \r | |
1126 | \r | |
1127 | }\r | |
1128 | \r | |
1129 | \r | |
1130 | /**\r | |
1131 | The Response() function queues an HTTP response to this HTTP instance, similar to\r | |
1132 | Receive() function in the EFI TCP driver. When the HTTP request is sent successfully,\r | |
1133 | or if there is an error, Status in token will be updated and Event will be signaled.\r | |
1134 | \r | |
1135 | The HTTP driver will queue a receive token to the underlying TCP instance. When data\r | |
1136 | is received in the underlying TCP instance, the data will be parsed and Token will\r | |
1137 | be populated with the response data. If the data received from the remote host\r | |
1138 | contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting\r | |
1139 | (asynchronously) for more data to be sent from the remote host before signaling\r | |
1140 | Event in Token.\r | |
1141 | \r | |
1142 | It is the responsibility of the caller to allocate a buffer for Body and specify the\r | |
1143 | size in BodyLength. If the remote host provides a response that contains a content\r | |
1144 | body, up to BodyLength bytes will be copied from the receive buffer into Body and\r | |
1145 | BodyLength will be updated with the amount of bytes received and copied to Body. This\r | |
1146 | allows the client to download a large file in chunks instead of into one contiguous\r | |
1147 | block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is\r | |
1148 | non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive\r | |
1149 | token to underlying TCP instance. If data arrives in the receive buffer, up to\r | |
1150 | BodyLength bytes of data will be copied to Body. The HTTP driver will then update\r | |
1151 | BodyLength with the amount of bytes received and copied to Body.\r | |
1152 | \r | |
1153 | If the HTTP driver does not have an open underlying TCP connection with the host\r | |
1154 | specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is\r | |
1155 | consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain\r | |
1156 | an open TCP connection between client and host.\r | |
1157 | \r | |
1158 | @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r | |
1159 | @param[in] Token Pointer to storage containing HTTP response token.\r | |
1160 | \r | |
1161 | @retval EFI_SUCCESS Allocation succeeded.\r | |
1162 | @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been\r | |
1163 | initialized.\r | |
1164 | @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r | |
1165 | This is NULL.\r | |
1166 | Token is NULL.\r | |
1167 | Token->Message->Headers is NULL.\r | |
1168 | Token->Message is NULL.\r | |
1169 | Token->Message->Body is not NULL,\r | |
1170 | Token->Message->BodyLength is non-zero, and\r | |
1171 | Token->Message->Data is NULL, but a previous call to\r | |
1172 | Response() has not been completed successfully.\r | |
1173 | @retval EFI_OUT_OF_RESOURCES Could not allocate enough system resources.\r | |
1174 | @retval EFI_ACCESS_DENIED An open TCP connection is not present with the host\r | |
1175 | specified by response URL.\r | |
1176 | **/\r | |
1177 | EFI_STATUS\r | |
1178 | EFIAPI\r | |
1179 | EfiHttpResponse (\r | |
1180 | IN EFI_HTTP_PROTOCOL *This,\r | |
1181 | IN EFI_HTTP_TOKEN *Token\r | |
1182 | )\r | |
1183 | {\r | |
1184 | EFI_STATUS Status;\r | |
1185 | EFI_HTTP_MESSAGE *HttpMsg;\r | |
1186 | HTTP_PROTOCOL *HttpInstance;\r | |
1187 | HTTP_TOKEN_WRAP *Wrap;\r | |
1188 | \r | |
1189 | if ((This == NULL) || (Token == NULL)) {\r | |
1190 | return EFI_INVALID_PARAMETER;\r | |
1191 | }\r | |
1192 | \r | |
1193 | HttpMsg = Token->Message;\r | |
1194 | if (HttpMsg == NULL) {\r | |
1195 | return EFI_INVALID_PARAMETER;\r | |
1196 | }\r | |
1197 | \r | |
1198 | HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r | |
1199 | ASSERT (HttpInstance != NULL);\r | |
1200 | \r | |
1201 | if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED) {\r | |
1202 | return EFI_NOT_STARTED;\r | |
1203 | }\r | |
1204 | \r | |
47f51a06 YT |
1205 | //\r |
1206 | // Check whether the token already existed.\r | |
1207 | //\r | |
1208 | if (EFI_ERROR (NetMapIterate (&HttpInstance->RxTokens, HttpTokenExist, Token))) {\r | |
1209 | return EFI_ACCESS_DENIED; \r | |
1210 | }\r | |
1211 | \r | |
1212 | Wrap = AllocateZeroPool (sizeof (HTTP_TOKEN_WRAP));\r | |
1213 | if (Wrap == NULL) {\r | |
1214 | return EFI_OUT_OF_RESOURCES;\r | |
1215 | }\r | |
1216 | \r | |
1217 | Wrap->HttpInstance = HttpInstance;\r | |
1218 | Wrap->HttpToken = Token;\r | |
1219 | \r | |
b659408b | 1220 | Status = HttpCreateTcpRxEvent (Wrap);\r |
47f51a06 YT |
1221 | if (EFI_ERROR (Status)) {\r |
1222 | goto Error;\r | |
1223 | }\r | |
1224 | \r | |
1225 | Status = NetMapInsertTail (&HttpInstance->RxTokens, Token, Wrap);\r | |
1226 | if (EFI_ERROR (Status)) {\r | |
1227 | goto Error;\r | |
1228 | }\r | |
1229 | \r | |
1230 | //\r | |
1231 | // If already have pending RxTokens, return directly.\r | |
1232 | //\r | |
1233 | if (NetMapGetCount (&HttpInstance->RxTokens) > 1) {\r | |
1234 | return EFI_SUCCESS;\r | |
1235 | }\r | |
1236 | \r | |
1237 | return HttpResponseWorker (Wrap);\r | |
1238 | \r | |
1239 | Error:\r | |
1240 | if (Wrap != NULL) {\r | |
b659408b ZL |
1241 | if (Wrap->TcpWrap.Rx4Token.CompletionToken.Event != NULL) {\r |
1242 | gBS->CloseEvent (Wrap->TcpWrap.Rx4Token.CompletionToken.Event);\r | |
1243 | }\r | |
1244 | \r | |
1245 | if (Wrap->TcpWrap.Rx6Token.CompletionToken.Event != NULL) {\r | |
1246 | gBS->CloseEvent (Wrap->TcpWrap.Rx6Token.CompletionToken.Event);\r | |
47f51a06 YT |
1247 | }\r |
1248 | FreePool (Wrap);\r | |
1249 | } \r | |
1250 | \r | |
1251 | return Status; \r | |
1252 | }\r | |
1253 | \r | |
1254 | /**\r | |
1255 | The Poll() function can be used by network drivers and applications to increase the\r | |
1256 | rate that data packets are moved between the communication devices and the transmit\r | |
1257 | and receive queues.\r | |
1258 | \r | |
1259 | In some systems, the periodic timer event in the managed network driver may not poll\r | |
1260 | the underlying communications device fast enough to transmit and/or receive all data\r | |
1261 | packets without missing incoming packets or dropping outgoing packets. Drivers and\r | |
1262 | applications that are experiencing packet loss should try calling the Poll() function\r | |
1263 | more often.\r | |
1264 | \r | |
1265 | @param[in] This Pointer to EFI_HTTP_PROTOCOL instance.\r | |
1266 | \r | |
1267 | @retval EFI_SUCCESS Incoming or outgoing data was processed.\r | |
1268 | @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r | |
1269 | @retval EFI_INVALID_PARAMETER This is NULL.\r | |
1270 | @retval EFI_NOT_READY No incoming or outgoing data is processed.\r | |
1271 | @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been started.\r | |
1272 | \r | |
1273 | **/\r | |
1274 | EFI_STATUS\r | |
1275 | EFIAPI\r | |
1276 | EfiHttpPoll (\r | |
1277 | IN EFI_HTTP_PROTOCOL *This\r | |
1278 | )\r | |
1279 | {\r | |
49c9f74c | 1280 | EFI_STATUS Status;\r |
b659408b | 1281 | HTTP_PROTOCOL *HttpInstance;\r |
47f51a06 YT |
1282 | \r |
1283 | if (This == NULL) {\r | |
1284 | return EFI_INVALID_PARAMETER;\r | |
1285 | }\r | |
1286 | \r | |
1287 | HttpInstance = HTTP_INSTANCE_FROM_PROTOCOL (This);\r | |
1288 | ASSERT (HttpInstance != NULL);\r | |
1289 | \r | |
b659408b ZL |
1290 | if (HttpInstance->State != HTTP_STATE_TCP_CONNECTED || (HttpInstance->Tcp4 == NULL && \r |
1291 | HttpInstance->Tcp6 == NULL)) {\r | |
47f51a06 YT |
1292 | return EFI_NOT_STARTED;\r |
1293 | }\r | |
b659408b ZL |
1294 | \r |
1295 | if (HttpInstance->LocalAddressIsIPv6) {\r | |
1296 | Status = HttpInstance->Tcp6->Poll (HttpInstance->Tcp6);\r | |
1297 | } else {\r | |
1298 | Status = HttpInstance->Tcp4->Poll (HttpInstance->Tcp4);\r | |
1299 | }\r | |
1300 | \r | |
49c9f74c | 1301 | DispatchDpc ();\r |
b659408b | 1302 | \r |
49c9f74c | 1303 | return Status;\r |
47f51a06 | 1304 | }\r |