| | 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> |
| 336 | 22 | | public StorageRequestValidationPipelinePolicy(ClientOptions options) |
| | 23 | | { |
| 336 | 24 | | _clientDiagnostics = new ClientDiagnostics(options); |
| 336 | 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 | | { |
| 806 | 34 | | if (message.HasResponse && |
| 806 | 35 | | message.Request.Headers.TryGetValue(Constants.HeaderNames.ClientRequestId, out var original) && |
| 806 | 36 | | message.Response.Headers.TryGetValues(Constants.HeaderNames.ClientRequestId, out var echo) && |
| 806 | 37 | | !String.Equals(original, echo.First(), StringComparison.OrdinalIgnoreCase)) |
| | 38 | | { |
| 0 | 39 | | throw Errors.ClientRequestIdMismatch(_clientDiagnostics, message.Response, echo.First(), original); |
| | 40 | | } |
| 806 | 41 | | } |
| | 42 | | } |
| | 43 | | } |