public void AddHandler(string method, string contentType, string path, bool exactPath, bool sendResponseAfterCallback, HttpRequestCallback callback) { HttpRequestSignature signature = new HttpRequestSignature(method, contentType, path, exactPath); HttpRequestHandler handler = new HttpRequestHandler(signature, callback, sendResponseAfterCallback); lock (m_handlersWriteLock) { HttpRequestHandler[] newHandlers = new HttpRequestHandler[m_requestHandlers.Length + 1]; for (int i = 0; i < m_requestHandlers.Length; i++) { newHandlers[i] = m_requestHandlers[i]; } newHandlers[m_requestHandlers.Length] = handler; m_requestHandlers = newHandlers; } }
private void RequestReceivedHandler(object sender, RequestEventArgs e) { IHttpClientContext context = (IHttpClientContext)sender; IHttpRequest request = e.Request; IHttpResponse response = request.CreateResponse(context); // Load cookies if they exist RequestCookies cookies = (request.Headers["cookie"] != null) ? new RequestCookies(request.Headers["cookie"]) : new RequestCookies(String.Empty); request.SetCookies(cookies); // Create a request signature HttpRequestSignature signature = new HttpRequestSignature(request); // Look for a signature match in our handlers HttpRequestHandler foundHandler = null; for (int i = 0; i < m_requestHandlers.Length; i++) { HttpRequestHandler handler = m_requestHandlers[i]; if (signature == handler.Signature) { foundHandler = handler; break; } } if (foundHandler != null) { FireRequestCallback(context, request, response, foundHandler); } else { FireRequestCallback(context, request, response, m_notFoundHandler); } }
public bool Start(Simian simian) { int port = DEFAULT_HTTP_PORT; string hostname = null; string sslCertFile = null; IPHostEntry entry; IPAddress address; // Create a logger for the HTTP server HttpLogWriter httpLogger = new HttpLogWriter(m_log); // Create a default 404 handler m_notFoundHandler = new HttpRequestHandler(null, Default404Handler, true); #region Config Variables IConfig config = simian.Config.Configs["HTTP"]; if (config != null) { port = config.GetInt("ListenPort", DEFAULT_HTTP_PORT); hostname = config.GetString("Hostname", null); sslCertFile = config.GetString("SSLCertFile", null); } if (String.IsNullOrEmpty(hostname)) { hostname = Dns.GetHostName(); entry = Dns.GetHostEntry(hostname); address = IPAddress.Any; } else { entry = Dns.GetHostEntry(hostname); if (entry != null && entry.AddressList.Length > 0) { address = entry.AddressList[0]; } else { m_log.Warn("Could not resolve an IP address from hostname " + hostname + ", binding to all interfaces"); address = IPAddress.Any; } } #endregion Config Variables #region Initialization if (!String.IsNullOrEmpty(sslCertFile)) { // HTTPS mode try { m_sslCertificate = new X509Certificate2(sslCertFile); } catch (Exception ex) { m_log.Error("Failed to load SSL certificate file \"" + sslCertFile + "\": " + ex.Message); return(false); } m_uri = new Uri("https://" + hostname + (port != 80 ? (":" + port) : String.Empty)); m_httpServer = HttpServer.HttpListener.Create(address, port, m_sslCertificate, RemoteCertificateValidationHandler, SslProtocols.Default, false); } else { // HTTP mode m_uri = new Uri("http://" + hostname + (port != 80 ? (":" + port) : String.Empty)); m_httpServer = HttpServer.HttpListener.Create(address, port); } m_httpServer.LogWriter = httpLogger; m_httpServer.RequestReceived += RequestReceivedHandler; m_httpServer.Start(64); m_log.Info("HTTP server is listening at " + m_uri); #endregion Initialization return(true); }
private void FireRequestCallback(IHttpClientContext client, IHttpRequest request, IHttpResponse response, HttpRequestHandler handler) { try { handler.Callback(client, request, response); } catch (Exception ex) { m_log.Error("Exception in HTTP handler: " + ex); response.Status = HttpStatusCode.InternalServerError; response.Send(); } if (handler.SendResponseAfterCallback && !response.Sent) { try { response.Send(); } catch (Exception ex) { m_log.ErrorFormat("Failed to send HTTP response for request to {0}: {1}", request.Uri, ex.Message); } } request.Clear(); }
public void Set404Handler(HttpRequestCallback callback) { m_notFoundHandler = new HttpRequestHandler(null, callback, true); }