/** This function returns the image type according to server replied HTTP message and also the image's URI info. @param[in] Uri The pointer to the image's URI string. @param[in] UriParser URI Parse result returned by NetHttpParseUrl(). @param[in] HeaderCount Number of HTTP header structures in Headers list. @param[in] Headers Array containing list of HTTP headers. @param[out] ImageType The image type of the downloaded file. @retval EFI_SUCCESS The image type is returned in ImageType. @retval EFI_INVALID_PARAMETER ImageType, Uri or UriParser is NULL. @retval EFI_INVALID_PARAMETER HeaderCount is not zero, and Headers is NULL. @retval EFI_NOT_FOUND Failed to identify the image type. @retval Others Unexpect error happened. **/ EFI_STATUS HttpBootCheckImageType ( IN CHAR8 *Uri, IN VOID *UriParser, IN UINTN HeaderCount, IN EFI_HTTP_HEADER *Headers, OUT HTTP_BOOT_IMAGE_TYPE *ImageType ) { EFI_STATUS Status; EFI_HTTP_HEADER *Header; CHAR8 *FilePath; CHAR8 *FilePost; if (Uri == NULL || UriParser == NULL || ImageType == NULL) { return EFI_INVALID_PARAMETER; } if (HeaderCount != 0 && Headers == NULL) { return EFI_INVALID_PARAMETER; } // // Determine the image type by the HTTP Content-Type header field first. // "application/efi" -> EFI Image // "application/vnd.efi-iso" -> CD/DVD Image // "application/vnd.efi-img" -> Virtual Disk Image // Header = HttpFindHeader (HeaderCount, Headers, HTTP_HEADER_CONTENT_TYPE); if (Header != NULL) { if (AsciiStriCmp (Header->FieldValue, HTTP_CONTENT_TYPE_APP_EFI) == 0) { *ImageType = ImageTypeEfi; return EFI_SUCCESS; } else if (AsciiStriCmp (Header->FieldValue, HTTP_CONTENT_TYPE_APP_ISO) == 0) { *ImageType = ImageTypeVirtualCd; return EFI_SUCCESS; } else if (AsciiStriCmp (Header->FieldValue, HTTP_CONTENT_TYPE_APP_IMG) == 0) { *ImageType = ImageTypeVirtualDisk; return EFI_SUCCESS; } } // // Determine the image type by file extension: // *.efi -> EFI Image // *.iso -> CD/DVD Image // *.img -> Virtual Disk Image // Status = HttpUrlGetPath ( Uri, UriParser, &FilePath ); if (EFI_ERROR (Status)) { return Status; } FilePost = FilePath + AsciiStrLen (FilePath) - 4; if (AsciiStrCmp (FilePost, ".efi") == 0) { *ImageType = ImageTypeEfi; } else if (AsciiStrCmp (FilePost, ".iso") == 0) { *ImageType = ImageTypeVirtualCd; } else if (AsciiStrCmp (FilePost, ".img") == 0) { *ImageType = ImageTypeVirtualDisk; } else { *ImageType = ImageTypeMax; } FreePool (FilePath); return (*ImageType < ImageTypeMax) ? EFI_SUCCESS : EFI_NOT_FOUND; }
/** Set or update a HTTP header with the field name and corresponding value. @param[in] HttpIoHeader Point to the HTTP header holder. @param[in] FieldName Null terminated string which describes a field name. @param[in] FieldValue Null terminated string which describes the corresponding field value. @retval EFI_SUCCESS The HTTP header has been set or updated. @retval EFI_INVALID_PARAMETER Any input parameter is invalid. @retval EFI_OUT_OF_RESOURCES Insufficient resource to complete the operation. @retval Other Unexpected error happened. **/ EFI_STATUS HttpBootSetHeader ( IN HTTP_IO_HEADER *HttpIoHeader, IN CHAR8 *FieldName, IN CHAR8 *FieldValue ) { EFI_HTTP_HEADER *Header; UINTN StrSize; CHAR8 *NewFieldValue; if (HttpIoHeader == NULL || FieldName == NULL || FieldValue == NULL) { return EFI_INVALID_PARAMETER; } Header = HttpFindHeader (HttpIoHeader->HeaderCount, HttpIoHeader->Headers, FieldName); if (Header == NULL) { // // Add a new header. // if (HttpIoHeader->HeaderCount >= HttpIoHeader->MaxHeaderCount) { return EFI_OUT_OF_RESOURCES; } Header = &HttpIoHeader->Headers[HttpIoHeader->HeaderCount]; StrSize = AsciiStrSize (FieldName); Header->FieldName = AllocatePool (StrSize); if (Header->FieldName == NULL) { return EFI_OUT_OF_RESOURCES; } CopyMem (Header->FieldName, FieldName, StrSize); Header->FieldName[StrSize -1] = '\0'; StrSize = AsciiStrSize (FieldValue); Header->FieldValue = AllocatePool (StrSize); if (Header->FieldValue == NULL) { FreePool (Header->FieldName); return EFI_OUT_OF_RESOURCES; } CopyMem (Header->FieldValue, FieldValue, StrSize); Header->FieldValue[StrSize -1] = '\0'; HttpIoHeader->HeaderCount++; } else { // // Update an existing one. // StrSize = AsciiStrSize (FieldValue); NewFieldValue = AllocatePool (StrSize); if (NewFieldValue == NULL) { return EFI_OUT_OF_RESOURCES; } CopyMem (NewFieldValue, FieldValue, StrSize); NewFieldValue[StrSize -1] = '\0'; if (Header->FieldValue != NULL) { FreePool (Header->FieldValue); } Header->FieldValue = NewFieldValue; } return EFI_SUCCESS; }
/** Create HTTP header based on a combination of seed header, fields to delete, and fields to append. The Build() function is used to manage the headers portion of an HTTP message by providing the ability to add, remove, or replace HTTP headers. @param[in] This Pointer to EFI_HTTP_UTILITIES_PROTOCOL instance. @param[in] SeedMessageSize Size of the initial HTTP header. This can be zero. @param[in] SeedMessage Initial HTTP header to be used as a base for building a new HTTP header. If NULL, SeedMessageSize is ignored. @param[in] DeleteCount Number of null-terminated HTTP header field names in DeleteList. @param[in] DeleteList List of null-terminated HTTP header field names to remove from SeedMessage. Only the field names are in this list because the field values are irrelevant to this operation. @param[in] AppendCount Number of header fields in AppendList. @param[in] AppendList List of HTTP headers to populate NewMessage with. If SeedMessage is not NULL, AppendList will be appended to the existing list from SeedMessage in NewMessage. @param[out] NewMessageSize Pointer to number of header fields in NewMessage. @param[out] NewMessage Pointer to a new list of HTTP headers based on. @retval EFI_SUCCESS Add, remove, and replace operations succeeded. @retval EFI_OUT_OF_RESOURCES Could not allocate memory for NewMessage. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: This is NULL. **/ 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 ) { EFI_STATUS Status; EFI_HTTP_HEADER *SeedHeaderFields; UINTN SeedFieldCount; UINTN Index; EFI_HTTP_HEADER *TempHeaderFields; UINTN TempFieldCount; EFI_HTTP_HEADER *NewHeaderFields; UINTN NewFieldCount; EFI_HTTP_HEADER *HttpHeader; UINTN StrLength; UINT8 *NewMessagePtr; SeedHeaderFields = NULL; SeedFieldCount = 0; TempHeaderFields = NULL; TempFieldCount = 0; NewHeaderFields = NULL; NewFieldCount = 0; HttpHeader = NULL; StrLength = 0; NewMessagePtr = NULL; *NewMessageSize = 0; Status = EFI_SUCCESS; if (This == NULL) { return EFI_INVALID_PARAMETER; } if (SeedMessage != NULL) { Status = This->Parse ( This, SeedMessage, SeedMessageSize, &SeedHeaderFields, &SeedFieldCount ); if (EFI_ERROR (Status)) { goto ON_EXIT; } } // // Handle DeleteList // if (SeedFieldCount != 0 && DeleteCount != 0) { TempHeaderFields = AllocateZeroPool (SeedFieldCount * sizeof(EFI_HTTP_HEADER)); if (TempHeaderFields == NULL) { Status = EFI_OUT_OF_RESOURCES; goto ON_EXIT; } for (Index = 0, TempFieldCount = 0; Index < SeedFieldCount; Index++) { // // Check whether each SeedHeaderFields member is in DeleteList // if (HttpIsValidHttpHeader( DeleteList, DeleteCount, SeedHeaderFields[Index].FieldName)) { Status = HttpSetFieldNameAndValue ( &TempHeaderFields[TempFieldCount], SeedHeaderFields[Index].FieldName, SeedHeaderFields[Index].FieldValue ); if (EFI_ERROR (Status)) { goto ON_EXIT; } TempFieldCount++; } } } else { TempHeaderFields = SeedHeaderFields; TempFieldCount = SeedFieldCount; } // // Handle AppendList // NewHeaderFields = AllocateZeroPool ((TempFieldCount + AppendCount) * sizeof (EFI_HTTP_HEADER)); if (NewHeaderFields == NULL) { Status = EFI_OUT_OF_RESOURCES; goto ON_EXIT; } for (Index = 0; Index < TempFieldCount; Index++) { Status = HttpSetFieldNameAndValue ( &NewHeaderFields[Index], TempHeaderFields[Index].FieldName, TempHeaderFields[Index].FieldValue ); if (EFI_ERROR (Status)) { goto ON_EXIT; } } NewFieldCount = TempFieldCount; for (Index = 0; Index < AppendCount; Index++) { HttpHeader = HttpFindHeader (NewFieldCount, NewHeaderFields, AppendList[Index]->FieldName); if (HttpHeader != NULL) { Status = HttpSetFieldNameAndValue ( HttpHeader, AppendList[Index]->FieldName, AppendList[Index]->FieldValue ); if (EFI_ERROR (Status)) { goto ON_EXIT; } } else { Status = HttpSetFieldNameAndValue ( &NewHeaderFields[NewFieldCount], AppendList[Index]->FieldName, AppendList[Index]->FieldValue ); if (EFI_ERROR (Status)) { goto ON_EXIT; } NewFieldCount++; } } // // Calculate NewMessageSize, then build NewMessage // for (Index = 0; Index < NewFieldCount; Index++) { HttpHeader = &NewHeaderFields[Index]; StrLength = AsciiStrLen (HttpHeader->FieldName); *NewMessageSize += StrLength; StrLength = sizeof(": ") - 1; *NewMessageSize += StrLength; StrLength = AsciiStrLen (HttpHeader->FieldValue); *NewMessageSize += StrLength; StrLength = sizeof("\r\n") - 1; *NewMessageSize += StrLength; } StrLength = sizeof("\r\n") - 1; *NewMessageSize += StrLength; // // Final 0 for end flag // *NewMessageSize += 1; *NewMessage = AllocateZeroPool (*NewMessageSize); if (*NewMessage == NULL) { Status = EFI_OUT_OF_RESOURCES; goto ON_EXIT; } NewMessagePtr = (UINT8 *)(*NewMessage); for (Index = 0; Index < NewFieldCount; Index++) { HttpHeader = &NewHeaderFields[Index]; StrLength = AsciiStrLen (HttpHeader->FieldName); CopyMem (NewMessagePtr, HttpHeader->FieldName, StrLength); NewMessagePtr += StrLength; StrLength = sizeof(": ") - 1; CopyMem (NewMessagePtr, ": ", StrLength); NewMessagePtr += StrLength; StrLength = AsciiStrLen (HttpHeader->FieldValue); CopyMem (NewMessagePtr, HttpHeader->FieldValue, StrLength); NewMessagePtr += StrLength; StrLength = sizeof("\r\n") - 1; CopyMem (NewMessagePtr, "\r\n", StrLength); NewMessagePtr += StrLength; } StrLength = sizeof("\r\n") - 1; CopyMem (NewMessagePtr, "\r\n", StrLength); NewMessagePtr += StrLength; *NewMessagePtr = 0; ASSERT (*NewMessageSize == (UINTN)NewMessagePtr - (UINTN)(*NewMessage) + 1); // // Free allocated buffer // ON_EXIT: if (SeedHeaderFields != NULL) { HttpFreeHeaderFields(SeedHeaderFields, SeedFieldCount); } if (TempHeaderFields != NULL) { HttpFreeHeaderFields(TempHeaderFields, TempFieldCount); } if (NewHeaderFields != NULL) { HttpFreeHeaderFields(NewHeaderFields, NewFieldCount); } return Status; }