Skip to content

Commit 2c0dc8b

Browse files
committed
update dependency - bug fixes, stage http handling
1 parent abb276b commit 2c0dc8b

6 files changed

Lines changed: 61 additions & 34 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
3030
find_package(raii QUIET)
3131
if(NOT raii_FOUND)
3232
FetchContent_Declare(raii
33-
URL https://github.com/zelang-dev/c-raii/archive/refs/tags/2.1.3.zip
34-
URL_MD5 147623b14f65c8201547225708ced4e3
33+
URL https://github.com/zelang-dev/c-raii/archive/refs/tags/2.2.0.zip
34+
URL_MD5 d6909031ab3bd47cdbd81a95c7eb4458
3535
)
3636
FetchContent_MakeAvailable(raii)
3737
endif()

include/asio.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ typedef enum {
6060
ASIO_PIPE,
6161
ASIO_TCP,
6262
ASIO_UDP,
63+
ASIO_TLS,
6364
ASIO_SPAWN,
6465
ASIO_SOCKET,
6566
ASIO_PIPE_0,
@@ -71,7 +72,7 @@ typedef enum {
7172
ASIO_SSL_CERT,
7273
ASIO_SSL_REQ,
7374
ASIO_SSL_PKEY,
74-
ASIO_ASYNC_TLS,
75+
ASIO_HTTP,
7576
ASIO_THIS,
7677
ASIO_ARGS = ASIO_THIS + UV_HANDLE_TYPE_MAX
7778
} asio_types;
@@ -586,6 +587,14 @@ C_API uv_tls_t *get_handle_tls_socket(void_t);
586587

587588
C_API sockaddr_t *sockaddr(string_t host, int port);
588589

590+
/**
591+
* Creates an `this` instance for `data` `asio_types`
592+
* on current `coroutine` `user_data`.
593+
*
594+
* WILL `panic` ~logic_error~, if `user_data` not empty.
595+
*/
596+
C_API void uv_this(void_t *data, asio_types type);
597+
589598
#ifdef __cplusplus
590599
}
591600
#endif

