Example #1
0
        /// <summary>
        /// Add a request handler
        /// </summary>
        /// <param name="method">HTTP verb to match, or null to skip verb matching</param>
        /// <param name="contentType">Content-Type header to match, or null to skip Content-Type matching</param>
        /// <param name="path">Request URI path regular expression to match, or null to skip URI path matching</param>
        /// <param name="callback">Callback to fire when an incoming request matches the given pattern</param>
        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));
        }
Example #2
0
 /// <summary>
 /// Default constructor
 /// </summary>
 /// <param name="signature">Signature pattern for matching against incoming requests</param>
 /// <param name="callback">Callback for handling the request</param>
 public HttpRequestHandler(HttpRequestSignature signature, HttpRequestCallback callback)
 {
     Signature = signature;
     Callback  = callback;
 }
Example #3
0
        void ProcessRequest(IHttpClientContext context, IHttpRequest request)
        {
            LogWriter.Write(this, LogPrio.Trace, "Processing request...");

            IHttpResponse response = request.CreateResponse(context);

            try
            {
                // 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
                bool found = false;
                for (int i = 0; i < _requestHandlers.Length; i++)
                {
                    HttpRequestHandler handler = _requestHandlers[i];

                    if (signature == handler.Signature)
                    {
                        FireRequestCallback(context, request, response, handler.Callback);
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    // No registered handler matched this request's signature
                    if (_notFoundHandler != null)
                    {
                        FireRequestCallback(context, request, response, _notFoundHandler);
                    }
                    else
                    {
                        // Send a default 404 response
                        try
                        {
                            response.Status = HttpStatusCode.NotFound;
                            response.Reason = String.Format("No request handler registered for Method=\"{0}\", Content-Type=\"{1}\", Path=\"{2}\"",
                                                            signature.Method, signature.ContentType, signature.Path);
                            string notFoundResponse = "<html><head><title>Page Not Found</title></head><body><h3>" + response.Reason + "</h3></body></html>";
                            byte[] buffer           = System.Text.Encoding.UTF8.GetBytes(notFoundResponse);
                            response.Body.Write(buffer, 0, buffer.Length);
                            response.Send();
                        }
                        catch (Exception) { }
                    }
                }
            }
            catch (Exception err)
            {
                ThrowException(err);

                bool errorResponse = true;

                Exception e = err;
                while (e != null)
                {
                    if (e is SocketException)
                    {
                        errorResponse = false;
                        break;
                    }

                    e = e.InnerException;
                }

                if (errorResponse)
                {
                    try
                    {
#if DEBUG
                        context.Respond(HttpHelper.HTTP11, HttpStatusCode.InternalServerError, "Internal server error", err.ToString(), "text/plain");
#else
                        context.Respond(HttpHelper.HTTP10, HttpStatusCode.InternalServerError, "Internal server error");
#endif
                    }
                    catch (Exception err2)
                    {
                        LogWriter.Write(this, LogPrio.Fatal, "Failed to respond on message with Internal Server Error: " + err2);
                    }
                }
            }

            request.Clear();
            LogWriter.Write(this, LogPrio.Trace, "...done processing request.");
        }