示例#1
0
        public async Task Respond(HttpContext context)
        {
            // Get path
            Match match = new Regex(_request_regex_pattern).Match(context.Request.Path);

            int length = match.Groups.Count;
            if (length < 2)
                length = 2;
            string[] matches = new string[length];
            matches[1] = "";
            for (int i = 0; i < match.Groups.Count; i++)
            {
                matches[i] = match.Groups[i].Value;
            }
            string file_path = String.Format(_file_directory, matches);
             
            // Check existance
            if (!File.Exists(file_path))
            {
                IHttpHandler error_handler = new ErrorHandler(404);
                await error_handler.Respond(context);
                return;
            }

            // Change content type if needed
            string ext = Path.GetExtension(file_path);
            if (_content_types.ContainsKey(ext))
            {
                context.Response.ContentType = _content_types[ext];
            }
            
            await context.Response.SendFile(file_path);
        }
示例#2
0
 public static HandlerCreater Create(int code)
 {
     return (Application app) =>
     {
         ErrorHandler handler = new ErrorHandler(code);
         handler._app = app;
         return handler;
     };
 }