Esempio n. 1
0
        /// <summary>
        /// Creates an HttpRequest from the given System.Net.HttpListenerContext.
        /// </summary>
        /// <remarks>
        /// Used internally by <see cref="App.RunTestServer(string)"/>.
        /// </remarks>
        public static HttpRequest FromHttpListenerContext(HttpListenerContext context)
        {
            var fakeRequest = new FastCGI.Request(FakeRequestId++, new MemoryStream());

            fakeRequest.Parameters.Add("REQUEST_METHOD", Encoding.ASCII.GetBytes(context.Request.HttpMethod));
            fakeRequest.Parameters.Add("QUERY_STRING", Encoding.ASCII.GetBytes(context.Request.RawUrl));
            fakeRequest.Parameters.Add("DOCUMENT_URI", Encoding.ASCII.GetBytes(context.Request.RawUrl));
            fakeRequest.Parameters.Add("REQUEST_URI", Encoding.ASCII.GetBytes(context.Request.RawUrl));

            return(new HttpRequest(fakeRequest));
        }
Esempio n. 2
0
        /// <summary>
        /// Handles an incoming FastCGI request. You usually do not need to call this.
        /// </summary>
        public void ReceiveFcgiRequest(object sender, FastCGI.Request fcgiRequest)
        {
            var httpRequest = new HttpRequest(fcgiRequest);
            var response    = HandleRequest(httpRequest);

            if (response.RawBody != null)
            {
                fcgiRequest.WriteResponse(response.RawBody);
            }
            fcgiRequest.Close();
        }
Esempio n. 3
0
        public string GetResponseFromUrl(string url)
        {
            var fcgiParams = new Dictionary <string, byte[]>
            {
                { "QUERY_STRING", Encoding.ASCII.GetBytes("") },
                { "REQUEST_METHOD", Encoding.ASCII.GetBytes("GET") },
                { "CONTENT_TYPE", Encoding.ASCII.GetBytes("") },
                { "CONTENT_LENGTH", Encoding.ASCII.GetBytes("") },
                { "SCRIPT_NAME", Encoding.ASCII.GetBytes(url) },
                { "REQUEST_URI", Encoding.ASCII.GetBytes(url) },
                { "DOCUMENT_URI", Encoding.ASCII.GetBytes(url) },
                { "SERVER_PROTOCOL", Encoding.ASCII.GetBytes("HTTP/1.1") },
                { "REQUEST_SCHEME", Encoding.ASCII.GetBytes("http") },
                { "GATEWAY_INTERFACE", Encoding.ASCII.GetBytes("CGI/1.1") },
                { "SERVER_SOFTWARE", Encoding.ASCII.GetBytes("nginx/1.9.2") },
                { "REMOTE_ADDR", Encoding.ASCII.GetBytes("127.0.0.1") },
                { "REMOTE_PORT", Encoding.ASCII.GetBytes("11192") },
                { "SERVER_ADDR", Encoding.ASCII.GetBytes("127.0.0.1") },
                { "SERVER_PORT", Encoding.ASCII.GetBytes("12121") },
                { "SERVER_NAME", Encoding.ASCII.GetBytes("localhost") },
                { "HTTP_HOST", Encoding.ASCII.GetBytes("localhost:12121") },
                { "HTTP_CONNECTION", Encoding.ASCII.GetBytes("keep-alive") },
                { "HTTP_CACHE_CONTROL", Encoding.ASCII.GetBytes("max-age=0") },
                { "HTTP_ACCEPT", Encoding.ASCII.GetBytes("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8") },
                { "HTTP_UPGRADE_INSECURE_REQUESTS", Encoding.ASCII.GetBytes("1") },
                { "HTTP_USER_AGENT", Encoding.ASCII.GetBytes("Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.78 Safari/537.36") },
                { "HTTP_ACCEPT_ENCODING", Encoding.ASCII.GetBytes("gzip, deflate, sdch") },
                { "HTTP_ACCEPT_LANGUAGE", Encoding.ASCII.GetBytes("de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4") },
            };

            var fcgiRequest = new FastCGI.Request(0, null);

            fcgiRequest.Parameters = fcgiParams;
            var request  = new HttpRequest(fcgiRequest);
            var response = app.HandleRequest(request);

            return(Encoding.UTF8.GetString(response.RawBody));
        }
Esempio n. 4
0
        /// <summary>
        /// Creates a new request object.
        /// </summary>
        /// <param name="fcgiRequest"></param>
        public HttpRequest(FastCGI.Request fcgiRequest)
        {
            FcgiRequest = fcgiRequest;
            string httpMethod = fcgiRequest.GetParameterASCII("REQUEST_METHOD");

            switch (httpMethod)
            {
            case "GET":
                Method = HttpMethod.GET;
                break;

            case "POST":
                Method = HttpMethod.POST;
                break;

            case "PUT":
                Method = HttpMethod.PUT;
                break;

            case "DELETE":
                Method = HttpMethod.DELETE;
                break;

            default:
                Method = HttpMethod.UNKNOWN;
                break;
            }

            GET = new Dictionary <string, string>();
            // Todo: perform correct parsing and decoding according to HTTP standard
            var getParams = Encoding.ASCII.GetString(ServerParameters["QUERY_STRING"]).Split('&');

            foreach (var param in getParams)
            {
                if (param.Length > 0)
                {
                    var keyValue = param.Split(new char[] { '=' }, 2);
                    if (keyValue.Length >= 1)
                    {
                        var key   = keyValue[0];
                        var value = "";
                        if (keyValue.Length >= 2)
                        {
                            value = keyValue[1];
                        }
                        GET[key] = System.Uri.UnescapeDataString(value.Replace('+', ' '));
                    }
                }
            }

            POST = new Dictionary <string, string>();
            // Todo: perform correct parsing and decoding according to HTTP standard
            var postParams = Body.Split('&');

            foreach (var param in postParams)
            {
                if (param.Length > 0)
                {
                    var keyValue = param.Split(new char[] { '=' }, 2);
                    if (keyValue.Length >= 1)
                    {
                        var key   = keyValue[0];
                        var value = "";
                        if (keyValue.Length >= 2)
                        {
                            value = keyValue[1];
                        }
                        POST[key] = System.Uri.UnescapeDataString(value.Replace('+', ' '));
                    }
                }
            }

            if (ServerParameters.ContainsKey("HTTP_COOKIE"))
            {
                var cookieHeader = GetParameterASCII("HTTP_COOKIE");
                var cookies      = cookieHeader.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var cookie in cookies)
                {
                    var cookieNameValue = cookie.Split('=');
                    Cookies[cookieNameValue[0].Trim()] = System.Uri.UnescapeDataString(cookieNameValue[1].Trim().Replace('+', ' '));
                }
            }
        }