Created
November 25, 2010 14:02
-
-
Save motemen/715431 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// %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