Skip to content

Instantly share code, notes, and snippets.

@motemen
Created November 25, 2010 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motemen/715431 to your computer and use it in GitHub Desktop.
Save motemen/715431 to your computer and use it in GitHub Desktop.
// %WINDIR%\Microsoft.NET\Framework\v3.5\csc.exe /r:Microsoft.JScript.dll JScriptHTTPGateway.cs
using System;
using System.Net;
using System.CodeDom.Compiler;
using System.Reflection;
class JScriptHTTPGateway {
static void Main (string[] args) {
Microsoft.JScript.JScriptCodeProvider jsProvider = new Microsoft.JScript.JScriptCodeProvider();
CompilerParameters compilerParams = new CompilerParameters();
compilerParams.GenerateExecutable = true;
compilerParams.GenerateInMemory = true;
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://" + args[0] + "/");
listener.Start();
Console.WriteLine("Listening on http://" + args[0] + "/");
while (true) {
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
System.IO.Stream inputStream = request.InputStream;
System.IO.StreamReader reader = new System.IO.StreamReader(inputStream);
string content = reader.ReadToEnd();
System.IO.Stream outputStream = response.OutputStream;
System.IO.StreamWriter writer = new System.IO.StreamWriter(outputStream);
Console.WriteLine("{0} - {1}", request.UserHostAddress, content);
CompilerResults result = jsProvider.CompileAssemblyFromSource(
compilerParams, "package JScriptHTTPGateway { class Evaluator { public static function evaluate () { " + content + " } } }"
);
if (result.Errors.Count > 0) {
response.StatusCode = 400;
foreach (CompilerError e in result.Errors) {
writer.WriteLine(e);
}
} else {
try {
Type evaluatorClass = result.CompiledAssembly.GetType("JScriptHTTPGateway.Evaluator");
object evalResult = evaluatorClass.InvokeMember(
"evaluate", BindingFlags.InvokeMethod, null, null, new object[] {}
);
writer.WriteLine(evalResult);
} catch (Exception e) {
response.StatusCode = 500;
writer.WriteLine(e);
}
}
writer.Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment