Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// We try to prevent such cases through early validation, e.g. validate sub request count >= 1.
// While in unexpected sub request invalid case, we allow sub response to be parsed and return to user.
if (subResponseCount != this.subRequests.size && subResponseCount != 1) {
throw new Error("Invalid state: sub responses' count is not equal to sub requests' count.");
}
let deserializedSubResponses: Array = new Array(subResponseCount);
let subResponsesSucceededCount: number = 0;
let subResponsesFailedCount: number = 0;
// Parse sub subResponses.
for (let index = 0; index < subResponseCount; index++) {
const subResponse = subResponses[index];
deserializedSubResponses[index] = {} as BatchSubResponse;
let deserializedSubResponse = deserializedSubResponses[index];
deserializedSubResponse.headers = new HttpHeaders();
let responseLines = subResponse.split(`${HTTP_LINE_ENDING}`);
let subRespHeaderStartFound = false;
let subRespHeaderEndFound = false;
let subRespFailed = false;
let contentId = NOT_FOUND;
for (const responseLine of responseLines) {
if (!subRespHeaderStartFound) {
// Convention line to indicate content ID
if (responseLine.startsWith(HeaderConstants.CONTENT_ID)) {
contentId = parseInt(responseLine.split(HTTP_HEADER_DELIMITER)[1]);
}
// Http version line with status code indicates the start of sub request's response.
// Example: HTTP/1.1 202 Accepted
public async sendRequest(
webResource: WebResource
): Promise {
if (!webResource.headers) webResource.headers = new HttpHeaders();
let originalBody = webResource.body;
if (this.challenge == undefined) {
// Use a blank to start the challenge
webResource.body = "";
} else {
// or use the cached token if we have one
await this.authenticateRequest(webResource);
}
let response = await this._nextPolicy.sendRequest(webResource);
if (response.status == 401) {
webResource.body = originalBody;
export function sanitizeHeaders(originalHeader: HttpHeaders): HttpHeaders {
const headers: HttpHeaders = new HttpHeaders();
for (const header of originalHeader.headersArray()) {
if (header.name.toLowerCase() === HeaderConstants.AUTHORIZATION.toLowerCase()) {
headers.set(header.name, "*****");
} else if (header.name.toLowerCase() === HeaderConstants.X_MS_COPY_SOURCE) {
headers.set(header.name, sanitizeURL(header.value));
} else {
headers.set(header.name, header.value);
}
}
return headers;
}
/**
public async sendRequest(request: WebResource): Promise {
if (isNode) {
if (!request.headers) {
request.headers = new HttpHeaders();
}
if (!request.headers.get(HeaderConstants.USER_AGENT)) {
request.headers.set(HeaderConstants.USER_AGENT, this.telemetry);
}
}
return this._nextPolicy.sendRequest(request);
}
}
export function sanitizeHeaders(originalHeader: HttpHeaders): HttpHeaders {
const headers: HttpHeaders = new HttpHeaders();
for (const header of originalHeader.headersArray()) {
if (header.name.toLowerCase() === HeaderConstants.AUTHORIZATION) {
headers.set(header.name, "*****");
} else if (header.name.toLowerCase() === HeaderConstants.X_MS_COPY_SOURCE) {
headers.set(header.name, sanitizeURL(header.value));
} else {
headers.set(header.name, header.value);
}
}
return headers;
}
async signRequest(webResource: WebResource): Promise {
if (!webResource.headers) webResource.headers = new HttpHeaders();
const targetUri = encodeURIComponent(webResource.url.toLowerCase()).toLowerCase();
const date = new Date();
date.setMinutes(date.getMinutes() + 5);
const expirationDate = Math.round(date.getTime() / 1000);
const signature = await this._generateSignature(targetUri, expirationDate);
webResource.headers.set(
"authorization",
`SharedAccessSignature sig=${signature}&se=${expirationDate}&skn=${this.keyName}&sr=${targetUri}`
);
webResource.withCredentials = true;
return webResource;
}
}
export function sanitizeHeaders(originalHeader: HttpHeaders): HttpHeaders {
const headers: HttpHeaders = new HttpHeaders();
for (const header of originalHeader.headersArray()) {
if (header.name.toLowerCase() === HeaderConstants.AUTHORIZATION) {
headers.set(header.name, "*****");
} else if (header.name.toLowerCase() === HeaderConstants.X_MS_COPY_SOURCE) {
headers.set(header.name, sanitizeURL(header.value));
} else {
headers.set(header.name, header.value);
}
}
return headers;
}