예제 #1
0
파일: web.cs 프로젝트: swax/Tornado.Net
 public override Tuple<int, HTTPHeaders, byte[]> transform_first_chunk(int status_code, HTTPHeaders headers, byte[] chunk, bool finishing)
 {
     if (_gzipping)
     {
         var ctype = headers.get("Content-Type", "").Split(';')[0];
         _gzipping = CONTENT_TYPES.Contains(ctype) &&
             (!finishing || chunk.Length >= MIN_LENGTH) &&
             (finishing || !headers.ContainsKey("Content-Length")) &&
             (!headers.ContainsKey("Content-Encoding"));
     }
     if (_gzipping)
     {
         headers["Content-Encoding"] = "gzip";
         _gzip_value = new MemoryStream(); // BytesIO();
         //_gzip_file = gzip.GzipFile(mode = "w", fileobj = self._gzip_value);
         _gzip_file = new GZipStream(_gzip_value, CompressionMode.Compress);
         chunk = transform_chunk(chunk, finishing);
         if (headers.ContainsKey("Content-Length"))
             headers["Content-Length"] = chunk.Length.ToString();
     }
     return new Tuple<int, HTTPHeaders, byte[]>(status_code, headers, chunk);
 }
예제 #2
0
        public HTTPRequest(string method_, string uri_, string version_="HTTP/1.0", HTTPHeaders headers_=null,
                     byte[] body_=null, string remote_ip_=null, string protocol_=null, string host_=null,
                     string files=null, HTTPConnection connection_=null)
        {
            method = method_;
            uri = uri_;
            version = version_;
            headers = headers_ ?? new HTTPHeaders();
            body = body_ ?? new byte[] {};
            if (connection != null && connection.xheaders)
            {
                // Squid uses X-Forwarded-For, others use X-Real-Ip
                remote_ip = headers.get(
                    "X-Real-Ip", headers.get("X-Forwarded-For", remote_ip_));
                if (!_valid_ip(remote_ip))
                    remote_ip = remote_ip_;
                // AWS uses X-Forwarded-Proto
                protocol = headers.get(
                    "X-Scheme", headers.get("X-Forwarded-Proto", protocol_));
                if (protocol != "http" && protocol != "https")
                    protocol = "http";
            }
            else
            {
                remote_ip = remote_ip_;
                if (protocol_ != null)
                    protocol = protocol_;
                //todo ssl else if (connection != null && isinstance(connection.stream, iostream.SSLIOStream):
                //    protocol = "https"
                else
                    protocol = "http";
            }
            host = host_ ?? headers.get("Host") ?? "127.0.0.1";
            //todo files = files_ ?? {}
            connection = connection_;
            _start_time = DateTime.Now;
            _finish_time = null;

            var parts = uri.Split(new char[] { '?' }, 2);
            path = parts.Length > 0 ? parts[0] : null;
            query = parts.Length > 1 ? parts[1] : null;

            var arguments_ = System.Web.HttpUtility.ParseQueryString(query ?? "");// parse_qs_bytes(query)
            arguments = new Dictionary<string, string[]>();
            foreach(var name in arguments_.AllKeys)
            {
                var values = arguments_.GetValues(name);

                if(values != null)
                    arguments[name] = values;
            }
        }