コード例 #1
0
ファイル: FileHttpHandler.cs プロジェクト: veblush/aardwolf
        public static HandlerDispatcher.HandlerDelegate Handler(string basePath, string contentType, string fileExt)
        {
            return delegate(IHttpRequestContext request, UriTemplateMatch uriMatch)
            {
                var requestPath = uriMatch.BoundVariables["path"];
                var path = Path.Combine(basePath, requestPath) + fileExt;
                if (path.StartsWith(basePath) == false)
                {
                    var r = new ContentResponse(400, "No File", "");
                    return Task.FromResult<IHttpResponseAction>(r);
                }

                try
                {
                    var file = File.OpenRead(path);
                    var r = new FileContentResponse(200, "Description", contentType, file);
                    return Task.FromResult<IHttpResponseAction>(r);
                }
                catch (Exception)
                {
                    var r = new ContentResponse(400, "No File", "");
                    return Task.FromResult<IHttpResponseAction>(r);
                }
            };
        }
コード例 #2
0
ファイル: HandlerDispatcher.cs プロジェクト: veblush/aardwolf
        public async Task<IHttpResponseAction> Execute(IHttpRequestContext state)
        {
            var baseUri = new Uri(state.Request.Url.Scheme + "://" + state.Request.Url.Authority);

            // Find a handler matched by URI

            HandlerDelegate handler = null;
            UriTemplateMatch uriMatch = null;
            foreach (var i in _uriHandlers)
            {
                var match = i.UriTemplate.Match(baseUri, state.Request.Url);
                if (match != null)
                {
                    handler = i.Handler;
                    uriMatch = match;
                    break;
                }
            }

            if (handler == null)
            {
                var r = new ContentResponse(400, "Cannot find handler", "");
                return r;
            }

            // Handle request

            try
            {
                var r = await handler(state, uriMatch);
                return r;
            }
            catch (Exception e)
            {
                var r = new ContentResponse(500, "", "Error!\n" + e);
                return r;
            }
        }