Example #1
0
        public Task Invoke(IDictionary <string, object> environment)
        {
            var path = Get <string>(environment, "owin.RequestPath");

            //get the cargo engine safely
            CargoEngine cargoEngine = null;

            try { cargoEngine = _getCargoEngine(); } catch { }

            if (cargoEngine != null)
            {
                string             cargoRoutePrefix = null;
                CargoConfiguration config           = null;

                //get configuration safely
                try
                {
                    config           = cargoEngine.Configuration;
                    cargoRoutePrefix = config?.CargoRoutePrefix;
                }
                catch { }

                //if the route prefix matches handle the request
                if (!string.IsNullOrEmpty(cargoRoutePrefix) &&
                    path.StartsWith(cargoRoutePrefix))
                {
                    bool   authorized   = cargoEngine.AuthenticateRequest(environment);
                    string strippedPath = path.Substring(cargoRoutePrefix.Length);

                    var method            = Get <string>(environment, "owin.RequestMethod");
                    var scheme            = Get <string>(environment, "owin.RequestScheme");
                    var headers           = Get <IDictionary <string, string[]> >(environment, "owin.RequestHeaders");
                    var pathBase          = Get <string>(environment, "owin.RequestPathBase");
                    var queryString       = Get <string>(environment, "owin.RequestQueryString");
                    var body              = Get <Stream>(environment, "owin.RequestBody");
                    var protocol          = Get <string>(environment, "owin.RequestProtocol");
                    var cancellationToken = Get <CancellationToken>(environment, "owin.CallCancelled");

                    switch (method)
                    {
                    case "HEAD":
                    case "GET":
                        return(HandleGetAsync(cargoEngine, environment, method == "HEAD", strippedPath, cancellationToken));

                    case "POST":
                        return(HandlePostAsync(cargoEngine, environment, strippedPath, cancellationToken));

                    default:
                        return(Return405Async(environment));
                    }
                }
            }

            //if we get here the request remained unhandled
            return(_next(environment));
        }
Example #2
0
 /// <summary>
 /// Creates a new <see cref="CargoEngine"/> with given <see cref="CargoConfiguration"/>.
 /// </summary>
 /// <param name="configuration"></param>
 public CargoEngine(CargoConfiguration configuration)
 {
     Configuration = configuration;
 }