Repository navigation
Expand file tree
/
Copy pathsample.c
More file actions
73 lines (57 loc) · 1.32 KB
/
Copy pathsample.c
File metadata and controls
73 lines (57 loc) · 1.32 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "net.h"
#include "tls.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void
conn_cb(struct uvv_net *net) {
char buff[] = "GET / HTTP/1.1\r\n\r\n";
printf("connected\n");
uvv_net__write(net, buff, sizeof buff);
}
static void
read_cb(struct uvv_net *net, size_t size, const char *buff) {
printf("%.*s\n", (int)size, buff);
}
static void
error_cb(struct uvv_net *net, int e, const char *msg) {
printf("%d %s\n", e, msg);
}
static void
close_cb(struct uvv_net *net) {
printf("closed\n");
}
int
main(int argc, char *argv[]) {
uvv_tls_ctx *ctx;
struct uvv_net *net;
uv_loop_t *loop;
char *host;
int port;
int timeout;
if (argc < 3) {
printf("usage: ./sample host port\n");
exit(0);
}
host = argv[1];
port = atoi(argv[2]);
timeout = 3000;
uvv_ssl__init();
ctx = uvv_tls_ctx__create();
loop = uv_default_loop();
net = uvv_net__create(loop);
net->conn_cb = conn_cb;
net->close_cb = close_cb;
net->read_cb = read_cb;
net->error_cb = error_cb;
net->data = 0;
if (port == 443) {
uvv_net__set_tls(net, ctx);
}
uvv_net__connect(net, host, port, timeout);
uv_run(loop, UV_RUN_DEFAULT);
uvv_net__destroy(net);
uvv_tls_ctx__destroy(ctx);
uvv_ssl__unit();
return 0;
}