HttpImpl.c 51 KB

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