Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Httpstatic method #2007

Merged
merged 10 commits into from
Dec 22, 2024
39 changes: 18 additions & 21 deletions demo/rest/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ typedef enum {
} job_state;

typedef struct rest_job {
nng_aio * http_aio; // aio from HTTP we must reply to
nng_http_res * http_res; // HTTP response object
nng_aio *http_aio; // aio from HTTP we must reply to
nng_http_res *http_res; // HTTP response object
job_state state; // 0 = sending, 1 = receiving
nng_msg * msg; // request message
nng_aio * aio; // request flow
nng_msg *msg; // request message
nng_aio *aio; // request flow
nng_ctx ctx; // context on the request socket
struct rest_job *next; // next on the freelist
} rest_job;
Expand All @@ -86,7 +86,7 @@ nng_socket req_sock;

// We maintain a queue of free jobs. This way we don't have to
// deallocate them from the callback; we just reuse them.
nng_mtx * job_lock;
nng_mtx *job_lock;
rest_job *job_freelist;

static void rest_job_cb(void *arg);
Expand Down Expand Up @@ -139,7 +139,7 @@ static void
rest_http_fatal(rest_job *job, const char *fmt, int rv)
{
char buf[128];
nng_aio * aio = job->http_aio;
nng_aio *aio = job->http_aio;
nng_http_res *res = job->http_res;

job->http_res = NULL;
Expand All @@ -156,7 +156,7 @@ static void
rest_job_cb(void *arg)
{
rest_job *job = arg;
nng_aio * aio = job->aio;
nng_aio *aio = job->aio;
int rv;

switch (job->state) {
Expand Down Expand Up @@ -205,13 +205,13 @@ void
rest_handle(nng_aio *aio)
{
struct rest_job *job;
nng_http_req * req = nng_aio_get_input(aio, 0);
nng_http_conn * conn = nng_aio_get_input(aio, 2);
const char * clen;
nng_http_req *req = nng_aio_get_input(aio, 0);
nng_http_conn *conn = nng_aio_get_input(aio, 2);
const char *clen;
size_t sz;
nng_iov iov;
int rv;
void * data;
void *data;

if ((job = rest_get_job()) == NULL) {
nng_aio_finish(aio, NNG_ENOMEM);
Expand Down Expand Up @@ -241,10 +241,10 @@ rest_handle(nng_aio *aio)
void
rest_start(uint16_t port)
{
nng_http_server * server;
nng_http_server *server;
nng_http_handler *handler;
char rest_addr[128];
nng_url * url;
nng_url *url;
int rv;

if ((rv = nng_mtx_alloc(&job_lock)) != 0) {
Expand Down Expand Up @@ -282,18 +282,15 @@ rest_start(uint16_t port)
fatal("nng_http_handler_alloc", rv);
}

if ((rv = nng_http_handler_set_method(handler, "POST")) != 0) {
fatal("nng_http_handler_set_method", rv);
}
nng_http_handler_set_method(handler, "POST");

// We want to collect the body, and we (arbitrarily) limit this to
// 128KB. The default limit is 1MB. You can explicitly collect
// the data yourself with another HTTP read transaction by disabling
// this, but that's a lot of work, especially if you want to handle
// chunked transfers.
if ((rv = nng_http_handler_collect_body(handler, true, 1024 * 128)) !=
0) {
fatal("nng_http_handler_collect_body", rv);
}
nng_http_handler_collect_body(handler, true, 1024 * 128);

if ((rv = nng_http_server_add_handler(server, handler)) != 0) {
fatal("nng_http_handler_add_handler", rv);
}
Expand All @@ -317,7 +314,7 @@ inproc_server(void *arg)
{
nng_socket s;
int rv;
nng_msg * msg;
nng_msg *msg;

if (((rv = nng_rep0_open(&s)) != 0) ||
((rv = nng_listen(s, INPROC_URL, NULL, 0)) != 0)) {
Expand Down
6 changes: 6 additions & 0 deletions docs/man/nng_http_handler_alloc.3http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ by the _path_ argument.
Only the path component of the Request URI is
considered when determining whether the handler should be called.

This implementation limits the _path_ length to 1024 bytes, including the
zero termination byte. This does not prevent requests with much longer
URIs from being supported, doing so will require setting the handler
to matching a parent path in the tree using
xref:nng_http_handler_set_tree.3http.adoc[`nng_http_handler_set_tree`()].

Additionally each handler has a method it is registered to handle
(the default is `GET`, see
xref:nng_http_handler_set_method.3http.adoc[`nng_http_handler_set_method()`]), and
Expand Down
11 changes: 1 addition & 10 deletions docs/man/nng_http_handler_collect_body.3http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ nng_http_handler_collect_body - set HTTP handler to collect request body
#include <nng/nng.h>
#include <nng/supplemental/http/http.h>

int nng_http_handler_collect_body(nng_http_handler *handler, bool want, size_t maxsz);
void nng_http_handler_collect_body(nng_http_handler *handler, bool want, size_t maxsz);
----

== DESCRIPTION
Expand Down Expand Up @@ -60,15 +60,6 @@ This is considered a bug, and is a deficiency for full HTTP/1.1 compliance.
However, few clients send data in this format, so in practice this should
create few limitations.

== RETURN VALUES

This function returns 0 on success, and non-zero otherwise.

== ERRORS

[horizontal]
`NNG_ENOTSUP`:: No support for HTTP in the library.

== SEE ALSO

[.text-left]
Expand Down
14 changes: 2 additions & 12 deletions docs/man/nng_http_handler_set_data.3http.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= nng_http_handler_set_data(3http)
//
// Copyright 2018 Staysail Systems, Inc. <[email protected]>
// Copyright 2024 Staysail Systems, Inc. <[email protected]>
// Copyright 2018 Capitar IT Group BV <[email protected]>
//
// This document is supplied under the terms of the MIT License, a
Expand All @@ -20,7 +20,7 @@ nng_http_handler_set_data - set extra data for HTTP handler
#include <nng/nng.h>
#include <nng/supplemental/http/http.h>

int nng_http_handler_set_data(nng_http_handler *handler, void *data,
void nng_http_handler_set_data(nng_http_handler *handler, void *data,
void (*dtor)(void *));
----

Expand All @@ -36,16 +36,6 @@ then it will be called with _data_ as its argument.
The intended use of
this function is deallocate any resources associated with _data_.

== RETURN VALUES

This function returns 0 on success, and non-zero otherwise.

== ERRORS

[horizontal]
`NNG_ENOMEM`:: Insufficient free memory to perform the operation.
`NNG_ENOTSUP`:: No support for HTTP in the library.

== SEE ALSO

[.text-left]
Expand Down
15 changes: 4 additions & 11 deletions docs/man/nng_http_handler_set_host.3http.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= nng_http_handler_set_host(3http)
//
// Copyright 2018 Staysail Systems, Inc. <[email protected]>
// Copyright 2024 Staysail Systems, Inc. <[email protected]>
// Copyright 2018 Capitar IT Group BV <[email protected]>
//
// This document is supplied under the terms of the MIT License, a
Expand All @@ -20,7 +20,7 @@ nng_http_handler_set_host - set host for HTTP handler
#include <nng/nng.h>
#include <nng/supplemental/http/http.h>

int nng_http_handler_set_host(nng_http_handler *handler, const char *host);
void nng_http_handler_set_host(nng_http_handler *handler, const char *host);
----

== DESCRIPTION
Expand All @@ -41,15 +41,8 @@ ports, the port number can be elided.
The matching test only considers
the hostname or IP address, and ignores any trailing port number.

== RETURN VALUES

This function returns 0 on success, and non-zero otherwise.

== ERRORS

[horizontal]
`NNG_ENOMEM`:: Insufficient free memory to perform the operation.
`NNG_ENOTSUP`:: No support for HTTP in the library.
NOTE: This should not be used with an IP address normally, as `Host:` header
is used with virtual hosts in HTTP/1.1, and not supported for HTTP/1.0.

== SEE ALSO

Expand Down
15 changes: 2 additions & 13 deletions docs/man/nng_http_handler_set_method.3http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ nng_http_handler_set_method - set HTTP handler method
#include <nng/nng.h>
#include <nng/supplemental/http/http.h>

int nng_http_handler_set_method(nng_http_handler *handler, const char *method);
void nng_http_handler_set_method(nng_http_handler *handler, const char *method);
----

== DESCRIPTION
Expand All @@ -36,22 +36,11 @@ NOTE: The server will automatically call "GET" handlers if the client
sends a "HEAD" request, and will suppress HTTP body data in the responses
sent for such requests.

NOTE: No validation of the _method_ is performed, but HTTP specifications
insist that the actual method sent over the wire be capitalized.
NOTE: If _method_ is longer than 32-bytes, it may be truncated silently.

The handler may always examine the actual method used using the
xref:nng_http_req_get_method.3http.adoc[`nng_http_req_get_method()`] function.

== RETURN VALUES

This function returns 0 on success, and non-zero otherwise.

== ERRORS

[horizontal]
`NNG_ENOMEM`:: Insufficient free memory exists.
`NNG_ENOTSUP`:: No support for HTTP in the library.

== SEE ALSO

[.text-left]
Expand Down
16 changes: 3 additions & 13 deletions docs/man/nng_http_handler_set_tree.3http.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= nng_http_handler_set_tree(3http)

// Copyright 2020 Staysail Systems, Inc. <[email protected]>
// Copyright 2024 Staysail Systems, Inc. <[email protected]>
// Copyright 2018 Capitar IT Group BV <[email protected]>
// Copyright 2020 Dirac Research <[email protected]>
//
Expand All @@ -20,9 +20,9 @@ nng_http_handler_set_tree - set HTTP handler to match trees
#include <nng/nng.h>
#include <nng/supplemental/http/http.h>

int nng_http_handler_set_tree(nng_http_handler *handler);
void nng_http_handler_set_tree(nng_http_handler *handler);

int nng_http_handler_set_tree_exclusive(nng_http_handler *handler);
void nng_http_handler_set_tree_exclusive(nng_http_handler *handler);
----

== DESCRIPTION
Expand All @@ -48,16 +48,6 @@ generated when a more specific child does not exist.
This can provide a better experience for users than the standard 404 error
handling.

== RETURN VALUES

This function returns 0 on success, and non-zero otherwise.

== ERRORS

[horizontal]
`NNG_ENOMEM`:: Insufficient free memory exists.
`NNG_ENOTSUP`:: No support for HTTP in the library.

== SEE ALSO

[.text-left]
Expand Down
15 changes: 4 additions & 11 deletions docs/man/nng_http_req_set_method.3http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ nng_http_req_set_method - set HTTP request method
#include <nng/nng.h>
#include <nng/supplemental/http/http.h>

int nng_http_req_set_method(nng_http_req *req, const char *method);
void nng_http_req_set_method(nng_http_req *req, const char *method);
----

== DESCRIPTION
Expand All @@ -32,17 +32,10 @@ be upper case.

The default value method for newly allocated requests is "GET".

A local copy of the _method_ is made in the request _req_.

== RETURN VALUES

This function returns 0 on success, and non-zero otherwise.
If the method is longer than 32 bytes, it may be silently truncated.
(There are no methods defined that are this long.)

== ERRORS

[horizontal]
`NNG_ENOMEM`:: Insufficient memory to perform the operation.
`NNG_ENOTSUP`:: No support for HTTP in the library.
A local copy of the _method_ is made in the request _req_.

== SEE ALSO

Expand Down
11 changes: 1 addition & 10 deletions docs/man/nng_http_res_set_status.3http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ nng_http_res_set_status - set HTTP response status
#include <nng/nng.h>
#include <nng/supplemental/http/http.h>

int nng_http_res_set_status(nng_http_res *res, uint16_t status);
void nng_http_res_set_status(nng_http_res *res, uint16_t status);
----

== DESCRIPTION
Expand Down Expand Up @@ -106,15 +106,6 @@ TIP: It is a good idea to also set the reason message with
xref:nng_http_res_set_reason.3http.adoc[`nng_http_set_reason()`].
This will help any humans who may have to diagnose a failure.

== RETURN VALUES

This function returns 0 on success, and non-zero otherwise.

== ERRORS

[horizontal]
`NNG_ENOTSUP`:: No support for HTTP in the library.

== SEE ALSO

[.text-left]
Expand Down
26 changes: 26 additions & 0 deletions docs/ref/migrate/nng1.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,32 @@ accessors functions are provided:
- `u_host` is removed - use [`nng_url_hostname`] and [`nng_url_port`] to construct if needed
- `u_rawurl` is removed - a "cooked" URL can be obtained from the new [`nng_url_sprintf`] function.

## HTTP API

A few limits on string lengths of certain values are now applied, which allows us to preallocate values
and eliminate certain unreasonable error paths. If values longer than these are supplied in certain APIs
they may be silently truncated to the limit:

- Hostnames are limited per RFC 1035 to 253 characters (not including terminating "." or zero byte.)
- HTTP Method names are limited to 32 bytes (the longest IANA registered method is currently 18 bytes, used for WebDAV.)
- The fixed part of URI pathnames used with HTTP handlers is limited to 1024 bytes. (Longer URIs may be accepted
by using [`nng_http_handler_set_tree`] and matching a parent of the directory component.)

The following API calls have changed so that they are `void` returns, and cannot fail.
They may silently truncate data.

- [`nng_http_req_set_method`]
- [`nng_http_res_set_status`]
- [`nng_http_handler_collect_body`]
- [`nng_http_handler_set_data`]
- [`nng_http_handler_set_host`]
- [`nng_http_handler_set_method`]
- [`nng_http_handler_set_tree`]
- [`nng_http_handler_set_tree_exclusive`]

The HTTP handler objects may not be modified once in use. Previously this would fail with `NNG_EBUSY`.
These checks are removed now, but debug builds will assert if an application tries to do so.

## Security Descriptors (Windows Only)

The `NNG_OPT_IPC_SECURITY_DESCRIPTOR` option is removed, and replaced
Expand Down
Loading
Loading