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