private byte[] SerializeHeader(string method, HttpConnectionRequest request)
 {
     using (MemoryStream memoryStream = new MemoryStream())
     {
         this._httpHeaderSerializer.WriteHeader((Stream)memoryStream, method, request);
         return(memoryStream.ToArray());
     }
 }
        public void WriteHeader(Stream stream, string method, HttpConnectionRequest request)
        {
            Uri url = request.Url;

            using (StreamWriter streamWriter = new StreamWriter(stream, this._headerEncoding, 1024, true))
            {
                streamWriter.NewLine = "\r\n";
                string requestTarget = HttpHeaderSerializer.GetRequestTarget(request);
                string host          = HttpHeaderSerializer.GetHost(url);
                streamWriter.WriteLine(method.ToUpperInvariant() + " " + requestTarget + " HTTP/1.1");
                streamWriter.WriteLine("Host: " + host);
                streamWriter.WriteLine(request.KeepAlive ? "Connection: Keep-Alive" : "Connection: Close");
                if ((Uri)null != request.Referrer)
                {
                    streamWriter.WriteLine("Referer:" + (object)request.Referrer);
                }
                long?nullable = request.RangeFrom;
                int  num;
                if (!nullable.HasValue)
                {
                    nullable = request.RangeTo;
                    num      = !nullable.HasValue ? 1 : 0;
                }
                else
                {
                    num = 0;
                }
                if (num == 0)
                {
                    streamWriter.WriteLine("Range: bytes={0}-{1}", new object[2]
                    {
                        (object)request.RangeFrom,
                        (object)request.RangeTo
                    });
                }
                if (null != this._userAgentLine)
                {
                    streamWriter.WriteLine(this._userAgentLine);
                }
                if (!string.IsNullOrWhiteSpace(request.Accept))
                {
                    streamWriter.WriteLine("Accept: " + request.Accept.Trim());
                }
                if (null != request.Headers)
                {
                    foreach (KeyValuePair <string, string> keyValuePair in request.Headers)
                    {
                        string str1 = keyValuePair.Value;
                        string str2 = !string.IsNullOrWhiteSpace(str1) ? str1.Trim() : string.Empty;
                        streamWriter.WriteLine(keyValuePair.Key.Trim() + ": " + str2);
                    }
                }
                streamWriter.WriteLine();
                streamWriter.Flush();
            }
        }
Esempio n. 3
0
        public void WriteHeader(Stream stream, string method, HttpConnectionRequest request)
        {
            var url = request.Url;

            using (var tw = new StreamWriter(stream, _headerEncoding, 1024, true))
            {
                tw.NewLine = HttpEol;

                var requestTarget = GetRequestTarget(request);
                var host = GetHost(url);

                tw.WriteLine(method.ToUpperInvariant() + " " + requestTarget + " HTTP/1.1");
                tw.WriteLine("Host: " + host);
                tw.WriteLine(request.KeepAlive ? "Connection: Keep-Alive" : "Connection: Close");

                if (null != request.Referrer && request.Referrer.IsAbsoluteUri)
                    tw.WriteLine("Referer:" + request.Referrer.AbsoluteUri);

                if (request.RangeFrom.HasValue || request.RangeTo.HasValue)
                    tw.WriteLine("Range: bytes={0}-{1}", request.RangeFrom, request.RangeTo);

                if (null != _userAgentLine)
                    tw.WriteLine(_userAgentLine);

                if (!string.IsNullOrWhiteSpace(request.Accept))
                    tw.WriteLine("Accept: " + request.Accept.Trim());

                if (null != request.Cookies)
                {
                    var cookieHeader = request.Cookies.GetCookieHeader(url);

                    if (!string.IsNullOrWhiteSpace(cookieHeader))
                        tw.WriteLine("Cookies: " + cookieHeader);
                }

                if (null != request.Headers)
                {
                    foreach (var header in request.Headers)
                    {
                        var value = header.Value;

                        if (string.IsNullOrWhiteSpace(value))
                            value = string.Empty;
                        else
                            value = value.Trim();

                        tw.WriteLine(header.Key.Trim() + ": " + value);
                    }
                }

                tw.WriteLine();

                tw.Flush();
            }
        }
        private static string GetRequestTarget(HttpConnectionRequest request)
        {
            Uri url = request.Url;

            if (!((Uri)null != request.Proxy))
            {
                return(url.PathAndQuery);
            }
            return(new UriBuilder(url)
            {
                Fragment = ((string)null)
            }.Uri.ToString());
        }
        public virtual HttpConnectionRequest CreateRequest(Uri url, Uri referrer, ContentType contentType, long? fromBytes, long? toBytes, IEnumerable<KeyValuePair<string, string>> headers)
        {
            var request = new HttpConnectionRequest
            {
                Url = url,
                Referrer = referrer,
                RangeFrom = fromBytes,
                RangeTo = toBytes,
                Proxy = _parameters.Proxy,
                Headers = headers,
                Cookies = _parameters.Cookies
            };

            if (null != contentType)
                request.Accept = CreateAcceptHeader(contentType);

            return request;
        }
Esempio n. 6
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");
            }
        }
        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);
        }
Esempio n. 8
0
        byte[] SerializeHeader(string method, HttpConnectionRequest request)
        {
            using (var ms = new MemoryStream())
            {
                _httpHeaderSerializer.WriteHeader(ms, method, request);

                return ms.ToArray();
            }
        }
        internal virtual async Task<IHttpConnectionResponse> GetAsync(HttpConnectionRequest request, CancellationToken cancellationToken)
        {
            var connection = _httpConnectionFactory.CreateHttpConnection();

            var requestUrl = request.Url;
            var url = requestUrl;

            var retry = 0;

            for (;;)
            {
                await connection.ConnectAsync(request.Proxy ?? url, cancellationToken).ConfigureAwait(false);

                request.Url = url; // TODO: Unhack this...
                var response = await connection.GetAsync(request, true, cancellationToken).ConfigureAwait(false);
                request.Url = requestUrl;

                var status = response.Status;
                if (HttpStatusCode.Moved != status.StatusCode && HttpStatusCode.Redirect != status.StatusCode)
                    return response;

                if (++retry >= 8)
                    return response;

                connection.Close();

                var location = response.Headers["Location"].FirstOrDefault();

                if (!Uri.TryCreate(request.Url, location, out url))
                    return response;
            }
        }
Esempio n. 10
0
        static string GetRequestTarget(HttpConnectionRequest request)
        {
            var url = request.Url;
            var proxy = request.Proxy;

            if (null != proxy)
            {
                var ub = new UriBuilder(url) { Fragment = null };

                return ub.Uri.AbsoluteUri;
            }

            var target = url.PathAndQuery;

            return target;
        }
Esempio n. 11
0
        public async Task<IHttpConnectionResponse> SendAsync(HttpConnectionRequest request, bool allowBuffering, CancellationToken cancellationToken, WebResponse webResponse = null)
        {
            var url = request.Url;

            var response = await _webReaderManager.GetAsync(request, cancellationToken).ConfigureAwait(false);

            Update(url, response, webResponse);

            return response;
        }