| | | 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; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Collections.ObjectModel; |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.Net; |
| | | 9 | | using System.Net.Mime; |
| | | 10 | | using CoreWCF.Channels; |
| | | 11 | | using CoreWCF.Runtime; |
| | | 12 | | using Microsoft.Net.Http.Headers; |
| | | 13 | | |
| | | 14 | | namespace CoreWCF.Web |
| | | 15 | | { |
| | | 16 | | public class IncomingWebRequestContext |
| | | 17 | | { |
| | 0 | 18 | | private static readonly string s_httpGetMethod = "GET"; |
| | 0 | 19 | | private static readonly string s_httpHeadMethod = "HEAD"; |
| | 0 | 20 | | private static readonly string s_httpPutMethod = "PUT"; |
| | 0 | 21 | | private static readonly string s_httpPostMethod = "POST"; |
| | 0 | 22 | | private static readonly string s_httpDeleteMethod = "DELETE"; |
| | | 23 | | |
| | | 24 | | private Collection<ContentType> _cachedAcceptHeaderElements; |
| | | 25 | | private string _acceptHeaderWhenHeaderElementsCached; |
| | | 26 | | private readonly OperationContext _operationContext; |
| | | 27 | | |
| | | 28 | | internal const string UriTemplateMatchResultsPropertyName = "UriTemplateMatchResults"; |
| | | 29 | | |
| | 11 | 30 | | internal IncomingWebRequestContext(OperationContext operationContext) |
| | | 31 | | { |
| | | 32 | | Fx.Assert(operationContext != null, "operationContext is null"); |
| | 11 | 33 | | _operationContext = operationContext; |
| | 11 | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | public string Accept => EnsureMessageProperty().Headers[HttpRequestHeader.Accept]; |
| | | 37 | | |
| | 0 | 38 | | public long ContentLength => long.Parse(EnsureMessageProperty().Headers[HttpRequestHeader.ContentLength], Cultur |
| | | 39 | | |
| | 0 | 40 | | public string ContentType => EnsureMessageProperty().Headers[HttpRequestHeader.ContentType]; |
| | | 41 | | |
| | | 42 | | public IEnumerable<string> IfMatch |
| | | 43 | | { |
| | | 44 | | get |
| | | 45 | | { |
| | 0 | 46 | | string ifMatchHeader = MessageProperty.Headers[HttpRequestHeader.IfMatch]; |
| | 0 | 47 | | return (string.IsNullOrEmpty(ifMatchHeader)) ? null : Utility.QuoteAwareStringSplit(ifMatchHeader); |
| | | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | public IEnumerable<string> IfNoneMatch |
| | | 52 | | { |
| | | 53 | | get |
| | | 54 | | { |
| | 0 | 55 | | string ifNoneMatchHeader = MessageProperty.Headers[HttpRequestHeader.IfNoneMatch]; |
| | 0 | 56 | | return (string.IsNullOrEmpty(ifNoneMatchHeader)) ? null : Utility.QuoteAwareStringSplit(ifNoneMatchHeade |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | public DateTime? IfModifiedSince |
| | | 61 | | { |
| | | 62 | | get |
| | | 63 | | { |
| | 5 | 64 | | string dateTime = this.MessageProperty.Headers[HttpRequestHeader.IfModifiedSince]; |
| | 5 | 65 | | if (!string.IsNullOrEmpty(dateTime)) |
| | | 66 | | { |
| | 4 | 67 | | if (HeaderUtilities.TryParseDate(dateTime, out DateTimeOffset parsedDateTime)) |
| | | 68 | | { |
| | 3 | 69 | | return parsedDateTime.LocalDateTime; |
| | | 70 | | } |
| | | 71 | | } |
| | | 72 | | |
| | 2 | 73 | | return null; |
| | | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | public DateTime? IfUnmodifiedSince |
| | | 78 | | { |
| | | 79 | | get |
| | | 80 | | { |
| | 5 | 81 | | string dateTime = MessageProperty.Headers[HttpRequestHeader.IfUnmodifiedSince]; |
| | 5 | 82 | | if (!string.IsNullOrEmpty(dateTime)) |
| | | 83 | | { |
| | 4 | 84 | | if (HeaderUtilities.TryParseDate(dateTime, out DateTimeOffset parsedDateTime)) |
| | | 85 | | { |
| | 3 | 86 | | return parsedDateTime.LocalDateTime; |
| | | 87 | | } |
| | | 88 | | } |
| | | 89 | | |
| | 2 | 90 | | return null; |
| | | 91 | | } |
| | | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | public WebHeaderCollection Headers => EnsureMessageProperty().Headers; |
| | | 95 | | |
| | 0 | 96 | | public string Method => EnsureMessageProperty().Method; |
| | | 97 | | |
| | | 98 | | public UriTemplateMatch UriTemplateMatch |
| | | 99 | | { |
| | | 100 | | get |
| | | 101 | | { |
| | 1 | 102 | | if (_operationContext.IncomingMessageProperties.ContainsKey(UriTemplateMatchResultsPropertyName)) |
| | | 103 | | { |
| | 1 | 104 | | return _operationContext.IncomingMessageProperties[UriTemplateMatchResultsPropertyName] as UriTempla |
| | | 105 | | } |
| | | 106 | | else |
| | | 107 | | { |
| | 0 | 108 | | return null; |
| | | 109 | | } |
| | | 110 | | } |
| | | 111 | | set |
| | | 112 | | { |
| | 0 | 113 | | _operationContext.IncomingMessageProperties[UriTemplateMatchResultsPropertyName] = value; |
| | 0 | 114 | | } |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | public string UserAgent => EnsureMessageProperty().Headers[HttpRequestHeader.UserAgent]; |
| | | 118 | | |
| | | 119 | | private HttpRequestMessageProperty MessageProperty |
| | | 120 | | { |
| | | 121 | | get |
| | | 122 | | { |
| | 10 | 123 | | if (_operationContext.IncomingMessageProperties == null) |
| | | 124 | | { |
| | 0 | 125 | | return null; |
| | | 126 | | } |
| | | 127 | | |
| | 10 | 128 | | if (!_operationContext.IncomingMessageProperties.ContainsKey(HttpRequestMessageProperty.Name)) |
| | | 129 | | { |
| | 0 | 130 | | return null; |
| | | 131 | | } |
| | | 132 | | |
| | 10 | 133 | | return _operationContext.IncomingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessag |
| | | 134 | | } |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | public void CheckConditionalRetrieve(string entityTag) |
| | | 138 | | { |
| | 0 | 139 | | string validEtag = OutgoingWebResponseContext.GenerateValidEtagFromString(entityTag); |
| | 0 | 140 | | CheckConditionalRetrieveWithValidatedEtag(validEtag); |
| | 0 | 141 | | } |
| | | 142 | | |
| | | 143 | | public void CheckConditionalRetrieve(int entityTag) |
| | | 144 | | { |
| | 0 | 145 | | string validEtag = OutgoingWebResponseContext.GenerateValidEtag(entityTag); |
| | 0 | 146 | | CheckConditionalRetrieveWithValidatedEtag(validEtag); |
| | 0 | 147 | | } |
| | | 148 | | |
| | | 149 | | public void CheckConditionalRetrieve(long entityTag) |
| | | 150 | | { |
| | 0 | 151 | | string validEtag = OutgoingWebResponseContext.GenerateValidEtag(entityTag); |
| | 0 | 152 | | CheckConditionalRetrieveWithValidatedEtag(validEtag); |
| | 0 | 153 | | } |
| | | 154 | | |
| | | 155 | | public void CheckConditionalRetrieve(Guid entityTag) |
| | | 156 | | { |
| | 0 | 157 | | string validEtag = OutgoingWebResponseContext.GenerateValidEtag(entityTag); |
| | 0 | 158 | | CheckConditionalRetrieveWithValidatedEtag(validEtag); |
| | 0 | 159 | | } |
| | | 160 | | |
| | | 161 | | public void CheckConditionalRetrieve(DateTime lastModified) |
| | | 162 | | { |
| | 0 | 163 | | if (!string.Equals(Method, s_httpGetMethod, StringComparison.OrdinalIgnoreCase) && |
| | 0 | 164 | | !string.Equals(Method, s_httpHeadMethod, StringComparison.OrdinalIgnoreCase)) |
| | | 165 | | { |
| | 0 | 166 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 167 | | SR.Format(SR.ConditionalRetrieveGetAndHeadOnly, Method))); |
| | | 168 | | } |
| | | 169 | | |
| | 0 | 170 | | DateTime? ifModifiedSince = IfModifiedSince; |
| | 0 | 171 | | if (ifModifiedSince.HasValue) |
| | | 172 | | { |
| | 0 | 173 | | long ticksDifference = lastModified.ToUniversalTime().Ticks - ifModifiedSince.Value.ToUniversalTime().Ti |
| | 0 | 174 | | if (ticksDifference < TimeSpan.TicksPerSecond) |
| | | 175 | | { |
| | 0 | 176 | | WebOperationContext.Current.OutgoingResponse.LastModified = lastModified; |
| | 0 | 177 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new WebFaultException(HttpStatusCode.NotMo |
| | | 178 | | } |
| | | 179 | | } |
| | 0 | 180 | | } |
| | | 181 | | |
| | | 182 | | public void CheckConditionalUpdate(string entityTag) |
| | | 183 | | { |
| | 0 | 184 | | string validEtag = OutgoingWebResponseContext.GenerateValidEtagFromString(entityTag); |
| | 0 | 185 | | CheckConditionalUpdateWithValidatedEtag(validEtag); |
| | 0 | 186 | | } |
| | | 187 | | |
| | | 188 | | public void CheckConditionalUpdate(int entityTag) |
| | | 189 | | { |
| | 0 | 190 | | string validEtag = OutgoingWebResponseContext.GenerateValidEtag(entityTag); |
| | 0 | 191 | | CheckConditionalUpdateWithValidatedEtag(validEtag); |
| | 0 | 192 | | } |
| | | 193 | | |
| | | 194 | | public void CheckConditionalUpdate(long entityTag) |
| | | 195 | | { |
| | 0 | 196 | | string validEtag = OutgoingWebResponseContext.GenerateValidEtag(entityTag); |
| | 0 | 197 | | CheckConditionalUpdateWithValidatedEtag(validEtag); |
| | 0 | 198 | | } |
| | | 199 | | |
| | | 200 | | public void CheckConditionalUpdate(Guid entityTag) |
| | | 201 | | { |
| | 0 | 202 | | string validEtag = OutgoingWebResponseContext.GenerateValidEtag(entityTag); |
| | 0 | 203 | | CheckConditionalUpdateWithValidatedEtag(validEtag); |
| | 0 | 204 | | } |
| | | 205 | | |
| | | 206 | | public Collection<ContentType> GetAcceptHeaderElements() |
| | | 207 | | { |
| | 0 | 208 | | string acceptHeader = Accept; |
| | 0 | 209 | | if (_cachedAcceptHeaderElements == null || |
| | 0 | 210 | | (!string.Equals(_acceptHeaderWhenHeaderElementsCached, acceptHeader, StringComparison.OrdinalIgnoreCase) |
| | | 211 | | { |
| | 0 | 212 | | if (string.IsNullOrEmpty(acceptHeader)) |
| | | 213 | | { |
| | 0 | 214 | | _cachedAcceptHeaderElements = new Collection<ContentType>(); |
| | 0 | 215 | | _acceptHeaderWhenHeaderElementsCached = acceptHeader; |
| | | 216 | | } |
| | | 217 | | else |
| | | 218 | | { |
| | 0 | 219 | | List<ContentType> contentTypeList = new List<ContentType>(); |
| | 0 | 220 | | int offset = 0; |
| | 0 | 221 | | while (true) |
| | | 222 | | { |
| | 0 | 223 | | string nextItem = Utility.QuoteAwareSubString(acceptHeader, ref offset); |
| | 0 | 224 | | if (nextItem == null) |
| | | 225 | | { |
| | | 226 | | break; |
| | | 227 | | } |
| | | 228 | | |
| | 0 | 229 | | ContentType contentType = Utility.GetContentTypeOrNull(nextItem); |
| | 0 | 230 | | if (contentType != null) |
| | | 231 | | { |
| | 0 | 232 | | contentTypeList.Add(contentType); |
| | | 233 | | } |
| | | 234 | | } |
| | | 235 | | |
| | 0 | 236 | | contentTypeList.Sort(new AcceptHeaderElementComparer()); |
| | 0 | 237 | | _cachedAcceptHeaderElements = new Collection<ContentType>(contentTypeList); |
| | 0 | 238 | | _acceptHeaderWhenHeaderElementsCached = acceptHeader; |
| | | 239 | | } |
| | | 240 | | } |
| | | 241 | | |
| | 0 | 242 | | return _cachedAcceptHeaderElements; |
| | | 243 | | } |
| | | 244 | | |
| | | 245 | | private HttpRequestMessageProperty EnsureMessageProperty() |
| | | 246 | | { |
| | 0 | 247 | | if (MessageProperty == null) |
| | | 248 | | { |
| | 0 | 249 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 250 | | SR.Format(SR.HttpContextNoIncomingMessageProperty, typeof(HttpRequestMessageProperty).Name))); |
| | | 251 | | } |
| | | 252 | | |
| | 0 | 253 | | return MessageProperty; |
| | | 254 | | } |
| | | 255 | | |
| | | 256 | | |
| | | 257 | | private void CheckConditionalRetrieveWithValidatedEtag(string entityTag) |
| | | 258 | | { |
| | 0 | 259 | | if (!string.Equals(Method, s_httpGetMethod, StringComparison.OrdinalIgnoreCase) && |
| | 0 | 260 | | !string.Equals(Method, s_httpHeadMethod, StringComparison.OrdinalIgnoreCase)) |
| | | 261 | | { |
| | 0 | 262 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 263 | | SR.Format(SR.ConditionalRetrieveGetAndHeadOnly, Method))); |
| | | 264 | | } |
| | | 265 | | |
| | 0 | 266 | | if (!string.IsNullOrEmpty(entityTag)) |
| | | 267 | | { |
| | 0 | 268 | | string entityTagHeader = Headers[HttpRequestHeader.IfNoneMatch]; |
| | 0 | 269 | | if (!string.IsNullOrEmpty(entityTagHeader)) |
| | | 270 | | { |
| | 0 | 271 | | if (IsWildCardCharacter(entityTagHeader) || |
| | 0 | 272 | | DoesHeaderContainEtag(entityTagHeader, entityTag)) |
| | | 273 | | { |
| | | 274 | | // set response entityTag directly because it has already been validated |
| | 0 | 275 | | WebOperationContext.Current.OutgoingResponse.ETag = entityTag; |
| | 0 | 276 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new WebFaultException(HttpStatusCode.N |
| | | 277 | | } |
| | | 278 | | } |
| | | 279 | | } |
| | 0 | 280 | | } |
| | | 281 | | |
| | | 282 | | private void CheckConditionalUpdateWithValidatedEtag(string entityTag) |
| | | 283 | | { |
| | 0 | 284 | | bool isPutMethod = string.Equals(Method, s_httpPutMethod, StringComparison.OrdinalIgnoreCase); |
| | 0 | 285 | | if (!isPutMethod && |
| | 0 | 286 | | !string.Equals(Method, s_httpPostMethod, StringComparison.OrdinalIgnoreCase) && |
| | 0 | 287 | | !string.Equals(Method, s_httpDeleteMethod, StringComparison.OrdinalIgnoreCase)) |
| | | 288 | | { |
| | 0 | 289 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 290 | | SR.Format(SR.ConditionalUpdatePutPostAndDeleteOnly, Method))); |
| | | 291 | | } |
| | | 292 | | |
| | | 293 | | string headerOfInterest; |
| | | 294 | | |
| | | 295 | | // if the current entityTag is null then the resource doesn't currently exist and the |
| | | 296 | | // a PUT request should only succeed if If-None-Match equals '*'. |
| | 0 | 297 | | if (isPutMethod && string.IsNullOrEmpty(entityTag)) |
| | | 298 | | { |
| | 0 | 299 | | headerOfInterest = Headers[HttpRequestHeader.IfNoneMatch]; |
| | 0 | 300 | | if (string.IsNullOrEmpty(headerOfInterest) || |
| | 0 | 301 | | !IsWildCardCharacter(headerOfInterest)) |
| | | 302 | | { |
| | 0 | 303 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new WebFaultException(HttpStatusCode.Preco |
| | | 304 | | } |
| | | 305 | | } |
| | | 306 | | else |
| | | 307 | | { |
| | | 308 | | // all remaining cases are with an If-Match header |
| | 0 | 309 | | headerOfInterest = Headers[HttpRequestHeader.IfMatch]; |
| | 0 | 310 | | if (string.IsNullOrEmpty(headerOfInterest) || |
| | 0 | 311 | | (!IsWildCardCharacter(headerOfInterest) && |
| | 0 | 312 | | !DoesHeaderContainEtag(headerOfInterest, entityTag))) |
| | | 313 | | { |
| | | 314 | | // set response entityTag directly because it has already been validated |
| | 0 | 315 | | WebOperationContext.Current.OutgoingResponse.ETag = entityTag; |
| | 0 | 316 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new WebFaultException(HttpStatusCode.Preco |
| | | 317 | | } |
| | | 318 | | } |
| | 0 | 319 | | } |
| | | 320 | | |
| | | 321 | | private static bool DoesHeaderContainEtag(string header, string entityTag) |
| | | 322 | | { |
| | 0 | 323 | | int offset = 0; |
| | | 324 | | while (true) |
| | | 325 | | { |
| | 0 | 326 | | string nextEntityTag = Utility.QuoteAwareSubString(header, ref offset); |
| | 0 | 327 | | if (nextEntityTag == null) |
| | | 328 | | { |
| | | 329 | | break; |
| | | 330 | | } |
| | | 331 | | |
| | 0 | 332 | | if (string.Equals(nextEntityTag, entityTag, StringComparison.Ordinal)) |
| | | 333 | | { |
| | 0 | 334 | | return true; |
| | | 335 | | } |
| | | 336 | | } |
| | | 337 | | |
| | 0 | 338 | | return false; |
| | | 339 | | } |
| | | 340 | | |
| | 0 | 341 | | private static bool IsWildCardCharacter(string header) => header.Trim() == "*"; |
| | | 342 | | } |
| | | 343 | | |
| | | 344 | | internal class AcceptHeaderElementComparer : IComparer<ContentType> |
| | | 345 | | { |
| | | 346 | | private static readonly NumberStyles s_numberStyles = NumberStyles.AllowDecimalPoint; |
| | | 347 | | |
| | | 348 | | public int Compare(ContentType x, ContentType y) |
| | | 349 | | { |
| | | 350 | | string[] xTypeSubType = x.MediaType.Split('/'); |
| | | 351 | | string[] yTypeSubType = y.MediaType.Split('/'); |
| | | 352 | | |
| | | 353 | | Fx.Assert(xTypeSubType.Length == 2, "The creation of the ContentType would have failed if there wasn't a typ |
| | | 354 | | Fx.Assert(yTypeSubType.Length == 2, "The creation of the ContentType would have failed if there wasn't a typ |
| | | 355 | | |
| | | 356 | | if (string.Equals(xTypeSubType[0], yTypeSubType[0], StringComparison.OrdinalIgnoreCase)) |
| | | 357 | | { |
| | | 358 | | if (string.Equals(xTypeSubType[1], yTypeSubType[1], StringComparison.OrdinalIgnoreCase)) |
| | | 359 | | { |
| | | 360 | | // need to check the number of parameters to determine which is more specific |
| | | 361 | | bool xHasParam = HasParameters(x); |
| | | 362 | | bool yHasParam = HasParameters(y); |
| | | 363 | | if (xHasParam && !yHasParam) |
| | | 364 | | { |
| | | 365 | | return 1; |
| | | 366 | | } |
| | | 367 | | else if (!xHasParam && yHasParam) |
| | | 368 | | { |
| | | 369 | | return -1; |
| | | 370 | | } |
| | | 371 | | } |
| | | 372 | | else |
| | | 373 | | { |
| | | 374 | | if (xTypeSubType[1][0] == '*' && xTypeSubType[1].Length == 1) |
| | | 375 | | { |
| | | 376 | | return 1; |
| | | 377 | | } |
| | | 378 | | if (yTypeSubType[1][0] == '*' && yTypeSubType[1].Length == 1) |
| | | 379 | | { |
| | | 380 | | return -1; |
| | | 381 | | } |
| | | 382 | | } |
| | | 383 | | } |
| | | 384 | | else if (xTypeSubType[0][0] == '*' && xTypeSubType[0].Length == 1) |
| | | 385 | | { |
| | | 386 | | return 1; |
| | | 387 | | } |
| | | 388 | | else if (yTypeSubType[0][0] == '*' && yTypeSubType[0].Length == 1) |
| | | 389 | | { |
| | | 390 | | return -1; |
| | | 391 | | } |
| | | 392 | | |
| | | 393 | | decimal qualityDifference = GetQualityFactor(x) - GetQualityFactor(y); |
| | | 394 | | if (qualityDifference < 0) |
| | | 395 | | { |
| | | 396 | | return 1; |
| | | 397 | | } |
| | | 398 | | else if (qualityDifference > 0) |
| | | 399 | | { |
| | | 400 | | return -1; |
| | | 401 | | } |
| | | 402 | | |
| | | 403 | | return 0; |
| | | 404 | | } |
| | | 405 | | |
| | | 406 | | private decimal GetQualityFactor(ContentType contentType) |
| | | 407 | | { |
| | | 408 | | foreach (string key in contentType.Parameters.Keys) |
| | | 409 | | { |
| | | 410 | | if (string.Equals("q", key, StringComparison.OrdinalIgnoreCase)) |
| | | 411 | | { |
| | | 412 | | if (decimal.TryParse(contentType.Parameters[key], s_numberStyles, CultureInfo.InvariantCulture, out |
| | | 413 | | (result <= (decimal)1.0)) |
| | | 414 | | { |
| | | 415 | | return result; |
| | | 416 | | } |
| | | 417 | | } |
| | | 418 | | } |
| | | 419 | | |
| | | 420 | | return (decimal)1.0; |
| | | 421 | | } |
| | | 422 | | |
| | | 423 | | private bool HasParameters(ContentType contentType) |
| | | 424 | | { |
| | | 425 | | int number = 0; |
| | | 426 | | foreach (string param in contentType.Parameters.Keys) |
| | | 427 | | { |
| | | 428 | | if (!string.Equals("q", param, StringComparison.OrdinalIgnoreCase)) |
| | | 429 | | { |
| | | 430 | | number++; |
| | | 431 | | } |
| | | 432 | | } |
| | | 433 | | |
| | | 434 | | return number > 0; |
| | | 435 | | } |
| | | 436 | | } |
| | | 437 | | } |