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