HttpUtilitiesProtocol.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /** @file
  2. Implementation of EFI_HTTP_PROTOCOL protocol interfaces.
  3. Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
  4. (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "HttpUtilitiesDxe.h"
  8. EFI_HTTP_UTILITIES_PROTOCOL mHttpUtilitiesProtocol = {
  9. HttpUtilitiesBuild,
  10. HttpUtilitiesParse
  11. };
  12. /**
  13. Create HTTP header based on a combination of seed header, fields
  14. to delete, and fields to append.
  15. The Build() function is used to manage the headers portion of an
  16. HTTP message by providing the ability to add, remove, or replace
  17. HTTP headers.
  18. @param[in] This Pointer to EFI_HTTP_UTILITIES_PROTOCOL instance.
  19. @param[in] SeedMessageSize Size of the initial HTTP header. This can be zero.
  20. @param[in] SeedMessage Initial HTTP header to be used as a base for
  21. building a new HTTP header. If NULL,
  22. SeedMessageSize is ignored.
  23. @param[in] DeleteCount Number of null-terminated HTTP header field names
  24. in DeleteList.
  25. @param[in] DeleteList List of null-terminated HTTP header field names to
  26. remove from SeedMessage. Only the field names are
  27. in this list because the field values are irrelevant
  28. to this operation.
  29. @param[in] AppendCount Number of header fields in AppendList.
  30. @param[in] AppendList List of HTTP headers to populate NewMessage with.
  31. If SeedMessage is not NULL, AppendList will be
  32. appended to the existing list from SeedMessage in
  33. NewMessage.
  34. @param[out] NewMessageSize Pointer to number of header fields in NewMessage.
  35. @param[out] NewMessage Pointer to a new list of HTTP headers based on.
  36. @retval EFI_SUCCESS Add, remove, and replace operations succeeded.
  37. @retval EFI_OUT_OF_RESOURCES Could not allocate memory for NewMessage.
  38. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  39. This is NULL.
  40. **/
  41. EFI_STATUS
  42. EFIAPI
  43. HttpUtilitiesBuild (
  44. IN EFI_HTTP_UTILITIES_PROTOCOL *This,
  45. IN UINTN SeedMessageSize,
  46. IN VOID *SeedMessage OPTIONAL,
  47. IN UINTN DeleteCount,
  48. IN CHAR8 *DeleteList[] OPTIONAL,
  49. IN UINTN AppendCount,
  50. IN EFI_HTTP_HEADER *AppendList[] OPTIONAL,
  51. OUT UINTN *NewMessageSize,
  52. OUT VOID **NewMessage
  53. )
  54. {
  55. EFI_STATUS Status;
  56. EFI_HTTP_HEADER *SeedHeaderFields;
  57. UINTN SeedFieldCount;
  58. UINTN Index;
  59. EFI_HTTP_HEADER *TempHeaderFields;
  60. UINTN TempFieldCount;
  61. EFI_HTTP_HEADER *NewHeaderFields;
  62. UINTN NewFieldCount;
  63. EFI_HTTP_HEADER *HttpHeader;
  64. UINTN StrLength;
  65. UINT8 *NewMessagePtr;
  66. SeedHeaderFields = NULL;
  67. SeedFieldCount = 0;
  68. TempHeaderFields = NULL;
  69. TempFieldCount = 0;
  70. NewHeaderFields = NULL;
  71. NewFieldCount = 0;
  72. HttpHeader = NULL;
  73. StrLength = 0;
  74. NewMessagePtr = NULL;
  75. *NewMessageSize = 0;
  76. Status = EFI_SUCCESS;
  77. if (This == NULL) {
  78. return EFI_INVALID_PARAMETER;
  79. }
  80. if (SeedMessage != NULL) {
  81. Status = This->Parse (
  82. This,
  83. SeedMessage,
  84. SeedMessageSize,
  85. &SeedHeaderFields,
  86. &SeedFieldCount
  87. );
  88. if (EFI_ERROR (Status)) {
  89. goto ON_EXIT;
  90. }
  91. }
  92. //
  93. // Handle DeleteList
  94. //
  95. if ((SeedFieldCount != 0) && (DeleteCount != 0)) {
  96. TempHeaderFields = AllocateZeroPool (SeedFieldCount * sizeof (EFI_HTTP_HEADER));
  97. if (TempHeaderFields == NULL) {
  98. Status = EFI_OUT_OF_RESOURCES;
  99. goto ON_EXIT;
  100. }
  101. for (Index = 0, TempFieldCount = 0; Index < SeedFieldCount; Index++) {
  102. //
  103. // Check whether each SeedHeaderFields member is in DeleteList
  104. //
  105. if (HttpIsValidHttpHeader (DeleteList, DeleteCount, SeedHeaderFields[Index].FieldName)) {
  106. Status = HttpSetFieldNameAndValue (
  107. &TempHeaderFields[TempFieldCount],
  108. SeedHeaderFields[Index].FieldName,
  109. SeedHeaderFields[Index].FieldValue
  110. );
  111. if (EFI_ERROR (Status)) {
  112. goto ON_EXIT;
  113. }
  114. TempFieldCount++;
  115. }
  116. }
  117. } else {
  118. TempHeaderFields = SeedHeaderFields;
  119. TempFieldCount = SeedFieldCount;
  120. }
  121. //
  122. // Handle AppendList
  123. //
  124. NewHeaderFields = AllocateZeroPool ((TempFieldCount + AppendCount) * sizeof (EFI_HTTP_HEADER));
  125. if (NewHeaderFields == NULL) {
  126. Status = EFI_OUT_OF_RESOURCES;
  127. goto ON_EXIT;
  128. }
  129. for (Index = 0; Index < TempFieldCount; Index++) {
  130. Status = HttpSetFieldNameAndValue (
  131. &NewHeaderFields[Index],
  132. TempHeaderFields[Index].FieldName,
  133. TempHeaderFields[Index].FieldValue
  134. );
  135. if (EFI_ERROR (Status)) {
  136. goto ON_EXIT;
  137. }
  138. }
  139. NewFieldCount = TempFieldCount;
  140. for (Index = 0; Index < AppendCount; Index++) {
  141. HttpHeader = HttpFindHeader (NewFieldCount, NewHeaderFields, AppendList[Index]->FieldName);
  142. if (HttpHeader != NULL) {
  143. Status = HttpSetFieldNameAndValue (
  144. HttpHeader,
  145. AppendList[Index]->FieldName,
  146. AppendList[Index]->FieldValue
  147. );
  148. if (EFI_ERROR (Status)) {
  149. goto ON_EXIT;
  150. }
  151. } else {
  152. Status = HttpSetFieldNameAndValue (
  153. &NewHeaderFields[NewFieldCount],
  154. AppendList[Index]->FieldName,
  155. AppendList[Index]->FieldValue
  156. );
  157. if (EFI_ERROR (Status)) {
  158. goto ON_EXIT;
  159. }
  160. NewFieldCount++;
  161. }
  162. }
  163. //
  164. // Calculate NewMessageSize, then build NewMessage
  165. //
  166. for (Index = 0; Index < NewFieldCount; Index++) {
  167. HttpHeader = &NewHeaderFields[Index];
  168. StrLength = AsciiStrLen (HttpHeader->FieldName);
  169. *NewMessageSize += StrLength;
  170. StrLength = sizeof (": ") - 1;
  171. *NewMessageSize += StrLength;
  172. StrLength = AsciiStrLen (HttpHeader->FieldValue);
  173. *NewMessageSize += StrLength;
  174. StrLength = sizeof ("\r\n") - 1;
  175. *NewMessageSize += StrLength;
  176. }
  177. StrLength = sizeof ("\r\n") - 1;
  178. *NewMessageSize += StrLength;
  179. *NewMessage = AllocateZeroPool (*NewMessageSize);
  180. if (*NewMessage == NULL) {
  181. Status = EFI_OUT_OF_RESOURCES;
  182. goto ON_EXIT;
  183. }
  184. NewMessagePtr = (UINT8 *)(*NewMessage);
  185. for (Index = 0; Index < NewFieldCount; Index++) {
  186. HttpHeader = &NewHeaderFields[Index];
  187. StrLength = AsciiStrLen (HttpHeader->FieldName);
  188. CopyMem (NewMessagePtr, HttpHeader->FieldName, StrLength);
  189. NewMessagePtr += StrLength;
  190. StrLength = sizeof (": ") - 1;
  191. CopyMem (NewMessagePtr, ": ", StrLength);
  192. NewMessagePtr += StrLength;
  193. StrLength = AsciiStrLen (HttpHeader->FieldValue);
  194. CopyMem (NewMessagePtr, HttpHeader->FieldValue, StrLength);
  195. NewMessagePtr += StrLength;
  196. StrLength = sizeof ("\r\n") - 1;
  197. CopyMem (NewMessagePtr, "\r\n", StrLength);
  198. NewMessagePtr += StrLength;
  199. }
  200. StrLength = sizeof ("\r\n") - 1;
  201. CopyMem (NewMessagePtr, "\r\n", StrLength);
  202. NewMessagePtr += StrLength;
  203. ASSERT (*NewMessageSize == (UINTN)NewMessagePtr - (UINTN)(*NewMessage));
  204. //
  205. // Free allocated buffer
  206. //
  207. ON_EXIT:
  208. if (SeedHeaderFields != NULL) {
  209. HttpFreeHeaderFields (SeedHeaderFields, SeedFieldCount);
  210. }
  211. if (TempHeaderFields != NULL) {
  212. HttpFreeHeaderFields (TempHeaderFields, TempFieldCount);
  213. }
  214. if (NewHeaderFields != NULL) {
  215. HttpFreeHeaderFields (NewHeaderFields, NewFieldCount);
  216. }
  217. return Status;
  218. }
  219. /**
  220. Parses HTTP header and produces an array of key/value pairs.
  221. The Parse() function is used to transform data stored in HttpHeader
  222. into a list of fields paired with their corresponding values.
  223. @param[in] This Pointer to EFI_HTTP_UTILITIES_PROTOCOL instance.
  224. @param[in] HttpMessage Contains raw unformatted HTTP header string.
  225. @param[in] HttpMessageSize Size of HTTP header.
  226. @param[out] HeaderFields Array of key/value header pairs.
  227. @param[out] FieldCount Number of headers in HeaderFields.
  228. @retval EFI_SUCCESS Allocation succeeded.
  229. @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
  230. initialized.
  231. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  232. This is NULL.
  233. HttpMessage is NULL.
  234. HeaderFields is NULL.
  235. FieldCount is NULL.
  236. **/
  237. EFI_STATUS
  238. EFIAPI
  239. HttpUtilitiesParse (
  240. IN EFI_HTTP_UTILITIES_PROTOCOL *This,
  241. IN CHAR8 *HttpMessage,
  242. IN UINTN HttpMessageSize,
  243. OUT EFI_HTTP_HEADER **HeaderFields,
  244. OUT UINTN *FieldCount
  245. )
  246. {
  247. EFI_STATUS Status;
  248. CHAR8 *TempHttpMessage;
  249. CHAR8 *Token;
  250. CHAR8 *NextToken;
  251. CHAR8 *FieldName;
  252. CHAR8 *FieldValue;
  253. UINTN Index;
  254. UINTN HttpBufferSize;
  255. Status = EFI_SUCCESS;
  256. TempHttpMessage = NULL;
  257. Token = NULL;
  258. NextToken = NULL;
  259. FieldName = NULL;
  260. FieldValue = NULL;
  261. Index = 0;
  262. if ((This == NULL) || (HttpMessage == NULL) || (HeaderFields == NULL) || (FieldCount == NULL)) {
  263. return EFI_INVALID_PARAMETER;
  264. }
  265. //
  266. // Append the http response string along with a Null-terminator.
  267. //
  268. HttpBufferSize = HttpMessageSize + 1;
  269. TempHttpMessage = AllocatePool (HttpBufferSize);
  270. if (TempHttpMessage == NULL) {
  271. return EFI_OUT_OF_RESOURCES;
  272. }
  273. CopyMem (TempHttpMessage, HttpMessage, HttpMessageSize);
  274. *(TempHttpMessage + HttpMessageSize) = '\0';
  275. //
  276. // Get header number
  277. //
  278. *FieldCount = 0;
  279. Token = TempHttpMessage;
  280. while (TRUE) {
  281. FieldName = NULL;
  282. FieldValue = NULL;
  283. NextToken = HttpGetFieldNameAndValue (Token, &FieldName, &FieldValue);
  284. Token = NextToken;
  285. if ((FieldName == NULL) || (FieldValue == NULL)) {
  286. break;
  287. }
  288. (*FieldCount)++;
  289. }
  290. if (*FieldCount == 0) {
  291. Status = EFI_INVALID_PARAMETER;
  292. goto ON_EXIT;
  293. }
  294. //
  295. // Allocate buffer for header
  296. //
  297. *HeaderFields = AllocateZeroPool ((*FieldCount) * sizeof (EFI_HTTP_HEADER));
  298. if (*HeaderFields == NULL) {
  299. *FieldCount = 0;
  300. Status = EFI_OUT_OF_RESOURCES;
  301. goto ON_EXIT;
  302. }
  303. CopyMem (TempHttpMessage, HttpMessage, HttpMessageSize);
  304. //
  305. // Set Field and Value to each header
  306. //
  307. Token = TempHttpMessage;
  308. while (Index < *FieldCount) {
  309. FieldName = NULL;
  310. FieldValue = NULL;
  311. NextToken = HttpGetFieldNameAndValue (Token, &FieldName, &FieldValue);
  312. Token = NextToken;
  313. if ((FieldName == NULL) || (FieldValue == NULL)) {
  314. break;
  315. }
  316. Status = HttpSetFieldNameAndValue (&(*HeaderFields)[Index], FieldName, FieldValue);
  317. if (EFI_ERROR (Status)) {
  318. *FieldCount = 0;
  319. HttpFreeHeaderFields (*HeaderFields, Index);
  320. goto ON_EXIT;
  321. }
  322. Index++;
  323. }
  324. //
  325. // Free allocated buffer
  326. //
  327. ON_EXIT:
  328. if (TempHttpMessage != NULL) {
  329. FreePool (TempHttpMessage);
  330. }
  331. return Status;
  332. }