kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit eb0b8f21e37113736602db9bba210496b860e186
parent b77d727f729b4c2fd0b7cc1638cbed02c1d326c9
Author: Joris Vink <joris@coders.se>
Date:   Sun, 12 Sep 2021 14:13:24 +0200

Add http_response_close() to the C API.

This is the same as http_response() except it will automatically
close the connection after the response is sent.

This is a bit easier than setting CONN_CLOSE_EMPTY yourself manually.

Diffstat:
include/kore/http.h | 2++
src/http.c | 33++++++++++++++++++++++++++++-----
2 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/include/kore/http.h b/include/kore/http.h @@ -357,6 +357,8 @@ int http_body_digest(struct http_request *, char *, size_t); int http_redirect_add(struct kore_domain *, const char *, int, const char *); void http_response(struct http_request *, int, const void *, size_t); +void http_response_close(struct http_request *, int, + const void *, size_t); void http_response_fileref(struct http_request *, int, struct kore_fileref *); void http_serveable(struct http_request *, const void *, diff --git a/src/http.c b/src/http.c @@ -580,21 +580,44 @@ http_serveable(struct http_request *req, const void *data, size_t len, } void -http_response(struct http_request *req, int status, const void *d, size_t l) +http_response(struct http_request *req, int code, const void *d, size_t l) { if (req->owner == NULL) return; - kore_debug("http_response(%p, %d, %p, %zu)", req, status, d, l); + kore_debug("%s(%p, %d, %p, %zu)", __func__, req, code, d, l); + + req->status = code; + + switch (req->owner->proto) { + case CONN_PROTO_HTTP: + case CONN_PROTO_WEBSOCKET: + http_response_normal(req, req->owner, code, d, l); + break; + default: + fatal("%s: bad proto %d", __func__, req->owner->proto); + /* NOTREACHED. */ + } +} + +void +http_response_close(struct http_request *req, int code, const void *d, size_t l) +{ + if (req->owner == NULL) + return; + + kore_debug("%s(%p, %d, %p, %zu)", __func__, req, code, d, l); + + req->status = code; + req->owner->flags |= CONN_CLOSE_EMPTY; - req->status = status; switch (req->owner->proto) { case CONN_PROTO_HTTP: case CONN_PROTO_WEBSOCKET: - http_response_normal(req, req->owner, status, d, l); + http_response_normal(req, req->owner, code, d, l); break; default: - fatal("http_response() bad proto %d", req->owner->proto); + fatal("%s: bad proto %d", __func__, req->owner->proto); /* NOTREACHED. */ } }