Created
July 12, 2021 09:22
-
-
Save TheAthlete/5920ced9e768fffacbd14d9c9a9e07cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const net = require('net'); | |
const querystring = require('querystring'); | |
function isObject(obj) { | |
return obj !== null && typeof obj === 'object'; | |
} | |
function http_get(host, port, path, query, timeout, cb) { | |
const query_str = querystring.stringify(query); | |
console.log({query_str, query, path}) | |
const path_query = path + (isObject(query) && Object.keys(query).length ? `?${query_str}` : '') | |
if(!path_query.startsWith('/')) path_query = `/${path_query}`; | |
let timeout_at = Date.now() + timeout; | |
const socket = new net.Socket(); | |
socket.connect({ host, port }) | |
console.log({socket}) | |
if (socket.connecting) { | |
// connected | |
} else { | |
socket.setTimeout(timeout_at); | |
socket.on('timeout', () => { | |
console.log('socket timeout'); | |
socket.end(); | |
}); | |
} | |
console.log(`Connected to ${host} on port ${port}`) | |
let p = `GET ${path_query} HTTP/1.1\r\nHost: ${host}\r\n\r\n`; | |
console.log({p}) | |
socket.write(p); | |
socket.on('data', (data) => { | |
cb(data) | |
}) | |
} | |
http_get('planetperl.ru', 80, '/', {}, 10, (data) => { | |
console.log({data: data.toString}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment