summaryrefslogtreecommitdiffstats
path: root/test/httpget.c
blob: 29f7f14294ceaff6ce873384a7152690d8fc7dc3 (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
51
52
53
54
55
56
57
58
59
#include <libtf/tf.h>

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>

struct ctx {
	char *hostname;
};

static void ping_fiber(void *ptr)
{
	struct ctx *ctx = (struct ctx*) ptr;
	struct tf_sockaddr host;
	struct tf_fd fd;
	char buf[128];
	int bytes = 0, r;
	const char *req = "GET / HTTP/1.0\r\n\r\n";

	printf("Lookup %s\n", ctx->hostname);
	host.u.in.sin_family = AF_INET;
	host.u.in.sin_addr.s_addr = inet_addr(ctx->hostname);
	host.u.in.sin_port = htons(80);

	r = tf_socket(&fd, AF_INET, SOCK_STREAM, 0);
	if (r < 0)
		goto err;

	r = tf_timed(tf_connect(&fd, &host), 10000);
	if (r < 0)
		goto err_close;

	r = tf_write_fully(&fd, req, strlen(req));
	if (r < 0)
		goto err_close;

	while ((r = tf_read(&fd, buf, sizeof(buf))) > 0)
		bytes += r;
err_close:
	tf_close(&fd);
err:
	printf("Host: %s, received %d bytes (r=%d, %s)\n",
		ctx->hostname, bytes, -r, strerror(-r));
}

int main(int argc, char **argv)
{
	struct ctx *c;
	int i;

	tf_scheduler_enable(NULL);
	for (i = 1; i < argc; i++) {
		c = tf_fiber_create(ping_fiber, sizeof(struct ctx));
		c->hostname = argv[i];
		tf_fiber_put(c);
	}
	tf_scheduler_disable();
}