zhangmeng
2024-04-22 16935f4aebffdd1b6580b844391a0aa0f4f3012b
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//
// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
// Copyright 2019 Devolutions <info@devolutions.net>
//
// 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 <stdbool.h>
#include <stdlib.h>
#include <string.h>
 
#include "core/nng_impl.h"
 
#include <nng/supplemental/tls/tls.h>
 
#include "http_api.h"
 
static nni_mtx http_txn_lk;
 
struct nng_http_client {
    nni_list           aios;
    nni_mtx            mtx;
    bool               closed;
    nni_aio *          aio;
    nng_stream_dialer *dialer;
};
 
static void
http_dial_start(nni_http_client *c)
{
    if (nni_list_empty(&c->aios)) {
        return;
    }
    nng_stream_dialer_dial(c->dialer, c->aio);
}
 
static void
http_dial_cb(void *arg)
{
    nni_http_client *c = arg;
    nni_aio *        aio;
    int              rv;
    nng_stream *     stream;
    nni_http_conn *  conn;
 
    nni_mtx_lock(&c->mtx);
    rv = nni_aio_result(c->aio);
 
    if ((aio = nni_list_first(&c->aios)) == NULL) {
        // User abandoned request, and no residuals left.
        nni_mtx_unlock(&c->mtx);
        if (rv == 0) {
            stream = nni_aio_get_output(c->aio, 0);
            nng_stream_free(stream);
        }
        return;
    }
 
    if (rv != 0) {
        nni_aio_list_remove(aio);
        http_dial_start(c);
        nni_mtx_unlock(&c->mtx);
        nni_aio_finish_error(aio, rv);
        return;
    }
 
    nni_aio_list_remove(aio);
    stream = nni_aio_get_output(c->aio, 0);
    NNI_ASSERT(stream != NULL);
 
    rv = nni_http_conn_init(&conn, stream);
    http_dial_start(c);
    nni_mtx_unlock(&c->mtx);
 
    if (rv != 0) {
        // the conn_init function will have already discard stream.
        nni_aio_finish_error(aio, rv);
        return;
    }
 
    nni_aio_set_output(aio, 0, conn);
    nni_aio_finish(aio, 0, 0);
}
 
void
nni_http_client_fini(nni_http_client *c)
{
    nni_aio_free(c->aio);
    nng_stream_dialer_free(c->dialer);
    nni_mtx_fini(&c->mtx);
    NNI_FREE_STRUCT(c);
}
 
int
nni_http_client_init(nni_http_client **cp, const nni_url *url)
{
    int              rv;
    nni_http_client *c;
    nng_url          my_url;
    const char *     scheme;
 
    if ((scheme = nni_http_stream_scheme(url->u_scheme)) == NULL) {
        return (NNG_EADDRINVAL);
    }
    // Rewrite URLs to either TLS or TCP.
    memcpy(&my_url, url, sizeof(my_url));
    my_url.u_scheme = (char *) scheme;
 
    if (strlen(url->u_hostname) == 0) {
        // We require a valid hostname.
        return (NNG_EADDRINVAL);
    }
 
    if ((c = NNI_ALLOC_STRUCT(c)) == NULL) {
        return (NNG_ENOMEM);
    }
    nni_mtx_init(&c->mtx);
    nni_aio_list_init(&c->aios);
 
    if ((rv = nng_stream_dialer_alloc_url(&c->dialer, &my_url)) != 0) {
        nni_http_client_fini(c);
        return (rv);
    }
 
    if ((rv = nni_aio_alloc(&c->aio, http_dial_cb, c)) != 0) {
        nni_http_client_fini(c);
        return (rv);
    }
 
    *cp = c;
    return (0);
}
 
int
nni_http_client_set_tls(nni_http_client *c, nng_tls_config *tls)
{
    return (nng_stream_dialer_set_ptr(c->dialer, NNG_OPT_TLS_CONFIG, tls));
}
 
int
nni_http_client_get_tls(nni_http_client *c, nng_tls_config **tlsp)
{
    return (nng_stream_dialer_get_ptr(
        c->dialer, NNG_OPT_TLS_CONFIG, (void **) tlsp));
}
 
int
nni_http_client_set(nni_http_client *c, const char *name, const void *buf,
    size_t sz, nni_type t)
{
    // We have no local options, but we just pass them straight through.
    return (nni_stream_dialer_set(c->dialer, name, buf, sz, t));
}
 
int
nni_http_client_get(
    nni_http_client *c, const char *name, void *buf, size_t *szp, nni_type t)
{
    return (nni_stream_dialer_get(c->dialer, name, buf, szp, t));
}
 
static void
http_dial_cancel(nni_aio *aio, void *arg, int rv)
{
    nni_http_client *c = arg;
    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_abort(c->aio, rv);
    }
    nni_mtx_unlock(&c->mtx);
}
 
