Skip to content

Commit

Permalink
net: lib: http: Added Content-Range to http client.
Browse files Browse the repository at this point in the history
Content-Range functionality added in recent commits has been propagated
to http_client module. If "Content-Range" string is detected on header
field, Content-Range are returned via http_response structure.

Signed-off-by: Piotr Radecki <[email protected]>
  • Loading branch information
td-pradecki committed Jan 16, 2025
1 parent 0ee7520 commit cc547c0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
7 changes: 7 additions & 0 deletions include/zephyr/net/http/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,16 @@ struct http_response {
*/
uint16_t http_status_code;

/**
* HTTP Content-Range response field value. Consist of range_start,
* range_end and total_size. Total is set to 0 if not supplied.
*/
struct http_content_range content_range;

uint8_t cl_present : 1; /**< Is Content-Length field present */
uint8_t body_found : 1; /**< Is message body found */
uint8_t message_complete : 1; /**< Is HTTP message parsing complete */
uint8_t cr_present : 1; /**< Is Content-Range field present */
};

/** HTTP client internal data that the application should not touch
Expand Down
3 changes: 2 additions & 1 deletion include/zephyr/net/http/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ typedef unsigned __int64 uint64_t;
#include <zephyr/net/http/method.h>
#include <zephyr/net/http/parser_state.h>
#include <zephyr/net/http/parser_url.h>
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -167,7 +168,7 @@ struct http_parser {
uint64_t content_length; /* # bytes in body (0 if no Content-Length
* header)
*/
uint8_t content_range_present;
bool content_range_present;
struct http_content_range content_range;
/** READ-ONLY **/
unsigned short http_major;
Expand Down
20 changes: 16 additions & 4 deletions subsys/net/lib/http/http_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,17 @@ static int on_header_field(struct http_parser *parser, const char *at,
struct http_request *req = CONTAINER_OF(parser,
struct http_request,
internal.parser);
const char *content_len = "Content-Length";
uint16_t len;
static const char content_len[] = "Content-Length";
static const char content_range[] = "Content-Range";

uint16_t content_len_len = sizeof(content_len) - 1;
uint16_t content_range_len = sizeof(content_range) - 1;

len = strlen(content_len);
if (length >= len && strncasecmp(at, content_len, len) == 0) {
if (length >= content_len_len && strncasecmp(at, content_len, content_len_len) == 0) {
req->internal.response.cl_present = true;
} else if (length >= content_range_len &&
strncasecmp(at, content_range, content_range_len) == 0) {
req->internal.response.cr_present = true;
}

print_header_field(length, at);
Expand Down Expand Up @@ -264,6 +269,13 @@ static int on_header_value(struct http_parser *parser, const char *at,
req->internal.response.cl_present = false;
}

if (req->internal.response.cr_present) {
req->internal.response.content_range.start = parser->content_range.start;
req->internal.response.content_range.end = parser->content_range.end;
req->internal.response.content_range.total = parser->content_range.total;
req->internal.response.cr_present = false;
}

if (req->internal.response.http_cb &&
req->internal.response.http_cb->on_header_value) {
req->internal.response.http_cb->on_header_value(parser, at,
Expand Down
6 changes: 3 additions & 3 deletions subsys/net/lib/http/http_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ int parser_execute(struct http_parser *parser,
parser->content_range.start = 0U;
parser->content_range.end = 0U;
parser->content_range.total = 0U;
parser->content_range_present = 0;
parser->content_range_present = false;

if (ch == 'H') {
UPDATE_STATE(s_res_or_resp_H);
Expand Down Expand Up @@ -1267,7 +1267,7 @@ int parser_execute(struct http_parser *parser,
parser->content_range.start = 0U;
parser->content_range.end = 0U;
parser->content_range.total = 0U;
parser->content_range_present = 0;
parser->content_range_present = false;

if (UNLIKELY(!IS_ALPHA(ch))) {
SET_ERRNO(HPE_INVALID_METHOD);
Expand Down Expand Up @@ -1809,7 +1809,7 @@ int parser_execute(struct http_parser *parser,
goto error;
}

parser->content_range_present = 1;
parser->content_range_present = true;
parser->content_range.start = 0;
break;

Expand Down

0 comments on commit cc547c0

Please sign in to comment.