mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-18 11:03:50 +01:00
gcoap_fileserver: make use of coap_request_ctx_t
This commit is contained in:
parent
2daad4f5ff
commit
e8829d5591
@ -25,14 +25,9 @@
|
|||||||
#define MAIN_QUEUE_SIZE (4)
|
#define MAIN_QUEUE_SIZE (4)
|
||||||
static msg_t _main_msg_queue[MAIN_QUEUE_SIZE];
|
static msg_t _main_msg_queue[MAIN_QUEUE_SIZE];
|
||||||
|
|
||||||
static const gcoap_fileserver_entry_t _vfs_entry = {
|
|
||||||
.root = VFS_DEFAULT_DATA,
|
|
||||||
.resource = "/vfs",
|
|
||||||
};
|
|
||||||
|
|
||||||
/* CoAP resources. Must be sorted by path (ASCII order). */
|
/* CoAP resources. Must be sorted by path (ASCII order). */
|
||||||
static const coap_resource_t _resources[] = {
|
static const coap_resource_t _resources[] = {
|
||||||
{ "/vfs", COAP_GET | COAP_MATCH_SUBTREE, gcoap_fileserver_handler, (void *)&_vfs_entry },
|
{ "/vfs", COAP_GET | COAP_MATCH_SUBTREE, gcoap_fileserver_handler, VFS_DEFAULT_DATA },
|
||||||
};
|
};
|
||||||
|
|
||||||
static gcoap_listener_t _listener = {
|
static gcoap_listener_t _listener = {
|
||||||
|
|||||||
@ -36,27 +36,25 @@
|
|||||||
*
|
*
|
||||||
* * ``USEMODULE += gcoap_fileserver``
|
* * ``USEMODULE += gcoap_fileserver``
|
||||||
*
|
*
|
||||||
* * Have a @ref gcoap_fileserver_entry_t populated with the path you want to serve,
|
|
||||||
* and the number of path components to strip from incoming requests:
|
|
||||||
*
|
|
||||||
* ```
|
|
||||||
* static const gcoap_fileserver_entry_t files_sd = {
|
|
||||||
* .root = "/sd0",
|
|
||||||
* .resource = "/files/sd"
|
|
||||||
* };
|
|
||||||
* ```
|
|
||||||
*
|
|
||||||
* * Enter a @ref gcoap_fileserver_handler handler into your CoAP server's
|
* * Enter a @ref gcoap_fileserver_handler handler into your CoAP server's
|
||||||
* resource list like this:
|
* resource list like this:
|
||||||
*
|
*
|
||||||
* ```
|
* ```
|
||||||
* static const coap_resource_t _resources[] = {
|
* static const coap_resource_t _resources[] = {
|
||||||
* ...
|
* ...
|
||||||
* { "/files/sd", COAP_GET | COAP_MATCH_SUBTREE, gcoap_fileserver_handler, (void*)&files_sd },
|
* {
|
||||||
|
* .path = "/files/sd",
|
||||||
|
* .methods = COAP_GET | COAP_MATCH_SUBTREE,
|
||||||
|
* .handler = gcoap_fileserver_handler,
|
||||||
|
* .context = "/sd0"
|
||||||
|
* },
|
||||||
* ...
|
* ...
|
||||||
* }
|
* }
|
||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
|
* The path argument specifies under which path the folder is served via CoAP while
|
||||||
|
* the context argument contains the path on the local filesystem that will be served.
|
||||||
|
*
|
||||||
* The allowed methods dictate whether it's read-only (``COAP_GET``) or (in the
|
* The allowed methods dictate whether it's read-only (``COAP_GET``) or (in the
|
||||||
* future<!-- WRITESUPPORT -->) read-write (``COAP_GET | COAP_PUT | COAP_DELETE``).
|
* future<!-- WRITESUPPORT -->) read-write (``COAP_GET | COAP_PUT | COAP_DELETE``).
|
||||||
*
|
*
|
||||||
@ -77,26 +75,6 @@ extern "C" {
|
|||||||
|
|
||||||
#include "net/nanocoap.h"
|
#include "net/nanocoap.h"
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief File server starting point
|
|
||||||
*
|
|
||||||
* This struct needs to be present at the ctx of a gcoap_fileserver_handler entry
|
|
||||||
* in a resource list.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
typedef struct {
|
|
||||||
/**
|
|
||||||
* @brief Path in the VFS that should be served.
|
|
||||||
*
|
|
||||||
* This does not need a trailing slash.
|
|
||||||
*/
|
|
||||||
const char *root;
|
|
||||||
/**
|
|
||||||
* @brief The associated CoAP resource path
|
|
||||||
*/
|
|
||||||
const char *resource;
|
|
||||||
} gcoap_fileserver_entry_t;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief File server handler
|
* @brief File server handler
|
||||||
*
|
*
|
||||||
@ -106,12 +84,12 @@ typedef struct {
|
|||||||
* @param[in] pdu CoAP request package
|
* @param[in] pdu CoAP request package
|
||||||
* @param[out] buf Buffer for the response
|
* @param[out] buf Buffer for the response
|
||||||
* @param[in] len Response buffer length
|
* @param[in] len Response buffer length
|
||||||
* @param[in] ctx pointer to a @ref gcoap_fileserver_entry_t
|
* @param[in] ctx pointer to a @ref coap_request_ctx_t
|
||||||
*
|
*
|
||||||
* @return size of the response on success
|
* @return size of the response on success
|
||||||
* negative error
|
* negative error
|
||||||
*/
|
*/
|
||||||
ssize_t gcoap_fileserver_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, void *ctx);
|
ssize_t gcoap_fileserver_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, coap_request_ctx_t *ctx);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -207,7 +207,7 @@ late_err:
|
|||||||
|
|
||||||
static ssize_t gcoap_fileserver_directory_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len,
|
static ssize_t gcoap_fileserver_directory_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len,
|
||||||
struct requestdata *request,
|
struct requestdata *request,
|
||||||
gcoap_fileserver_entry_t *resource)
|
const char *root, const char* resource_dir)
|
||||||
{
|
{
|
||||||
vfs_DIR dir;
|
vfs_DIR dir;
|
||||||
coap_block_slicer_t slicer;
|
coap_block_slicer_t slicer;
|
||||||
@ -227,9 +227,8 @@ static ssize_t gcoap_fileserver_directory_handler(coap_pkt_t *pdu, uint8_t *buf,
|
|||||||
coap_opt_add_block2(pdu, &slicer, true);
|
coap_opt_add_block2(pdu, &slicer, true);
|
||||||
buf += coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);
|
buf += coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);
|
||||||
|
|
||||||
size_t root_len = resource->root ? strlen(resource->root) : 0;
|
size_t root_len = root ? strlen(root) : 0;
|
||||||
const char *root_dir = &request->namebuf[root_len];
|
const char *root_dir = &request->namebuf[root_len];
|
||||||
const char *resource_dir = resource->resource;
|
|
||||||
size_t root_dir_len = strlen(root_dir);
|
size_t root_dir_len = strlen(root_dir);
|
||||||
size_t resource_dir_len = strlen(resource_dir);
|
size_t resource_dir_len = strlen(resource_dir);
|
||||||
|
|
||||||
@ -264,8 +263,10 @@ static ssize_t gcoap_fileserver_directory_handler(coap_pkt_t *pdu, uint8_t *buf,
|
|||||||
return (uintptr_t)buf - (uintptr_t)pdu->hdr;
|
return (uintptr_t)buf - (uintptr_t)pdu->hdr;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t gcoap_fileserver_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, void *ctx) {
|
ssize_t gcoap_fileserver_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len,
|
||||||
gcoap_fileserver_entry_t *entry = ctx;
|
coap_request_ctx_t *ctx) {
|
||||||
|
const char *root = ctx->context;
|
||||||
|
const char *resource = ctx->resource->path;
|
||||||
struct requestdata request = {
|
struct requestdata request = {
|
||||||
.etag_sent = false,
|
.etag_sent = false,
|
||||||
.blocknum2 = 0,
|
.blocknum2 = 0,
|
||||||
@ -276,12 +277,12 @@ ssize_t gcoap_fileserver_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, void
|
|||||||
* zeroed to get a 0-terminated string. */
|
* zeroed to get a 0-terminated string. */
|
||||||
size_t namelength = 0;
|
size_t namelength = 0;
|
||||||
uint8_t errorcode = COAP_CODE_INTERNAL_SERVER_ERROR;
|
uint8_t errorcode = COAP_CODE_INTERNAL_SERVER_ERROR;
|
||||||
uint8_t strip_remaining = _count_char(entry->resource, '/');
|
uint8_t strip_remaining = _count_char(resource, '/');
|
||||||
|
|
||||||
/* If a root directory for the server was specified, use that */
|
/* If a root directory for the server was specified, use that */
|
||||||
if (entry->root && strlen(entry->root) > 1) {
|
if (root && strlen(root) > 1) {
|
||||||
strncpy(request.namebuf, entry->root, sizeof(request.namebuf));
|
strncpy(request.namebuf, root, sizeof(request.namebuf));
|
||||||
namelength = strlen(entry->root);
|
namelength = strlen(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_directory = true; /* either no path component at all or trailing '/' */
|
bool is_directory = true; /* either no path component at all or trailing '/' */
|
||||||
@ -360,7 +361,7 @@ ssize_t gcoap_fileserver_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, void
|
|||||||
* pass a struct pointer later. So far, those could even be hooked into the
|
* pass a struct pointer later. So far, those could even be hooked into the
|
||||||
* resource list, but that'll go away once we parse more options */
|
* resource list, but that'll go away once we parse more options */
|
||||||
if (is_directory) {
|
if (is_directory) {
|
||||||
return gcoap_fileserver_directory_handler(pdu, buf, len, &request, entry);
|
return gcoap_fileserver_directory_handler(pdu, buf, len, &request, root, resource);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return gcoap_fileserver_file_handler(pdu, buf, len, &request);
|
return gcoap_fileserver_file_handler(pdu, buf, len, &request);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user