- @override
Overridden by subclasses to modify or respond to an incoming request.
Subclasses override this method to provide their specific handling of a request.
If this method returns a Response
, it will be sent as the response for req
and req
will not be passed to any other controllers.
If this method returns req
, req
will be passed to nextController
.
If this method returns null, req
is not passed to any other controller and is not responded to. You must respond to req
through Request.innerRequest
.
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; }