Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions include/net/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,24 @@ int http_response_403(struct http_server_ctx *ctx, const char *html_payload);
*/
int http_response_404(struct http_server_ctx *ctx, const char *html_payload);

/**
* @brief Send some data to the client.
*
* @detail Send a piece of data to the client. If html_payload is NULL, then
* we close the connection.
*
* @param ctx HTTP context.
* @param http_header HTTP header to be sent. Can be NULL.
* @param html_payload HTML payload to send.
* @param timeout Timeout to wait until the connection is teared down.
*
* @return 0 if ok, <0 if error.
*/
int http_response_send_data(struct http_server_ctx *ctx,
const char *http_header,
const char *html_payload,
s32_t timeout);

#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
/**
* @brief Configure the net_pkt pool for this context.
Expand Down
42 changes: 42 additions & 0 deletions subsys/net/lib/http/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,48 @@ int http_response_404(struct http_server_ctx *ctx, const char *html_payload)
return http_response(ctx, HTTP_STATUS_404_NF, html_payload);
}

int http_response_send_data(struct http_server_ctx *ctx,
const char *http_header,
const char *html_payload,
s32_t timeout)
{
struct net_pkt *pkt;
int ret = -EINVAL;

pkt = net_pkt_get_tx(ctx->req.net_ctx, ctx->timeout);
if (!pkt) {
return ret;
}

if (http_header) {
ret = http_add_header(pkt, ctx->timeout, http_header);
if (ret != 0) {
goto exit_routine;
}
}

ret = http_add_chunk(pkt, ctx->timeout, html_payload);
if (ret != 0) {
goto exit_routine;
}

net_pkt_set_appdatalen(pkt, net_buf_frags_len(pkt->frags));

ret = ctx->send_data(pkt, pkt_sent, 0, INT_TO_POINTER(timeout), ctx);
if (ret != 0) {
goto exit_routine;
}

pkt = NULL;

exit_routine:
if (pkt) {
net_pkt_unref(pkt);
}

return ret;
}

int http_server_set_local_addr(struct sockaddr *addr, const char *myaddr,
u16_t port)
{
Expand Down