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
;
210 *NewMessage
= AllocateZeroPool (*NewMessageSize
);
211 if (*NewMessage
== NULL
) {
212 Status
= EFI_OUT_OF_RESOURCES
;
216 NewMessagePtr
= (UINT8
*)(*NewMessage
);
218 for (Index
= 0; Index
< NewFieldCount
; Index
++) {
219 HttpHeader
= &NewHeaderFields
[Index
];
221 StrLength
= AsciiStrLen (HttpHeader
->FieldName
);
222 CopyMem (NewMessagePtr
, HttpHeader
->FieldName
, StrLength
);
223 NewMessagePtr
+= StrLength
;
225 StrLength
= sizeof(": ") - 1;
226 CopyMem (NewMessagePtr
, ": ", StrLength
);
227 NewMessagePtr
+= StrLength
;
229 StrLength
= AsciiStrLen (HttpHeader
->FieldValue
);
230 CopyMem (NewMessagePtr
, HttpHeader
->FieldValue
, StrLength
);
231 NewMessagePtr
+= StrLength
;
233 StrLength
= sizeof("\r\n") - 1;
234 CopyMem (NewMessagePtr
, "\r\n", StrLength
);
235 NewMessagePtr
+= StrLength
;
237 StrLength
= sizeof("\r\n") - 1;
238 CopyMem (NewMessagePtr
, "\r\n", StrLength
);
239 NewMessagePtr
+= StrLength
;
241 ASSERT (*NewMessageSize
== (UINTN
)NewMessagePtr
- (UINTN
)(*NewMessage
));
244 // Free allocated buffer
247 if (SeedHeaderFields
!= NULL
) {
248 HttpFreeHeaderFields(SeedHeaderFields
, SeedFieldCount
);
251 if (TempHeaderFields
!= NULL
) {
252 HttpFreeHeaderFields(TempHeaderFields
, TempFieldCount
);
255 if (NewHeaderFields
!= NULL
) {
256 HttpFreeHeaderFields(NewHeaderFields
, NewFieldCount
);
264 Parses HTTP header and produces an array of key/value pairs.
266 The Parse() function is used to transform data stored in HttpHeader
267 into a list of fields paired with their corresponding values.
269 @param[in] This Pointer to EFI_HTTP_UTILITIES_PROTOCOL instance.
270 @param[in] HttpMessage Contains raw unformatted HTTP header string.
271 @param[in] HttpMessageSize Size of HTTP header.
272 @param[out] HeaderFields Array of key/value header pairs.
273 @param[out] FieldCount Number of headers in HeaderFields.
275 @retval EFI_SUCCESS Allocation succeeded.
276 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
278 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
281 HeaderFields is NULL.
287 IN EFI_HTTP_UTILITIES_PROTOCOL
*This
,
288 IN CHAR8
*HttpMessage
,
289 IN UINTN HttpMessageSize
,
290 OUT EFI_HTTP_HEADER
**HeaderFields
,
291 OUT UINTN
*FieldCount
295 CHAR8
*TempHttpMessage
;
302 Status
= EFI_SUCCESS
;
303 TempHttpMessage
= NULL
;
310 if (This
== NULL
|| HttpMessage
== NULL
|| HeaderFields
== NULL
|| FieldCount
== NULL
) {
311 return EFI_INVALID_PARAMETER
;
314 TempHttpMessage
= AllocateZeroPool (HttpMessageSize
);
315 if (TempHttpMessage
== NULL
) {
316 return EFI_OUT_OF_RESOURCES
;
319 CopyMem (TempHttpMessage
, HttpMessage
, HttpMessageSize
);
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
);