summaryrefslogtreecommitdiffstats
path: root/uclient-test.uc
blob: 7333aa4ef2370fa199a3d67f959f16f78a212b1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env ucode
'use strict';
import { basename, stdout } from "fs";
let uloop = require("uloop");
let uclient = require("uclient");

function fetch_data() {
	let data;
	while (length(data = uc.read()) > 0)
		print(data);
}

let url = shift(ARGV);
if (!url) {
	warn(`Usage: ${basename(sourcepath())} <url>\n`);
	exit(1);
}

uloop.init();
uc = uclient.new(url, null, {
	header_done: (cb) => {
		warn(sprintf("Headers: %.J\nStatus: %.J\n", uc.get_headers(), uc.status()));
	},
	data_read: fetch_data,
	data_eof: (cb) => {
		stdout.flush();
		uloop.end();
	},
	error: (cb, code) => {
		warn(`Error: ${code}\n`);
		uloop.end();
	}
});

if (!uc.ssl_init({ verify: false })) {
	warn(`Failed to initialize SSL\n`);
	exit(1);
}

if (!uc.connect()) {
	warn(`Failed to connect\n`);
	exit(1);
}

if (!uc.request("GET")) {
	warn(`Failed to send request\n`);
	exit(1);
}

uloop.run();