Пример #1
0
        // Add the given parameter to the list. Remove if date is null.
        private void SetDate(string parameter, DateTimeOffset?date)
        {
            var dateParameter = NameValueHeaderValue.Find(_parameters, parameter);

            if (date == null)
            {
                // Remove parameter
                if (dateParameter != null)
                {
                    _parameters.Remove(dateParameter);
                }
            }
            else
            {
                // Must always be quoted
                var dateString = HeaderUtilities.FormatDate(date.Value, quoted: true);
                if (dateParameter != null)
                {
                    dateParameter.Value = dateString;
                }
                else
                {
                    Parameters.Add(new NameValueHeaderValue(parameter, dateString));
                }
            }
        }
Пример #2
0
        public void ReturnsSameResultAsRfc1123String(DateTimeOffset dateTime, bool quoted)
        {
            var formatted = dateTime.ToString(Rfc1123Format);
            var expected  = quoted ? $"\"{formatted}\"" : formatted;
            var actual    = HeaderUtilities.FormatDate(dateTime, quoted);

            Assert.Equal(expected, actual);
        }
Пример #3
0
 public override string ToString()
 {
     if (_entityTag == null)
     {
         return(HeaderUtilities.FormatDate(_lastModified.GetValueOrDefault()));
     }
     return(_entityTag.ToString());
 }
Пример #4
0
        public void ToString_UseDifferentValues_MatchExpectation()
        {
            Assert.Equal("Sat, 31 Jul 2010 15:38:57 GMT",
                         HeaderUtilities.FormatDate(new DateTimeOffset(2010, 7, 31, 15, 38, 57, TimeSpan.Zero)));

            Assert.Equal("Fri, 01 Jan 2010 01:01:01 GMT",
                         HeaderUtilities.FormatDate(new DateTimeOffset(2010, 1, 1, 1, 1, 1, TimeSpan.Zero)));
        }
Пример #5
0
        /// <summary>
        /// Append string representation of this <see cref="SetCookieHeaderValue"/> to given
        /// <paramref name="builder"/>.
        /// </summary>
        /// <param name="builder">
        /// The <see cref="StringBuilder"/> to receive the string representation of this
        /// <see cref="SetCookieHeaderValue"/>.
        /// </param>
        public void AppendToStringBuilder(StringBuilder builder)
        {
            builder.Append(_name.AsSpan());
            builder.Append("=");
            builder.Append(_value.AsSpan());

            if (Expires.HasValue)
            {
                AppendSegment(builder, ExpiresToken, HeaderUtilities.FormatDate(Expires.GetValueOrDefault()));
            }

            if (MaxAge.HasValue)
            {
                AppendSegment(builder, MaxAgeToken, HeaderUtilities.FormatNonNegativeInt64((long)MaxAge.GetValueOrDefault().TotalSeconds));
            }

            if (Domain != null)
            {
                AppendSegment(builder, DomainToken, Domain);
            }

            if (Path != null)
            {
                AppendSegment(builder, PathToken, Path);
            }

            if (Secure)
            {
                AppendSegment(builder, SecureToken, null);
            }

            // Allow for Unspecified (-1) to skip SameSite
            if (SameSite == SameSiteMode.None)
            {
                AppendSegment(builder, SameSiteToken, SameSiteNoneToken);
            }
            else if (SameSite == SameSiteMode.Lax)
            {
                AppendSegment(builder, SameSiteToken, SameSiteLaxToken);
            }
            else if (SameSite == SameSiteMode.Strict)
            {
                AppendSegment(builder, SameSiteToken, SameSiteStrictToken);
            }

            if (HttpOnly)
            {
                AppendSegment(builder, HttpOnlyToken, null);
            }

            foreach (var extension in Extensions)
            {
                AppendSegment(builder, extension, null);
            }
        }
Пример #6
0
 private static Task EncodeHeadersToStreamAsync(Stream output, StringBuilder builder, TEntry entry, bool writeDivider, string boundary, EncodingContext context, byte[] buffer)
 {
     builder.Clear();
     if (writeDivider)
     {
         builder.Append(CrLf + DoubleDash).Append(boundary).Append(CrLf);
     }
     //write headers
     WriteHeader(builder, RequestVoteMessage.RecordTermHeader, entry.Term.ToString(InvariantCulture));
     WriteHeader(builder, HeaderNames.LastModified, HeaderUtils.FormatDate(entry.Timestamp));
     // Extra CRLF to end headers (even if there are no headers)
     builder.Append(CrLf);
     return(output.WriteStringAsync(builder.ToString(), context, buffer));
 }
        /// <summary>
        /// Append string representation of this <see cref="SetCookieHeaderValue"/> to given
        /// <paramref name="builder"/>.
        /// </summary>
        /// <param name="builder">
        /// The <see cref="StringBuilder"/> to receive the string representation of this
        /// <see cref="SetCookieHeaderValue"/>.
        /// </param>
        public void AppendToStringBuilder(StringBuilder builder)
        {
            builder.Append(_name);
            builder.Append("=");
            builder.Append(_value);

            if (Expires.HasValue)
            {
                AppendSegment(builder, ExpiresToken, HeaderUtilities.FormatDate(Expires.Value));
            }

            if (MaxAge.HasValue)
            {
                AppendSegment(builder, MaxAgeToken, HeaderUtilities.FormatNonNegativeInt64((long)MaxAge.Value.TotalSeconds));
            }

            if (Domain != null)
            {
                AppendSegment(builder, DomainToken, Domain);
            }

            if (Path != null)
            {
                AppendSegment(builder, PathToken, Path);
            }

            if (Secure)
            {
                AppendSegment(builder, SecureToken, null);
            }

            if (SameSite != SameSiteMode.None)
            {
                AppendSegment(builder, SameSiteToken, SameSite == SameSiteMode.Lax ? SameSiteLaxToken : SameSiteStrictToken);
            }

            if (HttpOnly)
            {
                AppendSegment(builder, HttpOnlyToken, null);
            }
        }
