minor - code readibility HTTP CODES as defines (#6539)
Timo committed
Jul 25, 2019 at 20:07 UTC
ca5c1836cec65904362289572708e2f3d871e1dc
2 files changed
+48
-36
web/server/web_client.c
+34
-34
@@ -16,8 +16,8 @@ inline int web_client_permission_denied(struct web_client *w) {
16
w->response.data->contenttype = CT_TEXT_PLAIN;
17
buffer_flush(w->response.data);
18
buffer_strcat(w->response.data, "You are not allowed to access this resource.");
19
- w->response.code = 403;
20
- return 403;
19
+ w->response.code = HTTP_RESP_FORBIDDEN;
20
+ return HTTP_RESP_FORBIDDEN;
21
}
22
23
static inline int web_client_crock_socket(struct web_client *w) {
@@ -337,7 +337,7 @@ static inline int access_to_file_is_not_permitted(struct web_client *w, const ch
337
w->response.data->contenttype = CT_TEXT_HTML;
338
buffer_strcat(w->response.data, "Access to file is not permitted: ");
339
buffer_strcat_htmlescape(w->response.data, filename);
340
- return 403;
340
+ return HTTP_RESP_FORBIDDEN;
341
}
342
343
int mysendfile(struct web_client *w, char *filename) {
@@ -357,7 +357,7 @@ int mysendfile(struct web_client *w, char *filename) {
357
w->response.data->contenttype = CT_TEXT_HTML;
358
buffer_sprintf(w->response.data, "Filename contains invalid characters: ");
359
buffer_strcat_htmlescape(w->response.data, filename);
360
- return 400;
360
+ return HTTP_RESP_BAD_REQUEST;
361
}
362
}
363
@@ -367,7 +367,7 @@ int mysendfile(struct web_client *w, char *filename) {
367
w->response.data->contenttype = CT_TEXT_HTML;
368
buffer_strcat(w->response.data, "Relative filenames are not supported: ");
369
buffer_strcat_htmlescape(w->response.data, filename);
370
- return 400;
370
+ return HTTP_RESP_BAD_REQUEST;
371
}
372
373
// find the physical file on disk
@@ -383,7 +383,7 @@ int mysendfile(struct web_client *w, char *filename) {
383
w->response.data->contenttype = CT_TEXT_HTML;
384
buffer_strcat(w->response.data, "File does not exist, or is not accessible: ");
385
buffer_strcat_htmlescape(w->response.data, webfilename);
386
- return 404;
386
+ return HTTP_RESP_NOT_FOUND;
387
}
388
389
if ((statbuf.st_mode & S_IFMT) == S_IFDIR) {
@@ -422,14 +422,14 @@ int mysendfile(struct web_client *w, char *filename) {
422
buffer_sprintf(w->response.header, "Location: /%s\r\n", filename);
423
buffer_strcat(w->response.data, "File is currently busy, please try again later: ");
424
buffer_strcat_htmlescape(w->response.data, webfilename);
425
- return 307;
425
+ return HTTP_RESP_REDIR_TEMP;
426
}
427
else {
428
error("%llu: Cannot open file '%s'.", w->id, webfilename);
429
w->response.data->contenttype = CT_TEXT_HTML;
430
buffer_strcat(w->response.data, "Cannot open file: ");
431
buffer_strcat_htmlescape(w->response.data, webfilename);
432
- return 404;
432
+ return HTTP_RESP_NOT_FOUND;
433
}
434
}
435
@@ -451,7 +451,7 @@ int mysendfile(struct web_client *w, char *filename) {
451
#endif /* __APPLE__ */
452
buffer_cacheable(w->response.data);
453
454
- return 200;
454
+ return HTTP_RESP_OK;
455
}
456
457
@@ -570,7 +570,7 @@ static inline int check_host_and_call(RRDHOST *host, struct web_client *w, char
570
//if(unlikely(host->rrd_memory_mode == RRD_MEMORY_MODE_NONE)) {
571
// buffer_flush(w->response.data);
572
// buffer_strcat(w->response.data, "This host does not maintain a database");
573
- // return 400;
573
+ // return HTTP_RESP_BAD_REQUEST;
574
//}
575
576
return func(host, w, url);
@@ -603,13 +603,13 @@ int web_client_api_request(RRDHOST *host, struct web_client *w, char *url)
603
w->response.data->contenttype = CT_TEXT_HTML;
604
buffer_strcat(w->response.data, "Unsupported API version: ");
605
buffer_strcat_htmlescape(w->response.data, tok);
606
- return 404;
606
+ return HTTP_RESP_NOT_FOUND;
607
}
608
}
609
else {
610
buffer_flush(w->response.data);
611
buffer_sprintf(w->response.data, "Which API version?");
612
- return 400;
612
+ return HTTP_RESP_BAD_REQUEST;
613
}
614
}
615
@@ -687,25 +687,25 @@ const char *web_content_type_to_string(uint8_t contenttype) {
687
688
const char *web_response_code_to_string(int code) {
689
switch(code) {
690
- case 200:
690
+ case HTTP_RESP_OK:
691
return "OK";
692
693
- case 301:
693
+ case HTTP_RESP_MOVED_PERM:
694
return "Moved Permanently";
695
696
- case 307:
696
+ case HTTP_RESP_REDIR_TEMP:
697
return "Temporary Redirect";
698
699
- case 400:
699
+ case HTTP_RESP_BAD_REQUEST:
700
return "Bad Request";
701
702
- case 403:
702
+ case HTTP_RESP_FORBIDDEN:
703
return "Forbidden";
704
705
- case 404:
705
+ case HTTP_RESP_NOT_FOUND:
706
return "Not Found";
707
708
- case 412:
708
+ case HTTP_RESP_PRECOND_FAIL:
709
return "Preconditions Failed";
710
711
default:
@@ -1117,7 +1117,7 @@ static inline ssize_t web_client_send_data(struct web_client *w,const void *buf,
1117
}
1118
1119
static inline void web_client_send_http_header(struct web_client *w) {
1120
- if(unlikely(w->response.code != 200))
1120
+ if(unlikely(w->response.code != HTTP_RESP_OK))
1121
buffer_no_cacheable(w->response.data);
1122
1123
// set a proper expiration date, if not already set
@@ -1147,7 +1147,7 @@ static inline void web_client_send_http_header(struct web_client *w) {
1147
}
1148
1149
char headerbegin[8328];
1150
- if (w->response.code == 301) {
1150
+ if (w->response.code == HTTP_RESP_MOVED_PERM) {
1151
memcpy(headerbegin,"\r\nLocation: https://",20);
1152
size_t headerlength = strlen(w->host);
1153
memcpy(&headerbegin[20],w->host,headerlength);
@@ -1330,7 +1330,7 @@ static inline int web_client_switch_host(RRDHOST *host, struct web_client *w, ch
1330
if(host != localhost) {
1331
buffer_flush(w->response.data);
1332
buffer_strcat(w->response.data, "Nesting of hosts is not allowed.");
1333
- return 400;
1333
+ return HTTP_RESP_BAD_REQUEST;
1334
}
1335
1336
char *tok = mystrsep(&url, "/");
@@ -1354,7 +1354,7 @@ static inline int web_client_switch_host(RRDHOST *host, struct web_client *w, ch
1354
w->response.data->contenttype = CT_TEXT_HTML;
1355
buffer_strcat(w->response.data, "This netdata does not maintain a database for host: ");
1356
buffer_strcat_htmlescape(w->response.data, tok?tok:"");
1357
- return 404;
1357
+ return HTTP_RESP_NOT_FOUND;
1358
}
1359
1360
static inline int web_client_process_url(RRDHOST *host, struct web_client *w, char *url) {
@@ -1399,7 +1399,7 @@ static inline int web_client_process_url(RRDHOST *host, struct web_client *w, ch
1399
w->response.data->contenttype = CT_TEXT_PLAIN;
1400
buffer_flush(w->response.data);
1401
config_generate(w->response.data, 0);
1402
- return 200;
1402
+ return HTTP_RESP_OK;
1403
}
1404
#ifdef NETDATA_INTERNAL_CHECKS
1405
else if(unlikely(hash == hash_exit && strcmp(tok, "exit") == 0)) {
@@ -1416,7 +1416,7 @@ static inline int web_client_process_url(RRDHOST *host, struct web_client *w, ch
1416
1417
error("web request to exit received.");
1418
netdata_cleanup_and_exit(0);
1419
- return 200;
1419
+ return HTTP_RESP_OK;
1420
}
1421
else if(unlikely(hash == hash_debug && strcmp(tok, "debug") == 0)) {
1422
if(unlikely(!web_client_can_access_netdataconf(w)))
@@ -1437,7 +1437,7 @@ static inline int web_client_process_url(RRDHOST *host, struct web_client *w, ch
1437
buffer_strcat(w->response.data, "Chart is not found: ");
1438
buffer_strcat_htmlescape(w->response.data, tok);
1439
debug(D_WEB_CLIENT_ACCESS, "%llu: %s is not found.", w->id, tok);
1440
- return 404;
1440
+ return HTTP_RESP_NOT_FOUND;
1441
}
1442
1443
debug_flags |= D_RRD_STATS;
@@ -1451,12 +1451,12 @@ static inline int web_client_process_url(RRDHOST *host, struct web_client *w, ch
1451
buffer_sprintf(w->response.data, "Chart has now debug %s: ", rrdset_flag_check(st, RRDSET_FLAG_DEBUG)?"enabled":"disabled");
1452
buffer_strcat_htmlescape(w->response.data, tok);
1453
debug(D_WEB_CLIENT_ACCESS, "%llu: debug for %s is %s.", w->id, tok, rrdset_flag_check(st, RRDSET_FLAG_DEBUG)?"enabled":"disabled");
1454
- return 200;
1454
+ return HTTP_RESP_OK;
1455
}
1456
1457
buffer_flush(w->response.data);
1458
buffer_strcat(w->response.data, "debug which chart?\r\n");
1459
- return 400;
1459
+ return HTTP_RESP_BAD_REQUEST;
1460
}
1461
else if(unlikely(hash == hash_mirror && strcmp(tok, "mirror") == 0)) {
1462
if(unlikely(!web_client_can_access_netdataconf(w)))
@@ -1470,7 +1470,7 @@ static inline int web_client_process_url(RRDHOST *host, struct web_client *w, ch
1470
// just leave the buffer as is
1471
// it will be copied back to the client
1472
1473
- return 200;
1473
+ return HTTP_RESP_OK;
1474
}
1475
#endif /* NETDATA_INTERNAL_CHECKS */
1476
}
@@ -1515,7 +1515,7 @@ void web_client_process_request(struct web_client *w) {
1515
w->response.data->contenttype = CT_TEXT_PLAIN;
1516
buffer_flush(w->response.data);
1517
buffer_strcat(w->response.data, "OK");
1518
- w->response.code = 200;
1518
+ w->response.code = HTTP_RESP_OK;
1519
break;
1520
1521
case WEB_CLIENT_MODE_FILECOPY:
@@ -1544,7 +1544,7 @@ void web_client_process_request(struct web_client *w) {
1544
1545
buffer_flush(w->response.data);
1546
buffer_sprintf(w->response.data, "Received request is too big (%zu bytes).\r\n", w->response.data->len);
1547
- w->response.code = HTTP_RESPONSE_BAD_REQUEST;
1547
+ w->response.code = HTTP_RESP_BAD_REQUEST;
1548
}
1549
else {
1550
// wait for more data
@@ -1557,7 +1557,7 @@ void web_client_process_request(struct web_client *w) {
1557
buffer_flush(w->response.data);
1558
w->response.data->contenttype = CT_TEXT_HTML;
1559
buffer_strcat(w->response.data, "<!DOCTYPE html><!-- SPDX-License-Identifier: GPL-3.0-or-later --><html><body onload=\"window.location.href ='https://'+ window.location.hostname + ':' + window.location.port + window.location.pathname\">Redirecting to safety connection, case your browser does not support redirection, please click <a onclick=\"window.location.href ='https://'+ window.location.hostname + ':' + window.location.port + window.location.pathname\">here</a>.</body></html>");
1560
- w->response.code = 301;
1560
+ w->response.code = HTTP_RESP_MOVED_PERM;
1561
break;
1562
}
1563
#endif
@@ -1566,14 +1566,14 @@ void web_client_process_request(struct web_client *w) {
1566
1567
buffer_flush(w->response.data);
1568
buffer_strcat(w->response.data, "URL not valid. I don't understand you...\r\n");
1569
- w->response.code = HTTP_RESPONSE_BAD_REQUEST;
1569
+ w->response.code = HTTP_RESP_BAD_REQUEST;
1570
break;
1571
case HTTP_VALIDATION_NOT_SUPPORTED:
1572
debug(D_WEB_CLIENT_ACCESS, "%llu: Cannot understand '%s'.", w->id, w->response.data->buffer);
1573
1574
buffer_flush(w->response.data);
1575
buffer_strcat(w->response.data, "I don't understand you...\r\n");
1576
- w->response.code = HTTP_RESPONSE_BAD_REQUEST;
1576
+ w->response.code = HTTP_RESP_BAD_REQUEST;
1577
break;
1578
}
1579
web/server/web_client.h
+14
-2
@@ -11,8 +11,20 @@ extern int web_enable_gzip,
11
web_gzip_strategy;
12
#endif /* NETDATA_WITH_ZLIB */
13
14
-//HTTP_CODES 4XX
15
-#define HTTP_RESPONSE_BAD_REQUEST 400
14
+// HTTP_CODES 2XX Success
15
+#define HTTP_RESP_OK 200
16
+
17
+// HTTP_CODES 3XX Redirections
18
+#define HTTP_RESP_MOVED_PERM 301
19
+#define HTTP_RESP_REDIR_TEMP 307
20
+#define HTTP_RESP_REDIR_PERM 308
21
+
22
+// HTTP_CODES 4XX Client Errors
23
+#define HTTP_RESP_BAD_REQUEST 400
24
+#define HTTP_RESP_FORBIDDEN 403
25
+#define HTTP_RESP_NOT_FOUND 404
26
+#define HTTP_RESP_PRECOND_FAIL 412
27
+
28
29
extern int respect_web_browser_do_not_track_policy;
30
extern char *web_x_frame_options;