Exemplo n.º 1
0
        private async Task ReadHeadersAsync(HttpReader httpReader, CancellationToken cancellationToken)
        {
            while (true)
            {
                Tuple <string, string> nameValue;
                string value;
                do
                {
                    nameValue = await HttpReaderExtensions.ReadHeaderAsync(httpReader, cancellationToken).ConfigureAwait(false);

                    if (null != nameValue)
                    {
                        this._headers.Add(nameValue);
                        value = nameValue.Item2;
                    }
                    else
                    {
                        goto label_12;
                    }
                }while (string.IsNullOrEmpty(value));
                string name = nameValue.Item1;
                if (string.Equals(name, "Content-Length", StringComparison.OrdinalIgnoreCase))
                {
                    long result;
                    if (long.TryParse(value, NumberStyles.Integer, (IFormatProvider)NumberFormatInfo.InvariantInfo, out result))
                    {
                        this._httpStatus.ContentLength = new long?(result);
                    }
                }
                else if (string.Equals(name, "Transfer-Encoding", StringComparison.OrdinalIgnoreCase))
                {
                    int length = value.IndexOf(';');
                    if (string.Equals(length > 1 ? value.Substring(0, length).Trim() : value, "chunked", StringComparison.OrdinalIgnoreCase))
                    {
                        this._httpStatus.ChunkedEncoding = true;
                    }
                }
            }
            label_12 :;
        }
Exemplo n.º 2
0
        public async Task <IHttpConnectionResponse> GetAsync(HttpConnectionRequest request, bool closeConnection, CancellationToken cancellationToken)
        {
            this.StartRequest();
            if (closeConnection)
            {
                request.KeepAlive = false;
            }
            byte[]     requestHeader   = this.SerializeHeader("GET", request);
            Task <int> writeHeaderTask = this.WriteSocketAsync(requestHeader, 0, requestHeader.Length, cancellationToken);
            HttpReader httpReader      = new HttpReader(new HttpReader.ReadAsyncDelegate(this.ReadSocketAsync), this._headerDecoding);
            IHttpConnectionResponse connectionResponse;

            try
            {
                string statusLine = await HttpReaderExtensions.ReadNonBlankLineAsync((IHttpReader)httpReader, cancellationToken).ConfigureAwait(false);

                this.ParseStatusLine(statusLine);
                await this.ReadHeadersAsync(httpReader, cancellationToken).ConfigureAwait(false);

                int num = await writeHeaderTask.ConfigureAwait(false);

                writeHeaderTask = (Task <int>)null;
                Stream stream = this._httpStatus.ChunkedEncoding ? (Stream) new ChunkedStream((IHttpReader)httpReader) : (Stream) new ContentLengthStream((IHttpReader)httpReader, this._httpStatus.ContentLength);
                HttpConnectionResponse response = new HttpConnectionResponse(request.Url, closeConnection ? (IHttpConnection)this : (IHttpConnection)null, (IHttpReader)httpReader, stream, Enumerable.ToLookup <Tuple <string, string>, string, string>((IEnumerable <Tuple <string, string> >) this._headers, (Func <Tuple <string, string>, string>)(kv => kv.Item1), (Func <Tuple <string, string>, string>)(kv => kv.Item2), (IEqualityComparer <string>)StringComparer.OrdinalIgnoreCase), (IHttpStatus)this._httpStatus);
                httpReader         = (HttpReader)null;
                connectionResponse = (IHttpConnectionResponse)response;
            }
            finally
            {
                if (null != httpReader)
                {
                    httpReader.Dispose();
                }
                if (null != writeHeaderTask)
                {
                    TaskCollector.Default.Add((Task)writeHeaderTask, "HttpConnection GetAsync writer");
                }
            }
            return(connectionResponse);
        }
        public static async Task <Tuple <string, string> > ReadHeaderAsync(this HttpReader httpReader, CancellationToken cancellationToken)
        {
            string header;
            int    colon;

            while (true)
            {
                header = await httpReader.ReadLineAsync(cancellationToken).ConfigureAwait(false);

                if (!string.IsNullOrEmpty(header))
                {
                    colon = header.IndexOf(':');
                    if (colon < 1)
                    {
                        Debug.WriteLine("Bad header: " + header);
                    }
                    else
                    {
                        goto label_5;
                    }
                }
                else
                {
                    break;
                }
            }
            Tuple <string, string> tuple = (Tuple <string, string>)null;

            goto label_7;
label_5:
            string name = header.Substring(0, colon).Trim();
            string value = colon + 1 < header.Length ? header.Substring(colon + 1).Trim() : string.Empty;

            tuple = Tuple.Create <string, string>(name, value);
label_7:
            return(tuple);
        }
