Skip to content

Commit

Permalink
fix: parsing Location header in response
Browse files Browse the repository at this point in the history
  • Loading branch information
birme committed Jul 5, 2023
1 parent e0db6cd commit ce975a2
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/adapters/WHEPAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export class WHEPAdapter implements Adapter {
onError: (error: string) => void
) {
this.channelUrl = channelUrl;
if (typeof this.channelUrl === 'string') {
throw new Error(
`channelUrl parameter expected to be an URL not a string`
);
}
this.whepType = WHEPType.Client;

this.onErrorHandler = onError;
Expand Down Expand Up @@ -134,6 +139,18 @@ export class WHEPAdapter implements Adapter {
}
}

private getResouceUrlFromHeaders(headers: Headers): string | null {
if (headers.get('Location') && headers.get('Location')?.match(/^\//)) {
const resourceUrl = new URL(
headers.get('Location')!,
this.channelUrl.origin
);
return resourceUrl.toString();
} else {
return headers.get('Location');
}
}

private async requestOffer() {
if (this.whepType === WHEPType.Server) {
this.log(`Requesting offer from: ${this.channelUrl}`);
Expand All @@ -145,18 +162,7 @@ export class WHEPAdapter implements Adapter {
body: ''
});
if (response.ok) {
if (
response.headers.get('Location') &&
response.headers.get('Location')?.match(/^\//)
) {
const resourceUrl = new URL(
response.headers.get('Location')!,
this.channelUrl.origin
);
this.resource = resourceUrl.toString();
} else {
this.resource = response.headers.get('Location');
}
this.resource = this.getResouceUrlFromHeaders(response.headers);
this.log('WHEP Resource', this.resource);
const offer = await response.text();
this.log('Received offer', offer);
Expand Down Expand Up @@ -210,7 +216,7 @@ export class WHEPAdapter implements Adapter {
});

if (response.ok) {
this.resource = response.headers.get('Location');
this.resource = this.getResouceUrlFromHeaders(response.headers);
this.log('WHEP Resource', this.resource);
const answer = await response.text();
await this.localPeer.setRemoteDescription({
Expand Down

0 comments on commit ce975a2

Please sign in to comment.