Skip to content

Commit 9606142

Browse files
mrodgers-witekiocarlescufi
authored andcommitted
tests: http_server: add tests for dynamic response context
Add tests covering new method of providing a response for dynamic resources. Tests cover the application sending response codes and headers, overriding "default" headers, and sending various combinations of headers and body data. Each case is tested for HTTP1 & HTTP2, both POST and GET methods. Signed-off-by: Matt Rodgers <[email protected]>
1 parent 4cc905c commit 9606142

File tree

3 files changed

+533
-47
lines changed

3 files changed

+533
-47
lines changed

subsys/net/lib/http/http_server_http1.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ static int handle_http1_static_resource(
117117
"Transfer-Encoding: chunked\r\n"
118118

119119
static int http1_send_headers(struct http_client_ctx *client, enum http_status status,
120-
const struct http_header *headers, size_t header_count)
120+
const struct http_header *headers, size_t header_count,
121+
struct http_resource_detail_dynamic *dynamic_detail)
121122
{
122123
int ret;
123124
bool content_type_sent = false;
@@ -183,8 +184,8 @@ static int http1_send_headers(struct http_client_ctx *client, enum http_status s
183184
if (!content_type_sent) {
184185
const char *content_type = NULL;
185186

186-
if (client->current_detail != NULL) {
187-
content_type = client->current_detail->content_type;
187+
if (dynamic_detail != NULL) {
188+
content_type = dynamic_detail->common.content_type;
188189
}
189190

190191
snprintk(http_response, sizeof(http_response), "Content-Type: %s\r\n",
@@ -208,7 +209,8 @@ static int http1_send_headers(struct http_client_ctx *client, enum http_status s
208209
return ret;
209210
}
210211

211-
static int http1_dynamic_response(struct http_client_ctx *client, struct http_response_ctx *rsp)
212+
static int http1_dynamic_response(struct http_client_ctx *client, struct http_response_ctx *rsp,
213+
struct http_resource_detail_dynamic *dynamic_detail)
212214
{
213215
int ret;
214216
char tmp[TEMP_BUF_LEN];
@@ -224,7 +226,8 @@ static int http1_dynamic_response(struct http_client_ctx *client, struct http_re
224226
rsp->status = 200;
225227
}
226228

227-
ret = http1_send_headers(client, rsp->status, rsp->headers, rsp->header_count);
229+
ret = http1_send_headers(client, rsp->status, rsp->headers, rsp->header_count,
230+
dynamic_detail);
228231
if (ret < 0) {
229232
return ret;
230233
}
@@ -285,7 +288,7 @@ static int dynamic_get_req(struct http_resource_detail_dynamic *dynamic_detail,
285288
return ret;
286289
}
287290

288-
ret = http1_dynamic_response(client, &response_ctx);
291+
ret = http1_dynamic_response(client, &response_ctx, dynamic_detail);
289292
if (ret < 0) {
290293
return ret;
291294
}
@@ -324,7 +327,7 @@ static int dynamic_post_req(struct http_resource_detail_dynamic *dynamic_detail,
324327
}
325328

326329
copy_len = MIN(remaining, dynamic_detail->data_buffer_len);
327-
while (copy_len > 0) {
330+
while (1) {
328331
enum http_data_status status;
329332

330333
ptr = &start[offset];
@@ -347,14 +350,14 @@ static int dynamic_post_req(struct http_resource_detail_dynamic *dynamic_detail,
347350
}
348351

349352
if (http_response_is_provided(&response_ctx)) {
350-
ret = http1_dynamic_response(client, &response_ctx);
353+
ret = http1_dynamic_response(client, &response_ctx, dynamic_detail);
351354
if (ret < 0) {
352355
return ret;
353356
}
357+
}
354358

355-
if (http_response_is_final(&response_ctx, status)) {
356-
break;
357-
}
359+
if (http_response_is_final(&response_ctx, status)) {
360+
break;
358361
}
359362

360363
offset += copy_len;
@@ -366,7 +369,7 @@ static int dynamic_post_req(struct http_resource_detail_dynamic *dynamic_detail,
366369
if (!client->http1_headers_sent) {
367370
memset(&response_ctx, 0, sizeof(response_ctx));
368371
response_ctx.final_chunk = true;
369-
ret = http1_dynamic_response(client, &response_ctx);
372+
ret = http1_dynamic_response(client, &response_ctx, dynamic_detail);
370373
if (ret < 0) {
371374
return ret;
372375
}

subsys/net/lib/http/http_server_http2.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ static int send_headers_frame(struct http_client_ctx *client, enum http_status s
178178

179179
if (!content_encoding_sent && detail_common && detail_common->content_encoding != NULL) {
180180
ret = add_header_field(client, &buf, &buflen, "content-encoding",
181-
"gzip");
181+
detail_common->content_encoding);
182182
if (ret < 0) {
183183
return ret;
184184
}
@@ -488,7 +488,8 @@ static int handle_http2_static_fs_resource(struct http_resource_detail_static_fs
488488
}
489489

490490
static int http2_dynamic_response(struct http_client_ctx *client, struct http2_frame *frame,
491-
struct http_response_ctx *rsp, enum http_data_status data_status)
491+
struct http_response_ctx *rsp, enum http_data_status data_status,
492+
struct http_resource_detail_dynamic *dynamic_detail)
492493
{
493494
int ret;
494495
uint8_t flags = 0;
@@ -522,8 +523,8 @@ static int http2_dynamic_response(struct http_client_ctx *client, struct http2_f
522523
}
523524

524525
ret = send_headers_frame(client, rsp->status, frame->stream_identifier,
525-
client->current_detail, flags, rsp->headers,
526-
rsp->header_count);
526+
(struct http_resource_detail *)dynamic_detail, flags,
527+
rsp->headers, rsp->header_count);
527528
if (ret < 0) {
528529
return ret;
529530
}
@@ -588,7 +589,7 @@ static int dynamic_get_req_v2(struct http_resource_detail_dynamic *dynamic_detai
588589
return ret;
589590
}
590591

591-
ret = http2_dynamic_response(client, frame, &response_ctx, status);
592+
ret = http2_dynamic_response(client, frame, &response_ctx, status, dynamic_detail);
592593
if (ret < 0) {
593594
return ret;
594595
}
@@ -660,7 +661,8 @@ static int dynamic_post_req_v2(struct http_resource_detail_dynamic *dynamic_deta
660661
}
661662

662663
if (http_response_is_provided(&response_ctx)) {
663-
ret = http2_dynamic_response(client, frame, &response_ctx, status);
664+
ret = http2_dynamic_response(client, frame, &response_ctx, status,
665+
dynamic_detail);
664666
if (ret < 0) {
665667
return ret;
666668
}
@@ -683,7 +685,7 @@ static int dynamic_post_req_v2(struct http_resource_detail_dynamic *dynamic_deta
683685
memset(&response_ctx, 0, sizeof(response_ctx));
684686
response_ctx.final_chunk = true;
685687
ret = http2_dynamic_response(client, frame, &response_ctx,
686-
HTTP_SERVER_DATA_FINAL);
688+
HTTP_SERVER_DATA_FINAL, dynamic_detail);
687689
}
688690

689691
if (ret < 0) {

0 commit comments

Comments
 (0)