From a836a244a3d2bdd4da1ee2641e3e957850668cea Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 8 May 2023 18:27:04 +0200 Subject: Adding upstream version 1.39.0. Signed-off-by: Daniel Baumann --- web/server/web_client.h | 172 +++++++++++++++++++++++++++++++----------------- 1 file changed, 113 insertions(+), 59 deletions(-) (limited to 'web/server/web_client.h') diff --git a/web/server/web_client.h b/web/server/web_client.h index d0360f4f9..c61a8b813 100644 --- a/web/server/web_client.h +++ b/web/server/web_client.h @@ -24,33 +24,37 @@ extern int web_enable_gzip, web_gzip_level, web_gzip_strategy; #define HTTP_RESP_NOT_FOUND 404 #define HTTP_RESP_CONFLICT 409 #define HTTP_RESP_PRECOND_FAIL 412 +#define HTTP_RESP_CONTENT_TOO_LONG 413 // HTTP_CODES 5XX Server Errors #define HTTP_RESP_INTERNAL_SERVER_ERROR 500 -#define HTTP_RESP_BACKEND_FETCH_FAILED 503 // 503 is right -#define HTTP_RESP_SERVICE_UNAVAILABLE 503 // 503 is right +#define HTTP_RESP_BACKEND_FETCH_FAILED 503 +#define HTTP_RESP_SERVICE_UNAVAILABLE 503 #define HTTP_RESP_GATEWAY_TIMEOUT 504 #define HTTP_RESP_BACKEND_RESPONSE_INVALID 591 +#define HTTP_REQ_MAX_HEADER_FETCH_TRIES 100 + extern int respect_web_browser_do_not_track_policy; extern char *web_x_frame_options; typedef enum web_client_mode { - WEB_CLIENT_MODE_NORMAL = 0, - WEB_CLIENT_MODE_FILECOPY = 1, - WEB_CLIENT_MODE_OPTIONS = 2, - WEB_CLIENT_MODE_STREAM = 3 + WEB_CLIENT_MODE_GET = 0, + WEB_CLIENT_MODE_POST = 1, + WEB_CLIENT_MODE_FILECOPY = 2, + WEB_CLIENT_MODE_OPTIONS = 3, + WEB_CLIENT_MODE_STREAM = 4, } WEB_CLIENT_MODE; typedef enum { HTTP_VALIDATION_OK, HTTP_VALIDATION_NOT_SUPPORTED, + HTTP_VALIDATION_TOO_MANY_READ_RETRIES, + HTTP_VALIDATION_EXCESS_REQUEST_DATA, HTTP_VALIDATION_MALFORMED_URL, -#ifdef ENABLE_HTTPS HTTP_VALIDATION_INCOMPLETE, +#ifdef ENABLE_HTTPS HTTP_VALIDATION_REDIRECT -#else - HTTP_VALIDATION_INCOMPLETE #endif } HTTP_VALIDATION; @@ -71,6 +75,9 @@ typedef enum web_client_flags { WEB_CLIENT_FLAG_DONT_CLOSE_SOCKET = 1 << 9, // don't close the socket when cleaning up (static-threaded web server) WEB_CLIENT_CHUNKED_TRANSFER = 1 << 10, // chunked transfer (used with zlib compression) + + WEB_CLIENT_FLAG_SSL_WAIT_RECEIVE = 1 << 11, // if set, we are waiting more input data from an ssl conn + WEB_CLIENT_FLAG_SSL_WAIT_SEND = 1 << 12, // if set, we have data to send to the client from an ssl conn } WEB_CLIENT_FLAGS; #define web_client_flag_check(w, flag) ((w)->flags & (flag)) @@ -100,6 +107,14 @@ typedef enum web_client_flags { #define web_client_enable_wait_send(w) web_client_flag_set(w, WEB_CLIENT_FLAG_WAIT_SEND) #define web_client_disable_wait_send(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_WAIT_SEND) +#define web_client_has_ssl_wait_receive(w) web_client_flag_check(w, WEB_CLIENT_FLAG_SSL_WAIT_RECEIVE) +#define web_client_enable_ssl_wait_receive(w) web_client_flag_set(w, WEB_CLIENT_FLAG_SSL_WAIT_RECEIVE) +#define web_client_disable_ssl_wait_receive(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_SSL_WAIT_RECEIVE) + +#define web_client_has_ssl_wait_send(w) web_client_flag_check(w, WEB_CLIENT_FLAG_SSL_WAIT_SEND) +#define web_client_enable_ssl_wait_send(w) web_client_flag_set(w, WEB_CLIENT_FLAG_SSL_WAIT_SEND) +#define web_client_disable_ssl_wait_send(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_SSL_WAIT_SEND) + #define web_client_set_tcp(w) web_client_flag_set(w, WEB_CLIENT_FLAG_TCP_CLIENT) #define web_client_set_unix(w) web_client_flag_set(w, WEB_CLIENT_FLAG_UNIX_CLIENT) #define web_client_check_unix(w) web_client_flag_check(w, WEB_CLIENT_FLAG_UNIX_CLIENT) @@ -107,90 +122,107 @@ typedef enum web_client_flags { #define web_client_is_corkable(w) web_client_flag_check(w, WEB_CLIENT_FLAG_TCP_CLIENT) -#define NETDATA_WEB_REQUEST_URL_SIZE 8192 +#define NETDATA_WEB_REQUEST_URL_SIZE 65536 // static allocation + #define NETDATA_WEB_RESPONSE_ZLIB_CHUNK_SIZE 16384 -#define NETDATA_WEB_RESPONSE_HEADER_SIZE 4096 -#define NETDATA_WEB_REQUEST_COOKIE_SIZE 1024 -#define NETDATA_WEB_REQUEST_ORIGIN_HEADER_SIZE 1024 -#define NETDATA_WEB_RESPONSE_INITIAL_SIZE 16384 -#define NETDATA_WEB_REQUEST_RECEIVE_SIZE 16384 -#define NETDATA_WEB_REQUEST_MAX_SIZE 16384 + +#define NETDATA_WEB_RESPONSE_HEADER_INITIAL_SIZE 4096 +#define NETDATA_WEB_RESPONSE_INITIAL_SIZE 8192 +#define NETDATA_WEB_REQUEST_INITIAL_SIZE 8192 +#define NETDATA_WEB_REQUEST_MAX_SIZE 65536 +#define NETDATA_WEB_DECODED_URL_INITIAL_SIZE 512 struct response { - BUFFER *header; // our response header - BUFFER *header_output; // internal use - BUFFER *data; // our response data buffer + BUFFER *header; // our response header + BUFFER *header_output; // internal use + BUFFER *data; // our response data buffer - int code; // the HTTP response code + short int code; // the HTTP response code + bool has_cookies; size_t rlen; // if non-zero, the excepted size of ifd (input of firecopy) size_t sent; // current data length sent to output - int zoutput; // if set to 1, web_client_send() will send compressed data + bool zoutput; // if set to 1, web_client_send() will send compressed data + #ifdef NETDATA_WITH_ZLIB + bool zinitialized; z_stream zstream; // zlib stream for sending compressed output to client - Bytef zbuffer[NETDATA_WEB_RESPONSE_ZLIB_CHUNK_SIZE]; // temporary buffer for storing compressed output size_t zsent; // the compressed bytes we have sent to the client size_t zhave; // the compressed bytes that we have received from zlib - unsigned int zinitialized : 1; + Bytef zbuffer[NETDATA_WEB_RESPONSE_ZLIB_CHUNK_SIZE]; // temporary buffer for storing compressed output #endif /* NETDATA_WITH_ZLIB */ }; +struct web_client; +typedef bool (*web_client_interrupt_t)(struct web_client *, void *data); + struct web_client { unsigned long long id; + size_t use_count; - WEB_CLIENT_FLAGS flags; // status flags for the client - WEB_CLIENT_MODE mode; // the operational mode of the client - WEB_CLIENT_ACL acl; // the access list of the client - int port_acl; // the operations permitted on the port the client connected to - char *auth_bearer_token; // the Bearer auth token (if sent) + WEB_CLIENT_FLAGS flags; // status flags for the client + WEB_CLIENT_MODE mode; // the operational mode of the client + WEB_CLIENT_ACL acl; // the access list of the client + int port_acl; // the operations permitted on the port the client connected to size_t header_parse_tries; size_t header_parse_last_size; - int tcp_cork; // 1 = we have a cork on the socket - + bool tcp_cork; int ifd; int ofd; - char client_ip[INET6_ADDRSTRLEN]; // Defined buffer sizes include null-terminators + char client_ip[INET6_ADDRSTRLEN]; // Defined buffer sizes include null-terminators char client_port[NI_MAXSERV]; - char server_host[NI_MAXHOST]; char client_host[NI_MAXHOST]; - char forwarded_host[NI_MAXHOST]; //Used with proxy - char decoded_url[NETDATA_WEB_REQUEST_URL_SIZE + 1]; // we decode the URL in this buffer - char decoded_query_string[NETDATA_WEB_REQUEST_URL_SIZE + 1]; // we decode the Query String in this buffer - char last_url[NETDATA_WEB_REQUEST_URL_SIZE + 1]; // we keep a copy of the decoded URL here - size_t url_path_length; - char separator; // This value can be either '?' or 'f' - char *url_search_path; //A pointer to the search path sent by the client + BUFFER *url_as_received; // the entire URL as received, used for logging - DO NOT MODIFY + BUFFER *url_path_decoded; // the path, decoded - it is incrementally parsed and altered + BUFFER *url_query_string_decoded; // the query string, decoded - it is incrementally parsed and altered - struct timeval tv_in, tv_ready; + // THESE NEED TO BE FREED + char *auth_bearer_token; // the Bearer auth token (if sent) + char *server_host; // the Host: header + char *forwarded_host; // the X-Forwarded-For: header + char *origin; // the Origin: header + char *user_agent; // the User-Agent: header - char cookie1[NETDATA_WEB_REQUEST_COOKIE_SIZE + 1]; - char cookie2[NETDATA_WEB_REQUEST_COOKIE_SIZE + 1]; - char origin[NETDATA_WEB_REQUEST_ORIGIN_HEADER_SIZE + 1]; - char *user_agent; - - struct response response; - - size_t stats_received_bytes; - size_t stats_sent_bytes; - - // cache of web_client allocations - struct web_client *prev; // maintain a linked list of web clients - struct web_client *next; // for the web servers that need it - - // MULTI-THREADED WEB SERVER MEMBERS - netdata_thread_t thread; // the thread servicing this client - volatile int running; // 1 when the thread runs, 0 otherwise + char *post_payload; // when this request is a POST, this has the payload + size_t post_payload_size; // the size of the buffer allocated for the payload + // the actual contents may be less than the size // STATIC-THREADED WEB SERVER MEMBERS - size_t pollinfo_slot; // POLLINFO slot of the web client - size_t pollinfo_filecopy_slot; // POLLINFO slot of the file read + size_t pollinfo_slot; // POLLINFO slot of the web client + size_t pollinfo_filecopy_slot; // POLLINFO slot of the file read + #ifdef ENABLE_HTTPS struct netdata_ssl ssl; #endif + + struct { // A callback to check if the query should be interrupted / stopped + web_client_interrupt_t callback; + void *callback_data; + } interrupt; + + struct { + size_t received_bytes; + size_t sent_bytes; + size_t *memory_accounting; // temporary pointer for constructor to use + } statistics; + + struct { + usec_t timeout_ut; // timeout if set, or zero + struct timeval tv_in; // request received + struct timeval tv_ready; // request processed - response ready + struct timeval tv_timeout_last_checkpoint; // last checkpoint + } timings; + + struct { + struct web_client *prev; + struct web_client *next; + } cache; + + struct response response; }; int web_client_permission_denied(struct web_client *w); @@ -211,6 +243,28 @@ char *strip_control_characters(char *url); int web_client_socket_is_now_used_for_streaming(struct web_client *w); +void web_client_zero(struct web_client *w); +struct web_client *web_client_create(size_t *statistics_memory_accounting); +void web_client_free(struct web_client *w); + +#ifdef ENABLE_HTTPS +void web_client_reuse_ssl(struct web_client *w); +#endif + +#include "web/api/web_api_v1.h" +#include "web/api/web_api_v2.h" #include "daemon/common.h" +void web_client_decode_path_and_query_string(struct web_client *w, const char *path_and_query_string); +int web_client_api_request(RRDHOST *host, struct web_client *w, char *url_path_fragment); +const char *web_content_type_to_string(HTTP_CONTENT_TYPE content_type); +void web_client_enable_deflate(struct web_client *w, int gzip); +int web_client_api_request_with_node_selection(RRDHOST *host, struct web_client *w, char *decoded_url_path); + +void web_client_timeout_checkpoint_init(struct web_client *w); +void web_client_timeout_checkpoint_set(struct web_client *w, int timeout_ms); +usec_t web_client_timeout_checkpoint(struct web_client *w); +bool web_client_timeout_checkpoint_and_check(struct web_client *w, usec_t *usec_since_last_checkpoint); +usec_t web_client_timeout_checkpoint_response_ready(struct web_client *w, usec_t *usec_since_last_checkpoint); + #endif -- cgit v1.2.3