Skip to content

Commit

Permalink
Safely read playlist response "age" header
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Walch committed Mar 26, 2021
1 parent c45c4ec commit 1009765
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
8 changes: 1 addition & 7 deletions src/loader/playlist-loader.ts
Expand Up @@ -70,8 +70,6 @@ class PlaylistLoader {
[key: string]: Loader<LoaderContext>;
} = Object.create(null);

private checkAgeHeader: boolean = true;

constructor(hls: Hls) {
this.hls = hls;
this.registerListeners();
Expand Down Expand Up @@ -148,7 +146,6 @@ class PlaylistLoader {
data: ManifestLoadingData
) {
const { url } = data;
this.checkAgeHeader = true;
this.load({
id: null,
groupId: null,
Expand Down Expand Up @@ -686,12 +683,9 @@ class PlaylistLoader {
return;
}

if (this.checkAgeHeader && levelDetails.live) {
if (levelDetails.live) {
const ageHeader = loader.getResponseHeader('age');
levelDetails.ageHeader = ageHeader ? parseFloat(ageHeader) : 0;
// Avoid repeated browser error log `Refused to get unsafe header "age"` when unnecessary or past attempts failed
// Add an "Access-Control-Expose-Headers: age" header to playlist responses to prevent this CORS error
this.checkAgeHeader = ageHeader !== null;
}

switch (type) {
Expand Down
9 changes: 3 additions & 6 deletions src/utils/fetch-loader.ts
Expand Up @@ -160,12 +160,9 @@ class FetchLoader implements Loader<LoaderContext> {
}

getResponseHeader(name: string): string | null {
if (this.response) {
try {
return this.response.headers.get(name);
} catch (error) {
/* Could not get header */
}
const response = this.response;
if (response?.headers.has(name)) {
return response.headers.get(name);
}
return null;
}
Expand Down

0 comments on commit 1009765

Please sign in to comment.