diff --git a/WebServer/CWebTemplateProcessor.cs b/WebServer/CWebTemplateProcessor.cs new file mode 100644 index 0000000..e4468cd --- /dev/null +++ b/WebServer/CWebTemplateProcessor.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace WebServer +{ + class CWebTemplateProcessor : IScriptProcessor + { + + private CscriptProcessor _scriptProcessor; + + /* Constructor + */ + public CWebTemplateProcessor() + { + _scriptProcessor = new CscriptProcessor(); + } + + public WebResult ProcessScript(string path, IDictionary requestParameters) + { + StringBuilder finalContent = new StringBuilder(); + + StringBuilder scriptBody = new StringBuilder(); + + /* read the contents of the file into a string + * builder line by line to create a single + * string that represents the script */ + using (FileStream fs = File.OpenRead(path)) + { + StreamReader reader = new StreamReader(fs); + string line = null; + while ((line = reader.ReadLine()) != null) + { + scriptBody.Append(line); + } + } + + // Need to replace @{ xx } with { wout.WriteLine(xx); } + string content = scriptBody.ToString(); + int start = 0; + + while (content.Contains("@{")) + { + int i = content.IndexOf("@{"); + int j = content.IndexOf("}", i); + + i = i + 2; + + string literal = content.Substring(i, (j - i)); + content = content.Replace("@{" + literal + "}", "{ wout.WriteLine(" + literal + ");}"); + } + + while(content.Contains("{")) + { + int i = content.IndexOf("{"); + + finalContent.Append("wout.WriteLine(\"" + content.Substring(start, (i - start)) + "\");"); + + int j = content.IndexOf("}"); + + i = i + 1; + + finalContent.Append(content.Substring(i, (j - i))); + + j = j + 1; + + content = content.Remove(start, j); + } + + finalContent.Append("wout.WriteLine(\"" + content + "\");"); + + Console.WriteLine(finalContent.ToString()); + + return _scriptProcessor.ProcessScriptString(finalContent.ToString(), requestParameters); + } + + } +} diff --git a/WebServer/CscriptProcessor.cs b/WebServer/CscriptProcessor.cs index c69f6c5..486eddb 100644 --- a/WebServer/CscriptProcessor.cs +++ b/WebServer/CscriptProcessor.cs @@ -48,7 +48,9 @@ public CscriptProcessor() _parameters.CompilerOptions = "/t:library"; } - public ScriptResult ProcessScript(string path, IDictionary requestParameters) + + + public WebResult ProcessScript(string path, IDictionary requestParameters) { StringBuilder scriptBody = new StringBuilder(); @@ -65,10 +67,15 @@ public ScriptResult ProcessScript(string path, IDictionary reque } } + return ProcessScriptString(scriptBody.ToString(), requestParameters); + } + + public WebResult ProcessScriptString(string script, IDictionary requestParameters) + { /* combine the script string with the class template in order to create something * that can be compiled. NOTE that the class template provides the wout and * request variables */ - string source = _classTemplate.Replace("{0}", scriptBody.ToString()); + string source = _classTemplate.Replace("{0}", script); /* compile the generated source */ CompilerResults result = _compiler.CompileAssemblyFromSource(_parameters, source); @@ -91,13 +98,13 @@ public ScriptResult ProcessScript(string path, IDictionary reque errorBody.Append(""); /* the script result with the list of errors as the result */ - return new ScriptResult() + return new WebResult() { Error = true, Result = errorBody.ToString() }; } - + /* if the class compiled, use reflection to get the assembly and * instantiate the class */ System.Reflection.Assembly codeAssembly = result.CompiledAssembly; @@ -123,20 +130,20 @@ public ScriptResult ProcessScript(string path, IDictionary reque /* return a script result with the contents of the string writer which * should be the HTML (or whatever) that was produced by the script * and written to wout */ - return new ScriptResult() - { - Error = false, - Result = s.ToString() + return new WebResult() + { + Error = false, + Result = s.ToString() }; } catch (Exception e) { /* if the method cannot be invoked (i.e. runtime error) send back * a failed result with the runtime error */ - return new ScriptResult() + return new WebResult() { Error = true, - Result = string.Format("

Runtime Error

The following runtime error occurred: {0}

", + Result = string.Format("

Runtime Error

The following runtime error occurred: {0}

", e.InnerException.Message) }; } diff --git a/WebServer/IScriptProcessor.cs b/WebServer/IScriptProcessor.cs index 18a5e07..f0565cd 100644 --- a/WebServer/IScriptProcessor.cs +++ b/WebServer/IScriptProcessor.cs @@ -14,6 +14,6 @@ namespace WebServer */ interface IScriptProcessor { - ScriptResult ProcessScript(string path, IDictionary requestParameters); + WebResult ProcessScript(string path, IDictionary requestParameters); } } diff --git a/WebServer/SimpleWebServer.csproj b/WebServer/SimpleWebServer.csproj index 0741281..1fef0cb 100644 --- a/WebServer/SimpleWebServer.csproj +++ b/WebServer/SimpleWebServer.csproj @@ -42,8 +42,9 @@ + - + diff --git a/WebServer/ScriptResult.cs b/WebServer/WebResult.cs similarity index 88% rename from WebServer/ScriptResult.cs rename to WebServer/WebResult.cs index 676f18e..4ef5b30 100644 --- a/WebServer/ScriptResult.cs +++ b/WebServer/WebResult.cs @@ -6,7 +6,7 @@ namespace WebServer { - public class ScriptResult + public class WebResult { public bool Error { get; set; } public string Result { get; set; } diff --git a/WebServer/WebServer.cs b/WebServer/WebServer.cs index da3d74f..2977caa 100644 --- a/WebServer/WebServer.cs +++ b/WebServer/WebServer.cs @@ -30,6 +30,7 @@ class WebServer { private readonly string _webRoot; private IScriptProcessor _scriptProcessor; + private IScriptProcessor _webScriptProcessor; static void Main(string[] args) { @@ -53,6 +54,7 @@ public WebServer(int port, string root) /* this script processor instance will be used to process files of type * csscript */ _scriptProcessor = new CscriptProcessor(); + _webScriptProcessor = new CWebTemplateProcessor(); /*TODO: add another instance of a IScriptProcessor to handle files of * type csweb */ @@ -211,6 +213,11 @@ private void _ProcessBody(Socket socket, string path, Dictionary /* TODO: add another handler for processing web template files * case ".csweb": */ + case ".csweb": + { + _GenerateCsWebResult(socket, path, requestParameters); + return; + } default: type = "application/octet-stream"; break; @@ -289,7 +296,7 @@ private void _SendResponse(Socket socket, byte[] body, string mimeType, Response private void _GenerateScriptResult(Socket socket, string path, Dictionary requestParameters) { /* get a script result from the script processor using the request parameter dictionary */ - ScriptResult result = _scriptProcessor.ProcessScript(path, requestParameters); + WebResult result = _scriptProcessor.ProcessScript(path, requestParameters); /* if the result was an error, send an HTTP Error (500) along with a summary of * what went wrong as the body */ @@ -303,5 +310,12 @@ private void _GenerateScriptResult(Socket socket, string path, Dictionary requestParameters) + { + WebResult result = _webScriptProcessor.ProcessScript(path, requestParameters); + + _SendResponse(socket, Encoding.ASCII.GetBytes(result.Result), "text/html; charset=utf8", result.Error ? ResponseType.ERROR : ResponseType.OK); + } } }