/// <summary>
        ///     Parses most common JSON date formats
        /// </summary>
        /// <param name="input">JSON value to parse</param>
        /// <param name="culture"></param>
        /// <returns>DateTime</returns>
        public static DateTime ParseJsonDate(this string input, CultureInfo culture)
        {
            const long maxAllowedTimestamp = 253402300799;

            input = input.Replace("\n", "");
            input = input.Replace("\r", "");
            input = input.RemoveSurroundingQuotes();

            long unix;

            if (Int64Ex.TryParse(input, out unix))
            {
                var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

                return(unix > maxAllowedTimestamp?epoch.AddMilliseconds(unix) : epoch.AddSeconds(unix));
            }

            if (input.Contains("/Date("))
            {
                return(ExtractDate(input, DateRegex, culture));
            }

            if (input.Contains("new Date("))
            {
                input = input.Replace(" ", "");

                // because all whitespace is removed, match against newDate( instead of new Date(
                return(ExtractDate(input, NewDateRegex, culture));
            }

            return(ParseFormattedDate(input, culture));
        }
Пример #2
0
        private static byte[] readEntityBody(Stream stream, string length)
        {
            long len;

#if NETCF
            if (!Int64Ex.TryParse(length, out len))
#else
            if (!Int64.TryParse(length, out len))
#endif
            { throw new ArgumentException("Cannot be parsed.", "length"); }

            if (len < 0)
            {
                throw new ArgumentOutOfRangeException("length", "Less than zero.");
            }

            return(len > 1024
                                   ? stream.ReadBytes(len, 1024)
                                   : len > 0
                                         ? stream.ReadBytes((int)len)
                                         : null);
        }
Пример #3
0
        internal void AddHeader(string headerField)
        {
            var start = headerField[0];

            if (start == ' ' || start == '\t')
            {
                _context.ErrorMessage = "Invalid header field";
                return;
            }

            var colon = headerField.IndexOf(':');

            if (colon < 1)
            {
                _context.ErrorMessage = "Invalid header field";
                return;
            }

            var name = headerField.Substring(0, colon).Trim();

            if (name.Length == 0 || !name.IsToken())
            {
                _context.ErrorMessage = "Invalid header name";
                return;
            }

            var val = colon < headerField.Length - 1
                                                 ? headerField.Substring(colon + 1).Trim()
                                                 : String.Empty;

            _headers.InternalSet(name, val, false);

            var lower = name.ToLower(CultureInfo.InvariantCulture);

            if (lower == "host")
            {
                if (_userHostName != null)
                {
                    _context.ErrorMessage = "Invalid Host header";
                    return;
                }

                if (val.Length == 0)
                {
                    _context.ErrorMessage = "Invalid Host header";
                    return;
                }

                _userHostName = val;

                return;
            }

            if (lower == "content-length")
            {
                if (_contentLength > -1)
                {
                    _context.ErrorMessage = "Invalid Content-Length header";
                    return;
                }

                long len;
#if SSHARP
                if (!Int64Ex.TryParse(val, out len))
#else
                if (!Int64.TryParse(val, out len))
#endif
                {
                    _context.ErrorMessage = "Invalid Content-Length header";
                    return;
                }

                if (len < 0)
                {
                    _context.ErrorMessage = "Invalid Content-Length header";
                    return;
                }

                _contentLength = len;
            }
        }
        internal void AddHeader(string header)
        {
            var colon = header.IndexOf(':');

            if (colon == -1)
            {
                _context.ErrorMessage = "Invalid header";
                return;
            }

            var name = header.Substring(0, colon).Trim();
            var val  = header.Substring(colon + 1).Trim();

            _headers.InternalSet(name, val, false);

            var lower = name.ToLower(CultureInfo.InvariantCulture);

            if (lower == "accept")
            {
                _acceptTypes = new List <string> (val.SplitHeaderValue(',')).ToArray();
                return;
            }

            if (lower == "accept-language")
            {
                _userLanguages = val.Split(',');
                return;
            }

            if (lower == "content-length")
            {
                long len;
#if SSHARP
                if (Int64Ex.TryParse(val, out len) && len >= 0)
#else
                if (Int64.TryParse(val, out len) && len >= 0)
#endif
                {
                    _contentLength    = len;
                    _contentLengthSet = true;
                }
                else
                {
                    _context.ErrorMessage = "Invalid Content-Length header";
                }

                return;
            }

            if (lower == "content-type")
            {
                try
                {
                    _contentType     = HttpUtility.GetMimeType(val);
                    _contentEncoding = HttpUtility.GetEncoding(val);
                }
                catch
                {
                    _context.ErrorMessage = "Invalid Content-Type header";
                }

                return;
            }

            if (lower == "referer")
            {
                _referer = val.ToUri();
            }
        }