-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
84 lines (59 loc) · 1.64 KB
/
main.c
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
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <glib.h>
#include "ghttp2.h"
GHTTP2Client *client;
GHTTP2Req *req;
static gboolean do_request(gpointer data)
{
printf("\n> Try request\n");
ghttp2_client_request(client, data);
return FALSE;
}
static void on_push(GHTTP2Req *req, GHashTable *headers, void *user_data)
{
GList *keys, *cur;
printf("push !!! (stream-id=%d)\n", ghttp2_request_get_stream_id(req));
keys = g_hash_table_get_keys(headers);
cur = keys;
while (cur) {
printf("[%s] = [%s]\n", (char *) cur->data,
(char *) g_hash_table_lookup(headers, cur->data));
cur = cur->next;
}
g_list_free(keys);
}
int main(int argc, char **argv)
{
GMainLoop *mainloop;
struct sigaction act;
char *host = "https://nghttp2.org";
if (argc == 2)
host = argv[1];
mainloop = g_main_loop_new(NULL, FALSE);
memset(&act, 0, sizeof(struct sigaction));
act.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &act, 0);
ghttp2_client_init();
client = ghttp2_client_new();
if (!client) {
return -1;
}
ghttp2_client_set_push_callback(client, on_push, NULL);
if (ghttp2_client_connect(client, host) < 0) {
fprintf(stderr, "ghttp2_client_connect() failed\n");
return -1;
}
printf("> session connected\n");
req = ghttp2_request_new("/");
ghttp2_request_set_response_callback(req, on_push, NULL);
ghttp2_request_set_header_authority(req, TRUE);
printf("> add timer 100 milliseconds uri '%s'\n", host);
g_timeout_add(100, do_request, req);
printf("> start mainloop\n");
g_main_loop_run(mainloop);
ghttp2_client_free(client);
return EXIT_SUCCESS;
}