diff options
author | rofl0r <rofl0r@users.noreply.github.com> | 2021-03-28 20:22:32 +0100 |
---|---|---|
committer | rofl0r <rofl0r@users.noreply.github.com> | 2021-03-28 20:24:23 +0100 |
commit | 48860bbe26867cd0ad880b6358cfcba8d142c267 (patch) | |
tree | 000aa44ff1703a08e1ac1829d407b90106a679fd | |
parent | c4231e58bf2613b6ab8b34f234c117c5d1488931 (diff) |
refactor html-error so send_http_headers() can take extra arg
we already required an extra argument inside the headers sent
for 401 and 407 error responses, move those to sent_http_error_message()
and refactor send_http_headers() to always take the extra argument.
in calling sites where the extra arg isn't needed, use "".
-rw-r--r-- | src/html-error.c | 33 | ||||
-rw-r--r-- | src/html-error.h | 2 | ||||
-rw-r--r-- | src/stats.c | 2 |
3 files changed, 20 insertions, 17 deletions
diff --git a/src/html-error.c b/src/html-error.c index 071d415..b6dd29d 100644 --- a/src/html-error.c +++ b/src/html-error.c @@ -133,7 +133,9 @@ send_html_file (FILE *infile, struct conn_s *connptr) return 1; } -int send_http_headers (struct conn_s *connptr, int code, const char *message) +int send_http_headers ( + struct conn_s *connptr, int code, + const char *message, const char *extra) { const char headers[] = "HTTP/1.0 %d %s\r\n" @@ -142,21 +144,9 @@ int send_http_headers (struct conn_s *connptr, int code, const char *message) "%s" "Connection: close\r\n" "\r\n"; - const char p_auth_str[] = - "Proxy-Authenticate: Basic realm=\"" - PACKAGE_NAME "\"\r\n"; - - const char w_auth_str[] = - "WWW-Authenticate: Basic realm=\"" - PACKAGE_NAME "\"\r\n"; - - /* according to rfc7235, the 407 error must be accompanied by - a Proxy-Authenticate header field. */ - const char *add = code == 407 ? p_auth_str : (code == 401 ? w_auth_str : ""); - return (write_message (connptr->client_fd, headers, code, message, PACKAGE, VERSION, - add)); + extra)); } /* @@ -180,8 +170,21 @@ int send_http_error_message (struct conn_s *connptr) "<p><em>Generated by %s version %s.</em></p>\n" "</body>\n" "</html>\n"; + const char p_auth_str[] = + "Proxy-Authenticate: Basic realm=\"" + PACKAGE_NAME "\"\r\n"; + + const char w_auth_str[] = + "WWW-Authenticate: Basic realm=\"" + PACKAGE_NAME "\"\r\n"; + + /* according to rfc7235, the 407 error must be accompanied by + a Proxy-Authenticate header field. */ + const char *add = connptr->error_number == 407 ? p_auth_str : + (connptr->error_number == 401 ? w_auth_str : ""); + send_http_headers (connptr, connptr->error_number, - connptr->error_string); + connptr->error_string, add); error_file = get_html_file (connptr->error_number); if (!(infile = fopen (error_file, "r"))) { diff --git a/src/html-error.h b/src/html-error.h index c133cef..bc9b7ce 100644 --- a/src/html-error.h +++ b/src/html-error.h @@ -33,7 +33,7 @@ extern int add_error_variable (struct conn_s *connptr, const char *key, const char *val); extern int send_html_file (FILE * infile, struct conn_s *connptr); extern int send_http_headers (struct conn_s *connptr, int code, - const char *message); + const char *message, const char *extra); extern int add_standard_vars (struct conn_s *connptr); #endif /* !TINYPROXY_HTML_ERROR_H */ diff --git a/src/stats.c b/src/stats.c index dfe054c..1228aa3 100644 --- a/src/stats.c +++ b/src/stats.c @@ -122,7 +122,7 @@ err_minus_one: add_error_variable (connptr, "deniedconns", denied); add_error_variable (connptr, "refusedconns", refused); add_standard_vars (connptr); - send_http_headers (connptr, 200, "Statistic requested"); + send_http_headers (connptr, 200, "Statistic requested", ""); send_html_file (statfile, connptr); fclose (statfile); pthread_mutex_unlock(&stats_file_lock); |