TianoCore EDK2 master
Loading...
Searching...
No Matches
HttpUtilitiesProtocol.c
Go to the documentation of this file.
1
10#include "HttpUtilitiesDxe.h"
11
12EFI_HTTP_UTILITIES_PROTOCOL mHttpUtilitiesProtocol = {
15};
16
50EFIAPI
53 IN UINTN SeedMessageSize,
54 IN VOID *SeedMessage OPTIONAL,
55 IN UINTN DeleteCount,
56 IN CHAR8 *DeleteList[] OPTIONAL,
57 IN UINTN AppendCount,
58 IN EFI_HTTP_HEADER *AppendList[] OPTIONAL,
59 OUT UINTN *NewMessageSize,
60 OUT VOID **NewMessage
61 )
62{
63 EFI_STATUS Status;
64 EFI_HTTP_HEADER *SeedHeaderFields;
65 UINTN SeedFieldCount;
66 UINTN Index;
67 EFI_HTTP_HEADER *TempHeaderFields;
68 UINTN TempFieldCount;
69 EFI_HTTP_HEADER *NewHeaderFields;
70 UINTN NewFieldCount;
71 EFI_HTTP_HEADER *HttpHeader;
72 UINTN StrLength;
73 UINT8 *NewMessagePtr;
74
75 SeedHeaderFields = NULL;
76 SeedFieldCount = 0;
77 TempHeaderFields = NULL;
78 TempFieldCount = 0;
79 NewHeaderFields = NULL;
80 NewFieldCount = 0;
81
82 HttpHeader = NULL;
83 StrLength = 0;
84 NewMessagePtr = NULL;
85 *NewMessageSize = 0;
86 Status = EFI_SUCCESS;
87
88 if (This == NULL) {
89 return EFI_INVALID_PARAMETER;
90 }
91
92 if (SeedMessage != NULL) {
93 Status = This->Parse (
94 This,
95 SeedMessage,
96 SeedMessageSize,
97 &SeedHeaderFields,
98 &SeedFieldCount
99 );
100 if (EFI_ERROR (Status)) {
101 goto ON_EXIT;
102 }
103 }
104
105 //
106 // Handle DeleteList
107 //
108 if ((SeedFieldCount != 0) && (DeleteCount != 0)) {
109 TempHeaderFields = AllocateZeroPool (SeedFieldCount * sizeof (EFI_HTTP_HEADER));
110 if (TempHeaderFields == NULL) {
111 Status = EFI_OUT_OF_RESOURCES;
112 goto ON_EXIT;
113 }
114
115 for (Index = 0, TempFieldCount = 0; Index < SeedFieldCount; Index++) {
116 //
117 // Check whether each SeedHeaderFields member is in DeleteList
118 //
119 if (HttpIsValidHttpHeader (DeleteList, DeleteCount, SeedHeaderFields[Index].FieldName)) {
120 Status = HttpSetFieldNameAndValue (
121 &TempHeaderFields[TempFieldCount],
122 SeedHeaderFields[Index].FieldName,
123 SeedHeaderFields[Index].FieldValue
124 );
125 if (EFI_ERROR (Status)) {
126 goto ON_EXIT;
127 }
128
129 TempFieldCount++;
130 }
131 }
132 } else {
133 TempHeaderFields = SeedHeaderFields;
134 TempFieldCount = SeedFieldCount;
135 }
136
137 //
138 // Handle AppendList
139 //
140 NewHeaderFields = AllocateZeroPool ((TempFieldCount + AppendCount) * sizeof (EFI_HTTP_HEADER));
141 if (NewHeaderFields == NULL) {
142 Status = EFI_OUT_OF_RESOURCES;
143 goto ON_EXIT;
144 }
145
146 for (Index = 0; Index < TempFieldCount; Index++) {
147 Status = HttpSetFieldNameAndValue (
148 &NewHeaderFields[Index],
149 TempHeaderFields[Index].FieldName,
150 TempHeaderFields[Index].FieldValue
151 );
152 if (EFI_ERROR (Status)) {
153 goto ON_EXIT;
154 }
155 }
156
157 NewFieldCount = TempFieldCount;
158
159 for (Index = 0; Index < AppendCount; Index++) {
160 HttpHeader = HttpFindHeader (NewFieldCount, NewHeaderFields, AppendList[Index]->FieldName);
161 if (HttpHeader != NULL) {
162 Status = HttpSetFieldNameAndValue (
163 HttpHeader,
164 AppendList[Index]->FieldName,
165 AppendList[Index]->FieldValue
166 );
167 if (EFI_ERROR (Status)) {
168 goto ON_EXIT;
169 }
170 } else {
171 Status = HttpSetFieldNameAndValue (
172 &NewHeaderFields[NewFieldCount],
173 AppendList[Index]->FieldName,
174 AppendList[Index]->FieldValue
175 );
176 if (EFI_ERROR (Status)) {
177 goto ON_EXIT;
178 }
179
180 NewFieldCount++;
181 }
182 }
183
184 //
185 // Calculate NewMessageSize, then build NewMessage
186 //
187 for (Index = 0; Index < NewFieldCount; Index++) {
188 HttpHeader = &NewHeaderFields[Index];
189
190 StrLength = AsciiStrLen (HttpHeader->FieldName);
191 *NewMessageSize += StrLength;
192
193 StrLength = sizeof (": ") - 1;
194 *NewMessageSize += StrLength;
195
196 StrLength = AsciiStrLen (HttpHeader->FieldValue);
197 *NewMessageSize += StrLength;
198
199 StrLength = sizeof ("\r\n") - 1;
200 *NewMessageSize += StrLength;
201 }
202
203 StrLength = sizeof ("\r\n") - 1;
204 *NewMessageSize += StrLength;
205
206 *NewMessage = AllocateZeroPool (*NewMessageSize);
207 if (*NewMessage == NULL) {
208 Status = EFI_OUT_OF_RESOURCES;
209 goto ON_EXIT;
210 }
211
212 NewMessagePtr = (UINT8 *)(*NewMessage);
213
214 for (Index = 0; Index < NewFieldCount; Index++) {
215 HttpHeader = &NewHeaderFields[Index];
216
217 StrLength = AsciiStrLen (HttpHeader->FieldName);
218 CopyMem (NewMessagePtr, HttpHeader->FieldName, StrLength);
219 NewMessagePtr += StrLength;
220
221 StrLength = sizeof (": ") - 1;
222 CopyMem (NewMessagePtr, ": ", StrLength);
223 NewMessagePtr += StrLength;
224
225 StrLength = AsciiStrLen (HttpHeader->FieldValue);
226 CopyMem (NewMessagePtr, HttpHeader->FieldValue, StrLength);
227 NewMessagePtr += StrLength;
228
229 StrLength = sizeof ("\r\n") - 1;
230 CopyMem (NewMessagePtr, "\r\n", StrLength);
231 NewMessagePtr += StrLength;
232 }
233
234 StrLength = sizeof ("\r\n") - 1;
235 CopyMem (NewMessagePtr, "\r\n", StrLength);
236 NewMessagePtr += StrLength;
237
238 ASSERT (*NewMessageSize == (UINTN)NewMessagePtr - (UINTN)(*NewMessage));
239
240 //
241 // Free allocated buffer
242 //
243ON_EXIT:
244 if (SeedHeaderFields != NULL) {
245 HttpFreeHeaderFields (SeedHeaderFields, SeedFieldCount);
246 }
247
248 if (TempHeaderFields != NULL) {
249 HttpFreeHeaderFields (TempHeaderFields, TempFieldCount);
250 }
251
252 if (NewHeaderFields != NULL) {
253 HttpFreeHeaderFields (NewHeaderFields, NewFieldCount);
254 }
255
256 return Status;
257}
258
281EFIAPI
284 IN CHAR8 *HttpMessage,
285 IN UINTN HttpMessageSize,
286 OUT EFI_HTTP_HEADER **HeaderFields,
287 OUT UINTN *FieldCount
288 )
289{
290 EFI_STATUS Status;
291 CHAR8 *TempHttpMessage;
292 CHAR8 *Token;
293 CHAR8 *NextToken;
294 CHAR8 *FieldName;
295 CHAR8 *FieldValue;
296 UINTN Index;
297 UINTN HttpBufferSize;
298
299 Status = EFI_SUCCESS;
300 TempHttpMessage = NULL;
301 Token = NULL;
302 NextToken = NULL;
303 FieldName = NULL;
304 FieldValue = NULL;
305 Index = 0;
306
307 if ((This == NULL) || (HttpMessage == NULL) || (HeaderFields == NULL) || (FieldCount == NULL)) {
308 return EFI_INVALID_PARAMETER;
309 }
310
311 //
312 // Append the http response string along with a Null-terminator.
313 //
314 HttpBufferSize = HttpMessageSize + 1;
315 TempHttpMessage = AllocatePool (HttpBufferSize);
316 if (TempHttpMessage == NULL) {
317 return EFI_OUT_OF_RESOURCES;
318 }
319
320 CopyMem (TempHttpMessage, HttpMessage, HttpMessageSize);
321 *(TempHttpMessage + HttpMessageSize) = '\0';
322
323 //
324 // Get header number
325 //
326 *FieldCount = 0;
327 Token = TempHttpMessage;
328 while (TRUE) {
329 FieldName = NULL;
330 FieldValue = NULL;
331 NextToken = HttpGetFieldNameAndValue (Token, &FieldName, &FieldValue);
332 Token = NextToken;
333 if ((FieldName == NULL) || (FieldValue == NULL)) {
334 break;
335 }
336
337 (*FieldCount)++;
338 }
339
340 if (*FieldCount == 0) {
341 Status = EFI_INVALID_PARAMETER;
342 goto ON_EXIT;
343 }
344
345 //
346 // Allocate buffer for header
347 //
348 *HeaderFields = AllocateZeroPool ((*FieldCount) * sizeof (EFI_HTTP_HEADER));
349 if (*HeaderFields == NULL) {
350 *FieldCount = 0;
351 Status = EFI_OUT_OF_RESOURCES;
352 goto ON_EXIT;
353 }
354
355 CopyMem (TempHttpMessage, HttpMessage, HttpMessageSize);
356
357 //
358 // Set Field and Value to each header
359 //
360 Token = TempHttpMessage;
361 while (Index < *FieldCount) {
362 FieldName = NULL;
363 FieldValue = NULL;
364 NextToken = HttpGetFieldNameAndValue (Token, &FieldName, &FieldValue);
365 Token = NextToken;
366 if ((FieldName == NULL) || (FieldValue == NULL)) {
367 break;
368 }
369
370 Status = HttpSetFieldNameAndValue (&(*HeaderFields)[Index], FieldName, FieldValue);
371 if (EFI_ERROR (Status)) {
372 *FieldCount = 0;
373 HttpFreeHeaderFields (*HeaderFields, Index);
374 goto ON_EXIT;
375 }
376
377 Index++;
378 }
379
380 //
381 // Free allocated buffer
382 //
383ON_EXIT:
384 if (TempHttpMessage != NULL) {
385 FreePool (TempHttpMessage);
386 }
387
388 return Status;
389}
UINT64 UINTN
UINTN EFIAPI AsciiStrLen(IN CONST CHAR8 *String)
Definition: String.c:641
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
EFI_STATUS EFIAPI HttpSetFieldNameAndValue(IN OUT EFI_HTTP_HEADER *HttpHeader, IN CONST CHAR8 *FieldName, IN CONST CHAR8 *FieldValue)
Definition: DxeHttpLib.c:1529
EFI_HTTP_HEADER *EFIAPI HttpFindHeader(IN UINTN HeaderCount, IN EFI_HTTP_HEADER *Headers, IN CHAR8 *FieldName)
Definition: DxeHttpLib.c:855
VOID EFIAPI HttpFreeHeaderFields(IN EFI_HTTP_HEADER *HeaderFields, IN UINTN FieldCount)
Definition: DxeHttpLib.c:1730
CHAR8 *EFIAPI HttpGetFieldNameAndValue(IN CHAR8 *String, OUT CHAR8 **FieldName, OUT CHAR8 **FieldValue)
Definition: DxeHttpLib.c:1585
BOOLEAN EFIAPI HttpIsValidHttpHeader(IN CHAR8 *DeleteList[], IN UINTN DeleteCount, IN CHAR8 *FieldName)
Definition: DxeHttpLib.c:2116
EFI_STATUS EFIAPI HttpUtilitiesParse(IN EFI_HTTP_UTILITIES_PROTOCOL *This, IN CHAR8 *HttpMessage, IN UINTN HttpMessageSize, OUT EFI_HTTP_HEADER **HeaderFields, OUT UINTN *FieldCount)
EFI_STATUS EFIAPI HttpUtilitiesBuild(IN EFI_HTTP_UTILITIES_PROTOCOL *This, IN UINTN SeedMessageSize, IN VOID *SeedMessage OPTIONAL, IN UINTN DeleteCount, IN CHAR8 *DeleteList[] OPTIONAL, IN UINTN AppendCount, IN EFI_HTTP_HEADER *AppendList[] OPTIONAL, OUT UINTN *NewMessageSize, OUT VOID **NewMessage)
#define NULL
Definition: Base.h:319
#define TRUE
Definition: Base.h:301
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
CHAR8 * FieldValue
Definition: Http.h:220
CHAR8 * FieldName
Definition: Http.h:215