From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 24C667803DE for ; Tue, 24 Oct 2023 08:39:27 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=6Lv5IyYOGwHW5JhTfYOqLdFvEXJfBKx4P2HiYfKOIgk=; c=relaxed/simple; d=groups.io; h=ARC-Seal:ARC-Message-Signature:ARC-Authentication-Results:Received-SPF:From:To:CC:Subject:Date:Message-ID:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding:Content-Type; s=20140610; t=1698136766; v=1; b=meoshwFBn8okYIaXPLjPtVsKG8rkSfx3l8A3oB/iD0p9ViGhrrePZmrGR9VCdnfsluA7zxUJ HhI9G3NHfHhLPe3W2KqCamWCK1cKyTpRIGT+/kHL9zncFQBf2cqCPBXtjptSELwUVRMlboh6IMi iXfT23j5IO9sJFGDbcFgTXNI= X-Received: by 127.0.0.2 with SMTP id fNBkYY7687511xIR8xIcZ1NG; Tue, 24 Oct 2023 01:39:26 -0700 X-Received: from NAM10-BN7-obe.outbound.protection.outlook.com (NAM10-BN7-obe.outbound.protection.outlook.com [40.107.92.61]) by mx.groups.io with SMTP id smtpd.web10.143292.1698136765925160908 for ; Tue, 24 Oct 2023 01:39:26 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BAV1ifGTzPN8ePVyclinylEFIV0JMHoSVtWczM8tS2ao5vXD9TaTC+f/xRJ5EotXUMitGSpvpjE4D5QpkknWlaRlsXYjyYu8N9UvSi2R1t3ukJWFfGt0z0HH5B3kDOtui5tgisJyQOBszchJz2YUHCp9OEs4giBQ6q2NYTIvgcKFeKOjuns11EJ78vkotBj85g8MGnc8NE6QogrB0uqu/ICqUoM5l4fJclYURWw1ZwRPz2siBYTPf/E1gxbeDMfJHFUsaLOFZYkWO55SrDsT0olWV7wfKFCFERoJbDx8EE/MHxFTPbgAoIhN9elvDrSr15ZXHIo1ufsqeDppdWoR5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=oOVLovmyEwIkfzkAsdxoyL1lbO2WwCZacqQmQPy5EJs=; b=hutSx8Vgk7waqNC5M68B/3Tyjc2VJn7FEoIKekj43oBesKaztsM9dJfKvXQ6UA9dg309GNh2VaDBwY9LxE1TMGOIooiSJEdcRlc/Zh6DA935rQJnmxY14GTpia8Al4dbDnbl4KWxrphFCP3d7X8rzG92vzPQqdR/ANvYkSkWD6bVHvzDgeqJ7kK1sFTXKEBT0Y0DoCqH3oBXRJLZhNwV9xMOBaCwkuuqsNembRCrXMeyQiVSqL7CLMlxf0HAzSdwt71zUeH6znnnLV/Pa0JiiTTbdJeBNiidxepN5RqEZnvCkU8f5YgIj3cUvqwzXjgP7LQCaySsJj5CtkKVFbXhIw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=edk2.groups.io smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none X-Received: from CH5PR03CA0012.namprd03.prod.outlook.com (2603:10b6:610:1f1::9) by DS0PR12MB8317.namprd12.prod.outlook.com (2603:10b6:8:f4::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6907.26; Tue, 24 Oct 2023 08:39:23 +0000 X-Received: from MWH0EPF000989E5.namprd02.prod.outlook.com (2603:10b6:610:1f1:cafe::cb) by CH5PR03CA0012.outlook.office365.com (2603:10b6:610:1f1::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6907.34 via Frontend Transport; Tue, 24 Oct 2023 08:39:22 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C X-Received: from mail.nvidia.com (216.228.117.160) by MWH0EPF000989E5.mail.protection.outlook.com (10.167.241.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6933.15 via Frontend Transport; Tue, 24 Oct 2023 08:39:22 +0000 X-Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Tue, 24 Oct 2023 01:39:09 -0700 X-Received: from rnnvmail202.nvidia.com (10.129.68.7) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Tue, 24 Oct 2023 01:39:09 -0700 X-Received: from NV-CL38DL3.nvidia.com (10.127.8.12) by mail.nvidia.com (10.129.68.7) with Microsoft SMTP Server id 15.2.986.41 via Frontend Transport; Tue, 24 Oct 2023 01:39:08 -0700 From: "Nickle Wang via groups.io" To: CC: Abner Chang , Igor Kulchytskyy , "Nick Ramirez" Subject: [edk2-devel] [PATCH 2/3] RedfishPkg/libredfish: introduce new interfaces. Date: Tue, 24 Oct 2023 16:39:06 +0800 Message-ID: <20231024083906.16121-1-nicklew@nvidia.com> MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWH0EPF000989E5:EE_|DS0PR12MB8317:EE_ X-MS-Office365-Filtering-Correlation-Id: edf307b3-b7f7-4923-97a3-08dbd46cb8cd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Message-Info: IjCSu99CSCR6RyV/2DESZRjJbg7ShJkHz8yr85H6SFe/sezPiZtYPQch7+bSb7kNsmD+6RbmP7GMUJ49skMGggBslC3slhTrrVYlTtVxGUNh5tXGz0lzXNH2nNrikUxUWXwQuNNiGFwovzOHELKXDBAP0u8B/5TjEvFAUCGvb8wrs1zdPPurxSDKrTDgZlu6M1+R9MjO0IT0csMO/dEGQe+qH1M7cHXEwYUlhVo4qahxAb02fSjPxotdQIe4Spn7VuYHdzU9hB6spaQidwgbOgVvpAQXVp31g3VX+A7lf1RI1bccCdaRxTuGANtzERkMu9luoPy2jdo7jBAXULiCGziV22bYOca0KaYEfs2nSMHR+Ybp8S9evEUejCOnswFWVSLK49m1DPJhkF790M9qQyz6fXCgFIdmK5xIaUmut+kNe0nKA246RkBqM0yx2cwVVvcREBIrV9QTDCbdDonlDS9AvuycO45ZVzPDQIrdIwwloDKZZ6Q9q9YvMEyxy9ht+7yA1sasDxZVl8MMBYxs44XOpDAWzMSIOFw7VHSBahAJLy5OtxR3i2va/+WLFP71M3slchCs4maKgLeSOPsCiBvBf77l9OHZRE+DmbbKrOUYuqalBU9X4mYs9R9GZLfbNvygYANV/R7721NYa9wx8IxrsVT4STPbj6upgqwjoRcho5njpK8WXm6jq7GtCbk5KPgk6yQ7ueAbjiYT/fQRCWyR1KjeRPoME5jFv43GBbqG/RS4b4pb9qBkdAuNoyFDhCgGaHULDsbYc61OOUz4UVA1pOiSTbnkud3h8pEUc2o= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Oct 2023 08:39:22.3845 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: edf307b3-b7f7-4923-97a3-08dbd46cb8cd X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: MWH0EPF000989E5.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8317 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,nicklew@nvidia.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: wy21QkyW9BGtqw5pakWlbDQKx7686176AA= Content-Transfer-Encoding: quoted-printable Content-Type: text/plain X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=meoshwFB; arc=reject ("signature check failed: fail, {[1] = sig:microsoft.com:reject}"); dmarc=none; spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io - Add new interfaces to return HTTP headers back to caller. New interfaces are: getUriFromServiceEx(), patchUriFromServiceEx(), postUriFromServiceEx() and putUriFromServiceEx(). - Fix compile error in payload.c Signed-off-by: Nickle Wang Cc: Abner Chang Cc: Igor Kulchytskyy Cc: Nick Ramirez --- .../edk2libredfish/include/redfishPayload.h | 3 +- .../edk2libredfish/include/redfishService.h | 44 +++ .../RedfishLib/edk2libredfish/src/payload.c | 3 +- .../RedfishLib/edk2libredfish/src/service.c | 369 ++++++++++++++++-- 4 files changed, 394 insertions(+), 25 deletions(-) diff --git a/RedfishPkg/PrivateLibrary/RedfishLib/edk2libredfish/include/re= dfishPayload.h b/RedfishPkg/PrivateLibrary/RedfishLib/edk2libredfish/includ= e/redfishPayload.h index 445153060a2a..25bd0b548192 100644 --- a/RedfishPkg/PrivateLibrary/RedfishLib/edk2libredfish/include/redfishPa= yload.h +++ b/RedfishPkg/PrivateLibrary/RedfishLib/edk2libredfish/include/redfishPa= yload.h @@ -10,6 +10,7 @@ =20 Copyright (c) 2019, Intel Corporation. All rights reserved.
(C) Copyright 2021 Hewlett Packard Enterprise Development LP
+ Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved= . =20 SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -19,7 +20,7 @@ #define LIBREDFISH_REDFISH_PAYLOAD_H_ =20 #include - +#include #include #include #include diff --git a/RedfishPkg/PrivateLibrary/RedfishLib/edk2libredfish/include/re= dfishService.h b/RedfishPkg/PrivateLibrary/RedfishLib/edk2libredfish/includ= e/redfishService.h index 75afadc0cece..30f839ea2e8e 100644 --- a/RedfishPkg/PrivateLibrary/RedfishLib/edk2libredfish/include/redfishSe= rvice.h +++ b/RedfishPkg/PrivateLibrary/RedfishLib/edk2libredfish/include/redfishSe= rvice.h @@ -10,6 +10,7 @@ =20 Copyright (c) 2019, Intel Corporation. All rights reserved.
(C) Copyright 2021 Hewlett Packard Enterprise Development LP
+ Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved= . =20 SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -104,6 +105,15 @@ getUriFromService ( EFI_HTTP_STATUS_CODE **StatusCode ); =20 +json_t * +getUriFromServiceEx ( + redfishService *service, + const char *uri, + EFI_HTTP_HEADER **Headers, + UINTN *HeaderCount, + EFI_HTTP_STATUS_CODE **StatusCode + ); + json_t * patchUriFromService ( redfishService *service, @@ -112,6 +122,16 @@ patchUriFromService ( EFI_HTTP_STATUS_CODE **StatusCode ); =20 +json_t * +patchUriFromServiceEx ( + redfishService *service, + const char *uri, + const char *content, + EFI_HTTP_HEADER **Headers, + UINTN *HeaderCount, + EFI_HTTP_STATUS_CODE **StatusCode + ); + json_t * postUriFromService ( redfishService *service, @@ -122,6 +142,30 @@ postUriFromService ( EFI_HTTP_STATUS_CODE **StatusCode ); =20 +json_t * +postUriFromServiceEx ( + redfishService *service, + const char *uri, + const char *content, + size_t contentLength, + const char *contentType, + EFI_HTTP_HEADER **Headers, + UINTN *HeaderCount, + EFI_HTTP_STATUS_CODE **StatusCode + ); + +json_t * +putUriFromServiceEx ( + redfishService *service, + const char *uri, + const char *content, + size_t contentLength, + const char *contentType, + EFI_HTTP_HEADER **Headers, + UINTN *HeaderCount, + EFI_HTTP_STATUS_CODE **StatusCode + ); + json_t * deleteUriFromService ( redfishService *service, diff --git a/RedfishPkg/PrivateLibrary/RedfishLib/edk2libredfish/src/payloa= d.c b/RedfishPkg/PrivateLibrary/RedfishLib/edk2libredfish/src/payload.c index 6c6e2246abe3..8fb24a5928a3 100644 --- a/RedfishPkg/PrivateLibrary/RedfishLib/edk2libredfish/src/payload.c +++ b/RedfishPkg/PrivateLibrary/RedfishLib/edk2libredfish/src/payload.c @@ -10,6 +10,7 @@ =20 Copyright (c) 2019, Intel Corporation. All rights reserved.
(C) Copyright 2021 Hewlett Packard Enterprise Development LP
+ Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved= . =20 SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -524,7 +525,7 @@ getOpResult ( } =20 stringProp =3D prop->json; - jsonType =3D prop->json->type; + jsonType =3D JsonGetType (prop->json); switch (jsonType) { case JSON_OBJECT: stringProp =3D json_object_get (prop->json, propName); diff --git a/RedfishPkg/PrivateLibrary/RedfishLib/edk2libredfish/src/servic= e.c b/RedfishPkg/PrivateLibrary/RedfishLib/edk2libredfish/src/service.c index 286f298e6a35..3377b431f8a5 100644 --- a/RedfishPkg/PrivateLibrary/RedfishLib/edk2libredfish/src/service.c +++ b/RedfishPkg/PrivateLibrary/RedfishLib/edk2libredfish/src/service.c @@ -10,6 +10,7 @@ =20 Copyright (c) 2019, Intel Corporation. All rights reserved.
(C) Copyright 2021 Hewlett Packard Enterprise Development LP
+ Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved= . =20 SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -401,10 +402,41 @@ ON_EXIT: return ret; } =20 +EFI_HTTP_HEADER * +cloneHttpHeaders ( + EFI_HTTP_MESSAGE *message, + UINTN *HeaderCount + ) +{ + EFI_HTTP_HEADER *Buffer; + UINTN Index; + + if ((message =3D=3D NULL) || (HeaderCount =3D=3D NULL)) { + return NULL; + } + + *HeaderCount =3D message->HeaderCount; + Buffer =3D AllocatePool (sizeof (EFI_HTTP_HEADER) * message->Head= erCount); + if (Buffer =3D=3D NULL) { + return NULL; + } + + for (Index =3D 0; Index < message->HeaderCount; Index++) { + Buffer[Index].FieldName =3D AllocateCopyPool (AsciiStrSize (message->H= eaders[Index].FieldName), message->Headers[Index].FieldName); + ASSERT (Buffer[Index].FieldName !=3D NULL); + Buffer[Index].FieldValue =3D AllocateCopyPool (AsciiStrSize (message->= Headers[Index].FieldValue), message->Headers[Index].FieldValue); + ASSERT (Buffer[Index].FieldValue !=3D NULL); + } + + return Buffer; +} + json_t * -getUriFromService ( +getUriFromServiceEx ( redfishService *service, const char *uri, + EFI_HTTP_HEADER **Headers OPTIONAL, + UINTN *HeaderCount OPTIONAL, EFI_HTTP_STATUS_CODE **StatusCode ) { @@ -422,13 +454,20 @@ getUriFromService ( } =20 *StatusCode =3D NULL; + if (HeaderCount !=3D NULL) { + *HeaderCount =3D 0; + } + + if (Headers !=3D NULL) { + *Headers =3D NULL; + } =20 url =3D makeUrlForService (service, uri); if (!url) { return NULL; } =20 - DEBUG ((DEBUG_MANAGEABILITY, "libredfish: getUriFromService(): %a\n", ur= l)); + DEBUG ((DEBUG_MANAGEABILITY, "%a: %a\n", __func__, url)); =20 // // Step 1: Create HTTP request message with 4 headers: @@ -491,6 +530,23 @@ getUriFromService ( Status =3D service->RestEx->SendReceive (service->RestEx, RequestMsg, &R= esponseMsg); if (EFI_ERROR (Status)) { ret =3D NULL; + + // + // Deliver status code to caller when error happens so caller can do e= rror handling. + // + if (ResponseMsg.Data.Response !=3D NULL) { + *StatusCode =3D AllocateZeroPool (sizeof (EFI_HTTP_STATUS_CODE)); + if (*StatusCode =3D=3D NULL) { + ret =3D NULL; + goto ON_EXIT; + } + + // + // The caller shall take the responsibility to free the buffer. + // + **StatusCode =3D ResponseMsg.Data.Response->StatusCode; + } + goto ON_EXIT; } =20 @@ -510,6 +566,10 @@ getUriFromService ( **StatusCode =3D ResponseMsg.Data.Response->StatusCode; } =20 + if ((ResponseMsg.Headers !=3D NULL) && (Headers !=3D NULL) && (HeaderCou= nt !=3D NULL)) { + *Headers =3D cloneHttpHeaders (&ResponseMsg, HeaderCount); + } + if ((ResponseMsg.BodyLength !=3D 0) && (ResponseMsg.Body !=3D NULL)) { // // Check if data is encoded. @@ -558,10 +618,14 @@ ON_EXIT: } =20 json_t * -patchUriFromService ( +putUriFromServiceEx ( redfishService *service, const char *uri, const char *content, + size_t contentLength, + const char *contentType, + EFI_HTTP_HEADER **Headers OPTIONAL, + UINTN *HeaderCount OPTIONAL, EFI_HTTP_STATUS_CODE **StatusCode ) { @@ -581,13 +645,224 @@ patchUriFromService ( } =20 *StatusCode =3D NULL; + if (HeaderCount !=3D NULL) { + *HeaderCount =3D 0; + } + + if (Headers !=3D NULL) { + *Headers =3D NULL; + } + + url =3D makeUrlForService (service, uri); + if (url =3D=3D NULL) { + return NULL; + } + + DEBUG ((DEBUG_MANAGEABILITY, "%a: %a\n", __func__, url)); + + if (contentLength =3D=3D 0) { + contentLength =3D strlen (content); + } + + // + // Step 1: Create HTTP request message with 4 headers: + // + HttpIoHeader =3D HttpIoCreateHeader ((service->sessionToken !=3D NULL ||= service->basicAuthStr !=3D NULL) ? 9 : 8); + if (HttpIoHeader =3D=3D NULL) { + ret =3D NULL; + goto ON_EXIT; + } + + if (service->sessionToken) { + Status =3D HttpIoSetHeader (HttpIoHeader, "X-Auth-Token", service->ses= sionToken); + ASSERT_EFI_ERROR (Status); + } else if (service->basicAuthStr) { + Status =3D HttpIoSetHeader (HttpIoHeader, "Authorization", service->ba= sicAuthStr); + ASSERT_EFI_ERROR (Status); + } + + if (contentType =3D=3D NULL) { + Status =3D HttpIoSetHeader (HttpIoHeader, "Content-Type", "application= /json"); + ASSERT_EFI_ERROR (Status); + } else { + Status =3D HttpIoSetHeader (HttpIoHeader, "Content-Type", (CHAR8 *)con= tentType); + ASSERT_EFI_ERROR (Status); + } + + Status =3D HttpIoSetHeader (HttpIoHeader, "Host", service->HostHeaderVal= ue); + ASSERT_EFI_ERROR (Status); + Status =3D HttpIoSetHeader (HttpIoHeader, "Content-Type", "application/j= son"); + ASSERT_EFI_ERROR (Status); + Status =3D HttpIoSetHeader (HttpIoHeader, "Accept", "application/json"); + ASSERT_EFI_ERROR (Status); + Status =3D HttpIoSetHeader (HttpIoHeader, "User-Agent", "libredfish"); + ASSERT_EFI_ERROR (Status); + Status =3D HttpIoSetHeader (HttpIoHeader, "Connection", "Keep-Alive"); + ASSERT_EFI_ERROR (Status); + + AsciiSPrint ( + ContentLengthStr, + sizeof (ContentLengthStr), + "%lu", + (UINT64)contentLength + ); + Status =3D HttpIoSetHeader (HttpIoHeader, "Content-Length", ContentLengt= hStr); + ASSERT_EFI_ERROR (Status); + Status =3D HttpIoSetHeader (HttpIoHeader, "OData-Version", "4.0"); + ASSERT_EFI_ERROR (Status); + + // + // Step 2: build the rest of HTTP request info. + // + RequestData =3D AllocateZeroPool (sizeof (EFI_HTTP_REQUEST_DATA)); + if (RequestData =3D=3D NULL) { + ret =3D NULL; + goto ON_EXIT; + } + + RequestData->Method =3D HttpMethodPut; + RequestData->Url =3D C8ToC16 (url); + + // + // Step 3: fill in EFI_HTTP_MESSAGE + // + RequestMsg =3D AllocateZeroPool (sizeof (EFI_HTTP_MESSAGE)); + if (RequestMsg =3D=3D NULL) { + ret =3D NULL; + goto ON_EXIT; + } + + EncodedContent =3D (CHAR8 *)content; + EncodedContentLen =3D contentLength; + // + // We currently only support gzip Content-Encoding. + // + Status =3D EncodeRequestContent ((CHAR8 *)HTTP_CONTENT_ENCODING_GZIP, (C= HAR8 *)content, (VOID **)&EncodedContent, &EncodedContentLen); + if (Status =3D=3D EFI_INVALID_PARAMETER) { + DEBUG ((DEBUG_ERROR, "%a: Error to encode content.\n", __func__)); + ret =3D NULL; + goto ON_EXIT; + } else if (Status =3D=3D EFI_UNSUPPORTED) { + DEBUG ((DEBUG_MANAGEABILITY, "No content coding for %a! Use raw data i= nstead.\n", HTTP_CONTENT_ENCODING_GZIP)); + Status =3D HttpIoSetHeader (HttpIoHeader, "Content-Encoding", HTTP_CON= TENT_ENCODING_IDENTITY); + ASSERT_EFI_ERROR (Status); + } else { + Status =3D HttpIoSetHeader (HttpIoHeader, "Content-Encoding", HTTP_CON= TENT_ENCODING_GZIP); + ASSERT_EFI_ERROR (Status); + } + + RequestMsg->Data.Request =3D RequestData; + RequestMsg->HeaderCount =3D HttpIoHeader->HeaderCount; + RequestMsg->Headers =3D HttpIoHeader->Headers; + RequestMsg->BodyLength =3D EncodedContentLen; + RequestMsg->Body =3D (VOID *)EncodedContent; + + ZeroMem (&ResponseMsg, sizeof (ResponseMsg)); + + // + // Step 4: call RESTEx to get response from REST service. + // + Status =3D service->RestEx->SendReceive (service->RestEx, RequestMsg, &R= esponseMsg); + if (EFI_ERROR (Status)) { + ret =3D NULL; + goto ON_EXIT; + } + + // + // Step 5: Return the HTTP StatusCode and Body message. + // + if (ResponseMsg.Data.Response !=3D NULL) { + *StatusCode =3D AllocateZeroPool (sizeof (EFI_HTTP_STATUS_CODE)); + if (*StatusCode =3D=3D NULL) { + ret =3D NULL; + goto ON_EXIT; + } + + // + // The caller shall take the responsibility to free the buffer. + // + **StatusCode =3D ResponseMsg.Data.Response->StatusCode; + } + + if ((ResponseMsg.Headers !=3D NULL) && (Headers !=3D NULL) && (HeaderCou= nt !=3D NULL)) { + *Headers =3D cloneHttpHeaders (&ResponseMsg, HeaderCount); + } + + if (EncodedContent !=3D content) { + FreePool (EncodedContent); + } + + if ((ResponseMsg.BodyLength !=3D 0) && (ResponseMsg.Body !=3D NULL)) { + ret =3D json_loadb (ResponseMsg.Body, ResponseMsg.BodyLength, 0, NULL)= ; + } else { + // + // There is no message body returned from server. + // + ret =3D NULL; + } + +ON_EXIT: + if (url !=3D NULL) { + free (url); + } + + if (HttpIoHeader !=3D NULL) { + HttpIoFreeHeader (HttpIoHeader); + } + + if (RequestData !=3D NULL) { + RestConfigFreeHttpRequestData (RequestData); + } + + if (RequestMsg !=3D NULL) { + FreePool (RequestMsg); + } + + RestConfigFreeHttpMessage (&ResponseMsg, FALSE); + + return ret; +} + +json_t * +patchUriFromServiceEx ( + redfishService *service, + const char *uri, + const char *content, + EFI_HTTP_HEADER **Headers OPTIONAL, + UINTN *HeaderCount OPTIONAL, + EFI_HTTP_STATUS_CODE **StatusCode + ) +{ + char *url; + json_t *ret; + HTTP_IO_HEADER *HttpIoHeader =3D NULL; + EFI_STATUS Status; + EFI_HTTP_REQUEST_DATA *RequestData =3D NULL; + EFI_HTTP_MESSAGE *RequestMsg =3D NULL; + EFI_HTTP_MESSAGE ResponseMsg; + CHAR8 ContentLengthStr[80]; + CHAR8 *EncodedContent; + UINTN EncodedContentLen; + + if ((service =3D=3D NULL) || (uri =3D=3D NULL) || (content =3D=3D NULL) = || (StatusCode =3D=3D NULL)) { + return NULL; + } + + *StatusCode =3D NULL; + if (HeaderCount !=3D NULL) { + *HeaderCount =3D 0; + } + + if (Headers !=3D NULL) { + *Headers =3D NULL; + } =20 url =3D makeUrlForService (service, uri); if (!url) { return NULL; } =20 - DEBUG ((DEBUG_MANAGEABILITY, "libredfish: patchUriFromService(): %a\n", = url)); + DEBUG ((DEBUG_MANAGEABILITY, "%a: %a\n", __func__, url)); =20 // // Step 1: Create HTTP request message with 4 headers: @@ -701,6 +976,10 @@ patchUriFromService ( **StatusCode =3D ResponseMsg.Data.Response->StatusCode; } =20 + if ((ResponseMsg.Headers !=3D NULL) && (Headers !=3D NULL) && (HeaderCou= nt !=3D NULL)) { + *Headers =3D cloneHttpHeaders (&ResponseMsg, HeaderCount); + } + if (EncodedContent !=3D content) { FreePool (EncodedContent); } @@ -737,12 +1016,14 @@ ON_EXIT: } =20 json_t * -postUriFromService ( +postUriFromServiceEx ( redfishService *service, const char *uri, const char *content, size_t contentLength, const char *contentType, + EFI_HTTP_HEADER **Headers OPTIONAL, + UINTN *HeaderCount OPTIONAL, EFI_HTTP_STATUS_CODE **StatusCode ) { @@ -763,13 +1044,20 @@ postUriFromService ( } =20 *StatusCode =3D NULL; + if (HeaderCount !=3D NULL) { + *HeaderCount =3D 0; + } + + if (Headers !=3D NULL) { + *Headers =3D NULL; + } =20 url =3D makeUrlForService (service, uri); if (!url) { return NULL; } =20 - DEBUG ((DEBUG_MANAGEABILITY, "libredfish: postUriFromService(): %a\n", u= rl)); + DEBUG ((DEBUG_MANAGEABILITY, "%a: %a\n", __func__, url)); =20 if (contentLength =3D=3D 0) { contentLength =3D strlen (content); @@ -850,7 +1138,12 @@ postUriFromService ( // Status =3D service->RestEx->SendReceive (service->RestEx, RequestMsg, &R= esponseMsg); if (EFI_ERROR (Status)) { - goto ON_EXIT; + // + // If there is no response to handle, go to error exit. + // + if (ResponseMsg.Data.Response =3D=3D NULL) { + goto ON_EXIT; + } } =20 // @@ -868,15 +1161,20 @@ postUriFromService ( **StatusCode =3D ResponseMsg.Data.Response->StatusCode; } =20 + if ((ResponseMsg.Headers !=3D NULL) && (Headers !=3D NULL) && (HeaderCou= nt !=3D NULL)) { + *Headers =3D cloneHttpHeaders (&ResponseMsg, HeaderCount); + } + if ((ResponseMsg.BodyLength !=3D 0) && (ResponseMsg.Body !=3D NULL)) { ret =3D json_loadb (ResponseMsg.Body, ResponseMsg.BodyLength, 0, NULL)= ; } =20 // - // Step 6: Parsing the HttpHeader to retrive the X-Auth-Token if the HTT= P StatusCode is correct. + // Step 6: Parsing the HttpHeader to retrieve the X-Auth-Token if the HT= TP StatusCode is correct. // - if ((ResponseMsg.Data.Response->StatusCode =3D=3D HTTP_STATUS_200_OK) || - (ResponseMsg.Data.Response->StatusCode =3D=3D HTTP_STATUS_204_NO_CON= TENT)) + if ((ResponseMsg.Data.Response !=3D NULL) && + ((ResponseMsg.Data.Response->StatusCode =3D=3D HTTP_STATUS_200_OK) |= | + (ResponseMsg.Data.Response->StatusCode =3D=3D HTTP_STATUS_204_NO_CO= NTENT))) { HttpHeader =3D HttpFindHeader (ResponseMsg.HeaderCount, ResponseMsg.He= aders, "X-Auth-Token"); if (HttpHeader !=3D NULL) { @@ -886,19 +1184,6 @@ postUriFromService ( =20 service->sessionToken =3D AllocateCopyPool (AsciiStrSize (HttpHeader= ->FieldValue), HttpHeader->FieldValue); } - - /* - // - // Below opeation seems to be unnecessary. - // Besides, the FieldValue for the Location is the full HTTP URI (Http= ://0.0.0.0:5000/XXX), so we can't use it as the - // parameter of getUriFromService () directly. - // - HttpHeader =3D HttpFindHeader (ResponseMsg.HeaderCount, ResponseMsg.He= aders, "Location"); - if (HttpHeader !=3D NULL) { - ret =3D getUriFromService(service, HttpHeader->FieldValue); - goto ON_EXIT; - } - */ } =20 ON_EXIT: @@ -923,6 +1208,40 @@ ON_EXIT: return ret; } =20 +json_t * +getUriFromService ( + redfishService *service, + const char *uri, + EFI_HTTP_STATUS_CODE **StatusCode + ) +{ + return getUriFromServiceEx (service, uri, NULL, NULL, StatusCode); +} + +json_t * +patchUriFromService ( + redfishService *service, + const char *uri, + const char *content, + EFI_HTTP_STATUS_CODE **StatusCode + ) +{ + return patchUriFromServiceEx (service, uri, content, NULL, NULL, StatusC= ode); +} + +json_t * +postUriFromService ( + redfishService *service, + const char *uri, + const char *content, + size_t contentLength, + const char *contentType, + EFI_HTTP_STATUS_CODE **StatusCode + ) +{ + return postUriFromServiceEx (service, uri, content, contentLength, conte= ntType, NULL, NULL, StatusCode); +} + json_t * deleteUriFromServiceEx ( redfishService *service, @@ -1498,6 +1817,10 @@ makeUrlForService ( } =20 url =3D (char *)malloc (strlen (service->host)+strlen (uri)+1); + if (url =3D=3D NULL) { + return NULL; + } + strcpy (url, service->host); strcat (url, uri); return url; --=20 2.17.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#109992): https://edk2.groups.io/g/devel/message/109992 Mute This Topic: https://groups.io/mt/102154013/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-