]>
git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/HttpUtilitiesDxe/HttpUtilitiesImpl.c
2 The functions for HttpUtilities driver.
4 Copyright (c) 2015, Intel Corporation. All rights reserved.<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"
20 Get the next string, which is distinguished by specified seperator.
22 @param[in] String Pointer to the string.
23 @param[in] Seperator Specified seperator used to distinguish where is the beginning
26 @return Pointer to the next string.
27 @return NULL if not find or String is NULL.
31 AsciiStrGetNextToken (
32 IN CONST CHAR8
*String
,
43 if (*Token
== Seperator
) {
44 return (CHAR8
*)(Token
+ 1);
52 Free existing HeaderFields.
54 @param[in] HeaderFields Pointer to array of key/value header pairs waitting for free.
55 @param[in] FieldCount The number of header pairs in HeaderFields.
60 IN EFI_HTTP_HEADER
*HeaderFields
,
66 if (HeaderFields
!= NULL
) {
67 for (Index
= 0; Index
< FieldCount
; Index
++) {
68 if (HeaderFields
[Index
].FieldName
!= NULL
) {
69 FreePool (HeaderFields
[Index
].FieldName
);
71 if (HeaderFields
[Index
].FieldValue
!= NULL
) {
72 FreePool (HeaderFields
[Index
].FieldValue
);
76 FreePool (HeaderFields
);
82 Find required header field in HeaderFields.
84 @param[in] HeaderFields Pointer to array of key/value header pairs.
85 @param[in] FieldCount The number of header pairs.
86 @param[in] FieldName Pointer to header field's name.
88 @return Pointer to the queried header field.
89 @return NULL if not find this required header field.
94 IN EFI_HTTP_HEADER
*HeaderFields
,
101 for (Index
= 0; Index
< FieldCount
; Index
++) {
102 if (AsciiStrCmp (FieldName
, HeaderFields
[Index
].FieldName
) == 0) {
104 // Find the required header field.
106 return &HeaderFields
[Index
];
114 Check whether header field called FieldName is in DeleteList.
116 @param[in] DeleteList Pointer to array of key/value header pairs.
117 @param[in] DeleteCount The number of header pairs.
118 @param[in] FieldName Pointer to header field's name.
120 @return TRUE if FieldName is not in DeleteList, that means this header field is valid.
121 @return FALSE if FieldName is in DeleteList, that means this header field is invalid.
126 IN CHAR8
*DeleteList
[],
127 IN UINTN DeleteCount
,
133 for (Index
= 0; Index
< DeleteCount
; Index
++) {
134 if (AsciiStrCmp (FieldName
, DeleteList
[Index
]) == 0) {
144 Set FieldName and FieldValue into specified HttpHeader.
146 @param[in] HttpHeader Specified HttpHeader.
147 @param[in] FieldName FieldName of this HttpHeader.
148 @param[in] FieldValue FieldValue of this HttpHeader.
151 @retval EFI_SUCCESS The FieldName and FieldValue are set into HttpHeader successfully.
152 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
156 SetFieldNameAndValue (
157 IN EFI_HTTP_HEADER
*HttpHeader
,
163 UINTN FieldValueSize
;
165 if (HttpHeader
->FieldName
!= NULL
) {
166 FreePool (HttpHeader
->FieldName
);
168 if (HttpHeader
->FieldValue
!= NULL
) {
169 FreePool (HttpHeader
->FieldValue
);
172 FieldNameSize
= AsciiStrSize (FieldName
);
173 HttpHeader
->FieldName
= AllocateZeroPool (FieldNameSize
);
174 if (HttpHeader
->FieldName
== NULL
) {
175 return EFI_OUT_OF_RESOURCES
;
177 CopyMem (HttpHeader
->FieldName
, FieldName
, FieldNameSize
);
178 HttpHeader
->FieldName
[FieldNameSize
- 1] = 0;
180 FieldValueSize
= AsciiStrSize (FieldValue
);
181 HttpHeader
->FieldValue
= AllocateZeroPool (FieldValueSize
);
182 if (HttpHeader
->FieldValue
== NULL
) {
183 return EFI_OUT_OF_RESOURCES
;
185 CopyMem (HttpHeader
->FieldValue
, FieldValue
, FieldValueSize
);
186 HttpHeader
->FieldValue
[FieldValueSize
- 1] = 0;
193 Get one key/value header pair from the raw string.
195 @param[in] String Pointer to the raw string.
196 @param[out] FieldName Pointer to header field's name.
197 @param[out] FieldValue Pointer to header field's value.
199 @return Pointer to the next raw string.
200 @return NULL if no key/value header pair from this raw string.
204 GetFieldNameAndValue (
206 OUT CHAR8
**FieldName
,
207 OUT CHAR8
**FieldValue
211 CHAR8
*FieldValueStr
;
214 if (String
== NULL
|| FieldName
== NULL
|| FieldValue
== NULL
) {
221 FieldValueStr
= NULL
;
225 // Each header field consists of a name followed by a colon (":") and the field value.
227 FieldNameStr
= String
;
228 FieldValueStr
= AsciiStrGetNextToken (FieldNameStr
, ':');
229 if (FieldValueStr
== NULL
) {
234 // Replace ':' with 0
236 *(FieldValueStr
- 1) = 0;
239 // The field value MAY be preceded by any amount of LWS, though a single SP is preferred.
242 if (*FieldValueStr
== ' ' || *FieldValueStr
== '\t') {
244 } else if (*FieldValueStr
== '\r' && *(FieldValueStr
+ 1) == '\n' &&
245 (*(FieldValueStr
+ 2) == ' ' || *(FieldValueStr
+ 2) == '\t')) {
246 FieldValueStr
= FieldValueStr
+ 3;
253 // Header fields can be extended over multiple lines by preceding each extra
254 // line with at least one SP or HT.
256 StrPtr
= FieldValueStr
;
258 StrPtr
= AsciiStrGetNextToken (StrPtr
, '\r');
259 if (StrPtr
== NULL
|| *StrPtr
!= '\n') {
264 } while (*StrPtr
== ' ' || *StrPtr
== '\t');
267 // Replace '\r' with 0
272 // Get FieldName and FieldValue.
274 *FieldName
= FieldNameStr
;
275 *FieldValue
= FieldValueStr
;