/// <summary> /// Shows the response to an HTTP request on the screen. /// </summary> /// <param name="server">The <see cref="T:Stumps.StumpsServer"/> that processed the request.</param> /// <param name="e">The <see cref="StumpsContextEventArgs"/> instance containing the event data.</param> public static void ShowHttpResponse(StumpsServer server, StumpsContextEventArgs e) { var message = FriendlyOrigin[e.ResponseOrigin]; if (e.ResponseOrigin == HttpResponseOrigin.Stump) { message += e.StumpId; } else if (e.ResponseOrigin == HttpResponseOrigin.RemoteServer) { message += server.RemoteHttpServer.DnsSafeHost; } Console.WriteLine("[{0}] => {1} {2}", message, e.Context.Request.HttpMethod, e.Context.Request.RawUrl); }
/// <summary> /// Defines the entry point of the application. /// </summary> /// <param name="args">The command-line arguments.</param> public static void Main(string[] args) { ConsoleHelper.ApplicationBanner("Hello World API"); // Create a new Stumps Server var server = new StumpsServer(); // An open port will be chosen automatically unless specified // server.ListensOnPort = 9100; // Create a new Stump for the server var stump = new Stump("HelloWorldStump"); // Add two rules that stumps out HTTP GET requests for the url /HeloWorld.htm stump.AddRule(new HttpMethodRule("GET")); stump.AddRule(new UrlRule("/HelloWorld.htm")); // Create a response for the rule var response = new BasicHttpResponse(); response.Headers["Content-Type"] = "text/html;charset=UTF-8"; response.AppendToBody( "<html><header><title>Stumps Hello World</title></header><body><p>Hello From Stumps</p></body></html>"); // Add the response to the stump stump.Response = response; // Add the stump to the server server.AddStump(stump); // Show the requests that are incomming server.RequestProcessed += (o, e) => ConsoleHelper.ShowHttpResponse(server, e); // Start the server and wait! server.Start(); // Show the URL to the user Console.WriteLine("Browse to http://localhost:{0}/HelloWorld.htm", server.ListeningPort); Console.WriteLine(); // Wait to exit ConsoleHelper.WaitForExit(); server.Shutdown(); server.Dispose(); }
/// <summary> /// Defines the entry point of the application. /// </summary> /// <param name="args">The command-line arguments.</param> public static void Main(string[] args) { ConsoleHelper.ApplicationBanner("Hello World Fluent API"); // Create a new Stumps Server var server = new StumpsServer(); // Showing off some chaining: Make the server return an HTTP 404 for all unknown // and then when the URL /HelloWorld.htm is requested, return back an HTML page // that was loaded from a file, but the response is delayed by 2000 milliseconds. server.RespondsWithHttp404() .HandlesRequest("HelloWorld").MatchingMethod("GET") .MatchingUrl("/HelloWorld.htm") .DelayedBy(2000) .Responds().WithFile("HelloWorld.htm"); // Showing off the ability to drop a connection for an incomming URL server.HandlesRequest("HelloDrop").MatchingMethod("GET") .MatchingUrl("/HelloDrop.htm") .DropsConnection(); // Showing off a stump // Show the requests that are incomming server.RequestProcessed += (o, e) => ConsoleHelper.ShowHttpResponse(server, e); // Start the server and wait! server.Start(); // Show the URL to the user Console.WriteLine("Browse to http://localhost:{0}/HelloWorld.htm", server.ListeningPort); Console.WriteLine(); // Wait to exit ConsoleHelper.WaitForExit(); server.Shutdown(); server.Dispose(); }