minisrv_service_file = true;
request_is_async = true;
proxyUrl = minisrv_config.services[service_name].wrp_url;
if (!proxyUrl.endsWith('/')) {
proxyUrl += '/';
}
if (!request_headers.query.url) {
headers = `200 OK
Content-Type: text/html`;
data = `
Web Rendering Proxy
Web Rendering Proxy
Welcome to the Web Rendering Proxy.
Please provide a valid URL to render.
`
sendToClient(socket, headers, data);
} else {
if (request_headers.query.err) {
finishPage(`Error
${request_headers.query.err}
`).join('
');
} else {
const params = new URLSearchParams({
url: request_headers.query.url,
z: request_headers.query.z || '1.0',
t: request_headers.query.t || 'jpg',
c: request_headers.query.c || '256',
m: request_headers.query.m || 'ismap'
});
const fullUrl = proxyUrl + '?' + params.toString();
const urlObj = new URL(fullUrl);
const lib = urlObj.protocol === 'https:' ? https : http;
if (request_headers.query.id) {
finishPage(`
`);
} else {
function fetch(url) {
return new Promise((resolve, reject) => {
lib.get(url, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => resolve({ text: () => Promise.resolve(data) }));
}).on('error', reject);
});
}
fetch(fullUrl)
.then(response => response.text())
.then(text => { process(text); })
.catch(err => { finishPage(`Error fetching page: ${err.message}`); });
}
}
}
function process(content) {
if (content.startsWith('
and extract the URLs
const aHrefMatch = content.match(/ `
`).join('
'));
// You can now use the `links` array as needed
} else {
var idx = content.indexOf('
');
content = content.substring(0, idx);
finishPage(content);
}
}
function finishPage(content) {
headers = `200 OK
Content-Type: text/html`;
data = `
Web Rendering Proxy
Start Over
`;
sendToClient(socket, headers, data);
}