// the method that handles an http request public void Run() { Request req = Request.Parse(stream); if (req == null || (req.type != "GET" && req.type != "POST" && req.type != "HEAD")) { Respond(400, null, "", true); client.Close(); return; } Headers headers = Headers.Parse(stream); string post = ""; if (req.type == "POST" && headers.Get("content-length") != null && headers.Get("content-length") != "0") { post = ParsePostData(Int32.Parse(headers.Get("content-length"))); } if (req.path.IndexOf("..") != -1) { Respond(403, null, "", true); client.Close(); return; } if (req.path == "/") { req.path = "/" + DEFAULT_PAGE; } string returnText = ""; string fullPath = GetFullPath(req.path); if (File.Exists(fullPath)) { if (IsPHP(fullPath)) { returnText = PHPHandler.Run(fullPath, req, headers, post); } else { returnText = File.ReadAllText(fullPath); } } if (returnText != null) { Respond(200, null, req.type == "HEAD" ? "" : returnText, !IsPHP(fullPath)); } else { Respond(404, null, "", !IsPHP(fullPath)); } client.Close(); }
public static string Run(string fullPath, Request req, Headers headers, string post) { if (!File.Exists(fullPath)) { return(null); } string filename = Path.GetFileName(fullPath); ProcessStartInfo info = new ProcessStartInfo(); info.FileName = exePath; info.WorkingDirectory = Connection.GetRootPath(); info.Arguments = ""; info.UseShellExecute = false; info.RedirectStandardInput = true; info.RedirectStandardOutput = true; info.CreateNoWindow = true; info.EnvironmentVariables["CONTENT_LENGTH"] = post != null?post.Length.ToString() : "0"; info.EnvironmentVariables["CONTENT_TYPE"] = "application/x-www-form-urlencoded"; info.EnvironmentVariables["DOCUMENT_ROOT"] = Connection.GetRootPath(); info.EnvironmentVariables["GATEWAY_INTERFACE"] = "CGI/1.1"; info.EnvironmentVariables["HTTP_ACCEPT"] = headers.Get("accept"); info.EnvironmentVariables["HTTP_ACCEPT_CHARSET"] = headers.Get("accept-charset"); info.EnvironmentVariables["HTTP_ACCEPT_ENCODING"] = headers.Get("accept-encoding"); info.EnvironmentVariables["HTTP_ACCEPT_LANGUAGE"] = headers.Get("accept-language"); info.EnvironmentVariables["HTTP_CONNECTION"] = headers.Get("connection"); info.EnvironmentVariables["HTTP_COOKIE"] = headers.Get("cookie"); info.EnvironmentVariables["HTTP_HOST"] = headers.Get("host"); info.EnvironmentVariables["HTTP_REFERER"] = headers.Get("referer"); info.EnvironmentVariables["HTTP_USER_AGENT"] = headers.Get("user-agent"); info.EnvironmentVariables["PHP_SELF"] = req.path; info.EnvironmentVariables["QUERY_STRING"] = req.query; info.EnvironmentVariables["REDIRECT_STATUS"] = "true"; info.EnvironmentVariables["REMOTE_ADDR"] = "127.0.0.1"; info.EnvironmentVariables["REQUEST_METHOD"] = post != null && post != "" ? "POST" : "GET"; info.EnvironmentVariables["REQUEST_TIME"] = DateTime.UtcNow.ToString(); info.EnvironmentVariables["REQUEST_URI"] = "/" + filename; info.EnvironmentVariables["SCRIPT_FILENAME"] = fullPath; info.EnvironmentVariables["SERVER_ADDR"] = "127.0.0.1"; info.EnvironmentVariables["SERVER_NAME"] = "localhost"; info.EnvironmentVariables["SERVER_PROTOCOL"] = Connection.PROTOCOL; info.EnvironmentVariables["SERVER_SOFTWARE"] = Server.NAME; info.EnvironmentVariables["SERVER_PORT"] = "80"; var proc = Process.Start(info); proc.StandardInput.Write(post); string ret = proc.StandardOutput.ReadToEnd(); proc.Close(); return(ret); }