public void AddHandler(string method, string contentType, string path, HttpRequestCallback callback) { HttpRequestSignature signature = new HttpRequestSignature(); signature.Method = method; signature.ContentType = contentType; signature.Path = path; AddHandler(new HttpRequestHandler(signature, callback)); }
public EventQueueServer(HttpServer server, string path) { HttpRequestSignature signature = new HttpRequestSignature(); signature.Method = "post"; signature.ContentType = String.Empty; signature.Path = path; HttpServer.HttpRequestCallback callback = new HttpServer.HttpRequestCallback(EventQueueHandler); HttpServer.HttpRequestHandler handler = new HttpServer.HttpRequestHandler(signature, callback); server.AddHandler(handler); }
protected void EventQueueHandler(HttpRequestSignature signature, ref HttpListenerContext context) { }
public HttpRequestHandler(HttpRequestSignature signature, HttpRequestCallback callback) { Signature = signature; Callback = callback; }
protected void BeginGetContextCallback(IAsyncResult result) { HttpListenerContext context = null; // Retrieve the incoming request try { context = server.EndGetContext(result); } catch (Exception) { } if (isRunning) { // Immediately start listening again try { server.BeginGetContext(serverCallback, server); } catch (Exception) { // Something went wrong, can't resume listening. Bail out now // since this is a shutdown (whether it was meant to be or not) return; } // Process the incoming request if (context != null) { // Create a request signature HttpRequestSignature signature = new HttpRequestSignature(context); // Look for a signature match in our handlers for (int i = 0; i < requestHandlers.Count; i++) { HttpRequestHandler handler = requestHandlers[i]; if (signature == handler.Signature) { // Request signature matched, handle it try { handler.Callback(signature, ref context); } catch (Exception e) { try { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; context.Response.StatusDescription = e.ToString(); } catch (Exception) { Logger.Log(e.ToString(), Helpers.LogLevel.Error); } } finally { context.Response.Close(); } return; } } // No registered handler matched this request's signature. Send a 404 context.Response.StatusCode = (int)HttpStatusCode.NotFound; context.Response.StatusDescription = String.Format( "No request handler registered for Method=\"{0}\", Content-Type=\"{1}\", Path=\"{2}\"", signature.Method, signature.ContentType, signature.Path); context.Response.Close(); } } }