public ServerCallback RouteServer(Routes routes) { return(request => { if (request.Method != "GET") { Error("Request method is not GET", 501); return new Response(body: PageCreator.Error(501), status: 501, contentType: "text/html"); } if (routes.ContainsKey(request.Path)) { Log("Trying to serve " + request.Path); return routes[request.Path](request); } else { try { Error("Requested page " + request.Path + " not found", 404); return new Response(body: PageCreator.Error(404), status: 404, contentType: "text/html"); } catch (ArgumentException) { Error("Could not create error page", 500); return new Response(body: File.ReadAllText("Vitesse/defaultError.html"), status: 500, contentType: "text/html"); } } }); }
public void Proxy() { this.Listener.Start(); Log("Listening at " + this.ServingURL); HttpListenerContext context; HttpListenerRequest request; HttpListenerResponse response; while (this.Listener.IsListening) { try { context = this.Listener.GetContext(); request = context.Request; response = context.Response; try { Log("Trying to form gateway to " + request.RawUrl); WebRequest req = WebRequest.Create(request.RawUrl); HttpWebResponse res = (HttpWebResponse)req.GetResponse(); Stream resStream = res.GetResponseStream(); response.ContentType = res.ContentType; response.StatusCode = 200; response.ContentEncoding = Encoding.UTF8; response.Headers = res.Headers; resStream.CopyTo(response.OutputStream); } catch (System.Net.WebException e) { Error(e.ToString(), 502); byte[] buf = Encoding.UTF8.GetBytes(PageCreator.Error(502)); response.ContentType = "text/html"; response.StatusCode = 502; response.ContentEncoding = Encoding.UTF8; response.ContentLength64 = buf.Length; response.OutputStream.Write(buf, 0, buf.Length); } finally { response.OutputStream.Close(); } } catch (Exception e) { Error(e.ToString()); this.Listener.Stop(); } } }
public ServerCallback StaticServer(Routes routes = null, int parentDirectories = 0, string anchor = "index.html") { return(request => { routes = routes ?? new Routes(); if (routes.ContainsKey(request.Path)) { Log("Trying to serve " + request.Path); return routes[request.Path](request); } else { if (request.Method != "GET") { Error("Request method is not GET for requested page " + request.Path, 501); return new Response(body: PageCreator.Error(501), status: 501, contentType: "text/html"); } try { StringBuilder pathPrefix = new StringBuilder(); for (int i = 0; i < parentDirectories; i++) { pathPrefix.Append("../"); } string fileName = request.Path == "/" ? anchor : request.Path.Substring(1); string text; try { text = File.ReadAllText(pathPrefix.ToString() + fileName); Log("Trying to serve " + pathPrefix.ToString() + fileName); return new Response(body: text, contentType: ServerUtil.Convert.MimeType(fileName.Split('.').Last())); } catch (DirectoryNotFoundException) { throw new FileNotFoundException(); } } catch (FileNotFoundException) { try { if (request.Path == "/favicon.ico") { Log("Trying to serve /favicon.ico"); return new Response(contentType: "text/html", redirect: "https://higgy.s3.amazonaws.com/images/v_italic.jpg", status: 302); } else { Error("Requested page " + request.Path + " not found", 404); return new Response(body: PageCreator.Error(404), status: 404, contentType: "text/html"); } } catch (ArgumentException) { Error("Could not create error page", 500); return new Response(body: File.ReadAllText("Vitesse/defaultError.html"), status: 500, contentType: "text/html"); } } } }); }