| | | 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.Globalization; |
| | | 7 | | using System.Net; |
| | | 8 | | using System.Text; |
| | | 9 | | using CoreWCF.Channels; |
| | | 10 | | using CoreWCF.Description; |
| | | 11 | | using CoreWCF.Runtime; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF.Web |
| | | 14 | | { |
| | | 15 | | public class OutgoingWebResponseContext |
| | | 16 | | { |
| | 1 | 17 | | internal static readonly string s_webResponseFormatPropertyName = "WebResponseFormatProperty"; |
| | 1 | 18 | | internal static readonly string s_automatedFormatSelectionContentTypePropertyName = "AutomatedFormatSelectionCon |
| | | 19 | | |
| | | 20 | | private Encoding _bindingWriteEncoding = null; |
| | | 21 | | private readonly OperationContext _operationContext; |
| | | 22 | | |
| | 41 | 23 | | internal OutgoingWebResponseContext(OperationContext operationContext) |
| | | 24 | | { |
| | | 25 | | Fx.Assert(operationContext != null, "operationContext is null"); |
| | | 26 | | |
| | 41 | 27 | | _operationContext = operationContext; |
| | 41 | 28 | | } |
| | | 29 | | |
| | | 30 | | public long ContentLength |
| | | 31 | | { |
| | 0 | 32 | | get { return long.Parse(MessageProperty.Headers[HttpResponseHeader.ContentLength], CultureInfo.InvariantCult |
| | 0 | 33 | | set { MessageProperty.Headers[HttpResponseHeader.ContentLength] = value.ToString(CultureInfo.InvariantCultur |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | public string ContentType |
| | | 37 | | { |
| | 30 | 38 | | get { return MessageProperty.Headers[HttpResponseHeader.ContentType]; } |
| | 50 | 39 | | set { MessageProperty.Headers[HttpResponseHeader.ContentType] = value; } |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | public string ETag |
| | | 43 | | { |
| | 0 | 44 | | get { return MessageProperty.Headers[HttpResponseHeader.ETag]; } |
| | 0 | 45 | | set { MessageProperty.Headers[HttpResponseHeader.ETag] = value; } |
| | | 46 | | } |
| | | 47 | | |
| | 1 | 48 | | public WebHeaderCollection Headers => MessageProperty.Headers; |
| | | 49 | | |
| | | 50 | | public DateTime LastModified |
| | | 51 | | { |
| | | 52 | | get |
| | | 53 | | { |
| | 0 | 54 | | string dateTime = MessageProperty.Headers[HttpRequestHeader.LastModified]; |
| | 0 | 55 | | if (!string.IsNullOrEmpty(dateTime)) |
| | | 56 | | { |
| | 0 | 57 | | if (DateTime.TryParse(dateTime, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime pars |
| | | 58 | | { |
| | 0 | 59 | | return parsedDateTime; |
| | | 60 | | } |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | return DateTime.MinValue; |
| | | 64 | | } |
| | | 65 | | set |
| | | 66 | | { |
| | 0 | 67 | | MessageProperty.Headers[HttpResponseHeader.LastModified] = |
| | 0 | 68 | | (value.Kind == DateTimeKind.Utc ? |
| | 0 | 69 | | value.ToString("R", CultureInfo.InvariantCulture) : |
| | 0 | 70 | | value.ToUniversalTime().ToString("R", CultureInfo.InvariantCulture)); |
| | 0 | 71 | | } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | public string Location |
| | | 75 | | { |
| | 0 | 76 | | get { return MessageProperty.Headers[HttpResponseHeader.Location]; } |
| | 0 | 77 | | set { MessageProperty.Headers[HttpResponseHeader.Location] = value; } |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | public HttpStatusCode StatusCode |
| | | 81 | | { |
| | 0 | 82 | | get { return MessageProperty.StatusCode; } |
| | 2 | 83 | | set { MessageProperty.StatusCode = value; } |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | public string StatusDescription |
| | | 87 | | { |
| | 0 | 88 | | get { return MessageProperty.StatusDescription; } |
| | 0 | 89 | | set { MessageProperty.StatusDescription = value; } |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | public bool SuppressEntityBody |
| | | 93 | | { |
| | 0 | 94 | | get { return MessageProperty.SuppressEntityBody; } |
| | 8 | 95 | | set { MessageProperty.SuppressEntityBody = value; } |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | public WebMessageFormat? Format |
| | | 99 | | { |
| | | 100 | | get |
| | | 101 | | { |
| | 30 | 102 | | if (!_operationContext.OutgoingMessageProperties.ContainsKey(s_webResponseFormatPropertyName)) |
| | | 103 | | { |
| | 30 | 104 | | return null; |
| | | 105 | | } |
| | | 106 | | |
| | 0 | 107 | | return _operationContext.OutgoingMessageProperties[s_webResponseFormatPropertyName] as WebMessageFormat? |
| | | 108 | | } |
| | | 109 | | set |
| | | 110 | | { |
| | 0 | 111 | | if (value.HasValue) |
| | | 112 | | { |
| | 0 | 113 | | if (!WebMessageFormatHelper.IsDefined(value.Value)) |
| | | 114 | | { |
| | 0 | 115 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof |
| | | 116 | | } |
| | | 117 | | else |
| | | 118 | | { |
| | 0 | 119 | | _operationContext.OutgoingMessageProperties[s_webResponseFormatPropertyName] = value.Value; |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | else |
| | | 123 | | { |
| | 0 | 124 | | _operationContext.OutgoingMessageProperties[s_webResponseFormatPropertyName] = null; |
| | | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | AutomatedFormatSelectionContentType = null; |
| | 0 | 128 | | } |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | // This is an internal property because we need to carry the content-type that was selected by the FormatSelecti |
| | | 132 | | // forward so that the formatter has access to it. However, we dond't want to use the ContentType property on th |
| | | 133 | | // developers would have to clear the ContentType property manually when overriding the format set by the |
| | | 134 | | // FormatSelectingMessageInspector |
| | | 135 | | internal string AutomatedFormatSelectionContentType |
| | | 136 | | { |
| | | 137 | | get |
| | | 138 | | { |
| | 29 | 139 | | if (!_operationContext.OutgoingMessageProperties.ContainsKey(s_automatedFormatSelectionContentTypeProper |
| | | 140 | | { |
| | 29 | 141 | | return null; |
| | | 142 | | } |
| | 0 | 143 | | return _operationContext.OutgoingMessageProperties[s_automatedFormatSelectionContentTypePropertyName] as |
| | | 144 | | } |
| | | 145 | | set |
| | | 146 | | { |
| | 0 | 147 | | _operationContext.OutgoingMessageProperties[s_automatedFormatSelectionContentTypePropertyName] = value; |
| | 0 | 148 | | } |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | public Encoding BindingWriteEncoding |
| | | 152 | | { |
| | | 153 | | get |
| | | 154 | | { |
| | 0 | 155 | | if (_bindingWriteEncoding == null) |
| | | 156 | | { |
| | 0 | 157 | | string endpointId = _operationContext.EndpointDispatcher.Id; |
| | | 158 | | Fx.Assert(endpointId != null, "There should always be an valid EndpointDispatcher.Id"); |
| | 0 | 159 | | foreach (ServiceEndpoint endpoint in _operationContext.Host.Description.Endpoints) |
| | | 160 | | { |
| | 0 | 161 | | if (endpoint.Id == endpointId) |
| | | 162 | | { |
| | 0 | 163 | | if (endpoint.Binding.CreateBindingElements().Find<WebMessageEncodingBindingElement>() is Web |
| | | 164 | | { |
| | 0 | 165 | | _bindingWriteEncoding = encodingElement.WriteEncoding; |
| | | 166 | | } |
| | | 167 | | } |
| | | 168 | | } |
| | | 169 | | } |
| | | 170 | | |
| | 0 | 171 | | return _bindingWriteEncoding; |
| | | 172 | | } |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | internal HttpResponseMessageProperty MessageProperty |
| | | 176 | | { |
| | | 177 | | get |
| | | 178 | | { |
| | 65 | 179 | | if (!_operationContext.OutgoingMessageProperties.ContainsKey(HttpResponseMessageProperty.Name)) |
| | | 180 | | { |
| | 34 | 181 | | _operationContext.OutgoingMessageProperties.Add(HttpResponseMessageProperty.Name, new HttpResponseMe |
| | | 182 | | } |
| | | 183 | | |
| | 65 | 184 | | return _operationContext.OutgoingMessageProperties[HttpResponseMessageProperty.Name] as HttpResponseMess |
| | | 185 | | } |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | public void SetETag(string entityTag) |
| | | 189 | | { |
| | 0 | 190 | | ETag = GenerateValidEtagFromString(entityTag); |
| | 0 | 191 | | } |
| | | 192 | | |
| | | 193 | | public void SetETag(int entityTag) |
| | | 194 | | { |
| | 0 | 195 | | ETag = GenerateValidEtag(entityTag); |
| | 0 | 196 | | } |
| | | 197 | | |
| | | 198 | | public void SetETag(long entityTag) |
| | | 199 | | { |
| | 0 | 200 | | ETag = GenerateValidEtag(entityTag); |
| | 0 | 201 | | } |
| | | 202 | | |
| | | 203 | | public void SetETag(Guid entityTag) |
| | | 204 | | { |
| | 0 | 205 | | ETag = GenerateValidEtag(entityTag); |
| | 0 | 206 | | } |
| | | 207 | | |
| | | 208 | | public void SetStatusAsCreated(Uri locationUri) |
| | | 209 | | { |
| | 0 | 210 | | if (locationUri == null) |
| | | 211 | | { |
| | 0 | 212 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(locationUri)); |
| | | 213 | | } |
| | | 214 | | |
| | 0 | 215 | | StatusCode = HttpStatusCode.Created; |
| | 0 | 216 | | Location = locationUri.ToString(); |
| | 0 | 217 | | } |
| | | 218 | | |
| | | 219 | | public void SetStatusAsNotFound() |
| | | 220 | | { |
| | 0 | 221 | | StatusCode = HttpStatusCode.NotFound; |
| | 0 | 222 | | } |
| | | 223 | | |
| | | 224 | | public void SetStatusAsNotFound(string description) |
| | | 225 | | { |
| | 0 | 226 | | StatusCode = HttpStatusCode.NotFound; |
| | 0 | 227 | | StatusDescription = description; |
| | 0 | 228 | | } |
| | | 229 | | |
| | | 230 | | internal static string GenerateValidEtagFromString(string entityTag) |
| | | 231 | | { |
| | | 232 | | // This method will generate a valid entityTag from a string by doing the following: |
| | | 233 | | // 1) Adding surrounding double quotes if the string doesn't already start and end with them |
| | | 234 | | // 2) Escaping any internal double quotes that aren't already escaped (preceded with a backslash) |
| | | 235 | | // 3) If a string starts with a double quote but doesn't end with one, or vice-versa, then the |
| | | 236 | | // double quote is considered internal and is escaped. |
| | | 237 | | |
| | 0 | 238 | | if (string.IsNullOrEmpty(entityTag)) |
| | | 239 | | { |
| | 0 | 240 | | return null; |
| | | 241 | | } |
| | | 242 | | |
| | 0 | 243 | | if (entityTag.StartsWith("W/\"", StringComparison.OrdinalIgnoreCase) && |
| | 0 | 244 | | entityTag.EndsWith("\"", StringComparison.OrdinalIgnoreCase)) |
| | | 245 | | { |
| | 0 | 246 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 247 | | SR.Format(SR.WeakEntityTagsNotSupported, entityTag))); |
| | | 248 | | } |
| | | 249 | | |
| | 0 | 250 | | List<int> escapeCharacterInsertIndices = null; |
| | 0 | 251 | | int lastEtagIndex = entityTag.Length - 1; |
| | 0 | 252 | | bool startsWithQuote = entityTag[0] == '\"'; |
| | 0 | 253 | | bool endsWithQuote = entityTag[lastEtagIndex] == '\"'; |
| | | 254 | | |
| | | 255 | | // special case where the entityTag is a single character, a double quote, '"' |
| | 0 | 256 | | if (lastEtagIndex == 0 && startsWithQuote) |
| | | 257 | | { |
| | 0 | 258 | | endsWithQuote = false; |
| | | 259 | | } |
| | | 260 | | |
| | 0 | 261 | | bool needsSurroundingQuotes = !startsWithQuote || !endsWithQuote; |
| | | 262 | | |
| | 0 | 263 | | if (startsWithQuote && !endsWithQuote) |
| | | 264 | | { |
| | 0 | 265 | | if (escapeCharacterInsertIndices == null) |
| | | 266 | | { |
| | 0 | 267 | | escapeCharacterInsertIndices = new List<int>(); |
| | | 268 | | } |
| | | 269 | | |
| | 0 | 270 | | escapeCharacterInsertIndices.Add(0); |
| | | 271 | | } |
| | | 272 | | |
| | 0 | 273 | | for (int x = 1; x < lastEtagIndex; x++) |
| | | 274 | | { |
| | 0 | 275 | | if (entityTag[x] == '\"' && entityTag[x - 1] != '\\') |
| | | 276 | | { |
| | 0 | 277 | | if (escapeCharacterInsertIndices == null) |
| | | 278 | | { |
| | 0 | 279 | | escapeCharacterInsertIndices = new List<int>(); |
| | | 280 | | } |
| | | 281 | | |
| | 0 | 282 | | escapeCharacterInsertIndices.Add(x + escapeCharacterInsertIndices.Count); |
| | | 283 | | } |
| | | 284 | | } |
| | | 285 | | |
| | | 286 | | // Possible that the ending internal quote is already escaped so must check the character before it |
| | 0 | 287 | | if (!startsWithQuote && endsWithQuote && entityTag[lastEtagIndex - 1] != '\\') |
| | | 288 | | { |
| | 0 | 289 | | if (escapeCharacterInsertIndices == null) |
| | | 290 | | { |
| | 0 | 291 | | escapeCharacterInsertIndices = new List<int>(); |
| | | 292 | | } |
| | | 293 | | |
| | 0 | 294 | | escapeCharacterInsertIndices.Add(lastEtagIndex + escapeCharacterInsertIndices.Count); |
| | | 295 | | } |
| | | 296 | | |
| | 0 | 297 | | if (needsSurroundingQuotes || escapeCharacterInsertIndices != null) |
| | | 298 | | { |
| | 0 | 299 | | int escapeCharacterInsertIndicesCount = (escapeCharacterInsertIndices == null) ? 0 : escapeCharacterInse |
| | 0 | 300 | | StringBuilder editedEtag = new StringBuilder(entityTag, entityTag.Length + escapeCharacterInsertIndicesC |
| | 0 | 301 | | for (int x = 0; x < escapeCharacterInsertIndicesCount; x++) |
| | | 302 | | { |
| | 0 | 303 | | editedEtag.Insert(escapeCharacterInsertIndices[x], '\\'); |
| | | 304 | | } |
| | | 305 | | |
| | 0 | 306 | | if (needsSurroundingQuotes) |
| | | 307 | | { |
| | 0 | 308 | | editedEtag.Insert(entityTag.Length + escapeCharacterInsertIndicesCount, '\"'); |
| | 0 | 309 | | editedEtag.Insert(0, '\"'); |
| | | 310 | | } |
| | | 311 | | |
| | 0 | 312 | | entityTag = editedEtag.ToString(); |
| | | 313 | | } |
| | | 314 | | |
| | 0 | 315 | | return entityTag; |
| | | 316 | | } |
| | | 317 | | |
| | 0 | 318 | | internal static string GenerateValidEtag(object entityTag) => string.Format(CultureInfo.InvariantCulture, "\"{0} |
| | | 319 | | } |
| | | 320 | | } |