Exemple #1
0
 internal HttpResponse(Uri responseUri, HttpMethod method, CoreResponseData coreData, DecompressionMethods decompressionMethod)
 {
     _uri = responseUri;
     _method = method;
     _coreResponseData = coreData;
     _connectStream = coreData.ConnectStream;
     _headers = coreData.ResponseHeaders;            
     _statusCode = coreData.StatusCode;
     _contentLength = coreData.ContentLength;
     _statusDescription = coreData.StatusDescription;
     _version = coreData.HttpVersion;
     //if (this.m_ContentLength == 0L && this.m_ConnectStream is ConnectStream)
     //{
     //    ((ConnectStream)this.m_ConnectStream).CallDone();
     //}
     var text = _headers[HttpHeaderNames.ContentLocation];
     if (text != null)
     {
         try
         {
             Uri uri;
             if (Uri.TryCreate(_uri, text, out uri))
             {
                 _uri = uri;
             }
         }
         catch { }
     }
     if (decompressionMethod != DecompressionMethods.None)
     {
         if ((text = _headers[HttpHeaderNames.ContentEncoding]) != null)
         {
             if ((decompressionMethod & DecompressionMethods.GZip) == DecompressionMethods.GZip && text.IndexOf(HttpRequest.GZipHeader, 0, StringComparison.OrdinalIgnoreCase) >= 0)
             {
                 _connectStream = new GZipStream(_connectStream, CompressionMode.Decompress);
                 _contentLength = -1L;
                 _headers.SetInternal(HttpHeaderNames.TransferEncoding, null);
             }
             else if ((decompressionMethod & DecompressionMethods.Deflate) == DecompressionMethods.Deflate && text.IndexOf(HttpRequest.DeflateHeader, 0, StringComparison.OrdinalIgnoreCase) >= 0)
             {
                 _connectStream = new DeflateStream(_connectStream, CompressionMode.Decompress);
                 _contentLength = -1L;
                 _headers.SetInternal(HttpHeaderNames.TransferEncoding, null);
             }
         }
     }
 }
Exemple #2
0
 public HttpRequest(HttpMethod method, Uri uri, HttpVersion version)
 {
     _allowAutoRedirect = true;
     _automaticDecompression = DecompressionMethods.None;
     _contentLength = -1L;
     _httpWriteMode = HttpWriteMode.Unknown;
     _keepAlive = true;
     _maximumResponseHeadersLength = 8190;
     _maxAutomaticRedirections = 50;
     _originUri = uri;
     _originMethod = method;
     _timeout = 100000;
     _uri = _originUri;
     _method = _originMethod;
     _version = version;
     _sendChunked = false;
     _headers = new HttpRequestHeaders();
     _startTimestamp = DateTime.UtcNow.Ticks;          
 }
        } // ValidateVerbCharacter
 

        // read headers 
        public BaseTransportHeaders ReadHeaders() 
        {
            bool bSendContinue = false; 

            BaseTransportHeaders headers = new BaseTransportHeaders();

            // read first line 
            String verb, requestURI, version;
            ReadFirstLine(out verb, out requestURI, out version); 
 
            if ((verb == null) || (requestURI == null) || (version == null))
            { 
                throw new RemotingException(
                    CoreChannel.GetResourceString(
                        "Remoting_Http_UnableToReadFirstLine"));
            } 

            if (version.Equals("HTTP/1.1")) // most common case 
                _version = HttpVersion.V1_1; 
            else
            if (version.Equals("HTTP/1.0")) 
                _version = HttpVersion.V1_0;
            else
                _version = HttpVersion.V1_1; // (assume it will understand 1.1)
 
            if (_version == HttpVersion.V1_1)
            { 
                _keepAlive = true; 
            }
            else // it's a 1.0 client 
            {
                _keepAlive = false;
            }
 

            // update request uri to be sure that it has no channel data 
            String channelURI; 
            String objectURI;
            channelURI = HttpChannelHelper.ParseURL(requestURI, out objectURI); 
            if (channelURI == null)
            {
                objectURI = requestURI;
            } 

            headers["__RequestVerb"] = verb; 
            headers.RequestUri = objectURI; 
            headers["__HttpVersion"] = version;
 
            // check to see if we must send continue
            if ((_version == HttpVersion.V1_1) &&
                (verb.Equals("POST") || verb.Equals("PUT")))
            { 
                bSendContinue = true;
            } 
 
            ReadToEndOfHeaders(headers, out _chunkedEncoding, out _contentLength,
                               ref _keepAlive, ref bSendContinue); 

            if (bSendContinue && (_version != HttpVersion.V1_0))
                SendContinue();
 
            // add IP address and Connection Id to headers
            headers[CommonTransportKeys.IPAddress] = ((IPEndPoint)NetSocket.RemoteEndPoint).Address; 
            headers[CommonTransportKeys.ConnectionId] = _connectionId; 

            return headers; 
        } // ReadHeaders
Exemple #4
0
        public void ReadHeader(IEnumerable<string> headerLines)
        {
            int i = 0;

            string uriPath = "/";

            foreach (string headerLine in headerLines)
            {
                if (i == 0)
                {
                    string[] parts = headerLine.Split(SpaceArray, 3);

                    if (parts.Length != 3)
                        FailProtocol();

                    Method = parts[0].Trim();
                    uriPath = parts[1].Trim();

                    string version = parts[2].Trim();

                    Version = (version.Equals("HTTP/1.1", StringComparison.OrdinalIgnoreCase))
                                  ? HttpVersion.Http11
                                  : HttpVersion.Http10;
                }
                else
                {
                    string[] parts = headerLine.Split(ColonArray, 2);
                    string key = parts[0].Trim();
                    string value = parts[1].Trim();

                    Headers.Add(key, value);
                }

                i++;
            }

            string contentTypeHeader;

            if (Headers.TryGetValue("Content-Type", out contentTypeHeader))
            {
                string[] parts = contentTypeHeader.Split(SemicolonArray, 2);

                if (parts.Length >= 1)
                {
                    ContentType = parts[0].Trim();
                }

                if (parts.Length >= 2)
                {
                    var part = parts[1].Trim();

                    if (part.StartsWith(CharSetPrefix, StringComparison.OrdinalIgnoreCase))
                    {
                        ContentCharSet = part.Substring(CharSetPrefix.Length);
                    }
                }
            }

            string hostHeader;

            if (Headers.TryGetValue("Host", out hostHeader))
            {
                Host = hostHeader.Trim();
            }

            bool keepAlive = false;

            if (_connectionHandle.Server.SupportsKeepAlive)
            {
                // default by protocol
                keepAlive = (_connectionHandle.Request.Version != HttpVersion.Http10);

                string connectionHeader;

                // overrideable by explicit header. backported to http10
                if (_connectionHandle.Request.Headers.TryGetValue("Connection", out connectionHeader))
                {
                    keepAlive = connectionHeader.Trim().Equals("keep-alive", StringComparison.OrdinalIgnoreCase);
                }
            }

            // set on the connection
            _connectionHandle.KeepAlive = keepAlive;

            Url = ReconstructUri(uriPath);
        }