in alibabacloud-gateway-sls/ts/src/client.ts [56:143]
async modifyRequest(context: $SPI.InterceptorContext, attributeMap: $SPI.AttributeMap): Promise<void> {
let request = context.request;
let hostMap : {[key: string ]: string} = { };
if (!Util.isUnset(request.hostMap)) {
hostMap = request.hostMap;
}
let project = hostMap["project"];
let config = context.configuration;
let credential : Credential = request.credential;
let credentialModel = await credential.getCredential();
let accessKeyId = credentialModel.accessKeyId;
let accessKeySecret = credentialModel.accessKeySecret;
let securityToken = credentialModel.securityToken;
if (!Util.empty(securityToken)) {
request.headers["x-acs-security-token"] = securityToken;
}
let signatureVersion = Util.defaultString(request.signatureVersion, "v1");
let finalCompressType = await this.getFinalRequestCompressType(request.action, request.headers);
let contentHash = "";
// get body bytes
let bodyBytes : Buffer = null;
if (!Util.isUnset(request.body)) {
if (String.equals(request.reqBodyType, "json") || String.equals(request.reqBodyType, "formData")) {
request.headers["content-type"] = "application/json";
let bodyStr = Util.toJSONString(request.body);
bodyBytes = Util.toBytes(bodyStr);
} else if (String.equals(request.reqBodyType, "binary")) {
// content-type: application/octet-stream
bodyBytes = Util.assertAsBytes(request.body);
}
}
// get body raw size
let bodyRawSize : string = "0";
let rawSizeRef = request.headers["x-log-bodyrawsize"];
// for php bug, Argument #1 ($value) could not be passed by reference
if (!Util.isUnset(rawSizeRef)) {
bodyRawSize = rawSizeRef;
} else if (!Util.isUnset(request.body)) {
bodyRawSize = `${await SLS_Util.bytesLength(bodyBytes)}`;
}
// compress if needed, and set body and hash
if (!Util.isUnset(request.body)) {
if (!Util.empty(finalCompressType)) {
let compressed = await SLS_Util.compress(bodyBytes, finalCompressType);
bodyBytes = compressed;
}
contentHash = await this.makeContentHash(bodyBytes, signatureVersion);
request.stream = new $tea.BytesReadable(bodyBytes);
}
let host = await this.getHost(config.network, project, config.endpoint);
request.headers = {
accept: "application/json",
host: host,
'user-agent': request.userAgent,
'x-log-apiversion': "0.6.0",
...request.headers,
};
request.headers["x-log-bodyrawsize"] = bodyRawSize;
await this.setDefaultAcceptEncoding(request.action, request.headers);
await this.buildRequest(context);
// move param in path to query
if (String.equals(signatureVersion, "v4")) {
if (Util.empty(contentHash)) {
contentHash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
}
let date = await this.getDateISO8601();
request.headers["x-log-date"] = date;
request.headers["x-log-content-sha256"] = contentHash;
request.headers["authorization"] = await this.getAuthorizationV4(context, date, contentHash, accessKeyId, accessKeySecret);
return ;
}
if (!Util.empty(accessKeyId)) {
request.headers["x-log-signaturemethod"] = "hmac-sha256";
}
request.headers["date"] = Util.getDateUTCString();
request.headers["content-md5"] = contentHash;
request.headers["authorization"] = await this.getAuthorization(request.pathname, request.method, request.query, request.headers, accessKeyId, accessKeySecret);
}