Skip to content

Commit

Permalink
fixup! Update sys/net/application_layer/gcoap/fileserver.c
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed May 16, 2022
1 parent 7532471 commit 5d687ec
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions sys/net/application_layer/gcoap/fileserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,10 @@ static size_t gcoap_fileserver_errno_handler(coap_pkt_t *pdu, uint8_t *buf, size
return gcoap_response(pdu, buf, len, code);
}

static void _calc_szx2(coap_pkt_t *pdu, uint8_t *buf, size_t len, struct requestdata *request)
static void _calc_szx2(coap_pkt_t *pdu, size_t reserve, struct requestdata *request)
{
/* To best see how this works set CONFIG_GCAOP_PDU_BUF_SIZE to 532 or 533.
* If we did a sharper estimation (factoring in the block2 size option with
* the current blockum), we'd even pack 512 bytes into 530 until block
* numbers get large enough to eat another byte, which is when the block
* size would decrease in-flight. */
size_t remaining_length = len - (pdu->payload - buf);
remaining_length -= 5; /* maximum block2 option usable in nanocoap */
remaining_length -= 1; /* payload marker */
assert(pdu->payload_len > reserve);
size_t remaining_length = pdu->payload_len - reserve;
/* > 0: To not wrap around; if that still won't fit that's later caught in
* an assertion */
while ((coap_szx2size(request->szx2) > remaining_length) && (request->szx2 > 0)) {
Expand Down Expand Up @@ -162,7 +156,9 @@ static ssize_t gcoap_fileserver_file_handler(coap_pkt_t *pdu, uint8_t *buf, size
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
coap_opt_add_opaque(pdu, COAP_OPT_ETAG, &etag, sizeof(etag));
coap_block_slicer_t slicer;
_calc_szx2(pdu, buf, len, request);
_calc_szx2(pdu,
5 + 1 + 1 /* reserve BLOCK2 size + payload marker + more */,
request);
coap_block_slicer_init(&slicer, request->blocknum2, coap_szx2size(request->szx2));
coap_opt_add_block2(pdu, &slicer, true);
size_t resp_len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);
Expand All @@ -179,16 +175,13 @@ static ssize_t gcoap_fileserver_file_handler(coap_pkt_t *pdu, uint8_t *buf, size
* space by CONFIG_GCOAP_RESP_OPTIONS_BUF
* */
assert(pdu->payload + slicer.end - slicer.start <= buf + len);
int read = vfs_read(fd, pdu->payload, slicer.end - slicer.start);
bool more = 1;
int read = vfs_read(fd, pdu->payload, slicer.end - slicer.start + more);
if (read < 0) {
goto late_err;
}

uint8_t morebuf;
int more = vfs_read(fd, &morebuf, 1);
if (more < 0) {
goto late_err;
}
more = (unsigned)read > slicer.end - slicer.start;
read -= more;

vfs_close(fd);

Expand Down Expand Up @@ -221,11 +214,11 @@ static ssize_t gcoap_fileserver_directory_handler(coap_pkt_t *pdu, uint8_t *buf,
}
DEBUG("gcoap_fileserver: Serving directory listing\n");

coap_block2_init(pdu, &slicer);

gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
coap_opt_add_format(pdu, COAP_FORMAT_LINK);
_calc_szx2(pdu, buf, len, request);
_calc_szx2(pdu,
5 + 1 /* reserve BLOCK2 size + payload marker */,
request);
coap_block_slicer_init(&slicer, request->blocknum2, coap_szx2size(request->szx2));
coap_opt_add_block2(pdu, &slicer, true);
buf += coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);
Expand Down

0 comments on commit 5d687ec

Please sign in to comment.