private void InvokeResponseHandler(Dictionary<string, ResponseHandler> dict, HttpProcessor p) { foreach(KeyValuePair<string, ResponseHandler> pair in dict) { Match match = Regex.Match(p.http_path, pair.Key); if(match.Success) { try { pair.Value.Invoke(match.Groups, p); } catch { p.Respond(HttpStatusCode.InternalServerError, "Internal server error."); } return; } } p.Respond(HttpStatusCode.NotFound, "There's nothing here!"); }
internal void HandleGETRequest(HttpProcessor p) { InvokeResponseHandler(getUrlMap, p); }
internal void HandlePOSTRequest(HttpProcessor p) { InvokeResponseHandler(postUrlMap, p); }
/// <summary> /// Starts the server. /// /// Example usage: /// class MyServer : HttpServer {} /// ... /// MyServer server = new MyServer(8080); /// Thread serverThread = new Thread(new ThreadStarted(server.Listen)); /// serverThread.Start(); /// </summary> public void Listen() { listener = new TcpListener(System.Net.IPAddress.Any, port); listener.Start(); while(is_active) { TcpClient s = listener.AcceptTcpClient(); HttpProcessor processor = new HttpProcessor(s, this); Thread thread = new Thread(new ThreadStart(processor.Process)); thread.Start(); Thread.Sleep(1); } }