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.
This commit is contained in:
Joris Vink 2021-09-12 14:13:24 +02:00
parent b77d727f72
commit eb0b8f21e3
2 changed files with 30 additions and 5 deletions

View File

@ -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 *,

View File

@ -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;
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. */
}
}
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;
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. */
}
}