| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Net.Http.Headers; |
| | | 8 | | using System.Text; |
| | | 9 | | using System.Threading; |
| | | 10 | | |
| | | 11 | | namespace System.Net.Http |
| | | 12 | | { |
| | | 13 | | public class HttpRequestMessage : IDisposable |
| | | 14 | | { |
| | 1 | 15 | | internal static Version DefaultRequestVersion => HttpVersion.Version11; |
| | | 16 | | internal static HttpVersionPolicy DefaultVersionPolicy => HttpVersionPolicy.RequestVersionOrLower; |
| | | 17 | | |
| | | 18 | | private const int MessageNotYetSent = 0; |
| | | 19 | | private const int MessageAlreadySent = 1; |
| | | 20 | | private const int PropagatorStateInjectedByDiagnosticsHandler = 2; |
| | | 21 | | private const int MessageDisposed = 4; |
| | | 22 | | private const int AuthDisabled = 8; |
| | | 23 | | |
| | | 24 | | // Track whether the message has been sent. |
| | | 25 | | // The message shouldn't be sent again if this field is equal to MessageAlreadySent. |
| | 1 | 26 | | private int _sendStatus = MessageNotYetSent; |
| | | 27 | | |
| | | 28 | | private HttpMethod _method; |
| | | 29 | | private Uri? _requestUri; |
| | | 30 | | private HttpRequestHeaders? _headers; |
| | | 31 | | private Version _version; |
| | | 32 | | private HttpVersionPolicy _versionPolicy; |
| | | 33 | | private HttpContent? _content; |
| | | 34 | | internal HttpRequestOptions? _options; |
| | | 35 | | |
| | | 36 | | public Version Version |
| | | 37 | | { |
| | 0 | 38 | | get { return _version; } |
| | | 39 | | set |
| | 0 | 40 | | { |
| | 0 | 41 | | ArgumentNullException.ThrowIfNull(value); |
| | 0 | 42 | | CheckDisposed(); |
| | | 43 | | |
| | 0 | 44 | | _version = value; |
| | 0 | 45 | | } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets or sets the policy determining how <see cref="Version" /> is interpreted and how is the final HTTP vers |
| | | 50 | | /// </summary> |
| | | 51 | | public HttpVersionPolicy VersionPolicy |
| | | 52 | | { |
| | 0 | 53 | | get { return _versionPolicy; } |
| | | 54 | | set |
| | 0 | 55 | | { |
| | 0 | 56 | | CheckDisposed(); |
| | | 57 | | |
| | 0 | 58 | | _versionPolicy = value; |
| | 0 | 59 | | } |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | public HttpContent? Content |
| | | 63 | | { |
| | 0 | 64 | | get { return _content; } |
| | | 65 | | set |
| | 0 | 66 | | { |
| | 0 | 67 | | CheckDisposed(); |
| | | 68 | | |
| | 0 | 69 | | if (NetEventSource.Log.IsEnabled()) |
| | 0 | 70 | | { |
| | 0 | 71 | | if (value == null) |
| | 0 | 72 | | { |
| | 0 | 73 | | NetEventSource.ContentNull(this); |
| | 0 | 74 | | } |
| | | 75 | | else |
| | 0 | 76 | | { |
| | 0 | 77 | | NetEventSource.Associate(this, value); |
| | 0 | 78 | | } |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | // It's OK to set a 'null' content, even if the method is POST/PUT. |
| | 0 | 82 | | _content = value; |
| | 0 | 83 | | } |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | public HttpMethod Method |
| | | 87 | | { |
| | 0 | 88 | | get { return _method; } |
| | | 89 | | set |
| | 0 | 90 | | { |
| | 0 | 91 | | ArgumentNullException.ThrowIfNull(value); |
| | 0 | 92 | | CheckDisposed(); |
| | | 93 | | |
| | 0 | 94 | | _method = value; |
| | 0 | 95 | | } |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | public Uri? RequestUri |
| | | 99 | | { |
| | 0 | 100 | | get { return _requestUri; } |
| | | 101 | | set |
| | 0 | 102 | | { |
| | 0 | 103 | | CheckDisposed(); |
| | 0 | 104 | | _requestUri = value; |
| | 0 | 105 | | } |
| | | 106 | | } |
| | | 107 | | |
| | 1 | 108 | | public HttpRequestHeaders Headers => _headers ??= new HttpRequestHeaders(); |
| | | 109 | | |
| | 0 | 110 | | internal bool HasHeaders => _headers != null; |
| | | 111 | | |
| | | 112 | | [Obsolete("HttpRequestMessage.Properties has been deprecated. Use Options instead.")] |
| | 0 | 113 | | public IDictionary<string, object?> Properties => Options; |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Gets the collection of options to configure the HTTP request. |
| | | 117 | | /// </summary> |
| | 0 | 118 | | public HttpRequestOptions Options => _options ??= new HttpRequestOptions(); |
| | | 119 | | |
| | | 120 | | public HttpRequestMessage() |
| | 1 | 121 | | : this(HttpMethod.Get, (Uri?)null) |
| | 1 | 122 | | { |
| | 1 | 123 | | } |
| | | 124 | | |
| | 1 | 125 | | public HttpRequestMessage(HttpMethod method, Uri? requestUri) |
| | 1 | 126 | | { |
| | 1 | 127 | | ArgumentNullException.ThrowIfNull(method); |
| | | 128 | | |
| | | 129 | | // It's OK to have a 'null' request Uri. If HttpClient is used, the 'BaseAddress' will be added. |
| | | 130 | | // If there is no 'BaseAddress', sending this request message will throw. |
| | | 131 | | // Note that we also allow the string to be empty: null and empty are considered equivalent. |
| | 1 | 132 | | _method = method; |
| | 1 | 133 | | _requestUri = requestUri; |
| | 1 | 134 | | _version = DefaultRequestVersion; |
| | 1 | 135 | | _versionPolicy = DefaultVersionPolicy; |
| | 1 | 136 | | } |
| | | 137 | | |
| | | 138 | | public HttpRequestMessage(HttpMethod method, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri) |
| | 0 | 139 | | : this(method, string.IsNullOrEmpty(requestUri) ? null : new Uri(requestUri, UriKind.RelativeOrAbsolute)) |
| | 0 | 140 | | { |
| | 0 | 141 | | } |
| | | 142 | | |
| | | 143 | | public override string ToString() |
| | 0 | 144 | | { |
| | 0 | 145 | | ValueStringBuilder sb = new ValueStringBuilder(stackalloc char[512]); |
| | | 146 | | |
| | 0 | 147 | | sb.Append("Method: "); |
| | 0 | 148 | | sb.Append(_method.ToString()); |
| | | 149 | | |
| | 0 | 150 | | sb.Append(", RequestUri: '"); |
| | 0 | 151 | | if (_requestUri is null) |
| | 0 | 152 | | { |
| | 0 | 153 | | sb.Append("<null>"); |
| | 0 | 154 | | } |
| | | 155 | | else |
| | 0 | 156 | | { |
| | 0 | 157 | | sb.AppendSpanFormattable(_requestUri); |
| | 0 | 158 | | } |
| | | 159 | | |
| | 0 | 160 | | sb.Append("', Version: "); |
| | 0 | 161 | | sb.AppendSpanFormattable(_version); |
| | | 162 | | |
| | 0 | 163 | | sb.Append(", Content: "); |
| | 0 | 164 | | sb.Append(_content == null ? "<null>" : _content.GetType().ToString()); |
| | | 165 | | |
| | 0 | 166 | | sb.Append(", Headers:"); |
| | 0 | 167 | | sb.Append(Environment.NewLine); |
| | 0 | 168 | | HeaderUtilities.DumpHeaders(ref sb, _headers, _content?.Headers); |
| | | 169 | | |
| | 0 | 170 | | return sb.ToString(); |
| | 0 | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | internal bool MarkAsSent() => Interlocked.CompareExchange(ref _sendStatus, MessageAlreadySent, MessageNotYetSent |
| | | 174 | | |
| | 0 | 175 | | internal bool WasSentByHttpClient() => (_sendStatus & MessageAlreadySent) != 0; |
| | | 176 | | |
| | 0 | 177 | | internal void MarkPropagatorStateInjectedByDiagnosticsHandler() => _sendStatus |= PropagatorStateInjectedByDiagn |
| | | 178 | | |
| | 0 | 179 | | internal bool WasPropagatorStateInjectedByDiagnosticsHandler() => (_sendStatus & PropagatorStateInjectedByDiagno |
| | | 180 | | |
| | 0 | 181 | | internal void DisableAuth() => _sendStatus |= AuthDisabled; |
| | | 182 | | |
| | 0 | 183 | | internal bool IsAuthDisabled() => (_sendStatus & AuthDisabled) != 0; |
| | | 184 | | |
| | | 185 | | private bool Disposed |
| | | 186 | | { |
| | 0 | 187 | | get => (_sendStatus & MessageDisposed) != 0; |
| | | 188 | | set |
| | 0 | 189 | | { |
| | 0 | 190 | | Debug.Assert(value); |
| | 0 | 191 | | _sendStatus |= MessageDisposed; |
| | 0 | 192 | | } |
| | | 193 | | } |
| | | 194 | | |
| | 0 | 195 | | internal bool IsExtendedConnectRequest => Method == HttpMethod.Connect && _headers?.Protocol != null; |
| | | 196 | | |
| | | 197 | | #region IDisposable Members |
| | | 198 | | |
| | | 199 | | protected virtual void Dispose(bool disposing) |
| | 0 | 200 | | { |
| | | 201 | | // The reason for this type to implement IDisposable is that it contains instances of types that implement |
| | | 202 | | // IDisposable (content). |
| | 0 | 203 | | if (disposing && !Disposed) |
| | 0 | 204 | | { |
| | 0 | 205 | | Disposed = true; |
| | 0 | 206 | | _content?.Dispose(); |
| | 0 | 207 | | } |
| | 0 | 208 | | } |
| | | 209 | | |
| | | 210 | | public void Dispose() |
| | 0 | 211 | | { |
| | 0 | 212 | | Dispose(true); |
| | 0 | 213 | | GC.SuppressFinalize(this); |
| | 0 | 214 | | } |
| | | 215 | | |
| | | 216 | | #endregion |
| | | 217 | | |
| | | 218 | | private void CheckDisposed() |
| | 0 | 219 | | { |
| | 0 | 220 | | ObjectDisposedException.ThrowIf(Disposed, this); |
| | 0 | 221 | | } |
| | | 222 | | } |
| | | 223 | | } |