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