From 3d63689b91bb433894bc7d3becd5e7de20203806 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Tue, 1 Aug 2017 15:19:56 +0300 Subject: [PATCH] http: server: Add function to send a chunk of data The http_response_send_data() can be used to send a chunk of data to the peer. Signed-off-by: Jukka Rissanen --- include/net/http.h | 18 +++++++++++++ subsys/net/lib/http/http_server.c | 42 +++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/include/net/http.h b/include/net/http.h index 4c3945ecdf5bc..2ede662a8b1ac 100644 --- a/include/net/http.h +++ b/include/net/http.h @@ -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. diff --git a/subsys/net/lib/http/http_server.c b/subsys/net/lib/http/http_server.c index adc4f00fdb887..cc9065de3ce91 100644 --- a/subsys/net/lib/http/http_server.c +++ b/subsys/net/lib/http/http_server.c @@ -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) {