@@ -37,6 +37,40 @@ static const char conflict_response[] = "HTTP/1.1 409 Conflict\r\n\r\n";
37
37
static const char final_chunk [] = "0\r\n\r\n" ;
38
38
static const char * crlf = & final_chunk [3 ];
39
39
40
+ static int send_http1_error_common (struct http_client_ctx * client ,
41
+ const char * response , size_t len )
42
+ {
43
+ int ret ;
44
+
45
+ ret = http_server_sendall (client , response , len );
46
+ if (ret < 0 ) {
47
+ LOG_DBG ("Cannot write to socket (%d)" , ret );
48
+ return ret ;
49
+ }
50
+
51
+ client -> http1_headers_sent = true;
52
+
53
+ return 0 ;
54
+ }
55
+
56
+ static int send_http1_404 (struct http_client_ctx * client )
57
+ {
58
+ return send_http1_error_common (client , not_found_response ,
59
+ sizeof (not_found_response ) - 1 );
60
+ }
61
+
62
+ static int send_http1_405 (struct http_client_ctx * client )
63
+ {
64
+ return send_http1_error_common (client , not_allowed_response ,
65
+ sizeof (not_allowed_response ) - 1 );
66
+ }
67
+
68
+ static int send_http1_409 (struct http_client_ctx * client )
69
+ {
70
+ return send_http1_error_common (client , conflict_response ,
71
+ sizeof (conflict_response ) - 1 );
72
+ }
73
+
40
74
static int handle_http1_static_resource (
41
75
struct http_resource_detail_static * static_detail ,
42
76
struct http_client_ctx * client )
@@ -408,12 +442,7 @@ int handle_http1_static_fs_resource(struct http_resource_detail_static_fs *stati
408
442
sizeof (CONTENT_ENCODING_GZIP )];
409
443
410
444
if (!(static_fs_detail -> common .bitmask_of_supported_http_methods & BIT (HTTP_GET ))) {
411
- ret = http_server_sendall (client , not_allowed_response ,
412
- sizeof (not_allowed_response ) - 1 );
413
- if (ret < 0 ) {
414
- LOG_DBG ("Cannot write to socket (%d)" , ret );
415
- }
416
- return ret ;
445
+ return send_http1_405 (client );
417
446
}
418
447
419
448
/* get filename and content-type from url */
@@ -432,12 +461,7 @@ int handle_http1_static_fs_resource(struct http_resource_detail_static_fs *stati
432
461
ret = http_server_find_file (fname , sizeof (fname ), & file_size , & gzipped );
433
462
if (ret < 0 ) {
434
463
LOG_ERR ("fs_stat %s: %d" , fname , ret );
435
- ret = http_server_sendall (client , not_found_response ,
436
- sizeof (not_found_response ) - 1 );
437
- if (ret < 0 ) {
438
- LOG_DBG ("Cannot write to socket (%d)" , ret );
439
- }
440
- return ret ;
464
+ return send_http1_404 (client );
441
465
}
442
466
fs_file_t_init (& file );
443
467
ret = fs_open (& file , fname , FS_O_READ );
@@ -501,10 +525,8 @@ static int handle_http1_dynamic_resource(
501
525
}
502
526
503
527
if (dynamic_detail -> holder != NULL && dynamic_detail -> holder != client ) {
504
- ret = http_server_sendall (client , conflict_response ,
505
- sizeof (conflict_response ) - 1 );
528
+ ret = send_http1_409 (client );
506
529
if (ret < 0 ) {
507
- LOG_DBG ("Cannot write to socket (%d)" , ret );
508
530
return ret ;
509
531
}
510
532
@@ -931,10 +953,8 @@ int handle_http1_request(struct http_client_ctx *client)
931
953
}
932
954
} else {
933
955
not_found : ; /* Add extra semicolon to make clang to compile when using label */
934
- ret = http_server_sendall (client , not_found_response ,
935
- sizeof (not_found_response ) - 1 );
956
+ ret = send_http1_404 (client );
936
957
if (ret < 0 ) {
937
- LOG_DBG ("Cannot write to socket (%d)" , ret );
938
958
return ret ;
939
959
}
940
960
}
0 commit comments