RedfishRestExImpl.c 5.8 KB

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