async function getOriginalResponse()

in edge/nodejs/custom-response-with-replaced-url/custom-response-with-replaced-url/app.js [47:125]


async function getOriginalResponse(callback, resp, url, isHttps) {
    console.log("Get response from: " + url);
    console.log("ishttps: " + isHttps);
    
    var resp = {
        status: '200',
        statusDescription: 'OK',
        bodyEncoding: 'base64',
        headers: {
            'content-type': [{
                key: 'Content-Type',
                value: ''
            }],
            'accept-ranges': [{
                key: 'Accept-Ranges',
                value: 'bytes'
            }]
        },
        body: '',
    };

    if (isHttps) {
        return new Promise((resolve, reject) => {
            https.get(url, (response) => {
                let chunks_of_data = [];
                console.log("statusCode: ", response.statusCode);
                console.log("headers: ", response.headers);

                response.on('data', (fragments) => {
                    chunks_of_data.push(fragments);
                });

                response.on('end', () => {
                    let tempBody = Buffer.concat(chunks_of_data);
                    if (response.statusCode.toString() == '200') {
                        console.log("Get the original response successfully");

                        resp.body = updateBody(tempBody.toString('base64'));
                        resp.headers['content-type'] = [{ key: 'Content-Type', value: response.headers['content-type'] }];

                        callback(null, resp);
                    } else {
                        reject('Request failed. status: ' + response.statusCode.toString() + ', body: ' + tempBody.toString('base64'));
                    }
                });

                response.on('error', reject);
            });
        });
    } else {
        return new Promise((resolve, reject) => {
            http.get(url, (response) => {
                let chunks_of_data = [];
                console.log("statusCode: ", response.statusCode);
                console.log("headers: ", response.headers);

                response.on('data', (fragments) => {
                    chunks_of_data.push(fragments);
                });

                response.on('end', () => {
                    let tempBody = Buffer.concat(chunks_of_data);
                    if (response.statusCode.toString() == '200') {
                        console.log("Get the original response successfully");

                        resp.body = updateBody(tempBody.toString('base64'));
                        resp.headers['content-type'] = [{ key: 'Content-Type', value: response.headers['content-type'] }];

                        callback(null, resp);
                    } else {
                        reject('Request failed. status: ' + response.statusCode.toString() + ', body: ' + tempBody.toString('base64'));
                    }
                });

                response.on('error', reject);
            });
        });
    }
}