@@ -223,18 +223,16 @@ Dynamic resources
223
223
=================
224
224
225
225
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.
230
227
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:
232
230
233
- static uint8_t recv_buffer[1024];
231
+ .. code-block :: c
234
232
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)
238
236
{
239
237
#define MAX_TEMP_PRINT_LEN 32
240
238
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:
260
258
processed = 0;
261
259
}
262
260
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;
267
267
}
268
268
269
269
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:
273
273
BIT(HTTP_GET) | BIT(HTTP_POST),
274
274
},
275
275
.cb = dyn_handler,
276
- .data_buffer = recv_buffer,
277
- .data_buffer_len = sizeof(recv_buffer),
278
276
.user_data = NULL,
279
277
};
280
278
@@ -298,9 +296,25 @@ the application shall reset any progress recorded for the resource, and await
298
296
a new request to come. The server guarantees that the resource can only be
299
297
accessed by single client at a time.
300
298
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.
304
318
305
319
The server will call the resource callback until it provided all request data
306
320
to the application, and the application reports there is no more data to include
0 commit comments