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
= {
18 Create HTTP header based on a combination of seed header, fields
19 to delete, and fields to append.
21 The Build() function is used to manage the headers portion of an
22 HTTP message by providing the ability to add, remove, or replace
25 @param[in] This Pointer to EFI_HTTP_UTILITIES_PROTOCOL instance.
26 @param[in] SeedMessageSize Size of the initial HTTP header. This can be zero.
27 @param[in] SeedMessage Initial HTTP header to be used as a base for
28 building a new HTTP header. If NULL,
29 SeedMessageSize is ignored.
30 @param[in] DeleteCount Number of null-terminated HTTP header field names
32 @param[in] DeleteList List of null-terminated HTTP header field names to
33 remove from SeedMessage. Only the field names are
34 in this list because the field values are irrelevant
36 @param[in] AppendCount Number of header fields in AppendList.
37 @param[in] AppendList List of HTTP headers to populate NewMessage with.
38 If SeedMessage is not NULL, AppendList will be
39 appended to the existing list from SeedMessage in
41 @param[out] NewMessageSize Pointer to number of header fields in NewMessage.
42 @param[out] NewMessage Pointer to a new list of HTTP headers based on.
44 @retval EFI_SUCCESS Add, remove, and replace operations succeeded.
45 @retval EFI_OUT_OF_RESOURCES Could not allocate memory for NewMessage.
46 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
52 IN EFI_HTTP_UTILITIES_PROTOCOL
*This
,
53 IN UINTN SeedMessageSize
,
54 IN VOID
*SeedMessage OPTIONAL
,
56 IN CHAR8
*DeleteList
[] OPTIONAL
,
58 IN EFI_HTTP_HEADER
*AppendList
[] OPTIONAL
,
59 OUT UINTN
*NewMessageSize
,
64 EFI_HTTP_HEADER
*SeedHeaderFields
;
67 EFI_HTTP_HEADER
*TempHeaderFields
;
69 EFI_HTTP_HEADER
*NewHeaderFields
;
71 EFI_HTTP_HEADER
*HttpHeader
;
75 SeedHeaderFields
= NULL
;
77 TempHeaderFields
= NULL
;
79 NewHeaderFields
= NULL
;
89 return EFI_INVALID_PARAMETER
;
92 if (SeedMessage
!= NULL
) {
93 Status
= This
->Parse (
100 if (EFI_ERROR (Status
)) {
108 if ((SeedFieldCount
!= 0) && (DeleteCount
!= 0)) {
109 TempHeaderFields
= AllocateZeroPool (SeedFieldCount
* sizeof (EFI_HTTP_HEADER
));
110 if (TempHeaderFields
== NULL
) {
111 Status
= EFI_OUT_OF_RESOURCES
;
115 for (Index
= 0, TempFieldCount
= 0; Index
< SeedFieldCount
; Index
++) {
117 // Check whether each SeedHeaderFields member is in DeleteList
119 if (HttpIsValidHttpHeader (DeleteList
, DeleteCount
, SeedHeaderFields
[Index
].FieldName
)) {
120 Status
= HttpSetFieldNameAndValue (
121 &TempHeaderFields
[TempFieldCount
],
122 SeedHeaderFields
[Index
].FieldName
,
123 SeedHeaderFields
[Index
].FieldValue
125 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
)) {
185 // Calculate NewMessageSize, then build NewMessage
187 for (Index
= 0; Index
< NewFieldCount
; Index
++) {
188 HttpHeader
= &NewHeaderFields
[Index
];
190 StrLength
= AsciiStrLen (HttpHeader
->FieldName
);
191 *NewMessageSize
+= StrLength
;
193 StrLength
= sizeof (": ") - 1;
194 *NewMessageSize
+= StrLength
;
196 StrLength
= AsciiStrLen (HttpHeader
->FieldValue
);
197 *NewMessageSize
+= StrLength
;
199 StrLength
= sizeof ("\r\n") - 1;
200 *NewMessageSize
+= StrLength
;
203 StrLength
= sizeof ("\r\n") - 1;
204 *NewMessageSize
+= StrLength
;
206 *NewMessage
= AllocateZeroPool (*NewMessageSize
);
207 if (*NewMessage
== NULL
) {
208 Status
= EFI_OUT_OF_RESOURCES
;
212 NewMessagePtr
= (UINT8
*)(*NewMessage
);
214 for (Index
= 0; Index
< NewFieldCount
; Index
++) {
215 HttpHeader
= &NewHeaderFields
[Index
];
217 StrLength
= AsciiStrLen (HttpHeader
->FieldName
);
218 CopyMem (NewMessagePtr
, HttpHeader
->FieldName
, StrLength
);
219 NewMessagePtr
+= StrLength
;
221 StrLength
= sizeof (": ") - 1;
222 CopyMem (NewMessagePtr
, ": ", StrLength
);
223 NewMessagePtr
+= StrLength
;
225 StrLength
= AsciiStrLen (HttpHeader
->FieldValue
);
226 CopyMem (NewMessagePtr
, HttpHeader
->FieldValue
, StrLength
);
227 NewMessagePtr
+= StrLength
;
229 StrLength
= sizeof ("\r\n") - 1;
230 CopyMem (NewMessagePtr
, "\r\n", StrLength
);
231 NewMessagePtr
+= StrLength
;
234 StrLength
= sizeof ("\r\n") - 1;
235 CopyMem (NewMessagePtr
, "\r\n", StrLength
);
236 NewMessagePtr
+= StrLength
;
238 ASSERT (*NewMessageSize
== (UINTN
)NewMessagePtr
- (UINTN
)(*NewMessage
));
241 // Free allocated buffer
244 if (SeedHeaderFields
!= NULL
) {
245 HttpFreeHeaderFields (SeedHeaderFields
, SeedFieldCount
);
248 if (TempHeaderFields
!= NULL
) {
249 HttpFreeHeaderFields (TempHeaderFields
, TempFieldCount
);
252 if (NewHeaderFields
!= NULL
) {
253 HttpFreeHeaderFields (NewHeaderFields
, NewFieldCount
);
260 Parses HTTP header and produces an array of key/value pairs.
262 The Parse() function is used to transform data stored in HttpHeader
263 into a list of fields paired with their corresponding values.
265 @param[in] This Pointer to EFI_HTTP_UTILITIES_PROTOCOL instance.
266 @param[in] HttpMessage Contains raw unformatted HTTP header string.
267 @param[in] HttpMessageSize Size of HTTP header.
268 @param[out] HeaderFields Array of key/value header pairs.
269 @param[out] FieldCount Number of headers in HeaderFields.
271 @retval EFI_SUCCESS Allocation succeeded.
272 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
274 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
277 HeaderFields is NULL.
283 IN EFI_HTTP_UTILITIES_PROTOCOL
*This
,
284 IN CHAR8
*HttpMessage
,
285 IN UINTN HttpMessageSize
,
286 OUT EFI_HTTP_HEADER
**HeaderFields
,
287 OUT UINTN
*FieldCount
291 CHAR8
*TempHttpMessage
;
297 UINTN HttpBufferSize
;
299 Status
= EFI_SUCCESS
;
300 TempHttpMessage
= NULL
;
307 if ((This
== NULL
) || (HttpMessage
== NULL
) || (HeaderFields
== NULL
) || (FieldCount
== NULL
)) {
308 return EFI_INVALID_PARAMETER
;
312 // Append the http response string along with a Null-terminator.
314 HttpBufferSize
= HttpMessageSize
+ 1;
315 TempHttpMessage
= AllocatePool (HttpBufferSize
);
316 if (TempHttpMessage
== NULL
) {
317 return EFI_OUT_OF_RESOURCES
;
320 CopyMem (TempHttpMessage
, HttpMessage
, HttpMessageSize
);
321 *(TempHttpMessage
+ HttpMessageSize
) = '\0';
327 Token
= TempHttpMessage
;
331 NextToken
= HttpGetFieldNameAndValue (Token
, &FieldName
, &FieldValue
);
333 if ((FieldName
== NULL
) || (FieldValue
== NULL
)) {
340 if (*FieldCount
== 0) {
341 Status
= EFI_INVALID_PARAMETER
;
346 // Allocate buffer for header
348 *HeaderFields
= AllocateZeroPool ((*FieldCount
) * sizeof (EFI_HTTP_HEADER
));
349 if (*HeaderFields
== NULL
) {
351 Status
= EFI_OUT_OF_RESOURCES
;
355 CopyMem (TempHttpMessage
, HttpMessage
, HttpMessageSize
);
358 // Set Field and Value to each header
360 Token
= TempHttpMessage
;
361 while (Index
< *FieldCount
) {
364 NextToken
= HttpGetFieldNameAndValue (Token
, &FieldName
, &FieldValue
);
366 if ((FieldName
== NULL
) || (FieldValue
== NULL
)) {
370 Status
= HttpSetFieldNameAndValue (&(*HeaderFields
)[Index
], FieldName
, FieldValue
);
371 if (EFI_ERROR (Status
)) {
373 HttpFreeHeaderFields (*HeaderFields
, Index
);
381 // Free allocated buffer
384 if (TempHttpMessage
!= NULL
) {
385 FreePool (TempHttpMessage
);