Beispiel #1
0
 public async Task Something(Request req, Response res, string echo)
 {
     await res.Start();
     await res.Write("SomeOtherController.Something");
     await res.Write("\n" + echo);
     await res.Finish();
 }
Beispiel #2
0
 public async Task Index(Request req, Response res)
 {
     await res.Start();
     await res.Write("SomeController.Index");
     throw new Exception("bad news everyone");
     await res.Finish();
 }
Beispiel #3
0
        public Task Serve(Request req, Response res)
        {
            // Safely combine the two paths; this approach prevents
            // the combined path from getting escaping the base
            // directory.
            Uri fullpath;
            var path = req.Path;
            if (path[0] == '/') path = path.Substring(1); // Ensures we combine correctly
            if (!Uri.TryCreate(BasePath, path, out fullpath))
            {
                // If the URI is invalid for some reason, we'll throw
                // a 400 because that's just whacked out, man.
                return Error(res, 400, "<h1>Bad Request</h1><p>Invalid path.</p>");
            }

            // Load the file's info.  This will trigger any not found or no
            // permissions errors, so we'll handle them here.
            FileAttributes attributes = FileAttributes.Normal;
            try
            {
                attributes = File.GetAttributes(fullpath.AbsolutePath);
            }
            catch (Exception err)
            {
                if (err is ArgumentException || err is NotSupportedException)
                    return Error(res, 400, "<h1>Bad Request</h1><p>Path contains invalid characters.</p>");
                if (err is PathTooLongException)
                    return Error(res, 414, "<h1>Bad Request</h1><p>Path is too long.</p>");
                if (err is FileNotFoundException || err is DirectoryNotFoundException)
                    return Error(res, 404, "<h1>Not Found</h1>");
                if (err is IOException || err is UnauthorizedAccessException)
                    return Error(res, 403, "<h1>Forbidden</h1>");

                // We probably won't get here, because we've handed all documented
                // exceptions the function can raise.  However, for future compatibility
                // we'll keep this here.
                return Error(res, 500, "<h1>Internal Server Error");
            }

            // So we have the file info!  Fantastic.  If it's a directory
            // do a listing, otherwise serve it up.
            Console.WriteLine(fullpath.AbsolutePath);
            if ((attributes & FileAttributes.Directory) == FileAttributes.Directory)
                return ServeDirectory(res, fullpath.AbsolutePath);
            else
                return ServeFile(res, fullpath.AbsolutePath);
        }
Beispiel #4
0
 public CoolController(Request req, Response res)
 {
     Req = req;
     Res = res;
 }
Beispiel #5
0
 public async Task Some2(Request req, Response res)
 {
     await res.Start();
     await res.Write("SomeController.Index");
     await res.Finish();
 }