From 73f50e2679525e7df8734c875a3c12732565f953 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Mon, 6 Jan 2025 15:20:09 -0800 Subject: http: The big HTTP API refactoring of January 2025. This represents a major change in the HTTP code base, consisting of a complete revamp of the HTTP API. The changes here are too numerous to mention, but the end result should be a vastly simpler API for both server and client applications. Many needless allocations were removed by providing fixed buffers for various parameters and headers when possible. A few bugs were fixed. Most especially we have fixed some bugs around very large URIs and headers, and we have also addressed conformance bugs to more closely conform to RFCs 9110 and 9112. As part of this work, the APIs for WebSockets changed slightly as well. In particular the properties available for accessing headers have changed. There is still documentation conversion work to do, and additional functionality (such as proper support for chunked transfers), but this is a big step in the right direction. --- tests/httpclient.c | 129 +++++++++++++++++++++++++---------------------------- tests/wss.c | 13 ------ 2 files changed, 60 insertions(+), 82 deletions(-) (limited to 'tests') diff --git a/tests/httpclient.c b/tests/httpclient.c index f3307fc1..b23449f1 100644 --- a/tests/httpclient.c +++ b/tests/httpclient.c @@ -14,8 +14,8 @@ #include #endif +#include #include -#include #include "core/nng_impl.h" @@ -26,7 +26,7 @@ TestMain("HTTP Client", { Convey("Given a TCP connection to example.com", { nng_aio *aio; nng_http_client *cli; - nng_http_conn *http; + nng_http *http; nng_url *url; So(nng_aio_alloc(&aio, NULL, NULL) == 0); @@ -41,31 +41,24 @@ TestMain("HTTP Client", { http = nng_aio_get_output(aio, 0); Reset({ nng_http_client_free(cli); - nng_http_conn_close(http); + nng_http_close(http); nng_aio_free(aio); nng_url_free(url); }); Convey("We can initiate a message", { - nng_http_req *req; - nng_http_res *res; - So(http != NULL); - So(nng_http_req_alloc(&req, url) == 0); - So(nng_http_res_alloc(&res) == 0); - Reset({ - nng_http_req_free(req); - nng_http_res_free(res); - }); - nng_http_conn_write_req(http, req, aio); + So(nng_http_set_uri(http, nng_url_path(url), NULL) == + 0); + nng_http_write_request(http, aio); nng_aio_wait(aio); So(nng_aio_result(aio) == 0); - nng_http_conn_read_res(http, res, aio); + nng_http_read_response(http, aio); nng_aio_wait(aio); So(nng_aio_result(aio) == 0); - So(nng_http_res_get_status(res) == 200); + So(nng_http_get_status(http) == 200); Convey("The message contents are correct", { void *data; @@ -73,8 +66,8 @@ TestMain("HTTP Client", { size_t sz; nng_iov iov; - cstr = nng_http_res_get_header( - res, "Content-Length"); + cstr = nng_http_get_header( + http, "Content-Length"); So(cstr != NULL); sz = atoi(cstr); So(sz > 0); @@ -90,7 +83,7 @@ TestMain("HTTP Client", { nng_aio_wait(aio); So(nng_aio_result(aio) == 0); - nng_http_conn_read_all(http, aio); + nng_http_read_all(http, aio); nng_aio_wait(aio); So(nng_aio_result(aio) == 0); }); @@ -116,35 +109,34 @@ TestMain("HTTP Client", { }); Convey("One off exchange works", { - nng_http_req *req; - nng_http_res *res; - void *data; - size_t len; + nng_http *conn; + void *data; + size_t len; - So(nng_http_req_alloc(&req, url) == 0); - So(nng_http_res_alloc(&res) == 0); - Reset({ - nng_http_req_free(req); - nng_http_res_free(res); - }); + nng_http_client_connect(cli, aio); + nng_aio_wait(aio); + So(nng_aio_result(aio) == 0); + conn = nng_aio_get_output(aio, 0); + Reset({ nng_http_close(conn); }); - nng_http_client_transact(cli, req, res, aio); + So(nng_http_set_uri(conn, nng_url_path(url), NULL) == + 0); + + nng_http_transact(conn, aio); nng_aio_wait(aio); So(nng_aio_result(aio) == 0); - So(nng_http_res_get_status(res) == 200); - nng_http_res_get_data(res, &data, &len); + So(nng_http_get_status(conn) == 200); + nng_http_get_body(conn, &data, &len); }); Convey("Connection reuse works", { - nng_http_res *res; - nng_http_req *req; - void *data; - size_t len; - nng_http_conn *conn = NULL; + void *data; + size_t len; + nng_http *conn = NULL; Reset({ if (conn != NULL) { - nng_http_conn_close(conn); + nng_http_close(conn); } }); @@ -153,20 +145,22 @@ TestMain("HTTP Client", { So(nng_aio_result(aio) == 0); conn = nng_aio_get_output(aio, 0); - req = nng_http_conn_req(conn); - res = nng_http_conn_res(conn); - So(nng_http_req_set_url(req, url) == 0); - nng_http_conn_transact(conn, aio); + So(nng_http_set_uri(conn, nng_url_path(url), NULL) == + 0); + nng_http_transact(conn, aio); nng_aio_wait(aio); So(nng_aio_result(aio) == 0); - So(nng_http_res_get_status(res) == 200); - nng_http_res_get_data(res, &data, &len); + So(nng_http_get_status(conn) == 200); + nng_http_get_body(conn, &data, &len); - nng_http_conn_transact(conn, aio); + nng_http_reset(conn); + So(nng_http_set_uri(conn, nng_url_path(url), NULL) == + 0); + nng_http_transact(conn, aio); nng_aio_wait(aio); So(nng_aio_result(aio) == 0); - So(nng_http_res_get_status(res) == 200); - nng_http_res_get_data(res, &data, &len); + So(nng_http_get_status(conn) == 200); + nng_http_get_body(conn, &data, &len); }); }); @@ -178,29 +172,27 @@ TestMain("HTTP Client", { nng_aio *aio; nng_http_client *cli; nng_url *url; - nng_http_req *req; - nng_http_res *res; + nng_http *conn; So(nng_aio_alloc(&aio, NULL, NULL) == 0); So(nng_url_parse(&url, "http://httpbin.org/delay/30") == 0); So(nng_http_client_alloc(&cli, url) == 0); - So(nng_http_req_alloc(&req, url) == 0); - So(nng_http_res_alloc(&res) == 0); - + nng_http_client_connect(cli, aio); + nng_aio_wait(aio); + So(nng_aio_result(aio) == 0); + conn = nng_aio_get_output(aio, 0); Reset({ nng_http_client_free(cli); nng_url_free(url); nng_aio_free(aio); - nng_http_req_free(req); - nng_http_res_free(res); }); nng_aio_set_timeout(aio, 10); // 10 msec timeout - So(nng_http_req_set_header(req, "Cache-Control", "no-cache") == + So(nng_http_set_header(conn, "Cache-Control", "no-cache") == 0); - nng_http_client_transact(cli, req, res, aio); + nng_http_transact(conn, aio); nng_aio_wait(aio); So(nng_aio_result(aio) == NNG_ETIMEDOUT); }); @@ -227,23 +219,22 @@ TestMain("HTTP Client", { }); Convey("One off exchange works", { - nng_http_req *req; - nng_http_res *res; - void *data; - size_t len; - - So(nng_http_req_alloc(&req, url) == 0); - So(nng_http_res_alloc(&res) == 0); - Reset({ - nng_http_req_free(req); - nng_http_res_free(res); - }); + void *data; + size_t len; + nng_http *conn; - nng_http_client_transact(cli, req, res, aio); + nng_http_client_connect(cli, aio); + nng_aio_wait(aio); + So(nng_aio_result(aio) == 0); + conn = nng_aio_get_output(aio, 0); + Reset({ nng_http_close(conn); }); + So(nng_http_set_uri(conn, nng_url_path(url), NULL) == + 0); + nng_http_transact(conn, aio); nng_aio_wait(aio); So(nng_aio_result(aio) == 0); - So(nng_http_res_get_status(res) == 200); - nng_http_res_get_data(res, &data, &len); + So(nng_http_get_status(conn) == 200); + nng_http_get_body(conn, (void **) &data, &len); }); }); }) diff --git a/tests/wss.c b/tests/wss.c index 4fef74e0..8b028fbe 100644 --- a/tests/wss.c +++ b/tests/wss.c @@ -132,7 +132,6 @@ check_props(nng_msg *msg) nng_pipe p; nng_sockaddr la; nng_sockaddr ra; - char *buf; p = nng_msg_get_pipe(msg); So(nng_pipe_id(p) > 0); @@ -143,18 +142,6 @@ check_props(nng_msg *msg) So(nng_pipe_get_addr(p, NNG_OPT_REMADDR, &ra) == 0); So(validloopback(&ra)); - // Request header - buf = NULL; - So(nng_pipe_get_string(p, NNG_OPT_WS_REQUEST_HEADERS, &buf) == 0); - So(strstr(buf, "Sec-WebSocket-Key") != NULL); - nng_strfree(buf); - - // Response header - buf = NULL; - So(nng_pipe_get_string(p, NNG_OPT_WS_RESPONSE_HEADERS, &buf) == 0); - So(strstr(buf, "Sec-WebSocket-Accept") != NULL); - nng_strfree(buf); - return (0); } -- cgit v1.2.3-70-g09d2