Skip to content

Commit 9335644

Browse files
mrodgers-witekiocarlescufi
authored andcommitted
doc: http_server: Document response_ctx used for dynamic resources
Update documentation to describe the method of returning response data to the HTTP server using the response_ctx callback parameter. Signed-off-by: Matt Rodgers <[email protected]>
1 parent bc9eee4 commit 9335644

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

doc/connectivity/networking/api/http_server.rst

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -223,18 +223,16 @@ Dynamic resources
223223
=================
224224

225225
For dynamic resource, a resource callback is registered to exchange data between
226-
the server and the application. The application defines a resource buffer used
227-
to pass the request payload data from the server, and to provide response payload
228-
to the server. The following example code shows how to register a dynamic resource
229-
with a simple resource handler, which echoes received data back to the client:
226+
the server and the application.
230227

231-
.. code-block:: c
228+
The following example code shows how to register a dynamic resource with a simple
229+
resource handler, which echoes received data back to the client:
232230

233-
static uint8_t recv_buffer[1024];
231+
.. code-block:: c
234232
235-
static int dyn_handler(struct http_client_ctx *client,
236-
enum http_data_status status, uint8_t *buffer,
237-
size_t len, void *user_data)
233+
static int dyn_handler(struct http_client_ctx *client, enum http_data_status status,
234+
uint8_t *buffer, size_t len, struct http_response_ctx *response_ctx,
235+
void *user_data)
238236
{
239237
#define MAX_TEMP_PRINT_LEN 32
240238
static char print_str[MAX_TEMP_PRINT_LEN];
@@ -260,10 +258,12 @@ with a simple resource handler, which echoes received data back to the client:
260258
processed = 0;
261259
}
262260
263-
/* This will echo data back to client as the buffer and recv_buffer
264-
* point to same area.
265-
*/
266-
return len;
261+
/* Echo data back to client */
262+
response_ctx->body = buffer;
263+
response_ctx->body_len = len;
264+
response_ctx->final_chunk = (status == HTTP_SERVER_DATA_FINAL);
265+
266+
return 0;
267267
}
268268
269269
struct http_resource_detail_dynamic dyn_resource_detail = {
@@ -273,8 +273,6 @@ with a simple resource handler, which echoes received data back to the client:
273273
BIT(HTTP_GET) | BIT(HTTP_POST),
274274
},
275275
.cb = dyn_handler,
276-
.data_buffer = recv_buffer,
277-
.data_buffer_len = sizeof(recv_buffer),
278276
.user_data = NULL,
279277
};
280278
@@ -298,9 +296,25 @@ the application shall reset any progress recorded for the resource, and await
298296
a new request to come. The server guarantees that the resource can only be
299297
accessed by single client at a time.
300298

301-
The resource callback returns the number of bytes to be replied in the response
302-
payload to the server (provided in the resource data buffer). In case there is
303-
no more data to be included in the response, the callback should return 0.
299+
The ``response_ctx`` field is used by the application to pass response data to
300+
the HTTP server:
301+
302+
* The ``status`` field allows the application to send an HTTP response code. If
303+
not populated, the response code will be 200 by default.
304+
305+
* The ``headers`` and ``header_count`` fields can be used for the application to
306+
send any arbitrary HTTP headers. If not populated, only Transfer-Encoding and
307+
Content-Type are sent by default. The callback may override the Content-Type
308+
if desired.
309+
310+
* The ``body`` and ``body_len`` fields are used to send body data.
311+
312+
* The ``final_chunk`` field is used to indicate that the application has no more
313+
response data to send.
314+
315+
Headers and/or response codes may only be sent in the first populated
316+
``response_ctx``, after which only further body data is allowed in subsequent
317+
callbacks.
304318

305319
The server will call the resource callback until it provided all request data
306320
to the application, and the application reports there is no more data to include

0 commit comments

Comments
 (0)