public HttpTransaction(HttpServer server, ITcpSocket socket, HttpConnectionCallback callback, bool closeOnEnd = false) { Server = server; Socket = socket; this.closeOnEnd = closeOnEnd; Context = server.Context; ConnectionCallback = callback; gc_handle = GCHandle.Alloc(this); Request = new HttpRequest(this, socket); Request.Read(Close); }
public HelloWorld() { Route ("/", ctx => ctx.Response.End ("Hello, World")); Route ("/shutdown", ctx => System.Environment.Exit (0)); Route ("/info", ctx => { ctx.Response.Write ("Host: {0}", ctx.Request.Headers ["Host"]); ctx.Response.Write ("Remote Address: {0}", ctx.Request.Socket.Address); ctx.Response.Write ("Referer: '{0}'", ctx.Request.Headers ["Referer"]); ctx.Response.End (); }); Route ("/timeout", ctx => { ctx.Response.WriteLine ("Hello"); AddTimeout (TimeSpan.FromSeconds (2), (app, data) => { Console.WriteLine ("writing world."); ctx.Response.WriteLine ("World"); ctx.Response.End (); }); }); Route ("/encoding", ctx => { ctx.Response.ContentEncoding = System.Text.Encoding.UTF8; ctx.Response.SetHeader ("Content-Type","text/html; charset=UTF-8"); ctx.Response.Write ("À"); Console.WriteLine ("Writing: '{0}'", "À"); ctx.Response.End (); }); Get ("/upload", ctx => { ctx.Response.Write ("<html><head></head><body>"); ctx.Response.Write ("<a href='/info'>a link</a>"); ctx.Response.Write ("<form method=\"POST\">"); ctx.Response.Write ("<input type=\"text\" name=\"some_name\"><br>"); ctx.Response.Write ("<input type=\"file\" name=\"the_file\" >"); ctx.Response.Write ("<input type=\"file\" name=\"the_other_file\" >"); ctx.Response.Write ("<input type=\"submit\">"); ctx.Response.Write ("</form>"); ctx.Response.Write ("</body></html>"); ctx.Response.End (); }); Put ("/put", ctx => { Console.WriteLine ("got the PUT request"); ctx.Response.End ("this is some stuff!"); }); Get ("/request", ctx => { var r = new HttpRequest ("http://127.0.0.1:8080/put"); r.Method = HttpMethod.HTTP_PUT; Console.WriteLine ("got the request"); r.OnResponse += (response) => { Console.WriteLine ("got the response"); ctx.Response.End (response.PostBody); }; r.Execute (); }); int count = 0; Get ("/request_loop", ctx => { if (++count == 3) { ctx.Response.End ("Got this page three times!"); return; } }); Get ("/sync", ctx => { new System.Threading.Thread (() => AsyncCounter (ctx)).Start (); }); Post ("/upload", ctx => { ctx.Response.End ("handled upload!"); }); }