public ASPXHandler(String fileName, Dictionary<String, String> envVariables) { this.fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1); this.fullFilePath = fileName; this.fileExtension = this.fileName.Substring(this.fileName.IndexOf('.')); if (File.Exists(fileName)) { // Create an instance of Host class var aspxHost = new Host(); // Pass to it filename and query string this.responseBody = aspxHost.CreateHost( fileName, envVariables["server_root"], envVariables["query_string"]); this.responseHeader = new ResponseHeader("Status: 200 OK\r\nContent-Type: text/html\r\n"); this.responseHeader.setContentLength(this.responseBody.Length); } else { var error = new Error404(); this.responseHeader = error.getResponseHeader(); this.responseBody = error.getResponseBody(); } }
public FileHandler(String fileName) { this.contentTypes.Add(".txt", "text/plain"); this.contentTypes.Add(".html", "text/html"); this.contentTypes.Add(".htm", "text/html"); this.contentTypes.Add(".json", "application/json"); this.contentTypes.Add(".xml", "application/xml"); this.contentTypes.Add(".png", "image/png"); this.contentTypes.Add(".gif", "image/gif"); this.contentTypes.Add(".jpeg", "image/jpg"); this.contentTypes.Add(".jpg", "image/jpg"); this.contentTypes.Add(".css", "text/css"); this.contentTypes.Add(".js", "application/javascript"); this.fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1); this.fullFilePath = fileName; this.fileExtension = this.fileName.Substring(this.fileName.LastIndexOf('.')); if (File.Exists(fileName)) { var fs = new FileStream(this.fullFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); this.responseBody = new byte[fs.Length]; this.responseBodyLength = this.responseBody.Length; fs.Read(this.responseBody, 0, this.responseBodyLength); fs.Close(); string tmpHeader = "Status: 200 OK\r\n"; if (this.contentTypes.ContainsKey(this.fileExtension)) { tmpHeader += "Content-Type: " + this.contentTypes[this.fileExtension] + "\r\n"; } this.responseHeader = new ResponseHeader(tmpHeader); this.responseHeader.setContentLength(this.responseBodyLength); } else { var error = new Error404(); this.responseHeader = error.getResponseHeader(); this.responseBody = Encoding.ASCII.GetBytes(error.getResponseBody()); } }
public PHPHandler(String fileName, Dictionary<String, String> envVariables) { this.scriptName = fileName.Substring(fileName.LastIndexOf('\\') + 1); this.fullFilePath = fileName; if (File.Exists(fileName)) { Process proc = new Process(); proc.StartInfo.FileName = _config.Instance.CurrentConfig.WebHostPhpPath + "\\\\php-cgi.exe"; proc.StartInfo.EnvironmentVariables.Add("REMOTE_ADDR", envVariables["remote_addr"]); proc.StartInfo.EnvironmentVariables.Add("SCRIPT_NAME", this.scriptName); proc.StartInfo.EnvironmentVariables.Add("USER_AGENT", envVariables["user_agent"]); proc.StartInfo.EnvironmentVariables.Add("REQUEST_METHOD", envVariables["request_method"]); proc.StartInfo.EnvironmentVariables.Add("REFERER", envVariables["referer"]); proc.StartInfo.EnvironmentVariables.Add("SERVER_PROTOCOL", envVariables["server_protocol"]); proc.StartInfo.EnvironmentVariables.Add("QUERY_STRING", envVariables["query_string"]); proc.StartInfo.EnvironmentVariables.Add("HTTP_COOKIE", envVariables["cookie"]); proc.StartInfo.EnvironmentVariables.Add("SCRIPT_FILENAME", this.fullFilePath); proc.StartInfo.EnvironmentVariables.Add("REDIRECT_STATUS", "200"); proc.StartInfo.EnvironmentVariables.Add("CONTENT_LENGTH", envVariables["post"].Length.ToString()); proc.StartInfo.EnvironmentVariables.Add("CONTENT_TYPE", "application/x-www-form-urlencoded"); proc.StartInfo.EnvironmentVariables.Add("HTTP_RAW_POST_DATA", envVariables["post"]); if (proc.StartInfo.EnvironmentVariables.ContainsKey("PHPRC")) { proc.StartInfo.EnvironmentVariables["PHPRC"] = _config.Instance.CurrentConfig.WebHostPhpPath; } else { proc.StartInfo.EnvironmentVariables.Add("PHPRC", _config.Instance.CurrentConfig.WebHostPhpPath); } proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.CreateNoWindow = true; proc.Start(); proc.StandardInput.WriteLine(envVariables["post"]); this.phpOutput = proc.StandardOutput.ReadToEnd(); this.phpErrorOutput = proc.StandardError.ReadToEnd(); proc.Close(); this.responseHeaders = new ResponseHeader( this.phpOutput.Substring(0, this.phpOutput.IndexOf("\r\n\r\n")), this.fullFilePath); this.setResponseBody(this.phpOutput.Substring(this.phpOutput.IndexOf("\r\n\r\n"))); this.responseHeaders.setContentLength(this.getContentLength()); } else { var error = new Error404(); this.responseHeaders = error.getResponseHeader(); this.responseBody = error.getResponseBody(); } }
private void SendError404(ref Socket sockets) { try { var error = new Error404(); SendData(error.getResponseHeader().getResponseHeaders(), ref sockets); sockets.Close(); } catch (Exception ex) { Console.WriteLine(ex); } }