From nobody Mon Feb 9 09:34:32 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+91877+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+91877+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=hpe.com ARC-Seal: i=1; a=rsa-sha256; t=1658885904; cv=none; d=zohomail.com; s=zohoarc; b=eQmHyAT5o/7JmrwsS/MjsBLgkv453whsTRLU2ua0+N11kQ/oyTPOFOgMia2rhHNmW6eddN8hCl8CjkOLm8QNoROJq8vdNbIauhCLJbCAyEeZUx8hp2IDI7k2xK5u4kIT3TQbUFphS1oU3GOsQUZJY1ABnnx8iRnBkKRAxoB6Iec= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1658885904; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=EMP3N6VhhkX4FPSSf+LTDNlbSjVqTjKbGyX68Hhukbo=; b=J8qTWVN317s2S2BjG2f5CaIgulie7OwhRsFDeWyhxdXrYR3chwIQzDIBIDkesx8/WlTjYaJIVCg+rE2HS/I7qvcvXserXoes9LvtMhYCpzJATKHKgHs03CTlRD5bYte4EI/Ucm2aVO9aoPhZvcwZ1AyMU/h1+ZbuvHyAxi8Yw/s= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+91877+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1658885904910241.6621616250667; Tue, 26 Jul 2022 18:38:24 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id X3IsYY1788612x9AwKzPCFUP; Tue, 26 Jul 2022 18:38:23 -0700 X-Received: from mx0b-002e3701.pphosted.com (mx0b-002e3701.pphosted.com [148.163.143.35]) by mx.groups.io with SMTP id smtpd.web09.14923.1658885902732978517 for ; Tue, 26 Jul 2022 18:38:22 -0700 X-Received: from pps.filterd (m0148664.ppops.net [127.0.0.1]) by mx0b-002e3701.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 26R0q6Ab030646; Wed, 27 Jul 2022 01:38:19 GMT X-Received: from p1lg14880.it.hpe.com (p1lg14880.it.hpe.com [16.230.97.201]) by mx0b-002e3701.pphosted.com (PPS) with ESMTPS id 3hjew9xsrm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 27 Jul 2022 01:38:19 +0000 X-Received: from p1lg14885.dc01.its.hpecorp.net (unknown [10.119.18.236]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by p1lg14880.it.hpe.com (Postfix) with ESMTPS id 0CA2B800E80; Wed, 27 Jul 2022 01:38:19 +0000 (UTC) X-Received: from WAFM3XJD5N.asiapacific.hpqcorp.net (unknown [16.231.227.36]) by p1lg14885.dc01.its.hpecorp.net (Postfix) with ESMTP id C14DF804D10; Wed, 27 Jul 2022 01:38:17 +0000 (UTC) From: "Nickle Wang" To: devel@edk2.groups.io Cc: Abner Chang , Yang Atom , Nick Ramirez Subject: [edk2-devel] [edk2-staging][PATCH v3 09/15] edk2-staging/RedfishClientPkg: Update RedfishLib Date: Wed, 27 Jul 2022 09:37:56 +0800 Message-Id: <20220727013802.247-10-nickle.wang@hpe.com> In-Reply-To: <20220727013802.247-1-nickle.wang@hpe.com> References: <20220727013802.247-1-nickle.wang@hpe.com> X-Proofpoint-ORIG-GUID: oNIOjEjb7CS99JWCP4A94R042-ah3Wx6 X-Proofpoint-GUID: oNIOjEjb7CS99JWCP4A94R042-ah3Wx6 X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-HPE-SCL: -1 Precedence: Bulk List-Unsubscribe: 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,nickle.wang@hpe.com X-Gm-Message-State: EpdkjoHj8cDCERDsVOJKbs63x1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1658885903; bh=YrRGjTt0h9/k/wGzT+anqWCQr9jTeufCkP01h83dbe4=; h=Cc:Date:From:Reply-To:Subject:To; b=oOTiTLgax2KP6v7ZrgEYXYuSiZCS8iQQ46u5cwV2NX3ruaSVQdklvfpSVGXMNATzppt +I9vGZFaBMz3LVinLFKm6VA9s45+IoULPwm+JrZknh+gUdUHKzdIwctoD8Y080r+jCn/I cVG9c6nyswHp+KV4E2iW8lPZhbcPTmjXQqg= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1658885907085100004 Content-Type: text/plain; charset="utf-8" RedfishLib has no capability to return HTTP header in response. However, feature driver needs to know the information like "ETag" or "Location" in HTTP response header per Redfish specification. Add corresponding function to return HTTP header in response data. Signed-off-by: Nickle Wang Cc: Abner Chang Cc: Yang Atom Cc: Nick Ramirez Reviewed-by: Abner Chang --- .../PrivateLibrary/RedfishLib/RedfishLib.c | 12 +- .../edk2libredfish/include/redfishPayload.h | 5 +- .../edk2libredfish/include/redfishService.h | 5 +- .../RedfishLib/edk2libredfish/src/payload.c | 90 ++- .../RedfishLib/edk2libredfish/src/service.c | 554 +++++++++++++++++- 5 files changed, 657 insertions(+), 9 deletions(-) diff --git a/RedfishClientPkg/PrivateLibrary/RedfishLib/RedfishLib.c b/Redf= ishClientPkg/PrivateLibrary/RedfishLib/RedfishLib.c index 18aa4646e8..b8ca493e24 100644 --- a/RedfishClientPkg/PrivateLibrary/RedfishLib/RedfishLib.c +++ b/RedfishClientPkg/PrivateLibrary/RedfishLib/RedfishLib.c @@ -3,7 +3,7 @@ (CRUD) Redfish resources and provide basic query. =20 Copyright (c) 2019, Intel Corporation. All rights reserved.
- (C) Copyright 2021 Hewlett Packard Enterprise Development LP
+ (C) Copyright 2021-2022 Hewlett Packard Enterprise Development LP
=20 SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -323,7 +323,7 @@ RedfishGetByUri ( =20 ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE)); =20 - JsonValue =3D getUriFromService (RedfishService, Uri, &RedResponse->Stat= usCode); + JsonValue =3D getUriFromServiceEx (RedfishService, Uri, &RedResponse->He= aders, &RedResponse->HeaderCount, &RedResponse->StatusCode); RedResponse->Payload =3D createRedfishPayload(JsonValue, RedfishService); =20 // @@ -541,9 +541,11 @@ RedfishPatchToPayload ( =20 ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE)); =20 - RedResponse->Payload =3D (REDFISH_PAYLOAD) patchPayload ( + RedResponse->Payload =3D (REDFISH_PAYLOAD) patchPayloadEx ( Target, Payload, + &RedResponse->Headers, + &RedResponse->HeaderCount, &(RedResponse->StatusCode) ); =20 @@ -607,9 +609,11 @@ RedfishPostToPayload ( =20 ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE)); =20 - RedResponse->Payload =3D (REDFISH_PAYLOAD) postPayload ( + RedResponse->Payload =3D (REDFISH_PAYLOAD) postPayloadEx ( Target, Payload, + &RedResponse->Headers, + &RedResponse->HeaderCount, &(RedResponse->StatusCode) ); =20 diff --git a/RedfishClientPkg/PrivateLibrary/RedfishLib/edk2libredfish/incl= ude/redfishPayload.h b/RedfishClientPkg/PrivateLibrary/RedfishLib/edk2libre= dfish/include/redfishPayload.h index 43149f3c89..be74c64297 100644 --- a/RedfishClientPkg/PrivateLibrary/RedfishLib/edk2libredfish/include/red= fishPayload.h +++ b/RedfishClientPkg/PrivateLibrary/RedfishLib/edk2libredfish/include/red= fishPayload.h @@ -9,7 +9,7 @@ //------------------------------------------------------------------------= ---- =20 Copyright (c) 2019, Intel Corporation. All rights reserved.
- (C) Copyright 2021 Hewlett Packard Enterprise Development LP
+ (C) Copyright 2021-2022 Hewlett Packard Enterprise Development LP
=20 SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -29,8 +29,11 @@ redfishPayload* getPayloadByIndex(redfishPayload* payloa= d, size_t index, EFI_HTT redfishPayload* getPayloadForPath(redfishPayload* payload, redPathNode* re= dpath, EFI_HTTP_STATUS_CODE** StatusCode); redfishPayload* getPayloadForPathString(redfishPayload* payload, const cha= r* string, EFI_HTTP_STATUS_CODE** StatusCode); redfishPayload* patchPayload(redfishPayload* target, redfishPayload* paylo= ad, EFI_HTTP_STATUS_CODE** StatusCode); +redfishPayload* patchPayloadEx(redfishPayload* target, redfishPayload* pay= load, EFI_HTTP_HEADER **Headers, UINTN *HeaderCount, EFI_HTTP_STATUS_CODE**= StatusCode); redfishPayload* postContentToPayload(redfishPayload* target, const char* d= ata, size_t dataSize, const char* contentType, EFI_HTTP_STATUS_CODE** Statu= sCode); +redfishPayload* postContentToPayloadEx(redfishPayload* target, const char*= data, size_t dataSize, const char* contentType, EFI_HTTP_HEADER **Headers,= UINTN *HeaderCount, EFI_HTTP_STATUS_CODE** StatusCode); redfishPayload* postPayload(redfishPayload* target, redfishPayload* payloa= d, EFI_HTTP_STATUS_CODE** StatusCode); +redfishPayload* postPayloadEx(redfishPayload* target, redfishPayload* payl= oad, EFI_HTTP_HEADER **Headers, UINTN *HeaderCount, EFI_HTTP_STATUS_CODE** = StatusCode); void cleanupPayload(redfishPayload* payload); bool isPayloadCollection (redfishPayload *Payload); size_t getCollectionSize(redfishPayload* payload); diff --git a/RedfishClientPkg/PrivateLibrary/RedfishLib/edk2libredfish/incl= ude/redfishService.h b/RedfishClientPkg/PrivateLibrary/RedfishLib/edk2libre= dfish/include/redfishService.h index 0215caccfc..3d87fad85a 100644 --- a/RedfishClientPkg/PrivateLibrary/RedfishLib/edk2libredfish/include/red= fishService.h +++ b/RedfishClientPkg/PrivateLibrary/RedfishLib/edk2libredfish/include/red= fishService.h @@ -9,7 +9,7 @@ //------------------------------------------------------------------------= ---- =20 Copyright (c) 2019, Intel Corporation. All rights reserved.
- (C) Copyright 2021 Hewlett Packard Enterprise Development LP
+ (C) Copyright 2021-2022 Hewlett Packard Enterprise Development LP
=20 SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -91,8 +91,11 @@ typedef struct { #define REDFISH_FLAG_SERVICE_NO_VERSION_DOC 0x00000001 //The Redfish Servi= ce lacks the version document (in violation of the Redfish spec) redfishService* createServiceEnumerator(REDFISH_CONFIG_SERVICE_INFORMATION= *RedfishConfigServiceInfo, const char* rootUri, enumeratorAuthentication* = auth, unsigned int flags); json_t* getUriFromService(redfishService* service, const char* uri, EFI_HT= TP_STATUS_CODE** StatusCode); +json_t* getUriFromServiceEx(redfishService* service, const char* uri, EFI_= HTTP_HEADER **Headers, UINTN *HeaderCount, EFI_HTTP_STATUS_CODE **StatusCod= e); json_t* patchUriFromService(redfishService* service, const char* uri, cons= t char* content, EFI_HTTP_STATUS_CODE** StatusCode); +json_t* patchUriFromServiceEx(redfishService* service, const char* uri, co= nst char* content, EFI_HTTP_HEADER **Headers, UINTN *HeaderCount, EFI_HTTP_= STATUS_CODE** StatusCode); json_t* postUriFromService(redfishService* service, const char* uri, const= char* content, size_t contentLength, const char* contentType, EFI_HTTP_STA= TUS_CODE** StatusCode); +json_t* postUriFromServiceEx(redfishService* service, const char* uri, con= st char* content, size_t contentLength, const char* contentType, EFI_HTTP_H= EADER **Headers, UINTN *HeaderCount, EFI_HTTP_STATUS_CODE** StatusCode); json_t* deleteUriFromService(redfishService* service, const char* uri, EFI= _HTTP_STATUS_CODE** StatusCode); redfishPayload* getRedfishServiceRoot(redfishService* service, const char*= version, EFI_HTTP_STATUS_CODE** StatusCode); redfishPayload* getPayloadByPath(redfishService* service, const char* path= , EFI_HTTP_STATUS_CODE** StatusCode); diff --git a/RedfishClientPkg/PrivateLibrary/RedfishLib/edk2libredfish/src/= payload.c b/RedfishClientPkg/PrivateLibrary/RedfishLib/edk2libredfish/src/p= ayload.c index bd8d143c4e..8b49bec0df 100644 --- a/RedfishClientPkg/PrivateLibrary/RedfishLib/edk2libredfish/src/payload= .c +++ b/RedfishClientPkg/PrivateLibrary/RedfishLib/edk2libredfish/src/payload= .c @@ -9,7 +9,7 @@ //------------------------------------------------------------------------= ---- =20 Copyright (c) 2019, Intel Corporation. All rights reserved.
- (C) Copyright 2021 Hewlett Packard Enterprise Development LP
+ (C) Copyright 2021-2022 Hewlett Packard Enterprise Development LP
=20 SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -326,6 +326,40 @@ redfishPayload* getPayloadForPathString(redfishPayload= * payload, const char* str return ret; } =20 +redfishPayload* patchPayloadEx(redfishPayload* target, redfishPayload* pay= load, EFI_HTTP_HEADER **Headers, UINTN *HeaderCount, EFI_HTTP_STATUS_CODE**= StatusCode) +{ + json_t* json; + char* content; + char* uri; + + if(!target || !payload || StatusCode =3D=3D NULL) + { + return NULL; + } + + *StatusCode =3D NULL; + + json =3D json_object_get(target->json, "@odata.id"); + if(json =3D=3D NULL) + { + return NULL; + } + uri =3D strdup(json_string_value(json)); + + content =3D json_dumps(payload->json, 0); + json_decref(json); + + json =3D patchUriFromServiceEx(target->service, uri, content, Headers, H= eaderCount, StatusCode); + free(uri); + free(content); + if(json =3D=3D NULL) + { + return NULL; + } + + return createRedfishPayload(json, target->service); +} + redfishPayload* patchPayload(redfishPayload* target, redfishPayload* paylo= ad, EFI_HTTP_STATUS_CODE** StatusCode) { json_t* json; @@ -360,6 +394,38 @@ redfishPayload* patchPayload(redfishPayload* target, r= edfishPayload* payload, EF return createRedfishPayload(json, target->service); } =20 +redfishPayload* postContentToPayloadEx(redfishPayload* target, const char*= data, size_t dataSize, const char* contentType, EFI_HTTP_HEADER **Headers,= UINTN *HeaderCount, EFI_HTTP_STATUS_CODE** StatusCode) +{ + json_t* json; + char* uri; + + if(!target || !data || StatusCode =3D=3D NULL) + { + return NULL; + } + + *StatusCode =3D NULL; + + json =3D json_object_get(target->json, "@odata.id"); + if(json =3D=3D NULL) + { + json =3D json_object_get(target->json, "target"); + if(json =3D=3D NULL) + { + return NULL; + } + } + uri =3D strdup(json_string_value(json)); + json =3D postUriFromServiceEx(target->service, uri, data, dataSize, co= ntentType, Headers, HeaderCount, StatusCode); + free(uri); + if(json =3D=3D NULL) + { + return NULL; + } + + return createRedfishPayload(json, target->service); +} + redfishPayload* postContentToPayload(redfishPayload* target, const char* d= ata, size_t dataSize, const char* contentType, EFI_HTTP_STATUS_CODE** Statu= sCode) { json_t* json; @@ -392,6 +458,28 @@ redfishPayload* postContentToPayload(redfishPayload* t= arget, const char* data, s return createRedfishPayload(json, target->service); } =20 +redfishPayload* postPayloadEx(redfishPayload* target, redfishPayload* payl= oad, EFI_HTTP_HEADER **Headers, UINTN *HeaderCount, EFI_HTTP_STATUS_CODE** = StatusCode) +{ + char* content; + redfishPayload* ret; + + if(!target || !payload || StatusCode =3D=3D NULL) + { + return NULL; + } + + *StatusCode =3D NULL; + + if(!json_is_object(payload->json)) + { + return NULL; + } + content =3D payloadToString(payload, false); + ret =3D postContentToPayloadEx(target, content, strlen(content), NULL,= Headers, HeaderCount, StatusCode); + free(content); + return ret; +} + redfishPayload* postPayload(redfishPayload* target, redfishPayload* payloa= d, EFI_HTTP_STATUS_CODE** StatusCode) { char* content; diff --git a/RedfishClientPkg/PrivateLibrary/RedfishLib/edk2libredfish/src/= service.c b/RedfishClientPkg/PrivateLibrary/RedfishLib/edk2libredfish/src/s= ervice.c index 7713f89e6d..450fa78bbd 100644 --- a/RedfishClientPkg/PrivateLibrary/RedfishLib/edk2libredfish/src/service= .c +++ b/RedfishClientPkg/PrivateLibrary/RedfishLib/edk2libredfish/src/service= .c @@ -9,7 +9,7 @@ //------------------------------------------------------------------------= ---- =20 Copyright (c) 2019, Intel Corporation. All rights reserved.
- (C) Copyright 2021 Hewlett Packard Enterprise Development LP
+ (C) Copyright 2021-2022 Hewlett Packard Enterprise Development LP
=20 SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -338,6 +338,190 @@ ON_EXIT: return ret; } =20 +EFI_HTTP_HEADER *cloneHttpHeaders(EFI_HTTP_MESSAGE *message, UINTN *Header= Count) +{ + 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->HeaderCoun= t); + 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* getUriFromServiceEx(redfishService* service, const char* uri, EFI_= HTTP_HEADER **Headers, UINTN *HeaderCount, EFI_HTTP_STATUS_CODE **StatusCod= e) +{ + 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; + EFI_HTTP_HEADER *ContentEncodedHeader; + + if(service =3D=3D NULL || uri =3D=3D NULL || Headers =3D=3D NULL || Head= erCount =3D=3D NULL ||StatusCode =3D=3D NULL) + { + return NULL; + } + + *StatusCode =3D NULL; + *HeaderCount =3D 0; + *Headers =3D NULL; + + url =3D makeUrlForService(service, uri); + if(!url) + { + return NULL; + } + + DEBUG((DEBUG_INFO, "libredfish: getUriFromServiceEx(): %a\n", url)); + + // + // Step 1: Create HTTP request message with 4 headers: + // + HttpIoHeader =3D HttpIoCreateHeader ((service->sessionToken || service->= basicAuthStr) ? 6 : 5); + 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); + } + + Status =3D HttpIoSetHeader (HttpIoHeader, "Host", service->HostHeaderVal= ue); + ASSERT_EFI_ERROR (Status); + Status =3D HttpIoSetHeader (HttpIoHeader, "OData-Version", "4.0"); + 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); + + // + // 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 HttpMethodGet; + 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; + } + + RequestMsg->Data.Request =3D RequestData; + RequestMsg->HeaderCount =3D HttpIoHeader->HeaderCount; + RequestMsg->Headers =3D HttpIoHeader->Headers; + + 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 cloneHttpHeaders (&ResponseMsg, HeaderCount); + } + + if (ResponseMsg.BodyLength !=3D 0 && ResponseMsg.Body !=3D NULL) { + // + // Check if data is encoded. + // + ContentEncodedHeader =3D HttpFindHeader (ResponseMsg.HeaderCount, Resp= onseMsg.Headers, HTTP_HEADER_CONTENT_ENCODING); + if (ContentEncodedHeader !=3D NULL) { + // + // The content is encoded. + // + Status =3D DecodeResponseContent (ContentEncodedHeader->FieldValue, = &ResponseMsg.Body, &ResponseMsg.BodyLength); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: Failed to decompress the response conten= t %r\n.", __FUNCTION__, Status)); + ret =3D NULL; + goto ON_EXIT; + } + } + 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* getUriFromService(redfishService* service, const char* uri, EFI_HT= TP_STATUS_CODE** StatusCode) { char* url; @@ -491,7 +675,7 @@ ON_EXIT: return ret; } =20 -json_t* patchUriFromService(redfishService* service, const char* uri, cons= t char* content, EFI_HTTP_STATUS_CODE** StatusCode) +json_t* patchUriFromServiceEx(redfishService* service, const char* uri, co= nst char* content, EFI_HTTP_HEADER **Headers, UINTN *HeaderCount, EFI_HTTP_= STATUS_CODE** StatusCode) { char* url; json_t* ret; @@ -632,6 +816,10 @@ json_t* patchUriFromService(redfishService* service, c= onst char* uri, const char **StatusCode =3D ResponseMsg.Data.Response->StatusCode; } =20 + if (ResponseMsg.Headers !=3D NULL) { + *Headers =3D cloneHttpHeaders (&ResponseMsg, HeaderCount); + } + if (EncodedContent !=3D content) { FreePool (EncodedContent); } @@ -668,6 +856,368 @@ ON_EXIT: return ret; } =20 +json_t* patchUriFromService(redfishService* service, const char* uri, cons= t char* content, 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 || Stat= usCode =3D=3D NULL) + { + return NULL; + } + + *StatusCode =3D NULL; + + url =3D makeUrlForService(service, uri); + if(!url) + { + return NULL; + } + + DEBUG((DEBUG_INFO, "libredfish: patchUriFromService(): %a\n", url)); + + // + // Step 1: Create HTTP request message with 4 headers: + // + HttpIoHeader =3D HttpIoCreateHeader ((service->sessionToken || service->= basicAuthStr) ? 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); + } + + 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) strlen(content) + ); + 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 HttpMethodPatch; + 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 strlen(content); + // + // 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", __FUNCTION__)); + ret =3D NULL; + goto ON_EXIT; + } else if (Status =3D=3D EFI_UNSUPPORTED) { + DEBUG((DEBUG_INFO, "No content coding for %a! Use raw data instead.\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 (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* postUriFromServiceEx(redfishService* service, const char* uri, con= st char* content, size_t contentLength, const char* contentType, EFI_HTTP_H= EADER **Headers, UINTN *HeaderCount, EFI_HTTP_STATUS_CODE** StatusCode) +{ + char* url =3D NULL; + 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]; + EFI_HTTP_HEADER *HttpHeader =3D NULL; + + ret =3D NULL; + + if(service =3D=3D NULL || uri =3D=3D NULL || content =3D=3D NULL || Stat= usCode =3D=3D NULL) + { + return NULL; + } + + *StatusCode =3D NULL; + + url =3D makeUrlForService(service, uri); + if(!url) + { + return NULL; + } + + DEBUG((DEBUG_INFO, "libredfish: postUriFromService(): %a\n", url)); + + if(contentLength =3D=3D 0) + { + contentLength =3D strlen(content); + } + + // + // Step 1: Create HTTP request message with 4 headers: + // + HttpIoHeader =3D HttpIoCreateHeader ((service->sessionToken || service->= basicAuthStr) ? 8 : 7); + if (HttpIoHeader =3D=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 *) co= ntentType); + ASSERT_EFI_ERROR (Status); + } + Status =3D HttpIoSetHeader (HttpIoHeader, "Host", service->HostHeaderVal= ue); + 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) { + goto ON_EXIT; + } + + RequestData->Method =3D HttpMethodPost; + RequestData->Url =3D C8ToC16 (url); + + // + // Step 3: fill in EFI_HTTP_MESSAGE + // + RequestMsg =3D AllocateZeroPool (sizeof (EFI_HTTP_MESSAGE)); + if (RequestMsg =3D=3D NULL) { + goto ON_EXIT; + } + + RequestMsg->Data.Request =3D RequestData; + RequestMsg->HeaderCount =3D HttpIoHeader->HeaderCount; + RequestMsg->Headers =3D HttpIoHeader->Headers; + RequestMsg->BodyLength =3D contentLength; + RequestMsg->Body =3D (VOID*) content; + + 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)) { + 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) { + 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 cloneHttpHeaders (&ResponseMsg, HeaderCount); + } + + if (ResponseMsg.BodyLength !=3D 0 && ResponseMsg.Body !=3D NULL) { + ret =3D json_loadb (ResponseMsg.Body, ResponseMsg.BodyLength, 0, NULL); + } + + // + // Step 6: Parsing the HttpHeader to retrive the X-Auth-Token if the HTT= P StatusCode is correct. + // + if (ResponseMsg.Data.Response->StatusCode =3D=3D HTTP_STATUS_200_OK || + ResponseMsg.Data.Response->StatusCode =3D=3D HTTP_STATUS_204_NO_CONT= ENT) { + HttpHeader =3D HttpFindHeader (ResponseMsg.HeaderCount, ResponseMsg.He= aders, "X-Auth-Token"); + if (HttpHeader !=3D NULL) { + if(service->sessionToken) + { + free(service->sessionToken); + } + 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; + } + */ + } + +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* postUriFromService(redfishService* service, const char* uri, const= char* content, size_t contentLength, const char* contentType, EFI_HTTP_STA= TUS_CODE** StatusCode) { char* url =3D NULL; --=20 2.32.0.windows.2 -=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 (#91877): https://edk2.groups.io/g/devel/message/91877 Mute This Topic: https://groups.io/mt/92641651/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-