2 Implementation of EFI_HTTP_PROTOCOL protocol interfaces.
4 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "HttpUtilitiesDxe.h"
12 EFI_HTTP_UTILITIES_PROTOCOL mHttpUtilitiesProtocol
= {
19 Create HTTP header based on a combination of seed header, fields
20 to delete, and fields to append.
22 The Build() function is used to manage the headers portion of an
23 HTTP message by providing the ability to add, remove, or replace
26 @param[in] This Pointer to EFI_HTTP_UTILITIES_PROTOCOL instance.
27 @param[in] SeedMessageSize Size of the initial HTTP header. This can be zero.
28 @param[in] SeedMessage Initial HTTP header to be used as a base for
29 building a new HTTP header. If NULL,
30 SeedMessageSize is ignored.
31 @param[in] DeleteCount Number of null-terminated HTTP header field names
33 @param[in] DeleteList List of null-terminated HTTP header field names to
34 remove from SeedMessage. Only the field names are
35 in this list because the field values are irrelevant
37 @param[in] AppendCount Number of header fields in AppendList.
38 @param[in] AppendList List of HTTP headers to populate NewMessage with.
39 If SeedMessage is not NULL, AppendList will be
40 appended to the existing list from SeedMessage in
42 @param[out] NewMessageSize Pointer to number of header fields in NewMessage.
43 @param[out] NewMessage Pointer to a new list of HTTP headers based on.
45 @retval EFI_SUCCESS Add, remove, and replace operations succeeded.
46 @retval EFI_OUT_OF_RESOURCES Could not allocate memory for NewMessage.
47 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
53 IN EFI_HTTP_UTILITIES_PROTOCOL
*This
,
54 IN UINTN SeedMessageSize
,
55 IN VOID
*SeedMessage
, OPTIONAL
57 IN CHAR8
*DeleteList
[], OPTIONAL
59 IN EFI_HTTP_HEADER
*AppendList
[], OPTIONAL
60 OUT UINTN
*NewMessageSize
,
65 EFI_HTTP_HEADER
*SeedHeaderFields
;
68 EFI_HTTP_HEADER
*TempHeaderFields
;
70 EFI_HTTP_HEADER
*NewHeaderFields
;
72 EFI_HTTP_HEADER
*HttpHeader
;
76 SeedHeaderFields
= NULL
;
78 TempHeaderFields
= NULL
;
80 NewHeaderFields
= NULL
;
90 return EFI_INVALID_PARAMETER
;
93 if (SeedMessage
!= NULL
) {
94 Status
= This
->Parse (
101 if (EFI_ERROR (Status
)) {
109 if (SeedFieldCount
!= 0 && DeleteCount
!= 0) {
110 TempHeaderFields
= AllocateZeroPool (SeedFieldCount
* sizeof(EFI_HTTP_HEADER
));
111 if (TempHeaderFields
== NULL
) {
112 Status
= EFI_OUT_OF_RESOURCES
;
116 for (Index
= 0, TempFieldCount
= 0; Index
< SeedFieldCount
; Index
++) {
118 // Check whether each SeedHeaderFields member is in DeleteList
120 if (HttpIsValidHttpHeader( DeleteList
, DeleteCount
, SeedHeaderFields
[Index
].FieldName
)) {
121 Status
= HttpSetFieldNameAndValue (
122 &TempHeaderFields
[TempFieldCount
],
123 SeedHeaderFields
[Index
].FieldName
,
124 SeedHeaderFields
[Index
].FieldValue
126 if (EFI_ERROR (Status
)) {
133 TempHeaderFields
= SeedHeaderFields
;
134 TempFieldCount
= SeedFieldCount
;
140 NewHeaderFields
= AllocateZeroPool ((TempFieldCount
+ AppendCount
) * sizeof (EFI_HTTP_HEADER
));
141 if (NewHeaderFields
== NULL
) {
142 Status
= EFI_OUT_OF_RESOURCES
;
146 for (Index
= 0; Index
< TempFieldCount
; Index
++) {
147 Status
= HttpSetFieldNameAndValue (
148 &NewHeaderFields
[Index
],
149 TempHeaderFields
[Index
].FieldName
,
150 TempHeaderFields
[Index
].FieldValue
152 if (EFI_ERROR (Status
)) {
157 NewFieldCount
= TempFieldCount
;
159 for (Index
= 0; Index
< AppendCount
; Index
++) {
160 HttpHeader
= HttpFindHeader (NewFieldCount
, NewHeaderFields
, AppendList
[Index
]->FieldName
);
161 if (HttpHeader
!= NULL
) {
162 Status
= HttpSetFieldNameAndValue (
164 AppendList
[Index
]->FieldName
,
165 AppendList
[Index
]->FieldValue
167 if (EFI_ERROR (Status
)) {
171 Status
= HttpSetFieldNameAndValue (
172 &NewHeaderFields
[NewFieldCount
],
173 AppendList
[Index
]->FieldName
,
174 AppendList
[Index
]->FieldValue
176 if (EFI_ERROR (Status
)) {
184 // Calculate NewMessageSize, then build NewMessage
186 for (Index
= 0; Index
< NewFieldCount
; Index
++) {
187 HttpHeader
= &NewHeaderFields
[Index
];
189 StrLength
= AsciiStrLen (HttpHeader
->FieldName
);
190 *NewMessageSize
+= StrLength
;
192 StrLength
= sizeof(": ") - 1;
193 *NewMessageSize
+= StrLength
;
195 StrLength
= AsciiStrLen (HttpHeader
->FieldValue
);
196 *NewMessageSize
+= StrLength
;
198 StrLength
= sizeof("\r\n") - 1;
199 *NewMessageSize
+= StrLength
;
201 StrLength
= sizeof("\r\n") - 1;
202 *NewMessageSize
+= StrLength
;
204 *NewMessage
= AllocateZeroPool (*NewMessageSize
);
205 if (*NewMessage
== NULL
) {
206 Status
= EFI_OUT_OF_RESOURCES
;
210 NewMessagePtr
= (UINT8
*)(*NewMessage
);
212 for (Index
= 0; Index
< NewFieldCount
; Index
++) {
213 HttpHeader
= &NewHeaderFields
[Index
];
215 StrLength
= AsciiStrLen (HttpHeader
->FieldName
);
216 CopyMem (NewMessagePtr
, HttpHeader
->FieldName
, StrLength
);
217 NewMessagePtr
+= StrLength
;
219 StrLength
= sizeof(": ") - 1;
220 CopyMem (NewMessagePtr
, ": ", StrLength
);
221 NewMessagePtr
+= StrLength
;
223 StrLength
= AsciiStrLen (HttpHeader
->FieldValue
);
224 CopyMem (NewMessagePtr
, HttpHeader
->FieldValue
, StrLength
);
225 NewMessagePtr
+= StrLength
;
227 StrLength
= sizeof("\r\n") - 1;
228 CopyMem (NewMessagePtr
, "\r\n", StrLength
);
229 NewMessagePtr
+= StrLength
;
231 StrLength
= sizeof("\r\n") - 1;
232 CopyMem (NewMessagePtr
, "\r\n", StrLength
);
233 NewMessagePtr
+= StrLength
;
235 ASSERT (*NewMessageSize
== (UINTN
)NewMessagePtr
- (UINTN
)(*NewMessage
));
238 // Free allocated buffer
241 if (SeedHeaderFields
!= NULL
) {
242 HttpFreeHeaderFields(SeedHeaderFields
, SeedFieldCount
);
245 if (TempHeaderFields
!= NULL
) {
246 HttpFreeHeaderFields(TempHeaderFields
, TempFieldCount
);
249 if (NewHeaderFields
!= NULL
) {
250 HttpFreeHeaderFields(NewHeaderFields
, NewFieldCount
);
258 Parses HTTP header and produces an array of key/value pairs.
260 The Parse() function is used to transform data stored in HttpHeader
261 into a list of fields paired with their corresponding values.
263 @param[in] This Pointer to EFI_HTTP_UTILITIES_PROTOCOL instance.
264 @param[in] HttpMessage Contains raw unformatted HTTP header string.
265 @param[in] HttpMessageSize Size of HTTP header.
266 @param[out] HeaderFields Array of key/value header pairs.
267 @param[out] FieldCount Number of headers in HeaderFields.
269 @retval EFI_SUCCESS Allocation succeeded.
270 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
272 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
275 HeaderFields is NULL.
281 IN EFI_HTTP_UTILITIES_PROTOCOL
*This
,
282 IN CHAR8
*HttpMessage
,
283 IN UINTN HttpMessageSize
,
284 OUT EFI_HTTP_HEADER
**HeaderFields
,
285 OUT UINTN
*FieldCount
289 CHAR8
*TempHttpMessage
;
295 UINTN HttpBufferSize
;
297 Status
= EFI_SUCCESS
;
298 TempHttpMessage
= NULL
;
305 if (This
== NULL
|| HttpMessage
== NULL
|| HeaderFields
== NULL
|| FieldCount
== NULL
) {
306 return EFI_INVALID_PARAMETER
;
310 // Append the http response string along with a Null-terminator.
312 HttpBufferSize
= HttpMessageSize
+ 1;
313 TempHttpMessage
= AllocatePool (HttpBufferSize
);
314 if (TempHttpMessage
== NULL
) {
315 return EFI_OUT_OF_RESOURCES
;
318 CopyMem (TempHttpMessage
, HttpMessage
, HttpMessageSize
);
319 *(TempHttpMessage
+ HttpMessageSize
) = '\0';
325 Token
= TempHttpMessage
;
329 NextToken
= HttpGetFieldNameAndValue (Token
, &FieldName
, &FieldValue
);
331 if (FieldName
== NULL
|| FieldValue
== NULL
) {
338 if (*FieldCount
== 0) {
339 Status
= EFI_INVALID_PARAMETER
;
344 // Allocate buffer for header
346 *HeaderFields
= AllocateZeroPool ((*FieldCount
) * sizeof(EFI_HTTP_HEADER
));
347 if (*HeaderFields
== NULL
) {
349 Status
= EFI_OUT_OF_RESOURCES
;
353 CopyMem (TempHttpMessage
, HttpMessage
, HttpMessageSize
);
356 // Set Field and Value to each header
358 Token
= TempHttpMessage
;
359 while (Index
< *FieldCount
) {
362 NextToken
= HttpGetFieldNameAndValue (Token
, &FieldName
, &FieldValue
);
364 if (FieldName
== NULL
|| FieldValue
== NULL
) {
368 Status
= HttpSetFieldNameAndValue (&(*HeaderFields
)[Index
], FieldName
, FieldValue
);
369 if (EFI_ERROR (Status
)) {
371 HttpFreeHeaderFields (*HeaderFields
, Index
);
379 // Free allocated buffer
382 if (TempHttpMessage
!= NULL
) {
383 FreePool (TempHttpMessage
);