void
nni_http_client_connect(nni_http_client *c, nni_aio *aio)
{
    int rv;
    if (nni_aio_begin(aio) != 0) {
        return;
    }
    nni_mtx_lock(&c->mtx);
    if ((rv = nni_aio_schedule(aio, http_dial_cancel, c)) != 0) {
        nni_mtx_unlock(&c->mtx);
        nni_aio_finish_error(aio, rv);
        return;
    }
    nni_list_append(&c->aios, aio);
    if (nni_list_first(&c->aios) == aio) {
        http_dial_start(c);
    }
    nni_mtx_unlock(&c->mtx);
}
 
static int  http_client_sys_init(void);
static void http_client_sys_fini(void);
 
static nni_initializer http_client_initializer = {
    .i_init = http_client_sys_init,
    .i_fini = http_client_sys_fini,
    .i_once = 0,
};
 
typedef enum http_txn_state {
    HTTP_CONNECTING,
    HTTP_SENDING,
    HTTP_RECVING,
    HTTP_RECVING_BODY,
    HTTP_RECVING_CHUNKS,
} http_txn_state;
 
typedef struct http_txn {
    nni_aio *        aio;  // lower level aio
    nni_list         aios; // upper level aio(s) -- maximum one
    nni_http_client *client;
    nni_http_conn *  conn;
    nni_http_req *   req;
    nni_http_res *   res;
    nni_http_chunks *chunks;
    http_txn_state   state;
} http_txn;
 
static void
http_txn_fini(void *arg)
{
    http_txn *txn = arg;
    if (txn->client != NULL) {
        // We only close the connection if we created it.
        if (txn->conn != NULL) {
            nni_http_conn_fini(txn->conn);
            txn->conn = NULL;
        }
    }
    nni_http_chunks_free(txn->chunks);
    nni_aio_reap(txn->aio);
    NNI_FREE_STRUCT(txn);
}
 
static void
http_txn_finish_aios(http_txn *txn, int rv)
{
    nni_aio *aio;
    while ((aio = nni_list_first(&txn->aios)) != NULL) {
        nni_list_remove(&txn->aios, aio);
        nni_aio_finish_error(aio, rv);
    }
}
 
static void
http_txn_cb(void *arg)
{
    http_txn *      txn = arg;
    const char *    str;
    char *          end;
    int             rv;
    uint64_t        len;
    nni_iov         iov;
    char *          dst;
    size_t          sz;
    nni_http_chunk *chunk = NULL;
 
    nni_mtx_lock(&http_txn_lk);
    if ((rv = nni_aio_result(txn->aio)) != 0) {
        http_txn_finish_aios(txn, rv);
        nni_mtx_unlock(&http_txn_lk);
        http_txn_fini(txn);
        return;
    }
    switch (txn->state) {
    case HTTP_CONNECTING:
        txn->conn  = nni_aio_get_output(txn->aio, 0);
        txn->state = HTTP_SENDING;
        nni_http_write_req(txn->conn, txn->req, txn->aio);
        nni_mtx_unlock(&http_txn_lk);
        return;
 
    case HTTP_SENDING:
        txn->state = HTTP_RECVING;
        nni_http_read_res(txn->conn, txn->res, txn->aio);
        nni_mtx_unlock(&http_txn_lk);
        return;
 
    case HTTP_RECVING:
 
        // Detect chunked encoding.  You poor bastard.
        if (((str = nni_http_res_get_header(
                  txn->res, "Transfer-Encoding")) != NULL) &&
            (strstr(str, "chunked") != NULL)) {
 
            if ((rv = nni_http_chunks_init(&txn->chunks, 0)) !=
                0) {
                goto error;
            }
            txn->state = HTTP_RECVING_CHUNKS;
            nni_http_read_chunks(txn->conn, txn->chunks, txn->aio);
            nni_mtx_unlock(&http_txn_lk);
            return;
        }
 
        str = nni_http_req_get_method(txn->req);
        if ((nni_strcasecmp(str, "HEAD") == 0) ||
            ((str = nni_http_res_get_header(
                  txn->res, "Content-Length")) == NULL) ||
            ((len = (uint64_t) strtoull(str, &end, 10)) == 0) ||
            (end == NULL) || (*end != '\0')) {
            // If no content-length, or HEAD (which per RFC
            // never transfers data), then we are done.
            http_txn_finish_aios(txn, 0);
            nni_mtx_unlock(&http_txn_lk);
            http_txn_fini(txn);
            return;
        }
 
        if ((rv = nni_http_res_alloc_data(txn->res, (size_t) len)) !=
            0) {
            goto error;
        }
        nni_http_res_get_data(txn->res, &iov.iov_buf, &iov.iov_len);
        nni_aio_set_iov(txn->aio, 1, &iov);
        txn->state = HTTP_RECVING_BODY;
        nni_http_read_full(txn->conn, txn->aio);
        nni_mtx_unlock(&http_txn_lk);
        return;
 
    case HTTP_RECVING_BODY:
        // All done!
        http_txn_finish_aios(txn, 0);
        nni_mtx_unlock(&http_txn_lk);
        http_txn_fini(txn);
        return;
 
    case HTTP_RECVING_CHUNKS:
        // All done, but now we need to coalesce the chunks, for
        // yet *another* copy.  Chunked transfers are such crap.
        sz = nni_http_chunks_size(txn->chunks);
        if ((rv = nni_http_res_alloc_data(txn->res, sz)) != 0) {
            goto error;
        }
        nni_http_res_get_data(txn->res, (void **) &dst, &sz);
        while ((chunk = nni_http_chunks_iter(txn->chunks, chunk)) !=
            NULL) {
            memcpy(dst, nni_http_chunk_data(chunk),
                nni_http_chunk_size(chunk));
            dst += nni_http_chunk_size(chunk);
        }
        http_txn_finish_aios(txn, 0);
        nni_mtx_unlock(&http_txn_lk);
        http_txn_fini(txn);
        return;
    }
 
error:
    http_txn_finish_aios(txn, rv);
    nni_http_conn_close(txn->conn);
    nni_mtx_unlock(&http_txn_lk);
    http_txn_fini(txn);
}
 
