]> git.proxmox.com Git - mirror_edk2.git/blob - RedfishPkg/RedfishRestExDxe/RedfishRestExImpl.c
Maintainers.txt: Update email address
[mirror_edk2.git] / RedfishPkg / RedfishRestExDxe / RedfishRestExImpl.c
1 /** @file
2 RestExDxe support functions implementation.
3
4 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10 #include <Uefi.h>
11 #include "RedfishRestExInternal.h"
12
13 /**
14 Create a new TLS session becuase the previous on is closed.
15 status.
16
17 @param[in] Instance Pointer to EFI_REST_EX_PROTOCOL instance for a particular
18 REST service.
19 @retval EFI_SUCCESS operation succeeded.
20 @retval EFI_ERROR Other errors.
21
22 **/
23 EFI_STATUS
24 ResetHttpTslSession (
25 IN RESTEX_INSTANCE *Instance
26 )
27 {
28 EFI_STATUS Status;
29
30 DEBUG ((DEBUG_INFO, "%a: TCP connection is finished. Could be TSL session closure, reset HTTP instance for the new TLS session.\n", __FUNCTION__));
31
32 Status = Instance->HttpIo.Http->Configure (Instance->HttpIo.Http, NULL);
33 if (EFI_ERROR (Status)) {
34 DEBUG ((DEBUG_ERROR, "%a: Error to reset HTTP instance.\n", __FUNCTION__));
35 return Status;
36 }
37
38 Status = Instance->HttpIo.Http->Configure (Instance->HttpIo.Http, &((EFI_REST_EX_HTTP_CONFIG_DATA *)Instance->ConfigData)->HttpConfigData);
39 if (EFI_ERROR (Status)) {
40 DEBUG ((DEBUG_ERROR, "%a: Error to re-initiate HTTP instance.\n", __FUNCTION__));
41 }
42
43 return Status;
44 }
45
46 /**
47 This function check Http receive status.
48
49 @param[in] Instance Pointer to EFI_REST_EX_PROTOCOL instance for a particular
50 REST service.
51 @param[in] HttpIoReceiveStatus This is the status return from HttpIoRecvResponse
52
53 @retval EFI_SUCCESS The payload receive from Redfish service in successfully.
54 @retval EFI_NOT_READY May need to resend the HTTP request.
55 @retval EFI_DEVICE_ERROR Something wrong and can't be resolved.
56 @retval Others Other errors as indicated.
57
58 **/
59 EFI_STATUS
60 RedfishCheckHttpReceiveStatus (
61 IN RESTEX_INSTANCE *Instance,
62 IN EFI_STATUS HttpIoReceiveStatus
63 )
64 {
65 EFI_STATUS Status;
66 EFI_STATUS ReturnStatus;
67
68 if (!EFI_ERROR (HttpIoReceiveStatus)) {
69 ReturnStatus = EFI_SUCCESS;
70 } else if (HttpIoReceiveStatus != EFI_CONNECTION_FIN) {
71 if ((Instance->Flags & RESTEX_INSTANCE_FLAGS_TCP_ERROR_RETRY) == 0) {
72 DEBUG ((DEBUG_ERROR, "%a: TCP error, reset HTTP session.\n", __FUNCTION__));
73 Instance->Flags |= RESTEX_INSTANCE_FLAGS_TCP_ERROR_RETRY;
74 gBS->Stall (500);
75 Status = ResetHttpTslSession (Instance);
76 if (!EFI_ERROR (Status)) {
77 return EFI_NOT_READY;
78 }
79
80 DEBUG ((DEBUG_ERROR, "%a: Reset HTTP instance fail.\n", __FUNCTION__));
81 }
82
83 ReturnStatus = EFI_DEVICE_ERROR;
84 } else {
85 if ((Instance->Flags & RESTEX_INSTANCE_FLAGS_TLS_RETRY) != 0) {
86 DEBUG ((DEBUG_ERROR, "%a: REST_EX Send and receive fail even with a new TLS session.\n", __FUNCTION__));
87 ReturnStatus = EFI_DEVICE_ERROR;
88 }
89
90 Instance->Flags |= RESTEX_INSTANCE_FLAGS_TLS_RETRY;
91 Status = ResetHttpTslSession (Instance);
92 if (EFI_ERROR (Status)) {
93 DEBUG ((DEBUG_ERROR, "%a: Reset HTTP instance fail.\n", __FUNCTION__));
94 ReturnStatus = EFI_DEVICE_ERROR;
95 }
96
97 return EFI_NOT_READY;
98 }
99
100 //
101 // Clean TLS new session retry and error try flags.
102 //
103 Instance->Flags &= ~(RESTEX_INSTANCE_FLAGS_TLS_RETRY | RESTEX_INSTANCE_FLAGS_TCP_ERROR_RETRY);
104 return ReturnStatus;
105 }
106
107 /**
108 This function send the HTTP request without body to see
109 if the write to URL is permitted by Redfish service. This function
110 checks if the HTTP request has Content-length in HTTP header. If yes,
111 set HTTP body to NULL and then send to service. Check the HTTP status
112 for the firther actions.
113
114 @param[in] This Pointer to EFI_REST_EX_PROTOCOL instance for a particular
115 REST service.
116 @param[in] RequestMessage Pointer to the HTTP request data for this resource
117 @param[in] PreservedRequestHeaders The pointer to save the request headers
118 @param[in] ItsWrite This is write method to URL.
119
120 @retval EFI_INVALID_PARAMETER Improper given parameters.
121 @retval EFI_SUCCESS This HTTP request is free to send to Redfish service.
122 @retval EFI_OUT_OF_RESOURCES NOt enough memory to process.
123 @retval EFI_ACCESS_DENIED Not allowed to write to this URL.
124
125 @retval Others Other errors as indicated.
126
127 **/
128 EFI_STATUS
129 RedfishHttpAddExpectation (
130 IN EFI_REST_EX_PROTOCOL *This,
131 IN EFI_HTTP_MESSAGE *RequestMessage,
132 IN EFI_HTTP_HEADER **PreservedRequestHeaders,
133 IN BOOLEAN *ItsWrite
134 )
135 {
136 EFI_HTTP_HEADER *NewHeaders;
137
138 if ((This == NULL) || (RequestMessage == NULL)) {
139 return EFI_INVALID_PARAMETER;
140 }
141
142 *ItsWrite = FALSE;
143 if (PreservedRequestHeaders != NULL) {
144 *PreservedRequestHeaders = RequestMessage->Headers;
145 }
146
147 if ((RequestMessage->Data.Request->Method != HttpMethodPut) && (RequestMessage->Data.Request->Method != HttpMethodPost) &&
148 (RequestMessage->Data.Request->Method != HttpMethodPatch))
149 {
150 return EFI_SUCCESS;
151 }
152
153 *ItsWrite = TRUE;
154
155 NewHeaders = AllocateZeroPool ((RequestMessage->HeaderCount + 1) * sizeof (EFI_HTTP_HEADER));
156 CopyMem ((VOID *)NewHeaders, (VOID *)RequestMessage->Headers, RequestMessage->HeaderCount * sizeof (EFI_HTTP_HEADER));
157 HttpSetFieldNameAndValue (NewHeaders + RequestMessage->HeaderCount, HTTP_HEADER_EXPECT, HTTP_EXPECT_100_CONTINUE);
158 RequestMessage->HeaderCount++;
159 RequestMessage->Headers = NewHeaders;
160 return EFI_SUCCESS;
161 }