| | 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.Linq; |
| | 8 | | using System.Text; |
| | 9 | | using Azure.Core; |
| | 10 | |
|
| | 11 | | namespace Azure.Storage.Blobs.Specialized |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// A Response that can be constructed in memory without being tied to a |
| | 15 | | /// live request. |
| | 16 | | /// </summary> |
| | 17 | | internal class MemoryResponse : Response |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// The Response <see cref="Status"/>. |
| | 21 | | /// </summary> |
| | 22 | | private int _status = BatchConstants.NoStatusCode; |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// The Response <see cref="ReasonPhrase"/>. |
| | 26 | | /// </summary> |
| | 27 | | private string _reasonPhrase = null; |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// The <see cref="Response.Headers"/>. |
| | 31 | | /// </summary> |
| 3576 | 32 | | private readonly IDictionary<string, List<string>> _headers = |
| 3576 | 33 | | new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase); |
| | 34 | |
|
| | 35 | | /// <inheritdoc /> |
| 3728 | 36 | | public override int Status => _status; |
| | 37 | |
|
| | 38 | | /// <inheritdoc /> |
| 104 | 39 | | public override string ReasonPhrase => _reasonPhrase; |
| | 40 | |
|
| | 41 | | /// <inheritdoc /> |
| 3792 | 42 | | public override Stream ContentStream { get; set; } |
| | 43 | |
|
| | 44 | | /// <inheritdoc /> |
| | 45 | | public override string ClientRequestId |
| | 46 | | { |
| 0 | 47 | | get => TryGetHeader(BatchConstants.XmsClientRequestIdName, out string id) ? id : null; |
| 0 | 48 | | set => SetHeader(BatchConstants.XmsClientRequestIdName, value); |
| | 49 | | } |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Set the Response <see cref="Status"/>. |
| | 53 | | /// </summary> |
| | 54 | | /// <param name="status">The Response status.</param> |
| 1272 | 55 | | public void SetStatus(int status) => _status = status; |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Set the Response <see cref="ReasonPhrase"/>. |
| | 59 | | /// </summary> |
| | 60 | | /// <param name="reasonPhrase">The Response ReasonPhrase.</param> |
| 1272 | 61 | | public void SetReasonPhrase(string reasonPhrase) => _reasonPhrase = reasonPhrase; |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Set the Response <see cref="ContentStream"/>. |
| | 65 | | /// </summary> |
| | 66 | | /// <param name="content">The response content.</param> |
| 0 | 67 | | public void SetContent(byte[] content) => ContentStream = new MemoryStream(content); |
| | 68 | |
|
| | 69 | | /// <summary> |
| | 70 | | /// Set the Response <see cref="ContentStream"/>. |
| | 71 | | /// </summary> |
| | 72 | | /// <param name="content">The response content.</param> |
| 0 | 73 | | public void SetContent(string content) => SetContent(Encoding.UTF8.GetBytes(content)); |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// Dispose the Response. |
| | 77 | | /// </summary> |
| 164 | 78 | | public override void Dispose() => ContentStream?.Dispose(); |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// Set the value of a response header (and overwrite any existing |
| | 82 | | /// values). |
| | 83 | | /// </summary> |
| | 84 | | /// <param name="name">The name of the response header.</param> |
| | 85 | | /// <param name="value">The response header value.</param> |
| | 86 | | public void SetHeader(string name, string value) => |
| 0 | 87 | | SetHeader(name, new List<string> { value }); |
| | 88 | |
|
| | 89 | | /// <summary> |
| | 90 | | /// Set the values of a response header (and overwrite any existing |
| | 91 | | /// values). |
| | 92 | | /// </summary> |
| | 93 | | /// <param name="name">The name of the response header.</param> |
| | 94 | | /// <param name="values">The response header values.</param> |
| | 95 | | public void SetHeader(string name, IEnumerable<string> values) => |
| 0 | 96 | | _headers[name] = values.ToList(); |
| | 97 | |
|
| | 98 | | /// <summary> |
| | 99 | | /// Add a response header value. |
| | 100 | | /// </summary> |
| | 101 | | /// <param name="name">The name of the response header.</param> |
| | 102 | | /// <param name="value">The response header value.</param> |
| | 103 | | public void AddHeader(string name, string value) |
| | 104 | | { |
| 5192 | 105 | | if (!_headers.TryGetValue(name, out List<string> values)) |
| | 106 | | { |
| 5192 | 107 | | _headers[name] = values = new List<string>(); |
| | 108 | | } |
| 5192 | 109 | | values.Add(value); |
| 5192 | 110 | | } |
| | 111 | |
|
| | 112 | | /// <inheritdoc /> |
| | 113 | | protected override bool ContainsHeader(string name) => |
| 0 | 114 | | _headers.ContainsKey(name); |
| | 115 | |
|
| | 116 | | /// <inheritdoc /> |
| | 117 | | protected override IEnumerable<HttpHeader> EnumerateHeaders() => |
| 368 | 118 | | _headers.Select(header => new HttpHeader(header.Key, JoinHeaderValues(header.Value))); |
| | 119 | |
|
| | 120 | | /// <inheritdoc /> |
| | 121 | | protected override bool TryGetHeader(string name, out string value) |
| | 122 | | { |
| 0 | 123 | | if (_headers.TryGetValue(name, out List<string> headers)) |
| | 124 | | { |
| 0 | 125 | | value = JoinHeaderValues(headers); |
| 0 | 126 | | return true; |
| | 127 | | } |
| 0 | 128 | | value = null; |
| 0 | 129 | | return false; |
| | 130 | | } |
| | 131 | |
|
| | 132 | | /// <inheritdoc /> |
| | 133 | | protected override bool TryGetHeaderValues(string name, out IEnumerable<string> values) |
| | 134 | | { |
| 0 | 135 | | bool found = _headers.TryGetValue(name, out List<string> headers); |
| 0 | 136 | | values = headers; |
| 0 | 137 | | return found; |
| | 138 | | } |
| | 139 | |
|
| | 140 | | /// <summary> |
| | 141 | | /// Join multiple header values together with a comma. |
| | 142 | | /// </summary> |
| | 143 | | /// <param name="values">The header values.</param> |
| | 144 | | /// <returns>A single joined value.</returns> |
| | 145 | | private static string JoinHeaderValues(IEnumerable<string> values) => |
| 316 | 146 | | string.Join(",", values); |
| | 147 | | } |
| | 148 | | } |