| | | 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 | | |
| | | 6 | | namespace System.Net.Http.Headers |
| | | 7 | | { |
| | | 8 | | public sealed class HttpContentHeaders : HttpHeaders |
| | | 9 | | { |
| | | 10 | | internal HttpContent _parent; |
| | | 11 | | private bool _contentLengthSet; |
| | | 12 | | |
| | | 13 | | private HttpHeaderValueCollection<string>? _allow; |
| | | 14 | | private HttpHeaderValueCollection<string>? _contentEncoding; |
| | | 15 | | private HttpHeaderValueCollection<string>? _contentLanguage; |
| | | 16 | | |
| | | 17 | | public ICollection<string> Allow => |
| | 0 | 18 | | _allow ??= new HttpHeaderValueCollection<string>(KnownHeaders.Allow.Descriptor, this); |
| | | 19 | | |
| | | 20 | | public ContentDispositionHeaderValue? ContentDisposition |
| | | 21 | | { |
| | 0 | 22 | | get { return (ContentDispositionHeaderValue?)GetSingleParsedValue(KnownHeaders.ContentDisposition.Descriptor |
| | 0 | 23 | | set { SetOrRemoveParsedValue(KnownHeaders.ContentDisposition.Descriptor, value); } |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | // Must be a collection (and not provide properties like "GZip", "Deflate", etc.) since the |
| | | 27 | | // order matters! |
| | | 28 | | public ICollection<string> ContentEncoding => |
| | 0 | 29 | | _contentEncoding ??= new HttpHeaderValueCollection<string>(KnownHeaders.ContentEncoding.Descriptor, this); |
| | | 30 | | |
| | | 31 | | public ICollection<string> ContentLanguage => |
| | 0 | 32 | | _contentLanguage ??= new HttpHeaderValueCollection<string>(KnownHeaders.ContentLanguage.Descriptor, this); |
| | | 33 | | |
| | | 34 | | public long? ContentLength |
| | | 35 | | { |
| | | 36 | | get |
| | 0 | 37 | | { |
| | | 38 | | // 'Content-Length' can only hold one value. So either we get 'null' back or a boxed long value. |
| | 0 | 39 | | object? storedValue = GetSingleParsedValue(KnownHeaders.ContentLength.Descriptor); |
| | | 40 | | |
| | | 41 | | // Only try to calculate the length if the user didn't set the value explicitly using the setter. |
| | 0 | 42 | | if (!_contentLengthSet && (storedValue == null)) |
| | 0 | 43 | | { |
| | | 44 | | // If we don't have a value for Content-Length in the store, try to let the content calculate |
| | | 45 | | // it's length. If the content object is able to calculate the length, we'll store it in the |
| | | 46 | | // store. |
| | 0 | 47 | | long? calculatedLength = _parent.GetComputedOrBufferLength(); |
| | | 48 | | |
| | 0 | 49 | | if (calculatedLength != null) |
| | 0 | 50 | | { |
| | 0 | 51 | | SetParsedValue(KnownHeaders.ContentLength.Descriptor, (object)calculatedLength.Value); |
| | 0 | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | return calculatedLength; |
| | | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | if (storedValue == null) |
| | 0 | 58 | | { |
| | 0 | 59 | | return null; |
| | | 60 | | } |
| | | 61 | | else |
| | 0 | 62 | | { |
| | 0 | 63 | | return (long)storedValue; |
| | | 64 | | } |
| | 0 | 65 | | } |
| | | 66 | | set |
| | 0 | 67 | | { |
| | 0 | 68 | | SetOrRemoveParsedValue(KnownHeaders.ContentLength.Descriptor, value); // box long value |
| | 0 | 69 | | _contentLengthSet = true; |
| | 0 | 70 | | } |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | public Uri? ContentLocation |
| | | 74 | | { |
| | 0 | 75 | | get { return (Uri?)GetSingleParsedValue(KnownHeaders.ContentLocation.Descriptor); } |
| | 0 | 76 | | set { SetOrRemoveParsedValue(KnownHeaders.ContentLocation.Descriptor, value); } |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | public byte[]? ContentMD5 |
| | | 80 | | { |
| | 0 | 81 | | get { return (byte[]?)GetSingleParsedValue(KnownHeaders.ContentMD5.Descriptor); } |
| | 0 | 82 | | set { SetOrRemoveParsedValue(KnownHeaders.ContentMD5.Descriptor, value); } |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | public ContentRangeHeaderValue? ContentRange |
| | | 86 | | { |
| | 0 | 87 | | get { return (ContentRangeHeaderValue?)GetSingleParsedValue(KnownHeaders.ContentRange.Descriptor); } |
| | 0 | 88 | | set { SetOrRemoveParsedValue(KnownHeaders.ContentRange.Descriptor, value); } |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | public MediaTypeHeaderValue? ContentType |
| | | 92 | | { |
| | 0 | 93 | | get { return (MediaTypeHeaderValue?)GetSingleParsedValue(KnownHeaders.ContentType.Descriptor); } |
| | 0 | 94 | | set { SetOrRemoveParsedValue(KnownHeaders.ContentType.Descriptor, value); } |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | public DateTimeOffset? Expires |
| | | 98 | | { |
| | 0 | 99 | | get { return HeaderUtilities.GetDateTimeOffsetValue(KnownHeaders.Expires.Descriptor, this, DateTimeOffset.Mi |
| | 0 | 100 | | set { SetOrRemoveParsedValue(KnownHeaders.Expires.Descriptor, value); } |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | public DateTimeOffset? LastModified |
| | | 104 | | { |
| | 0 | 105 | | get { return HeaderUtilities.GetDateTimeOffsetValue(KnownHeaders.LastModified.Descriptor, this); } |
| | 0 | 106 | | set { SetOrRemoveParsedValue(KnownHeaders.LastModified.Descriptor, value); } |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | internal HttpContentHeaders(HttpContent parent) |
| | 1 | 110 | | : base(HttpHeaderType.Content | HttpHeaderType.Custom, HttpHeaderType.None) |
| | 1 | 111 | | { |
| | 1 | 112 | | _parent = parent; |
| | 1 | 113 | | } |
| | | 114 | | } |
| | | 115 | | } |