| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.IO; |
| | 7 | | using System.Threading; |
| | 8 | | using Azure.Core.Pipeline; |
| | 9 | |
|
| | 10 | | namespace Azure.Core |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Represents a context flowing through the <see cref="HttpPipeline"/>. |
| | 14 | | /// </summary> |
| | 15 | | public sealed class HttpMessage: IDisposable |
| | 16 | | { |
| | 17 | | private Dictionary<string, object>? _properties; |
| | 18 | |
|
| | 19 | | private Response? _response; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Creates a new instance of <see cref="HttpMessage"/>. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="request">The request.</param> |
| | 25 | | /// <param name="responseClassifier">The response classifier.</param> |
| 3692 | 26 | | public HttpMessage(Request request, ResponseClassifier responseClassifier) |
| | 27 | | { |
| 3692 | 28 | | Request = request; |
| 3692 | 29 | | ResponseClassifier = responseClassifier; |
| 3692 | 30 | | BufferResponse = true; |
| 3692 | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Gets the <see cref="Request"/> associated with this message. |
| | 35 | | /// </summary> |
| 22998 | 36 | | public Request Request { get; } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Gets the <see cref="Response"/> associated with this message. Throws an exception if it wasn't set yet. |
| | 40 | | /// To avoid the exception use <see cref="HasResponse"/> property to check. |
| | 41 | | /// </summary> |
| | 42 | | public Response Response |
| | 43 | | { |
| | 44 | | get |
| | 45 | | { |
| 21890 | 46 | | if (_response == null) |
| | 47 | | { |
| | 48 | | #pragma warning disable CA1065 // Do not raise exceptions in unexpected locations |
| 0 | 49 | | throw new InvalidOperationException("Response was not set, make sure SendAsync was called"); |
| | 50 | | #pragma warning restore CA1065 // Do not raise exceptions in unexpected locations |
| | 51 | | } |
| 21890 | 52 | | return _response; |
| | 53 | | } |
| 4194 | 54 | | set => _response = value; |
| | 55 | | } |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Gets the value indicating if the response is set on this message. |
| | 59 | | /// </summary> |
| 640 | 60 | | public bool HasResponse => _response != null; |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// The <see cref="System.Threading.CancellationToken"/> to be used during the <see cref="HttpMessage"/> process |
| | 64 | | /// </summary> |
| 18846 | 65 | | public CancellationToken CancellationToken { get; internal set; } |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// The <see cref="ResponseClassifier"/> instance to use for response classification during pipeline invocation. |
| | 69 | | /// </summary> |
| 5218 | 70 | | public ResponseClassifier ResponseClassifier { get; } |
| | 71 | |
|
| | 72 | | /// <summary> |
| | 73 | | /// Gets or sets the value indicating if response would be buffered as part of the pipeline. Defaults to true. |
| | 74 | | /// </summary> |
| 8208 | 75 | | public bool BufferResponse { get; set; } |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Gets a property that modifies the pipeline behavior. Please refer to individual policies documentation on wh |
| | 79 | | /// </summary> |
| | 80 | | /// <param name="name">The property name.</param> |
| | 81 | | /// <param name="value">The property value.</param> |
| | 82 | | /// <returns><c>true</c> if property exists, otherwise. <c>false</c>.</returns> |
| | 83 | | public bool TryGetProperty(string name, out object? value) |
| | 84 | | { |
| 6 | 85 | | value = null; |
| 6 | 86 | | return _properties?.TryGetValue(name, out value) == true; |
| | 87 | | } |
| | 88 | |
|
| | 89 | | /// <summary> |
| | 90 | | /// Sets a property that modifies the pipeline behavior. Please refer to individual policies documentation on wh |
| | 91 | | /// </summary> |
| | 92 | | /// <param name="name">The property name.</param> |
| | 93 | | /// <param name="value">The property value.</param> |
| | 94 | | public void SetProperty(string name, object value) |
| | 95 | | { |
| 4 | 96 | | _properties ??= new Dictionary<string, object>(); |
| | 97 | |
|
| 4 | 98 | | _properties[name] = value; |
| 4 | 99 | | } |
| | 100 | |
|
| | 101 | | /// <summary> |
| | 102 | | /// Returns the response content stream and releases it ownership to the caller. After calling this methods usin |
| | 103 | | /// </summary> |
| | 104 | | /// <returns>The content stream or null if response didn't have any.</returns> |
| | 105 | | public Stream? ExtractResponseContent() |
| | 106 | | { |
| 808 | 107 | | switch (_response?.ContentStream) |
| | 108 | | { |
| | 109 | | case ResponseShouldNotBeUsedStream responseContent: |
| 2 | 110 | | return responseContent.Original; |
| | 111 | | case Stream stream : |
| 804 | 112 | | _response.ContentStream = new ResponseShouldNotBeUsedStream(_response.ContentStream); |
| 804 | 113 | | return stream; |
| | 114 | | default: |
| 2 | 115 | | return null; |
| | 116 | | } |
| | 117 | | } |
| | 118 | |
|
| | 119 | | /// <summary> |
| | 120 | | /// Disposes the request and response. |
| | 121 | | /// </summary> |
| | 122 | | public void Dispose() |
| | 123 | | { |
| 1224 | 124 | | Request?.Dispose(); |
| 1224 | 125 | | _response?.Dispose(); |
| 1218 | 126 | | } |
| | 127 | |
|
| | 128 | | private class ResponseShouldNotBeUsedStream: Stream |
| | 129 | | { |
| 2 | 130 | | public Stream Original { get; } |
| | 131 | |
|
| 804 | 132 | | public ResponseShouldNotBeUsedStream(Stream original) |
| | 133 | | { |
| 804 | 134 | | Original = original; |
| 804 | 135 | | } |
| | 136 | |
|
| | 137 | | private static Exception CreateException() |
| | 138 | | { |
| 2 | 139 | | return new InvalidOperationException("The operation has called ExtractResponseContent and will provide t |
| | 140 | | } |
| | 141 | |
|
| | 142 | | public override void Flush() |
| | 143 | | { |
| 0 | 144 | | throw CreateException(); |
| | 145 | | } |
| | 146 | |
|
| | 147 | | public override int Read(byte[] buffer, int offset, int count) |
| | 148 | | { |
| 2 | 149 | | throw CreateException(); |
| | 150 | | } |
| | 151 | |
|
| | 152 | | public override long Seek(long offset, SeekOrigin origin) |
| | 153 | | { |
| 0 | 154 | | throw CreateException(); |
| | 155 | | } |
| | 156 | |
|
| | 157 | | public override void SetLength(long value) |
| | 158 | | { |
| 0 | 159 | | throw CreateException(); |
| | 160 | | } |
| | 161 | |
|
| | 162 | | public override void Write(byte[] buffer, int offset, int count) |
| | 163 | | { |
| 0 | 164 | | throw CreateException(); |
| | 165 | | } |
| | 166 | |
|
| 0 | 167 | | public override bool CanRead => throw CreateException(); |
| 0 | 168 | | public override bool CanSeek => throw CreateException(); |
| 0 | 169 | | public override bool CanWrite => throw CreateException(); |
| 0 | 170 | | public override long Length => throw CreateException(); |
| | 171 | |
|
| | 172 | | public override long Position |
| | 173 | | { |
| 0 | 174 | | get => throw CreateException(); |
| 0 | 175 | | set => throw CreateException(); |
| | 176 | | } |
| | 177 | | } |
| | 178 | | } |
| | 179 | | } |