2 Implementation of EFI_HTTP_PROTOCOL protocol interfaces.
4 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "HttpUtilitiesDxe.h"
17 EFI_HTTP_UTILITIES_PROTOCOL mHttpUtilitiesProtocol
= {
24 Create HTTP header based on a combination of seed header, fields
25 to delete, and fields to append.
27 The Build() function is used to manage the headers portion of an
28 HTTP message by providing the ability to add, remove, or replace
31 @param[in] This Pointer to EFI_HTTP_UTILITIES_PROTOCOL instance.
32 @param[in] SeedMessageSize Size of the initial HTTP header. This can be zero.
33 @param[in] SeedMessage Initial HTTP header to be used as a base for
34 building a new HTTP header. If NULL,
35 SeedMessageSize is ignored.
36 @param[in] DeleteCount Number of null-terminated HTTP header field names
38 @param[in] DeleteList List of null-terminated HTTP header field names to
39 remove from SeedMessage. Only the field names are
40 in this list because the field values are irrelevant
42 @param[in] AppendCount Number of header fields in AppendList.
43 @param[in] AppendList List of HTTP headers to populate NewMessage with.
44 If SeedMessage is not NULL, AppendList will be
45 appended to the existing list from SeedMessage in
47 @param[out] NewMessageSize Pointer to number of header fields in NewMessage.
48 @param[out] NewMessage Pointer to a new list of HTTP headers based on.
50 @retval EFI_SUCCESS Add, remove, and replace operations succeeded.
51 @retval EFI_OUT_OF_RESOURCES Could not allocate memory for NewMessage.
52 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
58 IN EFI_HTTP_UTILITIES_PROTOCOL
*This
,
59 IN UINTN SeedMessageSize
,
60 IN VOID
*SeedMessage
, OPTIONAL
62 IN CHAR8
*DeleteList
[], OPTIONAL
64 IN EFI_HTTP_HEADER
*AppendList
[], OPTIONAL
65 OUT UINTN
*NewMessageSize
,
70 EFI_HTTP_HEADER
*SeedHeaderFields
;
73 EFI_HTTP_HEADER
*TempHeaderFields
;
75 EFI_HTTP_HEADER
*NewHeaderFields
;
77 EFI_HTTP_HEADER
*HttpHeader
;
81 SeedHeaderFields
= NULL
;
83 TempHeaderFields
= NULL
;
85 NewHeaderFields
= NULL
;
95 return EFI_INVALID_PARAMETER
;
98 if (SeedMessage
!= NULL
) {
99 Status
= This
->Parse (
106 if (EFI_ERROR (Status
)) {
114 if (SeedFieldCount
!= 0 && DeleteCount
!= 0) {
115 TempHeaderFields
= AllocateZeroPool (SeedFieldCount
* sizeof(EFI_HTTP_HEADER
));
116 if (TempHeaderFields
== NULL
) {
117 Status
= EFI_OUT_OF_RESOURCES
;
121 for (Index
= 0, TempFieldCount
= 0; Index
< SeedFieldCount
; Index
++) {
123 // Check whether each SeedHeaderFields member is in DeleteList
125 if (IsValidHttpHeader( DeleteList
, DeleteCount
, SeedHeaderFields
[Index
].FieldName
)) {
126 Status
= SetFieldNameAndValue (
127 &TempHeaderFields
[TempFieldCount
],
128 SeedHeaderFields
[Index
].FieldName
,
129 SeedHeaderFields
[Index
].FieldValue
131 if (EFI_ERROR (Status
)) {
138 TempHeaderFields
= SeedHeaderFields
;
139 TempFieldCount
= SeedFieldCount
;
145 NewHeaderFields
= AllocateZeroPool ((TempFieldCount
+ AppendCount
) * sizeof (EFI_HTTP_HEADER
));
146 if (NewHeaderFields
== NULL
) {
147 Status
= EFI_OUT_OF_RESOURCES
;
151 for (Index
= 0; Index
< TempFieldCount
; Index
++) {
152 Status
= SetFieldNameAndValue (
153 &NewHeaderFields
[Index
],
154 TempHeaderFields
[Index
].FieldName
,
155 TempHeaderFields
[Index
].FieldValue
157 if (EFI_ERROR (Status
)) {
162 NewFieldCount
= TempFieldCount
;
164 for (Index
= 0; Index
< AppendCount
; Index
++) {
165 HttpHeader
= FindHttpHeader (NewHeaderFields
, NewFieldCount
, AppendList
[Index
]->FieldName
);
166 if (HttpHeader
!= NULL
) {
167 Status
= SetFieldNameAndValue (
169 AppendList
[Index
]->FieldName
,
170 AppendList
[Index
]->FieldValue
172 if (EFI_ERROR (Status
)) {
176 Status
= SetFieldNameAndValue (
177 &NewHeaderFields
[NewFieldCount
],
178 AppendList
[Index
]->FieldName
,
179 AppendList
[Index
]->FieldValue
181 if (EFI_ERROR (Status
)) {
189 // Calculate NewMessageSize, then build NewMessage
191 for (Index
= 0; Index
< NewFieldCount
; Index
++) {
192 HttpHeader
= &NewHeaderFields
[Index
];
194 StrLength
= AsciiStrLen (HttpHeader
->FieldName
);
195 *NewMessageSize
+= StrLength
;
197 StrLength
= sizeof(": ") - 1;
198 *NewMessageSize
+= StrLength
;
200 StrLength
= AsciiStrLen (HttpHeader
->FieldValue
);
201 *NewMessageSize
+= StrLength
;
203 StrLength
= sizeof("\r\n") - 1;
204 *NewMessageSize
+= StrLength
;
206 StrLength
= sizeof("\r\n") - 1;
207 *NewMessageSize
+= StrLength
;
210 // Final 0 for end flag
212 *NewMessageSize
+= 1;
214 *NewMessage
= AllocateZeroPool (*NewMessageSize
);
215 if (*NewMessage
== NULL
) {
216 Status
= EFI_OUT_OF_RESOURCES
;
220 NewMessagePtr
= (UINT8
*)(*NewMessage
);
222 for (Index
= 0; Index
< NewFieldCount
; Index
++) {
223 HttpHeader
= &NewHeaderFields
[Index
];
225 StrLength
= AsciiStrLen (HttpHeader
->FieldName
);
226 CopyMem (NewMessagePtr
, HttpHeader
->FieldName
, StrLength
);
227 NewMessagePtr
+= StrLength
;
229 StrLength
= sizeof(": ") - 1;
230 CopyMem (NewMessagePtr
, ": ", StrLength
);
231 NewMessagePtr
+= StrLength
;
233 StrLength
= AsciiStrLen (HttpHeader
->FieldValue
);
234 CopyMem (NewMessagePtr
, HttpHeader
->FieldValue
, StrLength
);
235 NewMessagePtr
+= StrLength
;
237 StrLength
= sizeof("\r\n") - 1;
238 CopyMem (NewMessagePtr
, "\r\n", StrLength
);
239 NewMessagePtr
+= StrLength
;
241 StrLength
= sizeof("\r\n") - 1;
242 CopyMem (NewMessagePtr
, "\r\n", StrLength
);
243 NewMessagePtr
+= StrLength
;
247 ASSERT (*NewMessageSize
== (UINTN
)NewMessagePtr
- (UINTN
)(*NewMessage
) + 1);
250 // Free allocated buffer
253 if (SeedHeaderFields
!= NULL
) {
254 FreeHeaderFields(SeedHeaderFields
, SeedFieldCount
);
257 if (TempHeaderFields
!= NULL
) {
258 FreeHeaderFields(TempHeaderFields
, TempFieldCount
);
261 if (NewHeaderFields
!= NULL
) {
262 FreeHeaderFields(NewHeaderFields
, NewFieldCount
);
270 Parses HTTP header and produces an array of key/value pairs.
272 The Parse() function is used to transform data stored in HttpHeader
273 into a list of fields paired with their corresponding values.
275 @param[in] This Pointer to EFI_HTTP_UTILITIES_PROTOCOL instance.
276 @param[in] HttpMessage Contains raw unformatted HTTP header string.
277 @param[in] HttpMessageSize Size of HTTP header.
278 @param[out] HeaderFields Array of key/value header pairs.
279 @param[out] FieldCount Number of headers in HeaderFields.
281 @retval EFI_SUCCESS Allocation succeeded.
282 @retval EFI_NOT_STARTED This EFI HTTP Protocol instance has not been
284 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
287 HeaderFields is NULL.
293 IN EFI_HTTP_UTILITIES_PROTOCOL
*This
,
294 IN CHAR8
*HttpMessage
,
295 IN UINTN HttpMessageSize
,
296 OUT EFI_HTTP_HEADER
**HeaderFields
,
297 OUT UINTN
*FieldCount
301 CHAR8
*TempHttpMessage
;
308 Status
= EFI_SUCCESS
;
309 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
);
331 Token
= TempHttpMessage
;
335 NextToken
= GetFieldNameAndValue (Token
, &FieldName
, &FieldValue
);
337 if (FieldName
== NULL
|| FieldValue
== NULL
) {
344 if (*FieldCount
== 0) {
345 Status
= EFI_INVALID_PARAMETER
;
350 // Allocate buffer for header
352 *HeaderFields
= AllocateZeroPool ((*FieldCount
) * sizeof(EFI_HTTP_HEADER
));
353 if (*HeaderFields
== NULL
) {
355 Status
= EFI_OUT_OF_RESOURCES
;
359 CopyMem (TempHttpMessage
, HttpMessage
, HttpMessageSize
);
362 // Set Field and Value to each header
364 Token
= TempHttpMessage
;
365 while (Index
< *FieldCount
) {
368 NextToken
= GetFieldNameAndValue (Token
, &FieldName
, &FieldValue
);
370 if (FieldName
== NULL
|| FieldValue
== NULL
) {
374 Status
= SetFieldNameAndValue (&(*HeaderFields
)[Index
], FieldName
, FieldValue
);
375 if (EFI_ERROR (Status
)) {
377 FreeHeaderFields (*HeaderFields
, Index
);
385 // Free allocated buffer
388 if (TempHttpMessage
!= NULL
) {
389 FreePool (TempHttpMessage
);