Skip to content

Commit

Permalink
WIP: Custom HTTP Headers in Network Requests #132
Browse files Browse the repository at this point in the history
  • Loading branch information
espresso3389 committed Apr 22, 2024
1 parent 0aa7b81 commit 5717726
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 28 deletions.
4 changes: 4 additions & 0 deletions lib/src/pdf_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ abstract class PdfDocumentFactory {
PdfDownloadProgressCallback? progressCallback,
PdfDownloadReportCallback? reportCallback,
bool preferRangeAccess = false,
Map<String, String>? headers,
});

/// Singleton [PdfDocumentFactory] instance.
Expand Down Expand Up @@ -222,13 +223,15 @@ abstract class PdfDocument {
/// [progressCallback] is called when the download progress is updated (Not supported on Web).
/// [reportCallback] is called when the download is completed (Not supported on Web).
/// [preferRangeAccess] to prefer range access to download the PDF (Not supported on Web).
/// [headers] is used to specify additional HTTP headers especially for authentication/authorization.
static Future<PdfDocument> openUri(
Uri uri, {
PdfPasswordProvider? passwordProvider,
bool firstAttemptByEmptyPassword = true,
PdfDownloadProgressCallback? progressCallback,
PdfDownloadReportCallback? reportCallback,
bool preferRangeAccess = false,
Map<String, String>? headers,
}) =>
PdfDocumentFactory.instance.openUri(
uri,
Expand All @@ -237,6 +240,7 @@ abstract class PdfDocument {
progressCallback: progressCallback,
reportCallback: reportCallback,
preferRangeAccess: preferRangeAccess,
headers: headers,
);

/// Pages.
Expand Down
30 changes: 20 additions & 10 deletions lib/src/pdf_file_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ Future<PdfDocument> pdfDocumentFromUri(
PdfDownloadProgressCallback? progressCallback,
PdfDownloadReportCallback? reportCallback,
bool useRangeAccess = true,
Map<String, String>? headers,
}) async {
final startTime = reportCallback != null ? DateTime.now() : null;
void report() {
Expand All @@ -334,13 +335,8 @@ Future<PdfDocument> pdfDocumentFromUri(
if (!cache.isInitialized) {
cache.setBlockSize(blockSize ?? PdfFileCache.defaultBlockSize);
final result = await _downloadBlock(
httpClient,
uri,
cache,
progressCallback,
0,
useRangeAccess: useRangeAccess,
);
httpClient, uri, cache, progressCallback, 0,
useRangeAccess: useRangeAccess, headers: headers);
if (result.isFullDownload) {
return await PdfDocument.openFile(
cache.filePath,
Expand All @@ -355,8 +351,14 @@ Future<PdfDocument> pdfDocumentFromUri(
// cache is valid; no need to download.
} else {
final result = await _downloadBlock(
httpClient, uri, cache, progressCallback, 0,
addCacheControlHeaders: true);
httpClient,
uri,
cache,
progressCallback,
0,
addCacheControlHeaders: true,
headers: headers,
);
if (result.isFullDownload) {
cache.close(); // close the cache file before opening it.
httpClient.close();
Expand All @@ -379,7 +381,13 @@ Future<PdfDocument> pdfDocumentFromUri(
final isAvailable = cache.isCached(blockId);
if (!isAvailable) {
await _downloadBlock(
httpClient, uri, cache, progressCallback, blockId);
httpClient,
uri,
cache,
progressCallback,
blockId,
headers: headers,
);
}
final readEnd = min(p + size, (blockId + 1) * cache.blockSize);
final sizeToRead = readEnd - p;
Expand Down Expand Up @@ -435,6 +443,7 @@ Future<_DownloadResult> _downloadBlock(
int blockCount = 1,
bool addCacheControlHeaders = false,
bool useRangeAccess = true,
Map<String, String>? headers,
}) async {
int? fileSize;
final blockOffset = blockId * cache.blockSize;
Expand All @@ -447,6 +456,7 @@ Future<_DownloadResult> _downloadBlock(
if (useRangeAccess) 'Range': 'bytes=$blockOffset-${end - 1}',
if (addCacheControlHeaders)
...cache.cacheControlState.getHeadersForFetch(),
if (headers != null) ...headers,
},
)
..sink.close(),
Expand Down
2 changes: 2 additions & 0 deletions lib/src/pdfium/pdfrx_pdfium.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ class PdfDocumentFactoryImpl extends PdfDocumentFactory {
PdfDownloadProgressCallback? progressCallback,
PdfDownloadReportCallback? reportCallback,
bool preferRangeAccess = false,
Map<String, String>? headers,
}) =>
pdfDocumentFromUri(
uri,
Expand All @@ -234,6 +235,7 @@ class PdfDocumentFactoryImpl extends PdfDocumentFactory {
progressCallback: progressCallback,
reportCallback: reportCallback,
useRangeAccess: preferRangeAccess,
headers: headers,
);

static bool _isPasswordError({int? error}) {
Expand Down
19 changes: 16 additions & 3 deletions lib/src/web/pdf.js.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extension type _PdfjsDocumentInitParameters._(JSObject _) implements JSObject {
external _PdfjsDocumentInitParameters({
String? url,
JSArrayBuffer? data,
JSObject httpHeaders,
JSAny? httpHeaders,
bool? withCredentials,
String? password,
int? length,
Expand All @@ -49,7 +49,7 @@ extension type _PdfjsDocumentInitParameters._(JSObject _) implements JSObject {

external String? get url;
external JSArrayBuffer? get data;
external JSObject get httpHeaders;
external JSAny? get httpHeaders;
external bool? get withCredentials;
external String? get password;
external int? get length;
Expand All @@ -66,13 +66,23 @@ extension type _PDFDocumentLoadingTask(JSObject _) implements JSObject {
external JSPromise<PdfjsDocument> get promise;
}

Future<PdfjsDocument> pdfjsGetDocument(String url, {String? password}) =>
Future<PdfjsDocument> pdfjsGetDocument(
String url, {
String? password,
Map<String, String>? headers,
bool withCredentials = false,
}) =>
_pdfjsGetDocument(
_PdfjsDocumentInitParameters(
url: url,
password: password,
httpHeaders: headers?.jsify(),
withCredentials: withCredentials,
cMapUrl: PdfJsConfiguration.configuration?.cMapUrl ?? _pdfjsCMapUrl,
cMapPacked: PdfJsConfiguration.configuration?.cMapPacked ?? true,
useSystemFonts: PdfJsConfiguration.configuration?.useSystemFonts,
standardFontDataUrl:
PdfJsConfiguration.configuration?.standardFontDataUrl,
),
).promise.toDart;

Expand All @@ -84,6 +94,9 @@ Future<PdfjsDocument> pdfjsGetDocumentFromData(ByteBuffer data,
password: password,
cMapUrl: PdfJsConfiguration.configuration?.cMapUrl,
cMapPacked: PdfJsConfiguration.configuration?.cMapPacked,
useSystemFonts: PdfJsConfiguration.configuration?.useSystemFonts,
standardFontDataUrl:
PdfJsConfiguration.configuration?.standardFontDataUrl,
),
).promise.toDart;

Expand Down
13 changes: 11 additions & 2 deletions lib/src/web/pdf_js_configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class PdfJsConfiguration {
required this.workerSrc,
required this.cMapUrl,
required this.cMapPacked,
this.useSystemFonts = true,
this.standardFontDataUrl,
this.pdfJsDownloadTimeout = const Duration(seconds: 10),
});

Expand All @@ -19,10 +21,17 @@ class PdfJsConfiguration {
/// `cmaps` directory URL such as https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/cmaps/
final String cMapUrl;

/// Whether to use the packed cmaps. Default is true.
/// Whether to use the packed cmaps. The default is true.
final bool cMapPacked;

/// The timeout for downloading the PDF.js library. Default is 10 seconds.
/// When true, fonts that aren't embedded in the PDF document will fallback to a system font.
/// The default is true.
final bool useSystemFonts;

/// The URL where the standard font files are located. Include the trailing slash.
final String? standardFontDataUrl;

/// The timeout for downloading the PDF.js library. The default is 10 seconds.
final Duration pdfJsDownloadTimeout;

/// The current configuration. null to use the default.
Expand Down
31 changes: 18 additions & 13 deletions lib/src/web/pdfrx_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,16 @@ class PdfDocumentFactoryImpl extends PdfDocumentFactory {
String filePath, {
PdfPasswordProvider? passwordProvider,
bool firstAttemptByEmptyPassword = true,
}) async {
return _openByFunc(
(password) => pdfjsGetDocument(
filePath,
password: password,
),
sourceName: filePath,
passwordProvider: passwordProvider,
firstAttemptByEmptyPassword: firstAttemptByEmptyPassword,
);
}
}) =>
_openByFunc(
(password) => pdfjsGetDocument(
filePath,
password: password,
),
sourceName: filePath,
passwordProvider: passwordProvider,
firstAttemptByEmptyPassword: firstAttemptByEmptyPassword,
);

@override
Future<PdfDocument> openUri(
Expand All @@ -102,9 +101,15 @@ class PdfDocumentFactoryImpl extends PdfDocumentFactory {
PdfDownloadProgressCallback? progressCallback,
PdfDownloadReportCallback? reportCallback,
bool preferRangeAccess = false,
Map<String, String>? headers,
}) =>
openFile(
uri.toString(),
_openByFunc(
(password) => pdfjsGetDocument(
uri.toString(),
password: password,
headers: headers,
),
sourceName: uri.toString(),
passwordProvider: passwordProvider,
firstAttemptByEmptyPassword: firstAttemptByEmptyPassword,
);
Expand Down

0 comments on commit 5717726

Please sign in to comment.