const request=require('request')
fetchUrl (params, tag, callBack) {
this.url = params.url
this.tag = tag
let headers = { 'User-Agent': params.useragent }
let options = { followRedirects: false, keepAlive: false, url: params.url, headers: headers, gzip: true, encoding: null }
let proxy = 'None'
let userAgent = params.useragent
if (parseInt(ConfigHandler.getConfig().spfUseProxy) === 1) {
if (parseInt(ConfigHandler.getConfig().spfUseHermesUserAgent) === 1) {
userAgent = params.useragent + ' Hughes-PFB/' +
params.agentid + '/' + params.tracetags
headers['User-Agent'] = userAgent
headers['Connection'] = 'Close'
}
proxy = 'http://' + ConfigHandler.getConfig().iosPrefetchProxyIp +
':' + ConfigHandler.getConfig().iosPrefetchProxyPort
options['proxy'] = proxy
}
this.httpClient = request.get(options, (err, response, body) => {
let result = {}
if (!err) {
this.response = response
this.bodyLen = body.length
logger.debug('%s Response info, %s', this.tag, this.resourceInfo())
result = {
statuscode: response.statusCode,
headers: response.headers,
body: body
}
} else {
logger.error('%s, fetchUrl() failed! cause=%s, url=%s', JSON.stringify(this), err.message,
this.url)
}
if (this.httpClient) {
logger.debug('%s, fetchUrl() ending connection!', JSON.stringify(this.httpClient))
this.httpClient.abort()
}
if (response && response.socket) {
response.socket.destroy()
}
callBack(result, err)
})
}
cancelFetch () {
if (this.httpClient) {
logger.debug('%s, Cancelling fetch for url=%s', this.tag, this.url)
this.httpClient.abort()
this.httpClient = null
this.url = null
}
this.response.socket.removeAllListeners()
}
}
close () {
if (this.response && this.response.socket) {
this.response.socket.destroy()
//logger.debug('%s, close(), after socket.destroy(), statusTCP=%s', JSON.stringify(this.response), this.response.socket)
}
if (this.httpClient) {
logger.debug('%s, Closing HTTP connection for url=%s', this.tag, this.url)
this.httpClient.abort()
this.httpClient = null
this.url = null
this.response = null
}
}