- @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.
RequestControllers 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())) {
return new Response.notFound();
}
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;
}