2 Implementation of EFI_HTTP_PROTOCOL protocol interfaces.
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
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.
16 #include "HttpUtilitiesDxe.h"
18 EFI_HTTP_UTILITIES_PROTOCOL mHttpUtilitiesProtocol
= {
25 Create HTTP header based on a combination of seed header, fields
26 to delete, and fields to append.
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
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
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
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
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.
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:
59 IN EFI_HTTP_UTILITIES_PROTOCOL
*This
,
60 IN UINTN SeedMessageSize
,
61 IN VOID
*SeedMessage
, OPTIONAL
63 IN CHAR8
*DeleteList
[], OPTIONAL
65 IN EFI_HTTP_HEADER
*AppendList
[], OPTIONAL
66 OUT UINTN
*NewMessageSize
,
71 EFI_HTTP_HEADER
*SeedHeaderFields
;
74 EFI_HTTP_HEADER
*TempHeaderFields
;
76 EFI_HTTP_HEADER
*NewHeaderFields
;
78 EFI_HTTP_HEADER
*HttpHeader
;
82 SeedHeaderFields
= NULL
;
84 TempHeaderFields
= NULL
;
86 NewHeaderFields
= NULL
;
96 return EFI_INVALID_PARAMETER
;
99 if (SeedMessage
!= NULL
) {
100 Status
= This
->Parse (
107 if (EFI_ERROR (Status
)) {
115 if (SeedFieldCount
!= 0 && DeleteCount
!= 0) {
116 TempHeaderFields
= AllocateZeroPool (SeedFieldCount
* sizeof(EFI_HTTP_HEADER
));
117 if (TempHeaderFields
== NULL
) {
118 Status
= EFI_OUT_OF_RESOURCES
;
122 for (Index
= 0, TempFieldCount
= 0; Index
< SeedFieldCount
; Index
++) {
124 // Check whether each SeedHeaderFields member is in DeleteList
126 if (HttpIsValidHttpHeader( DeleteList
, DeleteCount
, SeedHeaderFields
[Index
].FieldName
)) {
127 Status
= HttpSetFieldNameAndValue (
128 &TempHeaderFields
[TempFieldCount
],
129 SeedHeaderFields
[Index
].FieldName
,
130 SeedHeaderFields
[Index
].FieldValue
132 if (EFI_ERROR (Status
)) {
139 TempHeaderFields
= SeedHeaderFields
;
140 TempFieldCount
= SeedFieldCount
;
146 NewHeaderFields
= AllocateZeroPool ((TempFieldCount
+ AppendCount
) * sizeof (EFI_HTTP_HEADER
));
147 if (NewHeaderFields
== NULL
) {
148 Status
= EFI_OUT_OF_RESOURCES
;
152 for (Index
= 0; Index
< TempFieldCount
; Index
++) {
153 Status
= HttpSetFieldNameAndValue (
154 &NewHeaderFields
[Index
],
155 TempHeaderFields
[Index
].FieldName
,
156 TempHeaderFields
[Index
].FieldValue
158 if (EFI_ERROR (Status
)) {
163 NewFieldCount
= TempFieldCount
;
165 for (Index
= 0; Index
< AppendCount
; Index
++) {
166 HttpHeader
= HttpFindHeader (NewFieldCount
, NewHeaderFields
, AppendList
[Index
]->FieldName
);
167 if (HttpHeader
!= NULL
) {
168 Status
= HttpSetFieldNameAndValue (
170 AppendList
[Index
]->FieldName
,
171 AppendList
[Index
]->FieldValue
173 if (EFI_ERROR (Status
)) {
177 Status
= HttpSetFieldNameAndValue (
178 &NewHeaderFields
[NewFieldCount
],
179 AppendList
[Index
]->FieldName
,
180 AppendList
[Index
]->FieldValue
182 if (EFI_ERROR (Status
)) {
190 // Calculate NewMessageSize, then build NewMessage
192 for (Index
= 0; Index
< NewFieldCount
; Index
++) {
193 HttpHeader
= &NewHeaderFields
[Index
];
195 StrLength
= AsciiStrLen (HttpHeader
->FieldName
);
196 *NewMessageSize
+= StrLength
;
198 StrLength
= sizeof(": ") - 1;
199 *NewMessageSize
+= StrLength
;
201 StrLength
= AsciiStrLen (HttpHeader
->FieldValue
);
202 *NewMessageSize
+= StrLength
;
204 StrLength
= sizeof("\r\n") - 1;
205 *NewMessageSize
+= StrLength
;
207 StrLength
= sizeof("\r\n") - 1;
208 *NewMessageSize
+= StrLength
;
211 // Final 0 for end flag
213 *NewMessageSize
+= 1;
215 *NewMessage
= AllocateZeroPool (*NewMessageSize
);
216 if (*NewMessage
== NULL
) {
217 Status
= EFI_OUT_OF_RESOURCES
;
221 NewMessagePtr
= (UINT8
*)(*NewMessage
);
223 for (Index
= 0; Index
< NewFieldCount
; Index
++) {
224 HttpHeader
= &NewHeaderFields
[Index
];
226 StrLength
= AsciiStrLen (HttpHeader
->FieldName
);
227 CopyMem (NewMessagePtr
, HttpHeader
->FieldName
, StrLength
);
228 NewMessagePtr
+= StrLength
;
230 StrLength
= sizeof(": ") - 1;
231 CopyMem (NewMessagePtr
, ": ", StrLength
);
232 NewMessagePtr
+= StrLength
;
234 StrLength
= AsciiStrLen (HttpHeader
->FieldValue
);
235 CopyMem (NewMessagePtr
, HttpHeader
->FieldValue
, StrLength
);
236 NewMessagePtr
+= StrLength
;
238 StrLength
= sizeof("\r\n") - 1;
239 CopyMem (NewMessagePtr
, "\r\n", StrLength
);
240 NewMessagePtr
+= StrLength
;
242 StrLength
= sizeof("\r\n") - 1;
243 CopyMem (NewMessagePtr
, "\r\n", StrLength
);
244 NewMessagePtr
+= StrLength
;
248 ASSERT (*NewMessageSize
== (UINTN
)NewMessagePtr
- (UINTN
)(*NewMessage
) + 1);
251 // Free allocated buffer
254 if (SeedHeaderFields
!= NULL
) {
255 HttpFreeHeaderFields(SeedHeaderFields
, SeedFieldCount
);
258 if (TempHeaderFields
!= NULL
) {
259 HttpFreeHeaderFields(TempHeaderFields
, TempFieldCount
);
262 if (NewHeaderFields
!= NULL
) {
263 HttpFreeHeaderFields(NewHeaderFields
, NewFieldCount
);
271 Parses HTTP header and produces an array of key/value pairs.
273 The Parse() function is used to transform data stored in HttpHeader
274 into a list of fields paired with their corresponding values.
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.
282 @retval EFI_SUCCESS Allocation succeeded.
283 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
285 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
288 HeaderFields is NULL.
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
302 CHAR8
*TempHttpMessage
;
309 Status
= EFI_SUCCESS
;
310 TempHttpMessage
= NULL
;
317 if (This
== NULL
|| HttpMessage
== NULL
|| HeaderFields
== NULL
|| FieldCount
== NULL
) {
318 return EFI_INVALID_PARAMETER
;
321 TempHttpMessage
= AllocateZeroPool (HttpMessageSize
);
322 if (TempHttpMessage
== NULL
) {
323 return EFI_OUT_OF_RESOURCES
;
326 CopyMem (TempHttpMessage
, HttpMessage
, HttpMessageSize
);
332 Token
= TempHttpMessage
;
336 NextToken
= HttpGetFieldNameAndValue (Token
, &FieldName
, &FieldValue
);
338 if (FieldName
== NULL
|| FieldValue
== NULL
) {
345 if (*FieldCount
== 0) {
346 Status
= EFI_INVALID_PARAMETER
;
351 // Allocate buffer for header
353 *HeaderFields
= AllocateZeroPool ((*FieldCount
) * sizeof(EFI_HTTP_HEADER
));
354 if (*HeaderFields
== NULL
) {
356 Status
= EFI_OUT_OF_RESOURCES
;
360 CopyMem (TempHttpMessage
, HttpMessage
, HttpMessageSize
);
363 // Set Field and Value to each header
365 Token
= TempHttpMessage
;
366 while (Index
< *FieldCount
) {
369 NextToken
= HttpGetFieldNameAndValue (Token
, &FieldName
, &FieldValue
);
371 if (FieldName
== NULL
|| FieldValue
== NULL
) {
375 Status
= HttpSetFieldNameAndValue (&(*HeaderFields
)[Index
], FieldName
, FieldValue
);
376 if (EFI_ERROR (Status
)) {
378 HttpFreeHeaderFields (*HeaderFields
, Index
);
386 // Free allocated buffer
389 if (TempHttpMessage
!= NULL
) {
390 FreePool (TempHttpMessage
);