예제 #1
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);
        }
예제 #2
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 :;
        }