Пример #8
0
        // name="val ue"; expires=Sun, 06 Nov 1994 08:49:37 GMT; max-age=86400; domain=domain1; path=path1; secure; httponly
        public override string ToString()
        {
            StringBuilder header = new StringBuilder();

            header.Append(_name);
            header.Append("=");
            header.Append(_value);

            if (Expires.HasValue)
            {
                AppendSegment(header, ExpiresToken, HeaderUtilities.FormatDate(Expires.Value));
            }

            if (MaxAge.HasValue)
            {
                AppendSegment(header, MaxAgeToken, HeaderUtilities.FormatInt64((long)MaxAge.Value.TotalSeconds));
            }

            if (Domain != null)
            {
                AppendSegment(header, DomainToken, Domain);
            }

            if (Path != null)
            {
                AppendSegment(header, PathToken, Path);
            }

            if (Secure)
            {
                AppendSegment(header, SecureToken, null);
            }

            if (HttpOnly)
            {
                AppendSegment(header, HttpOnlyToken, null);
            }

            return(header.ToString());
        }
Пример #9
0
        /// <summary>
        /// Append string representation of this <see cref="SetCookieHeaderValue"/> to given
        /// <paramref name="builder"/>.
        /// </summary>
        /// <param name="builder">
        /// The <see cref="StringBuilder"/> to receive the string representation of this
        /// <see cref="SetCookieHeaderValue"/>.
        /// </param>
        public void AppendToStringBuilder(StringBuilder builder)
        {
            builder.Append(_name);
            builder.Append("=");
            builder.Append(_value);

            if (Expires.HasValue)
            {
                AppendSegment(builder, ExpiresToken, HeaderUtilities.FormatDate(Expires.Value));
            }

            if (MaxAge.HasValue)
            {
                AppendSegment(builder, MaxAgeToken, HeaderUtilities.FormatInt64((long)MaxAge.Value.TotalSeconds));
            }

            if (Domain != null)
            {
                AppendSegment(builder, DomainToken, Domain);
            }

            if (Path != null)
            {
                AppendSegment(builder, PathToken, Path);
            }

            if (Secure)
            {
                AppendSegment(builder, SecureToken, null);
            }

            if (HttpOnly)
            {
                AppendSegment(builder, HttpOnlyToken, null);
            }
        }
        // name="value"; expires=Sun, 06 Nov 1994 08:49:37 GMT; max-age=86400; domain=domain1; path=path1; secure; samesite={Strict|Lax}; httponly
        public override string ToString()
        {
            var length = _name.Length + EqualsToken.Length + _value.Length;

            string expires  = null;
            string maxAge   = null;
            string sameSite = null;

            if (Expires.HasValue)
            {
                expires = HeaderUtilities.FormatDate(Expires.Value);
                length += SeparatorToken.Length + ExpiresToken.Length + EqualsToken.Length + expires.Length;
            }

            if (MaxAge.HasValue)
            {
                maxAge  = HeaderUtilities.FormatNonNegativeInt64((long)MaxAge.Value.TotalSeconds);
                length += SeparatorToken.Length + MaxAgeToken.Length + EqualsToken.Length + maxAge.Length;
            }

            if (Domain != null)
            {
                length += SeparatorToken.Length + DomainToken.Length + EqualsToken.Length + Domain.Length;
            }

            if (Path != null)
            {
                length += SeparatorToken.Length + PathToken.Length + EqualsToken.Length + Path.Length;
            }

            if (Secure)
            {
                length += SeparatorToken.Length + SecureToken.Length;
            }

            if (SameSite != SameSiteMode.None)
            {
                sameSite = SameSite == SameSiteMode.Lax ? SameSiteLaxToken : SameSiteStrictToken;
                length  += SeparatorToken.Length + SameSiteToken.Length + EqualsToken.Length + sameSite.Length;
            }

            if (HttpOnly)
            {
                length += SeparatorToken.Length + HttpOnlyToken.Length;
            }

            var sb = new InplaceStringBuilder(length);

            sb.Append(_name);
            sb.Append(EqualsToken);
            sb.Append(_value);

            if (expires != null)
            {
                AppendSegment(ref sb, ExpiresToken, expires);
            }

            if (maxAge != null)
            {
                AppendSegment(ref sb, MaxAgeToken, maxAge);
            }

            if (Domain != null)
            {
                AppendSegment(ref sb, DomainToken, Domain);
            }

            if (Path != null)
            {
                AppendSegment(ref sb, PathToken, Path);
            }

            if (Secure)
            {
                AppendSegment(ref sb, SecureToken, null);
            }

            if (SameSite != SameSiteMode.None)
            {
                AppendSegment(ref sb, SameSiteToken, sameSite);
            }

            if (HttpOnly)
            {
                AppendSegment(ref sb, HttpOnlyToken, null);
            }

            return(sb.ToString());
        }