Created
December 20, 2010 21:25
-
-
Save loudej/749020 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| public Result App1(IDictionary<string, object> env) { | |
| var request = new Request(env); | |
| request.Logger.Info("Calling " + request.ScriptName); | |
| return new Result( | |
| 200, | |
| new Dictionary<string, object>{{"Content-Type","text/plain"}}, | |
| new[]{"<p>You are looking at", request.ScriptName, "</p>"}); | |
| } | |
| public Result App2(IDictionary<string, object> env) { | |
| var request = new Request(env); | |
| var response = new Response(); | |
| request.Logger.Info("Calling " + request.ScriptName); | |
| if (request.ScriptName != "/") { | |
| response.Redirect("/"); | |
| } | |
| else { | |
| response.ContentType = "text/plain"; | |
| response.Write("<p>Hello world!</p>"); | |
| } | |
| return response.Finish(); | |
| } |
This file contains hidden or 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
| // create hash from server variables | |
| IDictionary<string, object> env = http.Request.ServerVariables.AllKeys.ToDictionary( | |
| key => key, | |
| key => (object)http.Request.ServerVariables.Get(key)); | |
| // assign all required values | |
| new Context(env) { | |
| Version = new[] { 1, 1 }, | |
| MultiProcess = false, | |
| MultiThread = true, | |
| RunOnce = false, | |
| Errors = Console.Error, | |
| UrlScheme = http.Request.Url.Scheme, | |
| Input = http.Request.InputStream, | |
| }; | |
| // send to middleware/app tree | |
| var result = app(env); | |
This file contains hidden or 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
| public abstract class EnvironmentAccessor { | |
| readonly IDictionary<string, object> _env; | |
| protected EnvironmentAccessor(IDictionary<string, object> env) { | |
| _env = env; | |
| } | |
| protected T Get<T>(string rackMultithread) { | |
| object value; | |
| return _env.TryGetValue(rackMultithread, out value) ? (T)value : default(T); | |
| } | |
| protected void Set<T>(string rackMultithread, T value) { | |
| _env[rackMultithread] = value; | |
| } | |
| } | |
| public class Context : EnvironmentAccessor { | |
| public Context(IDictionary<string, object> env) : base(env) { } | |
| public int[] Version { get { return Get<int[]>("rack.version"); } set { Set("rack.version", value); } } | |
| public string UrlScheme { get { return Get<string>("rack.url_scheme"); } set { Set("rack.url_scheme", value); } } | |
| public Stream Input { get { return Get<Stream>("rack.input"); } set { Set("rack.input", value); } } | |
| public TextWriter Errors { get { return Get<TextWriter>("rack.errors"); } set { Set("rack.errors", value); } } | |
| public bool MultiThread { get { return Get<bool>("rack.multithread"); } set { Set("rack.multithread", value); } } | |
| public bool MultiProcess { get { return Get<bool>("rack.multiprocess"); } set { Set("rack.multiprocess", value); } } | |
| public bool RunOnce { get { return Get<bool>("rack.run_once"); } set { Set("rack.run_once", value); } } | |
| public ISession Session { get { return Get<ISession>("rack.session"); } set { Set("rack.session", value); } } | |
| public ILogger Logger { get { return Get<ILogger>("rack.logger"); } set { Set("rack.logger", value); } } | |
| } | |
| public class Request : EnvironmentAccessor { | |
| public Request(IDictionary<string, object> env) : base(env) { } | |
| public Stream Body { get { return Get<Stream>("rack.input"); } } | |
| public string ScriptName { get { return Get<string>("SCRIPT_NAME"); } } | |
| public string PathInfo { get { return Get<string>("PATH_INFO"); } } | |
| public string RequestMethod { get { return Get<string>("REQUEST_METHOD"); } } | |
| public string QueryString { get { return Get<string>("QUERY_STRING"); } } | |
| public string ContentLength { get { return Get<string>("CONTENT_LENGTH"); } } | |
| public string ContentType { get { return Get<string>("CONTENT_TYPE"); } } | |
| public ISession Session { get { return Get<ISession>("rack.session"); } } | |
| public ILogger Logger { get { return Get<ILogger>("rack.logger"); } } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment