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