-
-
Notifications
You must be signed in to change notification settings - Fork 498
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
98 changed files
with
2,251 additions
and
5,372 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// Copyright 2018 Staysail Systems, Inc. <[email protected]> | ||
// Copyright 2025 Staysail Systems, Inc. <[email protected]> | ||
// Copyright 2018 Capitar IT Group BV <[email protected]> | ||
// | ||
// This software is supplied under the terms of the MIT License, a | ||
|
@@ -33,8 +33,8 @@ | |
// % ./http_client http://httpbin.org/ip | ||
// | ||
|
||
#include <nng/http.h> | ||
#include <nng/nng.h> | ||
#include <nng/supplemental/http/http.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
|
@@ -49,11 +49,9 @@ int | |
main(int argc, char **argv) | ||
{ | ||
nng_http_client *client; | ||
nng_http_conn *conn; | ||
nng_http *conn; | ||
nng_url *url; | ||
nng_aio *aio; | ||
nng_http_req *req; | ||
nng_http_res *res; | ||
const char *hdr; | ||
int rv; | ||
int len; | ||
|
@@ -66,12 +64,12 @@ main(int argc, char **argv) | |
} | ||
|
||
if (((rv = nng_init(NULL)) != 0) || | ||
((rv = nng_aio_alloc(&aio, NULL, NULL)) != 0) || | ||
((rv = nng_url_parse(&url, argv[1])) != 0) || | ||
((rv = nng_http_client_alloc(&client, url)) != 0) || | ||
((rv = nng_http_req_alloc(&req, url)) != 0) || | ||
((rv = nng_http_res_alloc(&res)) != 0) || | ||
((rv = nng_aio_alloc(&aio, NULL, NULL)) != 0)) { | ||
fatal(rv); | ||
return 1; | ||
} | ||
|
||
// Start connection process... | ||
|
@@ -90,30 +88,29 @@ main(int argc, char **argv) | |
// The Host: header is already set up too. | ||
|
||
// Send the request, and wait for that to finish. | ||
nng_http_conn_write_req(conn, req, aio); | ||
nng_http_write_request(conn, aio); | ||
nng_aio_wait(aio); | ||
|
||
if ((rv = nng_aio_result(aio)) != 0) { | ||
fatal(rv); | ||
} | ||
|
||
// Read a response. | ||
nng_http_conn_read_res(conn, res, aio); | ||
nng_http_read_response(conn, aio); | ||
nng_aio_wait(aio); | ||
|
||
if ((rv = nng_aio_result(aio)) != 0) { | ||
fatal(rv); | ||
} | ||
|
||
if (nng_http_res_get_status(res) != NNG_HTTP_STATUS_OK) { | ||
if (nng_http_get_status(conn) != NNG_HTTP_STATUS_OK) { | ||
fprintf(stderr, "HTTP Server Responded: %d %s\n", | ||
nng_http_res_get_status(res), | ||
nng_http_res_get_reason(res)); | ||
nng_http_get_status(conn), nng_http_get_reason(conn)); | ||
} | ||
|
||
// This only supports regular transfer encoding (no Chunked-Encoding, | ||
// and a Content-Length header is required.) | ||
if ((hdr = nng_http_res_get_header(res, "Content-Length")) == NULL) { | ||
if ((hdr = nng_http_get_header(conn, "Content-Length")) == NULL) { | ||
fprintf(stderr, "Missing Content-Length header.\n"); | ||
exit(1); | ||
} | ||
|
@@ -134,7 +131,7 @@ main(int argc, char **argv) | |
nng_aio_set_iov(aio, 1, &iov); | ||
|
||
// Now attempt to receive the data. | ||
nng_http_conn_read_all(conn, aio); | ||
nng_http_read_all(conn, aio); | ||
|
||
// Wait for it to complete. | ||
nng_aio_wait(aio); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.