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