summaryrefslogtreecommitdiff
path: root/src/supplemental/http/client.c
blob: 4bb517ce9f39c369f3a5a757b03a77daef977ee5 (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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// Copyright 2017 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2017 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

#include "core/nng_impl.h"
#include "supplemental/tls/tls.h"

#include "http.h"

struct nni_http_client {
	nng_sockaddr     addr;
	nni_list         aios;
	nni_mtx          mtx;
	bool             closed;
	nng_tls_config * tls;
	nni_aio *        connaio;
	nni_plat_tcp_ep *tep;
};

static void
http_conn_start(nni_http_client *c)
{
	nni_plat_tcp_ep_connect(c->tep, c->connaio);
}

static void
http_conn_done(void *arg)
{
	nni_http_client *  c = arg;
	nni_aio *          aio;
	int                rv;
	nni_plat_tcp_pipe *p;
	nni_http *         http;

	nni_mtx_lock(&c->mtx);
	rv = nni_aio_result(c->connaio);
	p  = rv == 0 ? nni_aio_get_pipe(c->connaio) : NULL;
	if ((aio = nni_list_first(&c->aios)) == NULL) {
		if (p != NULL) {
			nni_plat_tcp_pipe_fini(p);
		}
		nni_mtx_unlock(&c->mtx);
		return;
	}
	nni_aio_list_remove(aio);

	if (rv != 0) {
		nni_aio_finish_error(aio, rv);
		nni_mtx_unlock(&c->mtx);
		return;
	}

	if (c->tls != NULL) {
		rv = nni_http_init_tls(&http, c->tls, p);
	} else {
		rv = nni_http_init_tcp(&http, p);
	}
	if (rv != 0) {
		nni_aio_finish_error(aio, rv);
		nni_mtx_unlock(&c->mtx);
		return;
	}

	nni_aio_set_output(aio, 0, http);
	nni_aio_finish(aio, 0, 0);

	if (!nni_list_empty(&c->aios)) {
		http_conn_start(c);
	}
	nni_mtx_unlock(&c->mtx);
}

void
nni_http_client_fini(nni_http_client *c)
{
	nni_aio_fini(c->connaio);
	nni_plat_tcp_ep_fini(c->tep);
	nni_mtx_fini(&c->mtx);
#ifdef NNG_SUPP_TLS
	if (c->tls != NULL) {
		nng_tls_config_fini(c->tls);
	}
#endif
	NNI_FREE_STRUCT(c);
}

int
nni_http_client_init(nni_http_client **cp, nng_sockaddr *sa)
{
	int rv;

	nni_http_client *c;
	if ((c = NNI_ALLOC_STRUCT(c)) == NULL) {
		return (NNG_ENOMEM);
	}
	c->addr = *sa;
	rv = nni_plat_tcp_ep_init(&c->tep, NULL, &c->addr, NNI_EP_MODE_DIAL);
	if (rv != 0) {
		NNI_FREE_STRUCT(c);
		return (rv);
	}
	nni_mtx_init(&c->mtx);
	nni_aio_list_init(&c->aios);

	if ((rv = nni_aio_init(&c->connaio, http_conn_done, c)) != 0) {
		nni_http_client_fini(c);
		return (rv);
	}
	*cp = c;
	return (0);
}

#ifdef NNG_SUPP_TLS
int
nni_http_client_set_tls(nni_http_client *c, nng_tls_config *tls)
{
	nng_tls_config *old;
	nni_mtx_lock(&c->mtx);
	old    = c->tls;
	c->tls = tls;
	if (tls != NULL) {
		nni_tls_config_hold(tls);
	}
	nni_mtx_unlock(&c->mtx);
	if (old != NULL) {
		nng_tls_config_fini(old);
	}
	return (0);
}
#endif

static void
http_connect_cancel(nni_aio *aio, int rv)
{
	nni_http_client *c = aio->a_prov_data;
	nni_mtx_lock(&c->mtx);
	if (nni_aio_list_active(aio)) {
		nni_aio_list_remove(aio);
		nni_aio_finish_error(aio, rv);
	}
	if (nni_list_empty(&c->aios)) {
		nni_aio_cancel(c->connaio, rv);
	}
	nni_mtx_unlock(&c->mtx);
}

void
nni_http_client_connect(nni_http_client *c, nni_aio *aio)
{
	if (nni_aio_start(aio, http_connect_cancel, aio) != 0) {
		return;
	}
	nni_mtx_lock(&c->mtx);
	nni_list_append(&c->aios, aio);
	if (nni_list_first(&c->aios) == aio) {
		http_conn_start(c);
	}
	nni_mtx_unlock(&c->mtx);
}