Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http Content Range #83528

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 10 additions & 0 deletions 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 @@ -133,6 +134,8 @@ enum http_errno {
HPE_INVALID_CONTENT_LENGTH,
HPE_UNEXPECTED_CONTENT_LENGTH,
HPE_INVALID_CHUNK_SIZE,
HPE_INVALID_CONTENT_RANGE,
HPE_UNEXPECTED_CONTENT_RANGE,
HPE_INVALID_CONSTANT,
HPE_INVALID_INTERNAL_STATE,
HPE_STRICT,
Expand All @@ -143,6 +146,11 @@ enum http_errno {
/* Get an http_errno value from an http_parser */
#define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)

struct http_content_range {
uint64_t start;
uint64_t end;
uint64_t total;
};

struct http_parser {
/** PRIVATE **/
Expand All @@ -160,6 +168,8 @@ struct http_parser {
uint64_t content_length; /* # bytes in body (0 if no Content-Length
* header)
*/
bool content_range_present;
struct http_content_range content_range;
/** READ-ONLY **/
unsigned short http_major;
unsigned short http_minor;
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
178 changes: 176 additions & 2 deletions subsys/net/lib/http/http_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ int count_header_size(struct http_parser *parser, int bytes)

#define PROXY_CONNECTION "proxy-connection"
#define CONNECTION "connection"
#define CONTENT "content-"
#define CONTENT_LENGTH "content-length"
#define CONTENT_RANGE "content-range"
#define TRANSFER_ENCODING "transfer-encoding"
#define UPGRADE "upgrade"
#define CHUNKED "chunked"
Expand Down Expand Up @@ -183,11 +185,17 @@ enum header_states {
h_CON,
h_matching_connection,
h_matching_proxy_connection,
h_matching_content,
h_CONTENT_,
h_matching_content_length,
h_matching_content_range,
h_matching_transfer_encoding,
h_matching_upgrade,
h_connection,
h_content_length,
h_content_range_start,
h_content_range_end,
h_content_range_total,
h_transfer_encoding,
h_upgrade,
h_matching_transfer_encoding_chunked,
Expand Down Expand Up @@ -356,6 +364,8 @@ static struct {
{"HPE_INVALID_CONTENT_LENGTH", "invalid character in content-length "
"header"},
{"HPE_UNEXPECTED_CONTENT_LENGTH", "unexpected content-length header"},
{"HPE_INVALID_CONTENT_RANGE", "invalid character in content-range header"},
{"HPE_UNEXPECTED_CONTENT_RANGE", "unexpected content-range header"},
{"HPE_INVALID_CHUNK_SIZE", "invalid character in chunk size header"},
{"HPE_INVALID_CONSTANT", "invalid constant string"},
{"HPE_INVALID_INTERNAL_STATE", "encountered unexpected internal state"},
Expand Down Expand Up @@ -392,7 +402,7 @@ int parser_header_state(struct http_parser *parser, char ch, char c)
parser->header_state = h_matching_connection;
break;
case 't':
parser->header_state = h_matching_content_length;
parser->header_state = h_matching_content;
break;
default:
parser->header_state = h_general;
Expand Down Expand Up @@ -424,7 +434,29 @@ int parser_header_state(struct http_parser *parser, char ch, char c)
}
break;

/* content-length */
/*content-length or content-range*/

case h_matching_content:
parser->index++;
cond1 = parser->index > sizeof(CONTENT) - 1;
if (cond1 || c != CONTENT[parser->index]) {
parser->header_state = h_general;
} else if (parser->index == sizeof(CONTENT) - 2) {
parser->header_state = h_CONTENT_;
}
break;

case h_CONTENT_:
parser->index++;
if (c == 'l') {
parser->header_state = h_matching_content_length;
} else if (c == 'r') {
parser->header_state = h_matching_content_range;
} else {
parser->header_state = h_general;
}
break;


case h_matching_content_length:
parser->index++;
Expand All @@ -436,6 +468,16 @@ int parser_header_state(struct http_parser *parser, char ch, char c)
}
break;

case h_matching_content_range:
parser->index++;
cond1 = parser->index > sizeof(CONTENT_RANGE) - 1;
if (cond1 || c != CONTENT_RANGE[parser->index]) {
parser->header_state = h_general;
} else if (parser->index == sizeof(CONTENT_RANGE) - 2) {
parser->header_state = h_content_range_start;
}
break;

/* transfer-encoding */

case h_matching_transfer_encoding:
Expand All @@ -462,6 +504,9 @@ int parser_header_state(struct http_parser *parser, char ch, char c)

case h_connection:
case h_content_length:
case h_content_range_start:
case h_content_range_end:
case h_content_range_total:
case h_transfer_encoding:
case h_upgrade:
if (ch != ' ') {
Expand Down Expand Up @@ -546,6 +591,117 @@ int header_states(struct http_parser *parser, const char *data, size_t len,
break;
}

case h_content_range_start: {
uint64_t t;
uint64_t value;

if (IS_ALPHA(ch)) {
break;
}

if (ch == ' ') {
break;
}

if (ch == '-' || ch == '*') {
h_state = h_content_range_end;
break;
}

if (UNLIKELY(!IS_NUM(ch))) {
SET_ERRNO(HPE_INVALID_CONTENT_RANGE);
parser->header_state = h_state;
return -1;
}

t = parser->content_range.start;
t *= 10U;
t += ch - '0';

/* Overflow? Test against a conservative limit for simplicity */
value = (ULLONG_MAX - 10) / 10;

if (UNLIKELY(value < parser->content_range.start)) {
SET_ERRNO(HPE_INVALID_CONTENT_RANGE);
parser->header_state = h_state;
return -1;
}

parser->content_range.start = t;
break;
}

case h_content_range_end: {
uint64_t t;
uint64_t value;

if (ch == ' ') {
break;
}

if (ch == '/') {
h_state = h_content_range_total;
break;
}

if (UNLIKELY(!IS_NUM(ch))) {
SET_ERRNO(HPE_INVALID_CONTENT_RANGE);
parser->header_state = h_state;
return -1;
}

t = parser->content_range.end;
t *= 10U;
t += ch - '0';

/* Overflow? Test against a conservative limit for simplicity */
value = (ULLONG_MAX - 10) / 10;

if (UNLIKELY(value < parser->content_range.end)) {
SET_ERRNO(HPE_INVALID_CONTENT_RANGE);
parser->header_state = h_state;
return -1;
}

parser->content_range.end = t;
break;
}

case h_content_range_total: {
uint64_t t;
uint64_t value;

if (ch == ' ') {
break;
}

if (ch == '*') {
break;
}

if (UNLIKELY(!IS_NUM(ch))) {
SET_ERRNO(HPE_INVALID_CONTENT_RANGE);
parser->header_state = h_state;
return -1;
}

t = parser->content_range.total;
t *= 10U;
t += ch - '0';

/* Overflow? Test against a conservative limit for simplicity */
value = (ULLONG_MAX - 10) / 10;

if (UNLIKELY(value < parser->content_range.total)) {
SET_ERRNO(HPE_INVALID_CONTENT_RANGE);
parser->header_state = h_state;
return -1;
}

parser->content_range.total = t;
break;
}

/* Transfer-Encoding: chunked */
case h_matching_transfer_encoding_chunked:
parser->index++;
Expand Down Expand Up @@ -811,6 +967,10 @@ int parser_execute(struct http_parser *parser,
}
parser->flags = 0U;
parser->content_length = ULLONG_MAX;
parser->content_range.start = 0U;
parser->content_range.end = 0U;
parser->content_range.total = 0U;
parser->content_range_present = false;

if (ch == 'H') {
UPDATE_STATE(s_res_or_resp_H);
Expand Down Expand Up @@ -1104,6 +1264,10 @@ int parser_execute(struct http_parser *parser,
}
parser->flags = 0U;
parser->content_length = ULLONG_MAX;
parser->content_range.start = 0U;
parser->content_range.end = 0U;
parser->content_range.total = 0U;
parser->content_range_present = false;

if (UNLIKELY(!IS_ALPHA(ch))) {
SET_ERRNO(HPE_INVALID_METHOD);
Expand Down Expand Up @@ -1639,6 +1803,16 @@ int parser_execute(struct http_parser *parser,
parser->content_length = ch - '0';
break;

case h_content_range_start:
if (parser->content_range_present) {
SET_ERRNO(HPE_UNEXPECTED_CONTENT_RANGE);
goto error;
}

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

case h_connection:
/* looking for 'Connection: keep-alive' */
if (c == 'k') {
Expand Down
Loading
Loading