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