Skip to content

Commit

Permalink
Uses instance's client and fix client don't close
Browse files Browse the repository at this point in the history
  • Loading branch information
rk0cc committed Feb 1, 2024
1 parent 0714c18 commit 3114cc2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 34 deletions.
3 changes: 3 additions & 0 deletions model/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## 3.4.0

* Add `MockOgHrefClient.advance` for building responding content with different content type applied.
* Fix client does not closes after stream closed when calling `determineContentTypes()` in `IteratedUrlInfoContentTypeResolver`.
* Uses final variable assignment rather than a getter for `MockOgHrefClient.redirect`.
* `IteratedUrlInfoContentTypeResolver` uses current instance of `MetaFetch`'s client to proceed data.

## 3.3.4

Expand Down
13 changes: 6 additions & 7 deletions model/lib/src/client/mock_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ final class MockOgHrefClient extends BaseClient

/// Redirect features is always disabled for [MockOgHrefClient].
@override
bool get redirect => false;
final bool redirect = false;

static Duration _generateResponseDelay() {
late Random rand;
Expand Down Expand Up @@ -237,12 +237,11 @@ final class MockOgHrefClient extends BaseClient
/// For setting [contentLinker] without identical [contentType],
/// please uses [MockOgHrefClient.advance] instead.
factory MockOgHrefClient(Map<Uri, String> contentLinker,
{String contentType = _PLAIN_TEXT_MIME}) {
return MockOgHrefClient.advance({
for (var MapEntry(key: url, value: body) in contentLinker.entries)
url: MockOgHrefClientContent(content: body, contentType: contentType)
});
}
{String contentType = _PLAIN_TEXT_MIME}) =>
MockOgHrefClient.advance({
for (var MapEntry(key: url, value: body) in contentLinker.entries)
url: MockOgHrefClientContent(content: body, contentType: contentType)
});

/// Uses [sample files](https://github.com/rk0cc/oghref/tree/main/model/sample) to defined
/// content of the simulated HTML files with hosted IP address as `127.0.0.2` with `HTTPS`
Expand Down
54 changes: 31 additions & 23 deletions model/lib/src/content_type_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import 'package:meta/meta.dart';
import 'package:mime_dart/mime_dart.dart';
import 'package:path/path.dart' as p;

import 'client/client.dart';
import 'fetch/fetch.dart';
import 'model/url.dart';


/// Determine the content type category from MIME.
enum ContentTypeCategory {
/// Audio files
Expand Down Expand Up @@ -148,34 +149,41 @@ extension IteratedUrlInfoContentTypeResolver on Iterable<UrlInfo> {
/// The condition of returned [ContentTypeCategory] will be depended on
/// given extendion from [Uri.path] first. If not offered, it will
/// try to find `Content-Type` in HTTP response header by making HTTP HEAD request.
///
/// This features will not be availabled during test and [UnsupportedError]
/// will be thrown if attempted.
Stream<UrlInfoContentTypeResult> determineContentTypes() async* {
Client c = OgHrefClient(false);

for (var ui in this) {
ContentTypeCategory httpCT;
Set<ContentTypeCategory> httpExtCt = ui.url!.extensionContentType;
Client c = MetaFetch.instance.createClient(false);

if (httpExtCt.isNotEmpty) {
httpCT = httpExtCt.first;
} else {
httpCT =
await c.head(ui.url!).then((value) => value.contentTypeCategory);
}
try {
for (var ui in this) {
ContentTypeCategory httpCT;
Set<ContentTypeCategory> httpExtCt = ui.url!.extensionContentType;

ContentTypeCategory? httpsCT;
if (ui.secureUrl != null) {
Set<ContentTypeCategory> httpsExtCt =
ui.secureUrl!.extensionContentType;
if (httpsExtCt.isNotEmpty) {
httpsCT = httpsExtCt.first;
if (httpExtCt.isNotEmpty) {
httpCT = httpExtCt.first;
} else {
httpsCT = await c
.head(ui.secureUrl!)
.then((value) => value.contentTypeCategory);
httpCT =
await c.head(ui.url!).then((value) => value.contentTypeCategory);
}
}

yield UrlInfoContentTypeResult._(ui, httpCT, httpsCT);
ContentTypeCategory? httpsCT;
if (ui.secureUrl != null) {
Set<ContentTypeCategory> httpsExtCt =
ui.secureUrl!.extensionContentType;
if (httpsExtCt.isNotEmpty) {
httpsCT = httpsExtCt.first;
} else {
httpsCT = await c
.head(ui.secureUrl!)
.then((value) => value.contentTypeCategory);
}
}

yield UrlInfoContentTypeResult._(ui, httpCT, httpsCT);
}
} finally {
c.close();
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions model/lib/src/fetch/fetch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ abstract final class MetaFetch {
}

@factory
OgHrefClient _createClient();
// ignore: unused_element
OgHrefClient _createClient(bool? redirectOverride);

/// Get the corresponded parser from [prefix].
///
Expand Down Expand Up @@ -323,7 +324,7 @@ abstract final class MetaFetch {
throw NonHttpUrlException(url);
}

Client client = _createClient();
Client client = _createClient(null);

late Response resp;

Expand Down Expand Up @@ -385,3 +386,12 @@ abstract final class MetaFetch {
return _fetchHtmlDocument(url).then(_buildAllMetaInfo);
}
}

/// Handle getting [Client] from [MetaFetch] to other components
/// internally.
@internal
extension MetaFetchClientFactoryExtension on MetaFetch {
/// An internal [Client] factory retrived from [MetaFetch].
Client createClient([bool? redirectOverride]) =>
_createClient(redirectOverride);
}
2 changes: 1 addition & 1 deletion model/lib/src/fetch/producer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ final class _MetaFetchProducer extends MetaFetch {
_MetaFetchProducer() : super._();

@override
OgHrefClient _createClient() => OgHrefClient(allowRedirect);
OgHrefClient _createClient(bool? redirectOverride) => OgHrefClient(redirectOverride ?? allowRedirect);
}
3 changes: 2 additions & 1 deletion model/lib/src/fetch/tester.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ final class MetaFetchTester extends MetaFetch {
additionalSupportedExtensions: const {"txt"});

@override
OgHrefClient _createClient() => _clientConstructor();
// ignore: unused_element
OgHrefClient _createClient(bool? redirectOverride) => _clientConstructor();
}

0 comments on commit 3114cc2

Please sign in to comment.