Provides mechanisms that allow to map a route to a route handler. This class is used mostly internally, except when adding post-route processors.
Ejemplo n.º 1
0
 public bool Remove(RouteMapper routeMapper)
 {
     lock (_lock)
     {
         return(_routes.Remove(routeMapper));
     }
 }
Ejemplo n.º 2
0
        internal void HandleRequest(HTTPRequest request, HTTPResponse response, Stream stream)
        {
            try
            {
                bool        matchFound = false;
                RouteMapper mapper     = null;
                int         i          = 0;

                for (;;)
                {
                    lock (_routes)
                    {
                        if (i < _routes.Count)
                        {
                            mapper = _routes[i++];
                        }
                        else
                        {
                            break;
                        }
                    }
                    string regex = "^" + mapper.Route;
                    if (!mapper.AcceptSubroutes)
                    {
                        regex += "$";
                    }
                    if (Regex.IsMatch(request.BaseURL, regex))
                    {
                        if ((mapper.Methods & request.Method) != 0)
                        {
                            if (mapper.AcceptSubroutes)
                            {
                                int pos = request.BaseURL.IndexOf(mapper.Route);
                                request.Subroute = request.BaseURL.Substring(0, pos) + request.BaseURL.Substring(pos + mapper.Route.Length);
                            }
                            matchFound = true;
                            break;
                        }
                    }
                }

                if (!matchFound)
                {
                    response.WriteError(HTTPStatusCode.NotFound, "Not Found");
                    response.Respond(stream);
                    OnError(RoutingErrorType.NO_MATCHES, "No route found for request", request);
                }
                else
                {
                    mapper.HandleRequest(request, response);
                    response.Respond(stream);
                }
            }
            catch (Exception e)
            {
                response.WriteError(HTTPStatusCode.InternalServerError, String.Format("Internal Server Error {0}", e.Message));
                response.Respond(stream);
                OnError(RoutingErrorType.INTERNAL, "A RouteHandler threw an exception.", request);
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Adds a route handler for the specified route and HTTP methods, using a class that
 /// implements the <see cref="IHTTPRouteHandler"/> delegate.
 /// </summary>
 /// <param name="route">The route, for example "/hello_world"</param>
 /// <param name="methods">
 /// The HTTP Methods to support. HTTPMethod has the <c>[Flag]</c> metadata,
 /// which makes it possible to support multiple methods in a single page.
 /// </param>
 /// <param name="handler">An object which implements the <see cref="IHTTPRouteHandler"/> interface</param>
 public RouteMapper Add(string route, HTTPMethod methods, IHTTPRouteHandler handler)
 {
     lock (_lock)
     {
         RouteMapper mapper = new RouteMapper(route, methods, handler);
         _routes.Add(mapper);
         return(mapper);
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Adds a route handler for the specified route and HTTP methods, using a delegate.
 /// <para>
 /// This is useful for quick, one-off serving of pages. For more complex cases, such as
 /// serving files from the file system, it is recommended to use the slightly more complex
 /// <see cref="IHTTPRouteHandler"/> interface.
 /// </para>
 /// </summary>
 /// <param name="route">The route, for example "/hello_world"</param>
 /// <param name="methods">
 /// The HTTP Methods to support. HTTPMethod has the <c>[Flag]</c> metadata,
 /// which makes it possible to support multiple methods in a single page.
 /// </param>
 /// <param name="handlerMethod">A method delegate. See <see cref="HandleRequestDelegate"/> for usage.</param>
 public RouteMapper Add(string route, HTTPMethod methods, HandleRequestDelegate handlerMethod, bool acceptSubroutes = false)
 {
     lock (_lock)
     {
         RouteHandlerDelegate newHandler = new RouteHandlerDelegate(handlerMethod, acceptSubroutes);
         RouteMapper          mapper     = new RouteMapper(route, methods, newHandler);
         _routes.Add(mapper);
         return(mapper);
     }
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Adds a route handler for the specified route and HTTP methods, using a delegate.
 /// <para>
 /// This is useful for quick, one-off serving of pages. For more complex cases, such as
 /// serving files from the file system, it is recommended to use the slightly more complex
 /// <see cref="IHTTPRouteHandler"/> interface.
 /// </para>
 /// </summary>
 /// <param name="route">The route, for example "/hello_world"</param>
 /// <param name="methods">
 /// The HTTP Methods to support. HTTPMethod has the <c>[Flag]</c> metadata,
 /// which makes it possible to support multiple methods in a single page.
 /// </param>
 /// <param name="handlerMethod">A method delegate. See <see cref="HandleRequestDelegate"/> for usage.</param>
 public RouteMapper Add(string route, HTTPMethod methods, HandleRequestDelegate handlerMethod, bool acceptSubroutes = false)
 {
     lock (_lock)
     {
         RouteHandlerDelegate newHandler = new RouteHandlerDelegate(handlerMethod, acceptSubroutes);
         RouteMapper mapper = new RouteMapper(route, methods, newHandler);
         _routes.Add(mapper);
         return mapper;
     }
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Adds a route handler for the specified route and HTTP methods, using a class that
 /// implements the <see cref="IHTTPRouteHandler"/> delegate.
 /// </summary>
 /// <param name="route">The route, for example "/hello_world"</param>
 /// <param name="methods">
 /// The HTTP Methods to support. HTTPMethod has the <c>[Flag]</c> metadata,
 /// which makes it possible to support multiple methods in a single page.
 /// </param>
 /// <param name="handler">An object which implements the <see cref="IHTTPRouteHandler"/> interface</param>
 public RouteMapper Add(string route, HTTPMethod methods, IHTTPRouteHandler handler)
 {
     lock (_lock)
     {
         RouteMapper mapper = new RouteMapper(route, methods, handler);
         _routes.Add(mapper);
         return mapper;
     }
 }
Ejemplo n.º 7
0
 public bool Remove(RouteMapper routeMapper)
 {
     lock (_lock)
     {
         return _routes.Remove(routeMapper);
     }
 }
Ejemplo n.º 8
0
 private void AddRoutes()
 {
     AllowCrossOrigin = true;
     _prefixRouteMapper = WebServer.Routes.Add(_routePrefix, HTTPMethod.OPTIONS, (Request, response) => { }, true);
     _prefixRouteMapper.AddProcessor(_accessControlProcessor);
     // meta route
     _metaRouteMapper = WebServer.Routes.Add(_routePrefix + WoopsaFormat.VerbMeta, HTTPMethod.GET,
         (request, response) => { HandleRequest(WoopsaVerb.Meta, request, response); }, true);
     _metaRouteMapper.AddProcessor(_accessControlProcessor);
     // read route
     _readRouteMapper = WebServer.Routes.Add(_routePrefix + WoopsaFormat.VerbRead, HTTPMethod.GET,
         (request, response) => { HandleRequest(WoopsaVerb.Read, request, response); }, true);
     _readRouteMapper.AddProcessor(_accessControlProcessor);
     // write route
     _writeRouteMapper = WebServer.Routes.Add(_routePrefix + WoopsaFormat.VerbWrite, HTTPMethod.POST,
         (request, response) => { HandleRequest(WoopsaVerb.Write, request, response); }, true);
     _writeRouteMapper.AddProcessor(_accessControlProcessor);
     // POST is used here instead of GET for two main reasons:
     //  - The length of a GET query is limited in HTTP. There is no official limit but most
     //    implementations have a 2-8 KB limit, which is not good when we want to do large
     //    multi-requestsList, for example
     //  - GET requestsList should not change the state of the server, as they can be triggered
     //    by crawlers and such. Invoking a function will, in most cases, change the state of
     //    the server.
     _invokeRouteMapper = WebServer.Routes.Add(_routePrefix + WoopsaFormat.VerbInvoke, HTTPMethod.POST,
         (request, response) => { HandleRequest(WoopsaVerb.Invoke, request, response); }, true);
     _invokeRouteMapper.AddProcessor(_accessControlProcessor);
 }