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