Skip to content

Commit

Permalink
Fix HTTP response header handling (#3065)
Browse files Browse the repository at this point in the history
Fixes #3059
  • Loading branch information
karolz-ms authored Jul 14, 2021
1 parent 962728f commit 26a267e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/tree/registries/dockerV2/DockerV2TagTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export class DockerV2TagTreeItem extends RemoteTagTreeItem {

const url = `v2/${this.parent.repoName}/manifests/${this.tag}`;
const response = await registryRequest(this.parent, 'GET', url, digestOptions);
return response.headers['docker-content-digest'] as string;
const digest = response.headers.get('docker-content-digest') as string;
return digest;
}

public async deleteTreeItemImpl(context: IActionContext): Promise<void> {
Expand Down
18 changes: 12 additions & 6 deletions src/utils/httpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export async function httpRequest<T>(

export class HttpResponse<T> implements ResponseLike {
private bodyPromise: Promise<T> | undefined;
private normalizedHeaders: { [key: string]: string } | undefined;
public readonly headers: HeadersLike;
public readonly status: number;
public readonly statusText: string;
Expand All @@ -50,14 +51,19 @@ export class HttpResponse<T> implements ResponseLike {
public constructor(private readonly innerResponse: Response, public readonly url: string) {
// Unfortunately Typescript will not consider a getter accessor when checking whether a class implements an interface.
// So we are forced to use readonly members to implement ResponseLike interface.
const headerStore: { [key: string]: string } = {};
for (const key of this.innerResponse.headers.keys()) {
headerStore[key] = this.innerResponse.headers.get(key);
}

this.headers = {
get: (key: string) => headerStore[key],
set: (key: string, value: string) => { headerStore[key] = value; }
get: (key: string) => {
if (!this.normalizedHeaders) {
this.normalizedHeaders = {};
for (const key of this.innerResponse.headers.keys()) {
this.normalizedHeaders[key] = this.innerResponse.headers.get(key);
}
}

return this.normalizedHeaders[key];
},
set: (key: string, value: string) => { this.innerResponse.headers.set(key, value); }
};

this.status = this.innerResponse.status;
Expand Down

0 comments on commit 26a267e

Please sign in to comment.