Exemplo n.º 4
0
        public async Task<IHttpConnectionResponse> GetAsync(HttpConnectionRequest request, bool closeConnection, CancellationToken cancellationToken)
        {
            StartRequest();

            // TODO:  Fix closeConnection vs KeepAlive hack.
            if (closeConnection)
                request.KeepAlive = false;

            var requestHeader = SerializeHeader("GET", request);

            var writeHeaderTask = WriteSocketAsync(requestHeader, 0, requestHeader.Length, cancellationToken);

            var httpReader = new HttpReader(ReadSocketAsync, _headerDecoding);

            try
            {
                var statusLine = await httpReader.ReadNonBlankLineAsync(cancellationToken).ConfigureAwait(false);

                ParseStatusLine(statusLine);

                await ReadHeadersAsync(httpReader, request.Url, request.Cookies, cancellationToken).ConfigureAwait(false);

                await writeHeaderTask.ConfigureAwait(false);
                writeHeaderTask = null;

                var stream = _httpStatus.ChunkedEncoding ? (Stream)new ChunkedStream(httpReader) : new ContentLengthStream(httpReader, _httpStatus.ContentLength);

                var response = new HttpConnectionResponse(request.Url, closeConnection ? this : null, httpReader, stream,
                    _headers.ToLookup(kv => kv.Item1, kv => kv.Item2, StringComparer.OrdinalIgnoreCase), _httpStatus);

                httpReader = null;

                return response;
            }
            finally
            {
                if (null != httpReader)
                    httpReader.Dispose();

                if (null != writeHeaderTask)
                    TaskCollector.Default.Add(writeHeaderTask, "HttpConnection GetAsync writer");
            }
        }
Exemplo n.º 5
0
        async Task ReadHeadersAsync(HttpReader httpReader, Uri url, CookieContainer cookies, CancellationToken cancellationToken)
        {
            for (; ; )
            {
                var nameValue = await httpReader.ReadHeaderAsync(cancellationToken).ConfigureAwait(false);

                if (null == nameValue)
                    break;

                _headers.Add(nameValue);

                var value = nameValue.Item2;

                if (string.IsNullOrEmpty(value))
                    continue;

                var name = nameValue.Item1;

                if (string.Equals(name, "Content-Length", StringComparison.OrdinalIgnoreCase))
                {
                    long n;
                    if (long.TryParse(value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out n))
                        _httpStatus.ContentLength = n;
                }
                else if (string.Equals(name, "Transfer-Encoding", StringComparison.OrdinalIgnoreCase))
                {
                    var semicolon = value.IndexOf(';');

                    var token = semicolon > 1 ? value.Substring(0, semicolon).Trim() : value;

                    if (string.Equals(token, "chunked", StringComparison.OrdinalIgnoreCase))
                        _httpStatus.ChunkedEncoding = true;
                }
                else if (null != cookies && (string.Equals(name, "Set-Cookie", StringComparison.OrdinalIgnoreCase)
                         || string.Equals(name, "Set-Cookie2", StringComparison.OrdinalIgnoreCase)))
                {
                    cookies.SetCookies(url, value);
                }
            }
        }