Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions WebServer/CWebTemplateProcessor.cs
Original file line number Diff line number Diff line change
@@ -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<string, string> 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);
}

}
}
27 changes: 17 additions & 10 deletions WebServer/CscriptProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public CscriptProcessor()
_parameters.CompilerOptions = "/t:library";
}

public ScriptResult ProcessScript(string path, IDictionary<string, string> requestParameters)


public WebResult ProcessScript(string path, IDictionary<string, string> requestParameters)
{
StringBuilder scriptBody = new StringBuilder();

Expand All @@ -65,10 +67,15 @@ public ScriptResult ProcessScript(string path, IDictionary<string, string> reque
}
}

return ProcessScriptString(scriptBody.ToString(), requestParameters);
}

public WebResult ProcessScriptString(string script, IDictionary<string, string> 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);
Expand All @@ -91,13 +98,13 @@ public ScriptResult ProcessScript(string path, IDictionary<string, string> reque
errorBody.Append("</body></html>");

/* 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;
Expand All @@ -123,20 +130,20 @@ public ScriptResult ProcessScript(string path, IDictionary<string, string> 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("<html><body><h1>Runtime Error</h1><p>The following runtime error occurred: {0}</p>",
Result = string.Format("<html><body><h1>Runtime Error</h1><p>The following runtime error occurred: {0}</p>",
e.InnerException.Message)
};
}
Expand Down
2 changes: 1 addition & 1 deletion WebServer/IScriptProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ namespace WebServer
*/
interface IScriptProcessor
{
ScriptResult ProcessScript(string path, IDictionary<string, string> requestParameters);
WebResult ProcessScript(string path, IDictionary<string, string> requestParameters);
}
}
3 changes: 2 additions & 1 deletion WebServer/SimpleWebServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CscriptProcessor.cs" />
<Compile Include="CWebTemplateProcessor.cs" />
<Compile Include="IScriptProcessor.cs" />
<Compile Include="ScriptResult.cs" />
<Compile Include="WebResult.cs" />
<Compile Include="WebServer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion WebServer/ScriptResult.cs → WebServer/WebResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace WebServer
{
public class ScriptResult
public class WebResult
{
public bool Error { get; set; }
public string Result { get; set; }
Expand Down
16 changes: 15 additions & 1 deletion WebServer/WebServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class WebServer
{
private readonly string _webRoot;
private IScriptProcessor _scriptProcessor;
private IScriptProcessor _webScriptProcessor;

static void Main(string[] args)
{
Expand All @@ -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 */
Expand Down Expand Up @@ -211,6 +213,11 @@ private void _ProcessBody(Socket socket, string path, Dictionary<string, string>
/* TODO: add another handler for processing web template files
* case ".csweb":
*/
case ".csweb":
{
_GenerateCsWebResult(socket, path, requestParameters);
return;
}
default:
type = "application/octet-stream";
break;
Expand Down Expand Up @@ -289,7 +296,7 @@ private void _SendResponse(Socket socket, byte[] body, string mimeType, Response
private void _GenerateScriptResult(Socket socket, string path, Dictionary<string, string> 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 */
Expand All @@ -303,5 +310,12 @@ private void _GenerateScriptResult(Socket socket, string path, Dictionary<string
_SendResponse(socket, Encoding.ASCII.GetBytes(result.Result), "text/html; charset=utf8", ResponseType.OK);
}
}

public void _GenerateCsWebResult(Socket socket, string path, Dictionary<string, string> requestParameters)
{
WebResult result = _webScriptProcessor.ProcessScript(path, requestParameters);

_SendResponse(socket, Encoding.ASCII.GetBytes(result.Result), "text/html; charset=utf8", result.Error ? ResponseType.ERROR : ResponseType.OK);
}
}
}