static void
http_txn_cancel(nni_aio *aio, void *arg, int rv)
{
    http_txn *txn = arg;
    nni_mtx_lock(&http_txn_lk);
    if (nni_aio_list_active(aio)) {
        nni_aio_abort(txn->aio, rv);
    }
    nni_mtx_unlock(&http_txn_lk);
}
 
// nni_http_transact_conn sends a request to an HTTP server, and reads the
// response.  It also attempts to read any associated data.  Note that
// at present it can only read data that comes in normally, as support
// for Chunked Transfer Encoding is missing.  Note that cancelling the aio
// is generally fatal to the connection.
void
nni_http_transact_conn(
    nni_http_conn *conn, nni_http_req *req, nni_http_res *res, nni_aio *aio)
{
    http_txn *txn;
    int       rv;
 
    nni_initialize(&http_client_initializer);
 
    if (nni_aio_begin(aio) != 0) {
        return;
    }
    if ((txn = NNI_ALLOC_STRUCT(txn)) == NULL) {
        nni_aio_finish_error(aio, NNG_ENOMEM);
        return;
    }
    if ((rv = nni_aio_alloc(&txn->aio, http_txn_cb, txn)) != 0) {
        NNI_FREE_STRUCT(txn);
        nni_aio_finish_error(aio, rv);
        return;
    }
    nni_aio_list_init(&txn->aios);
    txn->client = NULL;
    txn->conn   = conn;
    txn->req    = req;
    txn->res    = res;
    txn->state  = HTTP_SENDING;
 
    nni_mtx_lock(&http_txn_lk);
    if ((rv = nni_aio_schedule(aio, http_txn_cancel, txn)) != 0) {
        nni_mtx_unlock(&http_txn_lk);
        nni_aio_finish_error(aio, rv);
        http_txn_fini(txn);
        return;
    }
    nni_http_res_reset(txn->res);
    nni_list_append(&txn->aios, aio);
    nni_http_write_req(conn, req, txn->aio);
    nni_mtx_unlock(&http_txn_lk);
}
 
// nni_http_transact_simple does a single transaction, creating a connection
// just for the purpose, and closing it when done.  (No connection caching.)
// The reason we require a client to be created first is to deal with TLS
// settings.  A single global client (per server) may be used.
void
nni_http_transact(nni_http_client *client, nni_http_req *req,
    nni_http_res *res, nni_aio *aio)
{
    http_txn *txn;
    int       rv;
 
    nni_initialize(&http_client_initializer);
 
    if (nni_aio_begin(aio) != 0) {
        return;
    }
    if ((txn = NNI_ALLOC_STRUCT(txn)) == NULL) {
        nni_aio_finish_error(aio, NNG_ENOMEM);
        return;
    }
    if ((rv = nni_aio_alloc(&txn->aio, http_txn_cb, txn)) != 0) {
        NNI_FREE_STRUCT(txn);
        nni_aio_finish_error(aio, rv);
        return;
    }
 
    if ((rv = nni_http_req_set_header(req, "Connection", "close")) != 0) {
        nni_aio_finish_error(aio, rv);
        http_txn_fini(txn);
        return;
    }
 
    nni_aio_list_init(&txn->aios);
    txn->client = client;
    txn->conn   = NULL;
    txn->req    = req;
    txn->res    = res;
    txn->state  = HTTP_CONNECTING;
 
    nni_mtx_lock(&http_txn_lk);
    if ((rv = nni_aio_schedule(aio, http_txn_cancel, txn)) != 0) {
        nni_mtx_unlock(&http_txn_lk);
        nni_aio_finish_error(aio, rv);
        http_txn_fini(txn);
        return;
    }
    nni_http_res_reset(txn->res);
    nni_list_append(&txn->aios, aio);
    nni_http_client_connect(client, txn->aio);
    nni_mtx_unlock(&http_txn_lk);
}
 
static int
http_client_sys_init(void)
{
    nni_mtx_init(&http_txn_lk);
    return (0);
}
 
static void
http_client_sys_fini(void)
{
    nni_mtx_fini(&http_txn_lk);
}