Skip to content

Commit

Permalink
Fix documentation URL preprocessing to recognize directories with dot…
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Dec 12, 2024
1 parent 8cac40c commit f5181a1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
28 changes: 27 additions & 1 deletion app/lib/frontend/handlers/documentation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ DocFilePath? parseRequestUri(Uri uri) {
final relativeSegments =
uri.pathSegments.skip(3).where((s) => s.isNotEmpty).toList();
var pathSegments = relativeSegments;
if (relativeSegments.isEmpty || !relativeSegments.last.contains('.')) {
if (_expandToIndexHtml(relativeSegments)) {
pathSegments = [...relativeSegments, 'index.html'];
}
final path = p.normalize(p.joinAll(pathSegments));
Expand All @@ -143,6 +143,32 @@ DocFilePath? parseRequestUri(Uri uri) {
return DocFilePath(package, version, path);
}

const _nonExpandedExtensions = {
'.html',
'.json',
'.gz',
'.png',
'.svg',
};
// NOTE: This is a best-effort detection on the segments.
// Instead, we should rather check if the file (or the updated path)
// is in the generated output, and base the decision on the file list.
// However, with the current serving code it is costly, we need to refactor it.
// TODO: Use blob index to decide how the relative path should be handled
bool _expandToIndexHtml(List<String> segments) {
if (segments.isEmpty) {
return true;
}
if (!segments.last.contains('.')) {
return true;
}
if (segments.first.contains('static-assets')) {
return false;
}
final ext = p.extension(segments.last);
return !_nonExpandedExtensions.contains(ext);
}

bool _isValidVersion(String version) {
if (version.trim().isEmpty) return false;
if (version == 'latest') return true;
Expand Down
15 changes: 15 additions & 0 deletions app/test/frontend/handlers/documentation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ void main() {
testUri(
'/documentation/pkg/1.0.0/A%20_0.html', 'pkg', '1.0.0', 'A _0.html');
});

test('library with dots are recognized', () {
testUri('/documentation/pkg/latest/a.b.c', 'pkg', 'latest',
'a.b.c/index.html');
testUri('/documentation/pkg/latest/a.b.c/', 'pkg', 'latest',
'a.b.c/index.html');
});

test('static assets are left as-is', () {
testUri('/documentation/pkg/latest/static-assets/search.svg', 'pkg',
'latest', 'static-assets/search.svg');
});
});

group('dartdoc handlers', () {
Expand Down Expand Up @@ -110,6 +122,9 @@ void main() {
await expectRedirectResponse(
await issueGet('/documentation/oxygen/1.0.0/abc'),
'/documentation/oxygen/1.0.0/abc/');
await expectRedirectResponse(
await issueGet('/documentation/oxygen/1.0.0/abc.def'),
'/documentation/oxygen/1.0.0/abc.def/');
await expectRedirectResponse(
await issueGet('/documentation/oxygen/latest/abc/def'),
'/documentation/oxygen/latest/abc/def/');
Expand Down

0 comments on commit f5181a1

Please sign in to comment.