Exemplo n.º 1
0
        // Constructors

        HttpWebResponse(Uri uri, string method, WebConnectionData data, CookieContainer container)
        {
            _uri = uri;
            _method = method;
            _webHeaders = data.Headers;
            _version = data.Version;
            _statusCode = (HttpStatusCode)data.StatusCode;
            _statusDescription = data.StatusDescription;
            _stream = data.Stream;
            _contentLength = -1;

            try
            {
                var cl = _webHeaders["Content-Length"];
                if (String.IsNullOrEmpty(cl) || !Int64.TryParse(cl, out _contentLength))
                    _contentLength = -1;
            }
            catch (Exception)
            {
                _contentLength = -1;
            }

            if (container != null)
            {
                _cookieContainer = container;
                //FillCookiesAsync();
            }

#if false
            string content_encoding = webHeaders ["Content-Encoding"];
            if (content_encoding == "gzip" && (data.request.AutomaticDecompression & DecompressionMethods.GZip) != 0)
                stream = new GZipStream (stream, CompressionMode.Decompress);
            else if (content_encoding == "deflate" && (data.request.AutomaticDecompression & DecompressionMethods.Deflate) != 0)
                stream = new DeflateStream (stream, CompressionMode.Decompress);
#endif
        }
Exemplo n.º 2
0
        async Task<Tuple<int, WebHeaderCollection>> ReadHeadersAsync(StreamSocketStream stream)
        {
            var status = 200;

            //var ms = new MemoryStream();
            var gotStatus = false;
            using (var lineReader = new HttpLineReader(stream))
            {
                while (true)
                {
                    //var n = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
                    //if (n == 0)
                    //{
                    //    HandleError(WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders");
                    //    return null;
                    //}

                    //ms.Write(buffer, 0, n);
                    //var start = 0;
                    var headers = new WebHeaderCollection();
                    for (; ; )
                    {
                        var str = await lineReader.ReadLineAsync(CancellationToken.None).ConfigureAwait(false);

                        if (str == null)
                        {
                            var contentLen = 0L;
                            try
                            {
                                if (!long.TryParse(headers["Content-Length"], out contentLen))
                                    contentLen = 0;
                            }
                            catch
                            {
                                contentLen = 0;
                            }

                            lineReader.SyncStream();

                            //if (false) //ms.Length - start - contentLen > 0)
                            //{
                            //    // we've read more data than the response header and contents,
                            //    // give back extra data to the caller
                            //    //retBuffer = new byte[ms.Length - start - contentLen];
                            //    //Buffer.BlockCopy(ms.GetBuffer(), (int) (start + contentLen), retBuffer, 0, retBuffer.Length);
                            //}
                            if (contentLen > 0)
                            {
                                // haven't read in some or all of the contents for the response, do so now
                                await FlushContentsAsync(stream, contentLen).ConfigureAwait(false);
                            }

                            return Tuple.Create(status, headers);
                        }

                        if (gotStatus)
                        {
                            headers.Add(str);
                            continue;
                        }

                        var spaceidx = str.IndexOf(' ');
                        if (spaceidx == -1)
                        {
                            HandleError(WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders2");
                            return null;
                        }

                        status = (int)UInt32.Parse(str.Substring(spaceidx + 1, 3));
                        gotStatus = true;
                    }
                }
            }
        }