Skip to content

Commit

Permalink
Redesign MetaFetch
Browse files Browse the repository at this point in the history
  • Loading branch information
rk0cc committed Jan 4, 2024
1 parent 7a24eee commit 0e4c019
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 49 deletions.
5 changes: 5 additions & 0 deletions model/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.1.0

* Redesign `MetaFetch` that enabling further extensions internally.
* Update documents.

## 3.0.1

* Fix `allowRedirect` still enabled eventhough disabled already.
Expand Down
2 changes: 1 addition & 1 deletion model/lib/buffer_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export 'src/buffer/url.dart';
export 'src/model/media.dart';
export 'src/model/url.dart';
export 'src/parser/property_parser.dart';
export 'src/fetch.dart';
export 'src/fetch/fetch.dart';
2 changes: 1 addition & 1 deletion model/lib/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export 'src/exception/non_http_url.dart';
export 'src/model/metainfo.dart';
export 'src/parser/open_graph.dart';
export 'src/parser/twitter_card.dart';
export 'src/fetch.dart';
export 'src/fetch/fetch.dart';
9 changes: 6 additions & 3 deletions model/lib/src/content_type_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,19 @@ extension ContentTypeVerifier on Response {
orElse: () => ContentTypeCategory.text);
}

/// Determine the [contentType] is one of the expected [fileExtensions].
bool isSatisfiedExtension({Set<String> fileExtensions = const {}}) {
/// Determine the [contentType] is one of the expected [fileExtensions]
/// or found from [mimeOverride].
bool isSatisfiedExtension(
{Set<String> fileExtensions = const {},
Set<String> mimeOverride = const {}}) {
final Set<String> acceptedExtension = {...fileExtensions};
if (acceptedExtension.isEmpty) {
acceptedExtension.add("txt");
}

String? mimeData = contentType;

final List<String> extTypes = [];
final List<String> extTypes = [...mimeOverride];

if (mimeData != null) {
extTypes.addAll(
Expand Down
80 changes: 38 additions & 42 deletions model/lib/src/fetch.dart → model/lib/src/fetch/fetch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import 'package:http/http.dart'
hide delete, get, head, patch, post, put, read, readBytes, runWithClient;
import 'package:meta/meta.dart';

import 'exception/content_type_mismatched.dart';
import 'exception/non_http_url.dart';
import 'model/metainfo.dart';
import 'parser/property_parser.dart';
import 'client.dart';
import 'content_type_verifier.dart';
import '../exception/content_type_mismatched.dart';
import '../exception/non_http_url.dart';
import '../model/metainfo.dart';
import '../parser/property_parser.dart';
import '../client.dart';
import '../content_type_verifier.dart';

part 'producer.dart';
part 'tester.dart';

/// Read [Document] and find all metadata tags to generate corresponded
/// [MetaInfo].
Expand Down Expand Up @@ -42,7 +45,9 @@ abstract final class MetaFetch {
equals: (p0, p1) => p0.propertyNamePrefix == p1.propertyNamePrefix,
hashCode: (p0) => p0.propertyNamePrefix.hashCode);

//final OgHrefClient _client = OgHrefClient(true);
final Set<String> _additionalSupportedExtensions;

final Set<String> _additionalSupportedContentType;

/// Allow [MetaFetch] fetch redirected [Uri]'s metadata instead of
/// provided one.
Expand Down Expand Up @@ -81,12 +86,16 @@ abstract final class MetaFetch {
/// ```
bool get isPrimaryPrefixEnabled => _primaryPrefix != null;

MetaFetch._();
MetaFetch._(
{Set<String> additionalSupportedExtensions = const {},
Set<String> additionalSupportedContentType = const {}})
: assert(additionalSupportedExtensions.every(RegExp(r"^[0-9a-zA-Z]{1,5}$").hasMatch)),
assert(additionalSupportedContentType.every(RegExp(r"^[-\w]+\/[-\w]+(\.[-\w]+)*(\+[-\w]+)?$").hasMatch)),
_additionalSupportedExtensions = {...additionalSupportedExtensions},
_additionalSupportedContentType = {...additionalSupportedContentType};

/// Get a instance of [MetaFetch].
///
/// [MetaFetch] is a singleton object that it allows to uses same [register]
/// preference whatever been made.
/// Create new instance of [MetaFetch] which dedicated
/// from [instance].
factory MetaFetch() = _MetaFetchProducer;

/// A dedicated [MetaFetch] which ignore content type condition that allowing
Expand Down Expand Up @@ -245,7 +254,23 @@ abstract final class MetaFetch {
return Map.unmodifiable(metaInfoMap);
}

bool _verifyContentType(Response resp);
bool _verifyContentType(Response resp) {
if (!resp.isSatisfiedExtension(fileExtensions: {
"html",
"xhtml",
"htm",
..._additionalSupportedExtensions
}, mimeOverride: _additionalSupportedContentType)) {
throw ContentTypeMismatchedException(
resp.request!.url, resp.contentType, {
"text/html",
"application/xhtml+xml",
..._additionalSupportedContentType
});
}

return true;
}

Future<Document> _fetchHtmlDocument(Uri url) async {
if (!RegExp(r"^https?$").hasMatch(url.scheme)) {
Expand Down Expand Up @@ -314,32 +339,3 @@ abstract final class MetaFetch {
return _fetchHtmlDocument(url).then(_buildAllMetaInfo);
}
}

final class _MetaFetchTester extends MetaFetch {
_MetaFetchTester() : super._();

@override
bool _verifyContentType(Response resp) {
return true;
}
}

final class _MetaFetchProducer extends MetaFetch {
static const Set<String> eligableExtensions = <String>{"html", "xhtml"};
static const Set<String> supportedContentType = <String>{
"text/html",
"application/xhtml+xml"
};

_MetaFetchProducer() : super._();

@override
bool _verifyContentType(Response resp) {
if (!resp.isSatisfiedExtension(fileExtensions: eligableExtensions)) {
throw ContentTypeMismatchedException(
resp.request!.url, resp.contentType, supportedContentType);
}

return true;
}
}
5 changes: 5 additions & 0 deletions model/lib/src/fetch/producer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
part of 'fetch.dart';

final class _MetaFetchProducer extends MetaFetch {
_MetaFetchProducer() : super._();
}
8 changes: 8 additions & 0 deletions model/lib/src/fetch/tester.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
part of 'fetch.dart';

final class _MetaFetchTester extends MetaFetch {
_MetaFetchTester()
: super._(
additionalSupportedContentType: const {"text/plain"},
additionalSupportedExtensions: const {"txt"});
}
2 changes: 1 addition & 1 deletion model/lib/src/parser/open_graph.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '../buffer/metainfo.dart';
import '../fetch.dart';
import '../fetch/fetch.dart';

import 'property_parser.dart';

Expand Down
2 changes: 1 addition & 1 deletion model/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: oghref_model
description: Object standarized definition with parser interface for constructing rich information of given URL among various metadata protocols.
version: 3.0.1
version: 3.1.0
repository: https://github.com/rk0cc/oghref/tree/main/model
issue_tracker: https://github.com/rk0cc/oghref/issues
funding:
Expand Down

0 comments on commit 0e4c019

Please sign in to comment.