include/uv_http.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ extern "C" {
1111
* Make a GET request, will pause current task, and
1212
* continue other tasks until an response is received.
1313
*
14+
* WILL `panic` ~logic_error~, if current `coroutine` user_data ~field~, not `ASIO_TLS`.
15+
*
1416
* @param path
1517
* @param type defaults to `text/html; charset=utf-8`, if empty
1618
* @param numof number of additional headers
@@ -21,12 +23,14 @@ extern "C" {
2123
*
2224
* - `or:` `kv_custom("key", "value")`
2325
*/
24-
C_API string http_get(string path, string type, u32 numof, ...);
26+
C_API string uv_http_get(string path, string type, u32 numof, ...);
2527

2628
/**
2729
* Make a POST request, will pause current task, and
2830
* continue other tasks until an response is received.
2931
*
32+
* WILL `panic` ~logic_error~, if current `coroutine` user_data ~field~, not `ASIO_TLS`.
33+
*
3034
* @param path
3135
* @param data
3236
* @param type defaults to `text/html; charset=utf-8`, if empty
@@ -38,12 +42,14 @@ C_API string http_get(string path, string type, u32 numof, ...);
3842
*
3943
* - `or:` `kv_custom("key", "value")`
4044
*/
41-
C_API string http_post(string path, string data, string type, u32 numof, ...);
45+
C_API string uv_http_post(string path, string data, string type, u32 numof, ...);
4246

4347
/**
4448
* Make a DELETE request, will pause current task, and
4549
* continue other tasks until an response is received.
4650
*
51+
* WILL `panic` ~logic_error~, if current `coroutine` user_data ~field~, not `ASIO_TLS`.
52+
*
4753
* @param path
4854
* @param data
4955
* @param type defaults to `text/html; charset=utf-8`, if empty
@@ -55,12 +61,14 @@ C_API string http_post(string path, string data, string type, u32 numof, ...);
5561
*
5662
* - `or:` `kv_custom("key", "value")`
5763
*/
58-
C_API string http_delete(string path, string data, u32 numof, ...);
64+
C_API string uv_http_delete(string path, string data, u32 numof, ...);
5965

6066
/**
6167
* Make a PATCH request, will pause current task, and
6268
* continue other tasks until an response is received.
6369
*
70+
* WILL `panic` ~logic_error~, if current `coroutine` user_data ~field~, not `ASIO_TLS`.
71+
*
6472
* @param path
6573
* @param data
6674
* @param numof number of additional headers
@@ -71,12 +79,14 @@ C_API string http_delete(string path, string data, u32 numof, ...);
7179
*
7280
* - `or:` `kv_custom("key", "value")`
7381
*/
74-
C_API string http_patch(string path, string data, u32 numof, ...);
82+
C_API string uv_http_patch(string path, string data, u32 numof, ...);
7583

7684
/**
7785
* Make a OPTIONS request, will pause current task, and
7886
* continue other tasks until an response is received.
7987
*
88+
* WILL `panic` ~logic_error~, if current `coroutine` user_data ~field~, not `ASIO_TLS`.
89+
*
8090
* @param path
8191
* @param numof number of additional headers
8292
*
@@ -86,12 +96,14 @@ C_API string http_patch(string path, string data, u32 numof, ...);
8696
*
8797
* - `or:` `kv_custom("key", "value")`
8898
*/
89-
C_API string http_options(string path, u32 numof, ...);
99+
C_API string uv_http_options(string path, u32 numof, ...);
90100

91101
/**
92102
* Make a HEAD request, will pause current task, and
93103
* continue other tasks until an response is received.
94104
*
105+
* WILL `panic` ~logic_error~, if current `coroutine` user_data ~field~, not `ASIO_TLS`.
106+
*
95107
* @param path
96108
* @param numof number of additional headers
97109
*
@@ -101,13 +113,7 @@ C_API string http_options(string path, u32 numof, ...);
101113
*
102114
* - `or:` `kv_custom("key", "value")`
103115
*/
104-
C_API string http_head(string path, u32 numof, ...);
105-
106-
/**
107-
* Creates an `this` instance for `http_t`/`uv_tls_t`
108-
* on current `coroutine` ~connected~ `stream`.
109-
*/
110-
C_API void http_this(uv_tls_t *socket);
116+
C_API string uv_http_head(string path, u32 numof, ...);
111117

112118
#ifdef __cplusplus
113119
}

src/asio.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ static void on_connect(uv_connect_t *req, int status) {
362362
socket->is_client = true;
363363
socket->is_server = false;
364364
socket->is_connecting = true;
365-
socket->type = ASIO_ASYNC_TLS;
365+
socket->type = ASIO_TLS;
366366
status = uv_tls_connect((string_t)uv->args[3].char_ptr, socket);
367367
socket->is_connecting = false;
368368
}
@@ -392,7 +392,7 @@ static void connection_cb(uv_stream_t *server, int status) {
392392
client_args->tls->is_client = true;
393393
client_args->tls->is_server= true;
394394
client_args->tls->is_connecting = true;
395-
client_args->tls->type = ASIO_ASYNC_TLS;
395+
client_args->tls->type = ASIO_TLS;
396396
result = uv_tls_accept(uv->tls, client_args->tls);
397397
client_args->tls->is_connecting = false;
398398
if (result) {
@@ -1880,7 +1880,7 @@ uv_stream_t *stream_bind_ex(uv_handle_type scheme, string_t address, int port, i
18801880
uv_args->tls->is_server = true;
18811881
uv_args->tls->is_client = false;
18821882
uv_args->tls->is_connecting = false;
1883-
uv_args->tls->type = ASIO_ASYNC_TLS;
1883+
uv_args->tls->type = ASIO_TLS;
18841884
r = uv_tcp_bind(handle, (sockaddr_t *)addr_set, flags);
18851885
} else {
18861886
cerr("failed to bind: `tls_config_new`\n");

src/uv_http.c

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,98 @@
11
#include "asio.h"
22

3-
string http_get(string path, string type, u32 numof, ...) {
3+
string uv_http_get(string path, string type, u32 numof, ...) {
44
uv_tls_t *this = (uv_tls_t *)coro_data();
5-
if (!is_type(this, (raii_type)ASIO_ASYNC_TLS))
5+
if (!is_type(this, (raii_type)ASIO_TLS))
66
throw(logic_error);
77

88
va_list headers;
99
va_start(headers, numof);
1010
string req = http_request(this->http, HTTP_GET, path, type, nullptr, numof, headers);
1111
va_end(headers);
1212

13+
stream_write(this->stream, req);
14+
1315
return req;
1416
}
1517

16-
string http_post(string path, string data, string type, u32 numof, ...) {
18+
string uv_http_post(string path, string data, string type, u32 numof, ...) {
1719
uv_tls_t *this = (uv_tls_t *)coro_data();
18-
if (!is_type(this, (raii_type)ASIO_ASYNC_TLS))
20+
if (!is_type(this, (raii_type)ASIO_TLS))
1921
throw(logic_error);
2022

2123
va_list headers;
2224
va_start(headers, numof);
2325
string req = http_request(this->http, HTTP_POST, path, type, data, numof, headers);
2426
va_end(headers);
2527

28+
stream_write(this->stream, req);
29+
2630
return req;
2731
}
2832

29-
string http_delete(string path, string data, u32 numof, ...) {
33+
string uv_http_delete(string path, string data, u32 numof, ...) {
3034
uv_tls_t *this = (uv_tls_t *)coro_data();
31-
if (!is_type(this, (raii_type)ASIO_ASYNC_TLS))
35+
if (!is_type(this, (raii_type)ASIO_TLS))
3236
throw(logic_error);
3337

3438
va_list headers;
3539
va_start(headers, numof);
3640
string req = http_request(this->http, HTTP_DELETE, path, nullptr, data, numof, headers);
3741
va_end(headers);
3842

43+
stream_write(this->stream, req);
44+
3945
return req;
4046
}
4147

42-
string http_patch(string path, string data, u32 numof, ...) {
48+
string uv_http_patch(string path, string data, u32 numof, ...) {
4349
uv_tls_t *this = (uv_tls_t *)coro_data();
44-
if (!is_type(this, (raii_type)ASIO_ASYNC_TLS))
50+
if (!is_type(this, (raii_type)ASIO_TLS))
4551
throw(logic_error);
4652

4753
va_list headers;
4854
va_start(headers, numof);
4955
string req = http_request(this->http, HTTP_PATCH, path, nullptr, data, numof, headers);
5056
va_end(headers);
5157

58+
stream_write(this->stream, req);
59+
5260
return req;
5361
}
5462

55-
string http_options(string path, u32 numof, ...) {
63+
string uv_http_options(string path, u32 numof, ...) {
5664
uv_tls_t *this = (uv_tls_t *)coro_data();
57-
if (!is_type(this, (raii_type)ASIO_ASYNC_TLS))
65+
if (!is_type(this, (raii_type)ASIO_TLS))
5866
throw(logic_error);
5967

6068
va_list headers;
6169
va_start(headers, numof);
6270
string req = http_request(this->http, HTTP_OPTIONS, path, nullptr, nullptr, numof, headers);
6371
va_end(headers);
6472

73+
stream_write(this->stream, req);
74+
6575
return req;
6676
}
6777

68-
string http_head(string path, u32 numof, ...) {
78+
string uv_http_head(string path, u32 numof, ...) {
6979
uv_tls_t *this = (uv_tls_t *)coro_data();
70-
if (!is_type(this, (raii_type)ASIO_ASYNC_TLS))
80+
if (!is_type(this, (raii_type)ASIO_TLS))
7181
throw(logic_error);
7282

7383
va_list headers;
7484
va_start(headers, numof);
7585
string req = http_request(this->http, HTTP_HEAD, path, nullptr, nullptr, numof, headers);
7686
va_end(headers);
7787

88+
stream_write(this->stream, req);
89+
7890
return req;
7991
}
8092

81-
RAII_INLINE void http_this(uv_tls_t *socket) {
82-
if (is_type(socket, (raii_type)ASIO_ASYNC_TLS) && is_empty(coro_data()))
83-
coro_data_set(coro_active(), (void_t)socket);
93+
RAII_INLINE void uv_this(void_t *data, asio_types type) {
94+
if (is_type(socket, (raii_type)type) && is_empty(coro_data()))
95+
coro_data_set(coro_active(), (void_t)data);
8496

8597
throw(logic_error);
8698
}

src/uv_tls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ void uv_tls_close(uv_tls_t *const socket) {
203203
if (!socket)
204204
return;
205205

206-
if (is_type(socket, (raii_type)ASIO_ASYNC_TLS)) {
206+
if (is_type(socket, (raii_type)ASIO_TLS)) {
207207
socket->type = RAII_ERR;
208208
if (socket->err != UV_EOF && socket->secure)
209209
tls_close(socket->secure);

0 commit comments

Comments
 (0)