Exemplo n.º 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);
        }
Exemplo n.º 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 :;
        }
Exemplo n.º 3
0
        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);
        }