When I receive the request GET /log.json I can't
immediately build the response data. This because data are stored
in another device and must be downloaded through a serial link.
In this scenario, I think I have to use LWIP_HTTPD_FS_ASYNC_READ
and define four custom functions: fs_read_async_custom(),
fs_canread_custom(), fs_wait_read_custom(). I tried in the
following way:
int fs_open_custom(struct fs_file *file, const char
*name) {
if (!strcmp(name, "/log.json")) {
file->data = ""> // So
hs->left = 0 in http_init_file()
file->len = 300; // First
assumption of data length (must be >0)
file->index = 0; // It is needed for
fs_bytes_left()
get_log_from_other_device();
return 1;
}
return 0;
}
u8_t fs_canread_custom(struct fs_file *file) {
return 1;
}
u8_t fs_wait_read_custom(struct fs_file *file, fs_wait_cb
callback_fn, void *callback_arg) {
return 0;
}
int fs_read_async_custom(struct fs_file *file, char *buffer,
int count, fs_wait_cb callback_fn, void *callback_arg) {
if (!log_complete()) return FS_READ_DELAYED;
memcpy(buffer, log_data, count);
file->index = file->len = count; // So we are at
the end of file
return count;
}
I have to make some additional tests, but it seems it works. I
have some doubts.
- What is the exact purpose of fs_canread_custom() and
fs_wait_read_custom()? In this configuration, they are called
from fs_is_file_ready() for every file, custom
and not, static and dynamic, asyncronous and not. For most of
the files, fs_canread_custom() should returns 1 and
fs_wait_read_custom() should return 0. When they should return a
different value?
- I don't know the content length in advance. So I have to start
with a length that is surely greater than the final length,
otherwise the browser stops receveing data before the end.
However what happens if Content Length header says 300 and only
100 bytes were received? Chrome seems ok, but other browsers? Is
there a standard behaviour? From RFC7230 it seems sending a
Content Length value greater than real message body length isn't
good:
5. If a valid Content-Length header field is present without
Transfer-Encoding, its decimal value defines the expected message
body length in octets. If the sender closes the connection or
the recipient times out before the indicated number of octets are
received, the recipient MUST consider the message to be
incomplete and close the connection.
Is it possible to avoid sending Content Length header for
log.json file? Consider I'm using dynamic headers.
- I couldn't understand the purpose of callback_fn (and its
callback_arg parameter). They are passed to
fs_wait_read_custom() and fs_read_async_custom(). When this
callback (that is http_continue function) should be called from
those functions?
|