| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Linq; |
| | 6 | | using Azure.Core; |
| | 7 | | using Azure.Core.Pipeline; |
| | 8 | |
|
| | 9 | | namespace Azure.Storage |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Pipeline policy to verify x-ms-client-request-id and x-ms-client-return-request-id |
| | 13 | | /// headers that are echoed back from a request match. |
| | 14 | | /// </summary> |
| | 15 | | internal class StorageRequestValidationPipelinePolicy : HttpPipelineSynchronousPolicy |
| | 16 | | { |
| | 17 | | private readonly ClientDiagnostics _clientDiagnostics; |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Create a new StorageRequestValidationPipelinePolicy |
| | 21 | | /// </summary> |
| 4740 | 22 | | public StorageRequestValidationPipelinePolicy(ClientOptions options) |
| | 23 | | { |
| 4740 | 24 | | _clientDiagnostics = new ClientDiagnostics(options); |
| 4740 | 25 | | } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Verify x-ms-client-request-id and x-ms-client-return-request-id headers matches as |
| | 29 | | /// x-ms-client-return-request-id is an echo of x-mis-client-request-id. |
| | 30 | | /// </summary> |
| | 31 | | /// <param name="message">The message that was sent</param> |
| | 32 | | public override void OnReceivedResponse(HttpMessage message) |
| | 33 | | { |
| 21980 | 34 | | if (message.HasResponse && |
| 21980 | 35 | | message.Request.Headers.TryGetValue(Constants.HeaderNames.ClientRequestId, out var original) && |
| 21980 | 36 | | message.Response.Headers.TryGetValues(Constants.HeaderNames.ClientRequestId, out var echo) && |
| 21980 | 37 | | !String.Equals(original, echo.First(), StringComparison.OrdinalIgnoreCase)) |
| | 38 | | { |
| 0 | 39 | | throw Errors.ClientRequestIdMismatch(_clientDiagnostics, message.Response, echo.First(), original); |
| | 40 | | } |
| 21980 | 41 | | } |
| | 42 | | } |
| | 43 | | } |