- @override
Overridden by subclasses to modify or respond to an incoming request.
Subclasses override this method to provide their specific handling of a request. A RequestController
should either modify or respond to the request. For concrete subclasses of RequestController
- like HTTPController
-
this method has already been implemented.
RequestController
s should return a Response
from this method if they responded to the request.
If a RequestController
does not respond to the request, but instead modifies it, this method must return the same Request
.
Source
@override Future<RequestOrResponse> processRequest(Request request) async { if (request.innerRequest.method.toLowerCase() != "get") { return new Response(HttpStatus.METHOD_NOT_ALLOWED, null, null); } var relativePath = request.path.remainingPath; var fileUri = _servingDirectory.resolve(relativePath); File file; if (FileSystemEntity.isDirectorySync(fileUri.toFilePath())) { file = new File.fromUri(fileUri.resolve("index.html")); } else { file = new File.fromUri(fileUri); } if (!(await file.exists())) { if (_onFileNotFound != null) { return _onFileNotFound(this, request); } var response = new Response.notFound(); if (request.acceptsContentType(ContentType.HTML)) { response ..body = "<html><h3>404 Not Found</h3></html>" ..contentType = ContentType.HTML; } return response; } var lastModifiedDate = await file.lastModified(); var ifModifiedSince = request.innerRequest.headers.value(HttpHeaders.IF_MODIFIED_SINCE); if (ifModifiedSince != null) { var date = HttpDate.parse(ifModifiedSince); if (!lastModifiedDate.isAfter(date)) { return new Response.notModified(lastModifiedDate, _policyForFile(file)); } } var lastModifiedDateStringValue = HttpDate.format(lastModifiedDate); var contentType = contentTypeForExtension(path.extension(file.path)) ?? new ContentType("application", "octet-stream"); var byteStream = file.openRead(); return new Response.ok(byteStream, headers: {HttpHeaders.LAST_MODIFIED: lastModifiedDateStringValue}) ..cachePolicy = _policyForFile(file) ..encodeBody = false ..contentType = contentType; }