123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139 |
- // SPDX-License-Identifier: GPL-3.0-or-later
- #include "web_client.h"
- // this is an async I/O implementation of the web server request parser
- // it is used by all netdata web servers
- int respect_web_browser_do_not_track_policy = 0;
- char *web_x_frame_options = NULL;
- int web_enable_gzip = 1, web_gzip_level = 3, web_gzip_strategy = Z_DEFAULT_STRATEGY;
- void web_client_set_conn_tcp(struct web_client *w) {
- web_client_flags_clear_conn(w);
- web_client_flag_set(w, WEB_CLIENT_FLAG_CONN_TCP);
- }
- void web_client_set_conn_unix(struct web_client *w) {
- web_client_flags_clear_conn(w);
- web_client_flag_set(w, WEB_CLIENT_FLAG_CONN_UNIX);
- }
- void web_client_set_conn_cloud(struct web_client *w) {
- web_client_flags_clear_conn(w);
- web_client_flag_set(w, WEB_CLIENT_FLAG_CONN_CLOUD);
- }
- void web_client_set_conn_webrtc(struct web_client *w) {
- web_client_flags_clear_conn(w);
- web_client_flag_set(w, WEB_CLIENT_FLAG_CONN_WEBRTC);
- }
- inline int web_client_permission_denied(struct web_client *w) {
- w->response.data->content_type = CT_TEXT_PLAIN;
- buffer_flush(w->response.data);
- buffer_strcat(w->response.data, "You are not allowed to access this resource.");
- w->response.code = HTTP_RESP_FORBIDDEN;
- return HTTP_RESP_FORBIDDEN;
- }
- inline int web_client_bearer_required(struct web_client *w) {
- w->response.data->content_type = CT_TEXT_PLAIN;
- buffer_flush(w->response.data);
- buffer_strcat(w->response.data, "An authorization bearer is required to access the resource.");
- w->response.code = HTTP_RESP_PRECOND_FAIL;
- return HTTP_RESP_PRECOND_FAIL;
- }
- static inline int bad_request_multiple_dashboard_versions(struct web_client *w) {
- w->response.data->content_type = CT_TEXT_PLAIN;
- buffer_flush(w->response.data);
- buffer_strcat(w->response.data, "Multiple dashboard versions given at the URL.");
- w->response.code = HTTP_RESP_BAD_REQUEST;
- return HTTP_RESP_BAD_REQUEST;
- }
- static inline int web_client_cork_socket(struct web_client *w __maybe_unused) {
- #ifdef TCP_CORK
- if(likely(web_client_check_conn_tcp(w) && !w->tcp_cork && w->ofd != -1)) {
- w->tcp_cork = true;
- if(unlikely(setsockopt(w->ofd, IPPROTO_TCP, TCP_CORK, (char *) &w->tcp_cork, sizeof(int)) != 0)) {
- netdata_log_error("%llu: failed to enable TCP_CORK on socket.", w->id);
- w->tcp_cork = false;
- return -1;
- }
- }
- #endif /* TCP_CORK */
- return 0;
- }
- #ifdef ENABLE_HTTPS
- static inline void web_client_enable_wait_from_ssl(struct web_client *w) {
- if (w->ssl.ssl_errno == SSL_ERROR_WANT_READ)
- web_client_enable_ssl_wait_receive(w);
- else if (w->ssl.ssl_errno == SSL_ERROR_WANT_WRITE)
- web_client_enable_ssl_wait_send(w);
- else {
- web_client_disable_ssl_wait_receive(w);
- web_client_disable_ssl_wait_send(w);
- }
- }
- #endif
- static inline int web_client_uncork_socket(struct web_client *w __maybe_unused) {
- #ifdef TCP_CORK
- if(likely(w->tcp_cork && w->ofd != -1)) {
- w->tcp_cork = false;
- if(unlikely(setsockopt(w->ofd, IPPROTO_TCP, TCP_CORK, (char *) &w->tcp_cork, sizeof(int)) != 0)) {
- netdata_log_error("%llu: failed to disable TCP_CORK on socket.", w->id);
- w->tcp_cork = true;
- return -1;
- }
- }
- #endif /* TCP_CORK */
- w->tcp_cork = false;
- return 0;
- }
- static inline char *strip_control_characters(char *url) {
- if(!url) return "";
- for(char *s = url; *s ;s++)
- if(iscntrl(*s)) *s = ' ';
- return url;
- }
- static void web_client_reset_allocations(struct web_client *w, bool free_all) {
- if(free_all) {
- // the web client is to be destroyed
- buffer_free(w->url_as_received);
- w->url_as_received = NULL;
- buffer_free(w->url_path_decoded);
- w->url_path_decoded = NULL;
- buffer_free(w->url_query_string_decoded);
- w->url_query_string_decoded = NULL;
- buffer_free(w->response.header_output);
- w->response.header_output = NULL;
- buffer_free(w->response.header);
- w->response.header = NULL;
- buffer_free(w->response.data);
- w->response.data = NULL;
- buffer_free(w->payload);
- w->payload = NULL;
- }
- else {
- // the web client is to be re-used
- buffer_reset(w->url_as_received);
- buffer_reset(w->url_path_decoded);
- buffer_reset(w->url_query_string_decoded);
- buffer_reset(w->response.header_output);
- buffer_reset(w->response.header);
- buffer_reset(w->response.data);
- if(w->payload)
- buffer_reset(w->payload);
- // to add more items here,
- // web_client_reuse_from_cache() needs to be adjusted to maintain them
- }
- freez(w->server_host);
- w->server_host = NULL;
- freez(w->forwarded_host);
- w->forwarded_host = NULL;
- freez(w->forwarded_for);
- w->forwarded_for = NULL;
- freez(w->origin);
- w->origin = NULL;
- freez(w->user_agent);
- w->user_agent = NULL;
- freez(w->auth_bearer_token);
- w->auth_bearer_token = NULL;
- // if we had enabled compression, release it
- if(w->response.zinitialized) {
- deflateEnd(&w->response.zstream);
- w->response.zsent = 0;
- w->response.zhave = 0;
- w->response.zstream.avail_in = 0;
- w->response.zstream.avail_out = 0;
- w->response.zstream.total_in = 0;
- w->response.zstream.total_out = 0;
- w->response.zinitialized = false;
- web_client_flag_clear(w, WEB_CLIENT_CHUNKED_TRANSFER);
- }
- memset(w->transaction, 0, sizeof(w->transaction));
- web_client_flags_clear_auth(w);
- web_client_flag_clear(w, WEB_CLIENT_ENCODING_GZIP|WEB_CLIENT_ENCODING_DEFLATE);
- web_client_reset_path_flags(w);
- }
- void web_client_log_completed_request(struct web_client *w, bool update_web_stats) {
- struct timeval tv;
- now_monotonic_high_precision_timeval(&tv);
- size_t size = (w->mode == HTTP_REQUEST_MODE_FILECOPY) ? w->response.rlen : w->response.data->len;
- size_t sent = w->response.zoutput ? (size_t)w->response.zstream.total_out : size;
- if(update_web_stats)
- global_statistics_web_request_completed(dt_usec(&tv, &w->timings.tv_in),
- w->statistics.received_bytes,
- w->statistics.sent_bytes,
- size,
- sent);
- usec_t prep_ut = w->timings.tv_ready.tv_sec ? dt_usec(&w->timings.tv_ready, &w->timings.tv_in) : 0;
- usec_t sent_ut = w->timings.tv_ready.tv_sec ? dt_usec(&tv, &w->timings.tv_ready) : 0;
- usec_t total_ut = dt_usec(&tv, &w->timings.tv_in);
- strip_control_characters((char *)buffer_tostring(w->url_as_received));
- ND_LOG_STACK lgs[] = {
- ND_LOG_FIELD_U64(NDF_CONNECTION_ID, w->id),
- ND_LOG_FIELD_UUID(NDF_TRANSACTION_ID, &w->transaction),
- ND_LOG_FIELD_TXT(NDF_NIDL_NODE, w->client_host),
- ND_LOG_FIELD_TXT(NDF_REQUEST_METHOD, http_request_method2string(w->mode)),
- ND_LOG_FIELD_BFR(NDF_REQUEST, w->url_as_received),
- ND_LOG_FIELD_U64(NDF_RESPONSE_CODE, w->response.code),
- ND_LOG_FIELD_U64(NDF_RESPONSE_SENT_BYTES, sent),
- ND_LOG_FIELD_U64(NDF_RESPONSE_SIZE_BYTES, size),
- ND_LOG_FIELD_U64(NDF_RESPONSE_PREPARATION_TIME_USEC, prep_ut),
- ND_LOG_FIELD_U64(NDF_RESPONSE_SENT_TIME_USEC, sent_ut),
- ND_LOG_FIELD_U64(NDF_RESPONSE_TOTAL_TIME_USEC, total_ut),
- ND_LOG_FIELD_TXT(NDF_SRC_IP, w->client_ip),
- ND_LOG_FIELD_TXT(NDF_SRC_PORT, w->client_port),
- ND_LOG_FIELD_TXT(NDF_SRC_FORWARDED_FOR, w->forwarded_for),
- ND_LOG_FIELD_UUID(NDF_ACCOUNT_ID, &w->auth.cloud_account_id),
- ND_LOG_FIELD_TXT(NDF_USER_NAME, w->auth.client_name),
- ND_LOG_FIELD_TXT(NDF_USER_ROLE, http_id2access(w->access)),
- ND_LOG_FIELD_END(),
- };
- ND_LOG_STACK_PUSH(lgs);
- ND_LOG_FIELD_PRIORITY prio = NDLP_INFO;
- if(w->response.code >= 500)
- prio = NDLP_EMERG;
- else if(w->response.code >= 400)
- prio = NDLP_WARNING;
- else if(w->response.code >= 300)
- prio = NDLP_NOTICE;
- // cleanup progress
- if(web_client_flag_check(w, WEB_CLIENT_FLAG_PROGRESS_TRACKING)) {
- web_client_flag_clear(w, WEB_CLIENT_FLAG_PROGRESS_TRACKING);
- query_progress_finished(&w->transaction, 0, w->response.code, total_ut, size, sent);
- }
- // access log
- if(likely(buffer_strlen(w->url_as_received)))
- nd_log(NDLS_ACCESS, prio, NULL);
- }
- void web_client_request_done(struct web_client *w) {
- web_client_uncork_socket(w);
- netdata_log_debug(D_WEB_CLIENT, "%llu: Resetting client.", w->id);
- web_client_log_completed_request(w, true);
- if(unlikely(w->mode == HTTP_REQUEST_MODE_FILECOPY)) {
- if(w->ifd != w->ofd) {
- netdata_log_debug(D_WEB_CLIENT, "%llu: Closing filecopy input file descriptor %d.", w->id, w->ifd);
- if(web_server_mode != WEB_SERVER_MODE_STATIC_THREADED) {
- if (w->ifd != -1){
- close(w->ifd);
- }
- }
- w->ifd = w->ofd;
- }
- }
- web_client_reset_allocations(w, false);
- w->mode = HTTP_REQUEST_MODE_GET;
- web_client_disable_donottrack(w);
- web_client_disable_tracking_required(w);
- web_client_disable_keepalive(w);
- w->header_parse_tries = 0;
- w->header_parse_last_size = 0;
- web_client_enable_wait_receive(w);
- web_client_disable_wait_send(w);
- w->response.has_cookies = false;
- w->response.rlen = 0;
- w->response.sent = 0;
- w->response.code = 0;
- w->response.zoutput = false;
- w->statistics.received_bytes = 0;
- w->statistics.sent_bytes = 0;
- }
- static int append_slash_to_url_and_redirect(struct web_client *w) {
- // this function returns a relative redirect
- // it finds the last path component on the URL and just appends / to it
- //
- // So, if the URL is:
- //
- // /path/to/file?query_string
- //
- // It adds a Location header like this:
- //
- // Location: file/?query_string\r\n
- //
- // The web browser already knows that it is inside /path/to/
- // so it converts the path to /path/to/file/ and executes the
- // request again.
- buffer_strcat(w->response.header, "Location: ");
- const char *b = buffer_tostring(w->url_as_received);
- const char *q = strchr(b, '?');
- if(q && q > b) {
- const char *e = q - 1;
- while(e > b && *e != '/') e--;
- if(*e == '/') e++;
- size_t len = q - e;
- buffer_strncat(w->response.header, e, len);
- buffer_strncat(w->response.header, "/", 1);
- buffer_strcat(w->response.header, q);
- }
- else {
- const char *e = &b[buffer_strlen(w->url_as_received) - 1];
- while(e > b && *e != '/') e--;
- if(*e == '/') e++;
- buffer_strcat(w->response.header, e);
- buffer_strncat(w->response.header, "/", 1);
- }
- buffer_strncat(w->response.header, "\r\n", 2);
- w->response.data->content_type = CT_TEXT_HTML;
- buffer_flush(w->response.data);
- buffer_strcat(w->response.data,
- "<!DOCTYPE html><html>"
- "<body onload=\"window.location.href = window.location.origin + window.location.pathname + '/' + window.location.search + window.location.hash\">"
- "Redirecting. In case your browser does not support redirection, please click "
- "<a onclick=\"window.location.href = window.location.origin + window.location.pathname + '/' + window.location.search + window.location.hash\">here</a>."
- "</body></html>");
- return HTTP_RESP_MOVED_PERM;
- }
- // Work around a bug in the CMocka library by removing this function during testing.
- #ifndef REMOVE_MYSENDFILE
- static inline int dashboard_version(struct web_client *w) {
- if(!web_client_flag_check(w, WEB_CLIENT_FLAG_PATH_WITH_VERSION))
- return -1;
- if(web_client_flag_check(w, WEB_CLIENT_FLAG_PATH_IS_V0))
- return 0;
- if(web_client_flag_check(w, WEB_CLIENT_FLAG_PATH_IS_V1))
- return 1;
- if(web_client_flag_check(w, WEB_CLIENT_FLAG_PATH_IS_V2))
- return 2;
- return -1;
- }
- static bool find_filename_to_serve(const char *filename, char *dst, size_t dst_len, struct stat *statbuf, struct web_client *w, bool *is_dir) {
- int d_version = dashboard_version(w);
- bool has_extension = web_client_flag_check(w, WEB_CLIENT_FLAG_PATH_HAS_FILE_EXTENSION);
- int fallback = 0;
- if(has_extension) {
- if(d_version == -1)
- snprintfz(dst, dst_len, "%s/%s", netdata_configured_web_dir, filename);
- else {
- // check if the filename or directory exists
- // fallback to the same path without the dashboard version otherwise
- snprintfz(dst, dst_len, "%s/v%d/%s", netdata_configured_web_dir, d_version, filename);
- fallback = 1;
- }
- }
- else if(d_version != -1) {
- if(filename && *filename) {
- // check if the filename exists
- // fallback to /vN/index.html otherwise
- snprintfz(dst, dst_len, "%s/%s", netdata_configured_web_dir, filename);
- fallback = 2;
- }
- else {
- if(filename && *filename)
- web_client_flag_set(w, WEB_CLIENT_FLAG_PATH_HAS_TRAILING_SLASH);
- snprintfz(dst, dst_len, "%s/v%d", netdata_configured_web_dir, d_version);
- }
- }
- else {
- // check if filename exists
- // this is needed to serve {filename}/index.html, in case a user puts a html file into a directory
- // fallback to /index.html otherwise
- snprintfz(dst, dst_len, "%s/%s", netdata_configured_web_dir, filename);
- fallback = 3;
- }
- if (stat(dst, statbuf) != 0) {
- if(fallback == 1) {
- snprintfz(dst, dst_len, "%s/%s", netdata_configured_web_dir, filename);
- if (stat(dst, statbuf) != 0)
- return false;
- }
- else if(fallback == 2) {
- if(filename && *filename)
- web_client_flag_set(w, WEB_CLIENT_FLAG_PATH_HAS_TRAILING_SLASH);
- snprintfz(dst, dst_len, "%s/v%d", netdata_configured_web_dir, d_version);
- if (stat(dst, statbuf) != 0)
- return false;
- }
- else if(fallback == 3) {
- if(filename && *filename)
- web_client_flag_set(w, WEB_CLIENT_FLAG_PATH_HAS_TRAILING_SLASH);
- snprintfz(dst, dst_len, "%s", netdata_configured_web_dir);
- if (stat(dst, statbuf) != 0)
- return false;
- }
- else
- return false;
- }
- if((statbuf->st_mode & S_IFMT) == S_IFDIR) {
- size_t len = strlen(dst);
- if(len > dst_len - 11)
- return false;
- strncpyz(&dst[len], "/index.html", dst_len - len);
- if (stat(dst, statbuf) != 0)
- return false;
- *is_dir = true;
- }
- return true;
- }
- static int mysendfile(struct web_client *w, char *filename) {
- netdata_log_debug(D_WEB_CLIENT, "%llu: Looking for file '%s/%s'", w->id, netdata_configured_web_dir, filename);
- if(!http_can_access_dashboard(w))
- return web_client_permission_denied(w);
- // skip leading slashes
- while (*filename == '/') filename++;
- // if the filename contains "strange" characters, refuse to serve it
- char *s;
- for(s = filename; *s ;s++) {
- if( !isalnum(*s) && *s != '/' && *s != '.' && *s != '-' && *s != '_') {
- netdata_log_debug(D_WEB_CLIENT_ACCESS, "%llu: File '%s' is not acceptable.", w->id, filename);
- w->response.data->content_type = CT_TEXT_HTML;
- buffer_sprintf(w->response.data, "Filename contains invalid characters: ");
- buffer_strcat_htmlescape(w->response.data, filename);
- return HTTP_RESP_BAD_REQUEST;
- }
- }
- // if the filename contains a double dot refuse to serve it
- if(strstr(filename, "..") != 0) {
- netdata_log_debug(D_WEB_CLIENT_ACCESS, "%llu: File '%s' is not acceptable.", w->id, filename);
- w->response.data->content_type = CT_TEXT_HTML;
- buffer_strcat(w->response.data, "Relative filenames are not supported: ");
- buffer_strcat_htmlescape(w->response.data, filename);
- return HTTP_RESP_BAD_REQUEST;
- }
- // find the physical file on disk
- bool is_dir = false;
- char web_filename[FILENAME_MAX + 1];
- struct stat statbuf;
- if(!find_filename_to_serve(filename, web_filename, FILENAME_MAX, &statbuf, w, &is_dir)) {
- w->response.data->content_type = CT_TEXT_HTML;
- buffer_strcat(w->response.data, "File does not exist, or is not accessible: ");
- buffer_strcat_htmlescape(w->response.data, web_filename);
- return HTTP_RESP_NOT_FOUND;
- }
- if(is_dir && !web_client_flag_check(w, WEB_CLIENT_FLAG_PATH_HAS_TRAILING_SLASH))
- return append_slash_to_url_and_redirect(w);
- // open the file
- w->ifd = open(web_filename, O_NONBLOCK, O_RDONLY);
- if(w->ifd == -1) {
- w->ifd = w->ofd;
- if(errno == EBUSY || errno == EAGAIN) {
- netdata_log_error("%llu: File '%s' is busy, sending 307 Moved Temporarily to force retry.", w->id, web_filename);
- w->response.data->content_type = CT_TEXT_HTML;
- buffer_sprintf(w->response.header, "Location: /%s\r\n", filename);
- buffer_strcat(w->response.data, "File is currently busy, please try again later: ");
- buffer_strcat_htmlescape(w->response.data, web_filename);
- return HTTP_RESP_REDIR_TEMP;
- }
- else {
- netdata_log_error("%llu: Cannot open file '%s'.", w->id, web_filename);
- w->response.data->content_type = CT_TEXT_HTML;
- buffer_strcat(w->response.data, "Cannot open file: ");
- buffer_strcat_htmlescape(w->response.data, web_filename);
- return HTTP_RESP_NOT_FOUND;
- }
- }
- sock_setnonblock(w->ifd);
- w->response.data->content_type = contenttype_for_filename(web_filename);
- netdata_log_debug(D_WEB_CLIENT_ACCESS, "%llu: Sending file '%s' (%"PRId64" bytes, ifd %d, ofd %d).", w->id, web_filename, (int64_t)statbuf.st_size, w->ifd, w->ofd);
- w->mode = HTTP_REQUEST_MODE_FILECOPY;
- web_client_enable_wait_receive(w);
- web_client_disable_wait_send(w);
- buffer_flush(w->response.data);
- buffer_need_bytes(w->response.data, (size_t)statbuf.st_size);
- w->response.rlen = (size_t)statbuf.st_size;
- #ifdef __APPLE__
- w->response.data->date = statbuf.st_mtimespec.tv_sec;
- #else
- w->response.data->date = statbuf.st_mtim.tv_sec;
- #endif
- buffer_cacheable(w->response.data);
- return HTTP_RESP_OK;
- }
- #endif
- void buffer_data_options2string(BUFFER *wb, uint32_t options) {
- int count = 0;
- if(options & RRDR_OPTION_NONZERO) {
- if(count++) buffer_strcat(wb, " ");
- buffer_strcat(wb, "nonzero");
- }
- if(options & RRDR_OPTION_REVERSED) {
- if(count++) buffer_strcat(wb, " ");
- buffer_strcat(wb, "flip");
- }
- if(options & RRDR_OPTION_JSON_WRAP) {
- if(count++) buffer_strcat(wb, " ");
- buffer_strcat(wb, "jsonwrap");
- }
- if(options & RRDR_OPTION_MIN2MAX) {
- if(count++) buffer_strcat(wb, " ");
- buffer_strcat(wb, "min2max");
- }
- if(options & RRDR_OPTION_MILLISECONDS) {
- if(count++) buffer_strcat(wb, " ");
- buffer_strcat(wb, "ms");
- }
- if(options & RRDR_OPTION_ABSOLUTE) {
- if(count++) buffer_strcat(wb, " ");
- buffer_strcat(wb, "absolute");
- }
- if(options & RRDR_OPTION_SECONDS) {
- if(count++) buffer_strcat(wb, " ");
- buffer_strcat(wb, "seconds");
- }
- if(options & RRDR_OPTION_NULL2ZERO) {
- if(count++) buffer_strcat(wb, " ");
- buffer_strcat(wb, "null2zero");
- }
- if(options & RRDR_OPTION_OBJECTSROWS) {
- if(count++) buffer_strcat(wb, " ");
- buffer_strcat(wb, "objectrows");
- }
- if(options & RRDR_OPTION_GOOGLE_JSON) {
- if(count++) buffer_strcat(wb, " ");
- buffer_strcat(wb, "google_json");
- }
- if(options & RRDR_OPTION_PERCENTAGE) {
- if(count++) buffer_strcat(wb, " ");
- buffer_strcat(wb, "percentage");
- }
- if(options & RRDR_OPTION_NOT_ALIGNED) {
- if(count++) buffer_strcat(wb, " ");
- buffer_strcat(wb, "unaligned");
- }
- if(options & RRDR_OPTION_ANOMALY_BIT) {
- if(count++) buffer_strcat(wb, " ");
- buffer_strcat(wb, "anomaly-bit");
- }
- }
- static inline int check_host_and_call(RRDHOST *host, struct web_client *w, char *url, int (*func)(RRDHOST *, struct web_client *, char *)) {
- //if(unlikely(host->rrd_memory_mode == RRD_MEMORY_MODE_NONE)) {
- // buffer_flush(w->response.data);
- // buffer_strcat(w->response.data, "This host does not maintain a database");
- // return HTTP_RESP_BAD_REQUEST;
- //}
- return func(host, w, url);
- }
- static inline int UNUSED_FUNCTION(check_host_and_dashboard_acl_and_call)(RRDHOST *host, struct web_client *w, char *url, int (*func)(RRDHOST *, struct web_client *, char *)) {
- if(!http_can_access_dashboard(w))
- return web_client_permission_denied(w);
- return check_host_and_call(host, w, url, func);
- }
- static inline int UNUSED_FUNCTION(check_host_and_mgmt_acl_and_call)(RRDHOST *host, struct web_client *w, char *url, int (*func)(RRDHOST *, struct web_client *, char *)) {
- if(!http_can_access_mgmt(w))
- return web_client_permission_denied(w);
- return check_host_and_call(host, w, url, func);
- }
- int web_client_api_request(RRDHOST *host, struct web_client *w, char *url_path_fragment) {
- ND_LOG_STACK lgs[] = {
- ND_LOG_FIELD_TXT(NDF_SRC_IP, w->client_ip),
- ND_LOG_FIELD_TXT(NDF_SRC_PORT, w->client_port),
- ND_LOG_FIELD_TXT(NDF_SRC_FORWARDED_HOST, w->forwarded_host),
- ND_LOG_FIELD_TXT(NDF_SRC_FORWARDED_FOR, w->forwarded_for),
- ND_LOG_FIELD_TXT(NDF_NIDL_NODE, w->client_host),
- ND_LOG_FIELD_TXT(NDF_REQUEST_METHOD, http_request_method2string(w->mode)),
- ND_LOG_FIELD_BFR(NDF_REQUEST, w->url_as_received),
- ND_LOG_FIELD_U64(NDF_CONNECTION_ID, w->id),
- ND_LOG_FIELD_UUID(NDF_TRANSACTION_ID, &w->transaction),
- ND_LOG_FIELD_UUID(NDF_ACCOUNT_ID, &w->auth.cloud_account_id),
- ND_LOG_FIELD_TXT(NDF_USER_NAME, w->auth.client_name),
- ND_LOG_FIELD_TXT(NDF_USER_ROLE, http_id2access(w->access)),
- ND_LOG_FIELD_END(),
- };
- ND_LOG_STACK_PUSH(lgs);
- if(!web_client_flag_check(w, WEB_CLIENT_FLAG_PROGRESS_TRACKING)) {
- web_client_flag_set(w, WEB_CLIENT_FLAG_PROGRESS_TRACKING);
- query_progress_start_or_update(&w->transaction, 0, w->mode, w->acl,
- buffer_tostring(w->url_as_received),
- w->payload,
- w->forwarded_for ? w->forwarded_for : w->client_ip);
- }
- // get the api version
- char *tok = strsep_skip_consecutive_separators(&url_path_fragment, "/");
- if(tok && *tok) {
- if(strcmp(tok, "v2") == 0)
- return web_client_api_request_v2(host, w, url_path_fragment);
- else if(strcmp(tok, "v1") == 0)
- return web_client_api_request_v1(host, w, url_path_fragment);
- else {
- buffer_flush(w->response.data);
- w->response.data->content_type = CT_TEXT_HTML;
- buffer_strcat(w->response.data, "Unsupported API version: ");
- buffer_strcat_htmlescape(w->response.data, tok);
- return HTTP_RESP_NOT_FOUND;
- }
- }
- else {
- buffer_flush(w->response.data);
- buffer_sprintf(w->response.data, "Which API version?");
- return HTTP_RESP_BAD_REQUEST;
- }
- }
- /**
- * Valid Method
- *
- * Netdata accepts only three methods, including one of these three(STREAM) is an internal method.
- *
- * @param w is the structure with the client request
- * @param s is the start string to parse
- *
- * @return it returns the next address to parse case the method is valid and NULL otherwise.
- */
- static inline char *web_client_valid_method(struct web_client *w, char *s) {
- // is is a valid request?
- if(!strncmp(s, "GET ", 4)) {
- s = &s[4];
- w->mode = HTTP_REQUEST_MODE_GET;
- }
- else if(!strncmp(s, "OPTIONS ", 8)) {
- s = &s[8];
- w->mode = HTTP_REQUEST_MODE_OPTIONS;
- }
- else if(!strncmp(s, "POST ", 5)) {
- s = &s[5];
- w->mode = HTTP_REQUEST_MODE_POST;
- }
- else if(!strncmp(s, "PUT ", 4)) {
- s = &s[4];
- w->mode = HTTP_REQUEST_MODE_PUT;
- }
- else if(!strncmp(s, "DELETE ", 7)) {
- s = &s[7];
- w->mode = HTTP_REQUEST_MODE_DELETE;
- }
- else if(!strncmp(s, "STREAM ", 7)) {
- s = &s[7];
- #ifdef ENABLE_HTTPS
- if (!SSL_connection(&w->ssl) && http_is_using_ssl_force(w)) {
- w->header_parse_tries = 0;
- w->header_parse_last_size = 0;
- web_client_disable_wait_receive(w);
- char hostname[256];
- char *copyme = strstr(s,"hostname=");
- if ( copyme ){
- copyme += 9;
- char *end = strchr(copyme,'&');
- if(end){
- size_t length = MIN(255, end - copyme);
- memcpy(hostname,copyme,length);
- hostname[length] = 0X00;
- }
- else{
- memcpy(hostname,"not available",13);
- hostname[13] = 0x00;
- }
- }
- else{
- memcpy(hostname,"not available",13);
- hostname[13] = 0x00;
- }
- netdata_log_error("The server is configured to always use encrypted connections, please enable the SSL on child with hostname '%s'.",hostname);
- s = NULL;
- }
- #endif
- w->mode = HTTP_REQUEST_MODE_STREAM;
- }
- else {
- s = NULL;
- }
- return s;
- }
- /**
- * Request validate
- *
- * @param w is the structure with the client request
- *
- * @return It returns HTTP_VALIDATION_OK on success and another code present
- * in the enum HTTP_VALIDATION otherwise.
- */
- HTTP_VALIDATION http_request_validate(struct web_client *w) {
- char *s = (char *)buffer_tostring(w->response.data), *encoded_url = NULL;
- size_t last_pos = w->header_parse_last_size;
- w->header_parse_tries++;
- w->header_parse_last_size = buffer_strlen(w->response.data);
- int is_it_valid;
- if(w->header_parse_tries > 1) {
- if(last_pos > 4) last_pos -= 4; // allow searching for \r\n\r\n
- else last_pos = 0;
- if(w->header_parse_last_size < last_pos)
- last_pos = 0;
- is_it_valid =
- url_is_request_complete_and_extract_payload(s, &s[last_pos], w->header_parse_last_size, &w->payload);
- if(!is_it_valid) {
- if(w->header_parse_tries > HTTP_REQ_MAX_HEADER_FETCH_TRIES) {
- netdata_log_info("Disabling slow client after %zu attempts to read the request (%zu bytes received)", w->header_parse_tries, buffer_strlen(w->response.data));
- w->header_parse_tries = 0;
- w->header_parse_last_size = 0;
- web_client_disable_wait_receive(w);
- return HTTP_VALIDATION_TOO_MANY_READ_RETRIES;
- }
- return HTTP_VALIDATION_INCOMPLETE;
- }
- is_it_valid = 1;
- } else {
- last_pos = w->header_parse_last_size;
- is_it_valid =
- url_is_request_complete_and_extract_payload(s, &s[last_pos], w->header_parse_last_size, &w->payload);
- }
- s = web_client_valid_method(w, s);
- if (!s) {
- w->header_parse_tries = 0;
- w->header_parse_last_size = 0;
- web_client_disable_wait_receive(w);
- return HTTP_VALIDATION_NOT_SUPPORTED;
- } else if (!is_it_valid) {
- //Invalid request, we have more data after the end of message
- char *check = strstr((char *)buffer_tostring(w->response.data), "\r\n\r\n");
- if(check) {
- check += 4;
- if (*check) {
- w->header_parse_tries = 0;
- w->header_parse_last_size = 0;
- web_client_disable_wait_receive(w);
- return HTTP_VALIDATION_EXCESS_REQUEST_DATA;
- }
- }
- web_client_enable_wait_receive(w);
- return HTTP_VALIDATION_INCOMPLETE;
- }
- //After the method we have the path and query string together
- encoded_url = s;
- //we search for the position where we have " HTTP/", because it finishes the user request
- s = url_find_protocol(s);
- // incomplete requests
- if(unlikely(!*s)) {
- web_client_enable_wait_receive(w);
- return HTTP_VALIDATION_INCOMPLETE;
- }
- // we have the end of encoded_url - remember it
- char *ue = s;
- // make sure we have complete request
- // complete requests contain: \r\n\r\n
- while(*s) {
- // find a line feed
- while(*s && *s++ != '\r');
- // did we reach the end?
- if(unlikely(!*s)) break;
- // is it \r\n ?
- if(likely(*s++ == '\n')) {
- // is it again \r\n ? (header end)
- if(unlikely(*s == '\r' && s[1] == '\n')) {
- // a valid complete HTTP request found
- char c = *ue;
- *ue = '\0';
- web_client_decode_path_and_query_string(w, encoded_url);
- *ue = c;
- #ifdef ENABLE_HTTPS
- if ( (web_client_check_conn_tcp(w)) && (netdata_ssl_web_server_ctx) ) {
- if (!w->ssl.conn && (http_is_using_ssl_force(w) || http_is_using_ssl_default(w)) && (w->mode != HTTP_REQUEST_MODE_STREAM)) {
- w->header_parse_tries = 0;
- w->header_parse_last_size = 0;
- web_client_disable_wait_receive(w);
- return HTTP_VALIDATION_REDIRECT;
- }
- }
- #endif
- w->header_parse_tries = 0;
- w->header_parse_last_size = 0;
- web_client_disable_wait_receive(w);
- return HTTP_VALIDATION_OK;
- }
- // another header line
- s = http_header_parse_line(w, s);
- }
- }
- // incomplete request
- web_client_enable_wait_receive(w);
- return HTTP_VALIDATION_INCOMPLETE;
- }
- static inline ssize_t web_client_send_data(struct web_client *w,const void *buf,size_t len, int flags)
- {
- ssize_t bytes;
- #ifdef ENABLE_HTTPS
- if ((web_client_check_conn_tcp(w)) && (netdata_ssl_web_server_ctx)) {
- if (SSL_connection(&w->ssl)) {
- bytes = netdata_ssl_write(&w->ssl, buf, len) ;
- web_client_enable_wait_from_ssl(w);
- }
- else
- bytes = send(w->ofd,buf, len , flags);
- }
- else if(web_client_check_conn_tcp(w) || web_client_check_conn_unix(w))
- bytes = send(w->ofd,buf, len , flags);
- else
- bytes = -999;
- #else
- if(web_client_check_conn_tcp(w) || web_client_check_conn_unix(w))
- bytes = send(w->ofd, buf, len, flags);
- else
- bytes = -999;
- #endif
- return bytes;
- }
- void web_client_build_http_header(struct web_client *w) {
- if(unlikely(w->response.code != HTTP_RESP_OK))
- buffer_no_cacheable(w->response.data);
- if(unlikely(!w->response.data->date))
- w->response.data->date = now_realtime_sec();
- // set a proper expiration date, if not already set
- if(unlikely(!w->response.data->expires))
- w->response.data->expires = w->response.data->date +
- ((w->response.data->options & WB_CONTENT_NO_CACHEABLE) ? 0 : 86400);
- // prepare the HTTP response header
- netdata_log_debug(D_WEB_CLIENT, "%llu: Generating HTTP header with response %d.", w->id, w->response.code);
- const char *code_msg = http_response_code2string(w->response.code);
- // prepare the last modified and expiration dates
- char rfc7231_date[RFC7231_MAX_LENGTH], rfc7231_expires[RFC7231_MAX_LENGTH];
- rfc7231_datetime(rfc7231_date, sizeof(rfc7231_date), w->response.data->date);
- rfc7231_datetime(rfc7231_expires, sizeof(rfc7231_expires), w->response.data->expires);
- if (w->response.code == HTTP_RESP_HTTPS_UPGRADE) {
- buffer_sprintf(w->response.header_output,
- "HTTP/1.1 %d %s\r\n"
- "Location: https://%s%s\r\n",
- w->response.code, code_msg,
- w->server_host ? w->server_host : "",
- buffer_tostring(w->url_as_received));
- w->response.code = HTTP_RESP_MOVED_PERM;
- }
- else {
- buffer_sprintf(w->response.header_output,
- "HTTP/1.1 %d %s\r\n"
- "Connection: %s\r\n"
- "Server: Netdata Embedded HTTP Server %s\r\n"
- "Access-Control-Allow-Origin: %s\r\n"
- "Access-Control-Allow-Credentials: true\r\n"
- "Date: %s\r\n",
- w->response.code,
- code_msg,
- web_client_has_keepalive(w)?"keep-alive":"close",
- VERSION,
- w->origin ? w->origin : "*",
- rfc7231_date);
- http_header_content_type(w->response.header_output, w->response.data->content_type);
- }
- if(unlikely(web_x_frame_options))
- buffer_sprintf(w->response.header_output, "X-Frame-Options: %s\r\n", web_x_frame_options);
- if(w->response.has_cookies) {
- if(respect_web_browser_do_not_track_policy)
- buffer_sprintf(w->response.header_output,
- "Tk: T;cookies\r\n");
- }
- else {
- if(respect_web_browser_do_not_track_policy) {
- if(web_client_has_tracking_required(w))
- buffer_sprintf(w->response.header_output,
- "Tk: T;cookies\r\n");
- else
- buffer_sprintf(w->response.header_output,
- "Tk: N\r\n");
- }
- }
- if(w->mode == HTTP_REQUEST_MODE_OPTIONS) {
- buffer_strcat(w->response.header_output,
- "Access-Control-Allow-Methods: GET, OPTIONS\r\n"
- "Access-Control-Allow-Headers: accept, x-requested-with, origin, content-type, cookie, pragma, cache-control, x-auth-token\r\n"
- "Access-Control-Max-Age: 1209600\r\n" // 86400 * 14
- );
- }
- else {
- buffer_sprintf(w->response.header_output,
- "Cache-Control: %s\r\n"
- "Expires: %s\r\n",
- (w->response.data->options & WB_CONTENT_NO_CACHEABLE)?"no-cache, no-store, must-revalidate\r\nPragma: no-cache":"public",
- rfc7231_expires);
- }
- // copy a possibly available custom header
- if(unlikely(buffer_strlen(w->response.header)))
- buffer_strcat(w->response.header_output, buffer_tostring(w->response.header));
- // headers related to the transfer method
- if(likely(w->response.zoutput))
- buffer_strcat(w->response.header_output, "Content-Encoding: gzip\r\n");
- if(likely(w->flags & WEB_CLIENT_CHUNKED_TRANSFER))
- buffer_strcat(w->response.header_output, "Transfer-Encoding: chunked\r\n");
- else {
- if(likely((w->response.data->len || w->response.rlen))) {
- // we know the content length, put it
- buffer_sprintf(w->response.header_output, "Content-Length: %zu\r\n", w->response.data->len? w->response.data->len: w->response.rlen);
- }
- else {
- // we don't know the content length, disable keep-alive
- web_client_disable_keepalive(w);
- }
- }
- char uuid[UUID_COMPACT_STR_LEN];
- uuid_unparse_lower_compact(w->transaction, uuid);
- buffer_sprintf(w->response.header_output,
- "X-Transaction-ID: %s\r\n", uuid);
- // end of HTTP header
- buffer_strcat(w->response.header_output, "\r\n");
- }
- static inline void web_client_send_http_header(struct web_client *w) {
- web_client_build_http_header(w);
- // sent the HTTP header
- netdata_log_debug(D_WEB_DATA, "%llu: Sending response HTTP header of size %zu: '%s'"
- , w->id
- , buffer_strlen(w->response.header_output)
- , buffer_tostring(w->response.header_output)
- );
- web_client_cork_socket(w);
- size_t count = 0;
- ssize_t bytes;
- #ifdef ENABLE_HTTPS
- if ( (web_client_check_conn_tcp(w)) && (netdata_ssl_web_server_ctx) ) {
- if (SSL_connection(&w->ssl)) {
- bytes = netdata_ssl_write(&w->ssl, buffer_tostring(w->response.header_output), buffer_strlen(w->response.header_output));
- web_client_enable_wait_from_ssl(w);
- }
- else {
- while((bytes = send(w->ofd, buffer_tostring(w->response.header_output), buffer_strlen(w->response.header_output), 0)) == -1) {
- count++;
- if(count > 100 || (errno != EAGAIN && errno != EWOULDBLOCK)) {
- netdata_log_error("Cannot send HTTP headers to web client.");
- break;
- }
- }
- }
- }
- else if(web_client_check_conn_tcp(w) || web_client_check_conn_unix(w)) {
- while((bytes = send(w->ofd, buffer_tostring(w->response.header_output), buffer_strlen(w->response.header_output), 0)) == -1) {
- count++;
- if(count > 100 || (errno != EAGAIN && errno != EWOULDBLOCK)) {
- netdata_log_error("Cannot send HTTP headers to web client.");
- break;
- }
- }
- }
- else
- bytes = -999;
- #else
- if(web_client_check_conn_tcp(w) || web_client_check_conn_unix(w)) {
- while ((bytes = send(w->ofd, buffer_tostring(w->response.header_output), buffer_strlen(w->response.header_output), 0)) == -1) {
- count++;
- if (count > 100 || (errno != EAGAIN && errno != EWOULDBLOCK)) {
- netdata_log_error("Cannot send HTTP headers to web client.");
- break;
- }
- }
- }
- else
- bytes = -999;
- #endif
- if(bytes != (ssize_t) buffer_strlen(w->response.header_output)) {
- if(bytes > 0)
- w->statistics.sent_bytes += bytes;
- if (bytes < 0) {
- netdata_log_error("HTTP headers failed to be sent (I sent %zu bytes but the system sent %zd bytes). Closing web client."
- , buffer_strlen(w->response.header_output)
- , bytes);
- WEB_CLIENT_IS_DEAD(w);
- return;
- }
- }
- else
- w->statistics.sent_bytes += bytes;
- }
- static inline int web_client_switch_host(RRDHOST *host, struct web_client *w, char *url, bool nodeid, int (*func)(RRDHOST *, struct web_client *, char *)) {
- static uint32_t hash_localhost = 0;
- if(unlikely(!hash_localhost)) {
- hash_localhost = simple_hash("localhost");
- }
- if(host != localhost) {
- buffer_flush(w->response.data);
- buffer_strcat(w->response.data, "Nesting of hosts is not allowed.");
- return HTTP_RESP_BAD_REQUEST;
- }
- char *tok = strsep_skip_consecutive_separators(&url, "/");
- if(tok && *tok) {
- netdata_log_debug(D_WEB_CLIENT, "%llu: Searching for host with name '%s'.", w->id, tok);
- if(nodeid) {
- host = find_host_by_node_id(tok);
- if(!host) {
- host = rrdhost_find_by_hostname(tok);
- if (!host)
- host = rrdhost_find_by_guid(tok);
- }
- }
- else {
- host = rrdhost_find_by_hostname(tok);
- if(!host) {
- host = rrdhost_find_by_guid(tok);
- if (!host)
- host = find_host_by_node_id(tok);
- }
- }
- if(!host) {
- // we didn't find it, but it may be a uuid case mismatch for MACHINE_GUID
- // so, recreate the machine guid in lower-case.
- uuid_t uuid;
- char txt[UUID_STR_LEN];
- if (uuid_parse(tok, uuid) == 0) {
- uuid_unparse_lower(uuid, txt);
- host = rrdhost_find_by_guid(txt);
- }
- }
- if (host) {
- if(!url)
- //no delim found
- return append_slash_to_url_and_redirect(w);
- size_t len = strlen(url) + 2;
- char buf[len];
- buf[0] = '/';
- strcpy(&buf[1], url);
- buf[len - 1] = '\0';
- buffer_flush(w->url_path_decoded);
- buffer_strcat(w->url_path_decoded, buf);
- return func(host, w, buf);
- }
- }
- buffer_flush(w->response.data);
- w->response.data->content_type = CT_TEXT_HTML;
- buffer_strcat(w->response.data, "This netdata does not maintain a database for host: ");
- buffer_strcat_htmlescape(w->response.data, tok?tok:"");
- return HTTP_RESP_NOT_FOUND;
- }
- int web_client_api_request_with_node_selection(RRDHOST *host, struct web_client *w, char *decoded_url_path) {
- // entry point for all API requests
- ND_LOG_STACK lgs[] = {
- ND_LOG_FIELD_TXT(NDF_REQUEST_METHOD, http_request_method2string(w->mode)),
- ND_LOG_FIELD_BFR(NDF_REQUEST, w->url_as_received),
- ND_LOG_FIELD_U64(NDF_CONNECTION_ID, w->id),
- ND_LOG_FIELD_UUID(NDF_TRANSACTION_ID, &w->transaction),
- ND_LOG_FIELD_UUID(NDF_ACCOUNT_ID, &w->auth.cloud_account_id),
- ND_LOG_FIELD_TXT(NDF_USER_NAME, w->auth.client_name),
- ND_LOG_FIELD_TXT(NDF_USER_ROLE, http_id2access(w->access)),
- ND_LOG_FIELD_END(),
- };
- ND_LOG_STACK_PUSH(lgs);
- // give a new transaction id to the request
- if(uuid_is_null(w->transaction))
- uuid_generate_random(w->transaction);
- static uint32_t
- hash_api = 0,
- hash_host = 0,
- hash_node = 0;
- if(unlikely(!hash_api)) {
- hash_api = simple_hash("api");
- hash_host = simple_hash("host");
- hash_node = simple_hash("node");
- }
- char *tok = strsep_skip_consecutive_separators(&decoded_url_path, "/?");
- if(likely(tok && *tok)) {
- uint32_t hash = simple_hash(tok);
- if(unlikely(hash == hash_api && strcmp(tok, "api") == 0)) {
- // current API
- netdata_log_debug(D_WEB_CLIENT_ACCESS, "%llu: API request ...", w->id);
- return check_host_and_call(host, w, decoded_url_path, web_client_api_request);
- }
- else if(unlikely((hash == hash_host && strcmp(tok, "host") == 0) || (hash == hash_node && strcmp(tok, "node") == 0))) {
- // host switching
- netdata_log_debug(D_WEB_CLIENT_ACCESS, "%llu: host switch request ...", w->id);
- return web_client_switch_host(host, w, decoded_url_path, hash == hash_node, web_client_api_request_with_node_selection);
- }
- }
- buffer_flush(w->response.data);
- buffer_strcat(w->response.data, "Unknown API endpoint.");
- w->response.data->content_type = CT_TEXT_HTML;
- return HTTP_RESP_NOT_FOUND;
- }
- static inline int web_client_process_url(RRDHOST *host, struct web_client *w, char *decoded_url_path) {
- if(unlikely(!service_running(ABILITY_WEB_REQUESTS)))
- return web_client_permission_denied(w);
- static uint32_t
- hash_api = 0,
- hash_netdata_conf = 0,
- hash_host = 0,
- hash_node = 0,
- hash_v0 = 0,
- hash_v1 = 0,
- hash_v2 = 0;
- #ifdef NETDATA_INTERNAL_CHECKS
- static uint32_t hash_exit = 0, hash_debug = 0, hash_mirror = 0;
- #endif
- if(unlikely(!hash_api)) {
- hash_api = simple_hash("api");
- hash_netdata_conf = simple_hash("netdata.conf");
- hash_host = simple_hash("host");
- hash_node = simple_hash("node");
- hash_v0 = simple_hash("v0");
- hash_v1 = simple_hash("v1");
- hash_v2 = simple_hash("v2");
- #ifdef NETDATA_INTERNAL_CHECKS
- hash_exit = simple_hash("exit");
- hash_debug = simple_hash("debug");
- hash_mirror = simple_hash("mirror");
- #endif
- }
- // keep a copy of the decoded path, in case we need to serve it as a filename
- char filename[FILENAME_MAX + 1];
- strncpyz(filename, decoded_url_path ? decoded_url_path : "", FILENAME_MAX);
- char *tok = strsep_skip_consecutive_separators(&decoded_url_path, "/?");
- if(likely(tok && *tok)) {
- uint32_t hash = simple_hash(tok);
- netdata_log_debug(D_WEB_CLIENT, "%llu: Processing command '%s'.", w->id, tok);
- if(likely(hash == hash_api && strcmp(tok, "api") == 0)) { // current API
- netdata_log_debug(D_WEB_CLIENT_ACCESS, "%llu: API request ...", w->id);
- return check_host_and_call(host, w, decoded_url_path, web_client_api_request);
- }
- else if(unlikely((hash == hash_host && strcmp(tok, "host") == 0) || (hash == hash_node && strcmp(tok, "node") == 0))) { // host switching
- netdata_log_debug(D_WEB_CLIENT_ACCESS, "%llu: host switch request ...", w->id);
- return web_client_switch_host(host, w, decoded_url_path, hash == hash_node, web_client_process_url);
- }
- else if(unlikely(hash == hash_v2 && strcmp(tok, "v2") == 0)) {
- if(web_client_flag_check(w, WEB_CLIENT_FLAG_PATH_WITH_VERSION))
- return bad_request_multiple_dashboard_versions(w);
- web_client_flag_set(w, WEB_CLIENT_FLAG_PATH_IS_V2);
- return web_client_process_url(host, w, decoded_url_path);
- }
- else if(unlikely(hash == hash_v1 && strcmp(tok, "v1") == 0)) {
- if(web_client_flag_check(w, WEB_CLIENT_FLAG_PATH_WITH_VERSION))
- return bad_request_multiple_dashboard_versions(w);
- web_client_flag_set(w, WEB_CLIENT_FLAG_PATH_IS_V1);
- return web_client_process_url(host, w, decoded_url_path);
- }
- else if(unlikely(hash == hash_v0 && strcmp(tok, "v0") == 0)) {
- if(web_client_flag_check(w, WEB_CLIENT_FLAG_PATH_WITH_VERSION))
- return bad_request_multiple_dashboard_versions(w);
- web_client_flag_set(w, WEB_CLIENT_FLAG_PATH_IS_V0);
- return web_client_process_url(host, w, decoded_url_path);
- }
- else if(unlikely(hash == hash_netdata_conf && strcmp(tok, "netdata.conf") == 0)) { // netdata.conf
- if(unlikely(!http_can_access_netdataconf(w)))
- return web_client_permission_denied(w);
- netdata_log_debug(D_WEB_CLIENT_ACCESS, "%llu: generating netdata.conf ...", w->id);
- w->response.data->content_type = CT_TEXT_PLAIN;
- buffer_flush(w->response.data);
- config_generate(w->response.data, 0);
- return HTTP_RESP_OK;
- }
- #ifdef NETDATA_INTERNAL_CHECKS
- else if(unlikely(hash == hash_exit && strcmp(tok, "exit") == 0)) {
- if(unlikely(!http_can_access_netdataconf(w)))
- return web_client_permission_denied(w);
- w->response.data->content_type = CT_TEXT_PLAIN;
- buffer_flush(w->response.data);
- if(!netdata_exit)
- buffer_strcat(w->response.data, "ok, will do...");
- else
- buffer_strcat(w->response.data, "I am doing it already");
- netdata_log_error("web request to exit received.");
- netdata_cleanup_and_exit(0, NULL, NULL, NULL);
- return HTTP_RESP_OK;
- }
- else if(unlikely(hash == hash_debug && strcmp(tok, "debug") == 0)) {
- if(unlikely(!http_can_access_netdataconf(w)))
- return web_client_permission_denied(w);
- buffer_flush(w->response.data);
- // get the name of the data to show
- tok = strsep_skip_consecutive_separators(&decoded_url_path, "&");
- if(tok && *tok) {
- netdata_log_debug(D_WEB_CLIENT, "%llu: Searching for RRD data with name '%s'.", w->id, tok);
- // do we have such a data set?
- RRDSET *st = rrdset_find_byname(host, tok);
- if(!st) st = rrdset_find(host, tok);
- if(!st) {
- w->response.data->content_type = CT_TEXT_HTML;
- buffer_strcat(w->response.data, "Chart is not found: ");
- buffer_strcat_htmlescape(w->response.data, tok);
- netdata_log_debug(D_WEB_CLIENT_ACCESS, "%llu: %s is not found.", w->id, tok);
- return HTTP_RESP_NOT_FOUND;
- }
- debug_flags |= D_RRD_STATS;
- if(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))
- rrdset_flag_clear(st, RRDSET_FLAG_DEBUG);
- else
- rrdset_flag_set(st, RRDSET_FLAG_DEBUG);
- w->response.data->content_type = CT_TEXT_HTML;
- buffer_sprintf(w->response.data, "Chart has now debug %s: ", rrdset_flag_check(st, RRDSET_FLAG_DEBUG)?"enabled":"disabled");
- buffer_strcat_htmlescape(w->response.data, tok);
- netdata_log_debug(D_WEB_CLIENT_ACCESS, "%llu: debug for %s is %s.", w->id, tok, rrdset_flag_check(st, RRDSET_FLAG_DEBUG)?"enabled":"disabled");
- return HTTP_RESP_OK;
- }
- buffer_flush(w->response.data);
- buffer_strcat(w->response.data, "debug which chart?\r\n");
- return HTTP_RESP_BAD_REQUEST;
- }
- else if(unlikely(hash == hash_mirror && strcmp(tok, "mirror") == 0)) {
- if(unlikely(!http_can_access_netdataconf(w)))
- return web_client_permission_denied(w);
- netdata_log_debug(D_WEB_CLIENT_ACCESS, "%llu: Mirroring...", w->id);
- // replace the zero bytes with spaces
- buffer_char_replace(w->response.data, '\0', ' ');
- // just leave the buffer as-is
- // it will be copied back to the client
- return HTTP_RESP_OK;
- }
- #endif /* NETDATA_INTERNAL_CHECKS */
- }
- buffer_flush(w->response.data);
- return mysendfile(w, filename);
- }
- static bool web_server_log_transport(BUFFER *wb, void *ptr) {
- struct web_client *w = ptr;
- if(!w)
- return false;
- #ifdef ENABLE_HTTPS
- buffer_strcat(wb, SSL_connection(&w->ssl) ? "https" : "http");
- #else
- buffer_strcat(wb, "http");
- #endif
- return true;
- }
- void web_client_process_request_from_web_server(struct web_client *w) {
- // entry point for web server requests
- ND_LOG_STACK lgs[] = {
- ND_LOG_FIELD_CB(NDF_SRC_TRANSPORT, web_server_log_transport, w),
- ND_LOG_FIELD_TXT(NDF_SRC_IP, w->client_ip),
- ND_LOG_FIELD_TXT(NDF_SRC_PORT, w->client_port),
- ND_LOG_FIELD_TXT(NDF_SRC_FORWARDED_HOST, w->forwarded_host),
- ND_LOG_FIELD_TXT(NDF_SRC_FORWARDED_FOR, w->forwarded_for),
- ND_LOG_FIELD_TXT(NDF_NIDL_NODE, w->client_host),
- ND_LOG_FIELD_TXT(NDF_REQUEST_METHOD, http_request_method2string(w->mode)),
- ND_LOG_FIELD_BFR(NDF_REQUEST, w->url_as_received),
- ND_LOG_FIELD_U64(NDF_CONNECTION_ID, w->id),
- ND_LOG_FIELD_UUID(NDF_TRANSACTION_ID, &w->transaction),
- ND_LOG_FIELD_UUID(NDF_ACCOUNT_ID, &w->auth.cloud_account_id),
- ND_LOG_FIELD_TXT(NDF_USER_NAME, w->auth.client_name),
- ND_LOG_FIELD_TXT(NDF_USER_ROLE, http_id2access(w->access)),
- ND_LOG_FIELD_END(),
- };
- ND_LOG_STACK_PUSH(lgs);
- // give a new transaction id to the request
- if(uuid_is_null(w->transaction))
- uuid_generate_random(w->transaction);
- // start timing us
- web_client_timeout_checkpoint_init(w);
- switch(http_request_validate(w)) {
- case HTTP_VALIDATION_OK:
- if(!web_client_flag_check(w, WEB_CLIENT_FLAG_PROGRESS_TRACKING)) {
- web_client_flag_set(w, WEB_CLIENT_FLAG_PROGRESS_TRACKING);
- query_progress_start_or_update(&w->transaction, 0, w->mode, w->acl,
- buffer_tostring(w->url_as_received),
- w->payload,
- w->forwarded_for ? w->forwarded_for : w->client_ip);
- }
- switch(w->mode) {
- case HTTP_REQUEST_MODE_STREAM:
- if(unlikely(!http_can_access_stream(w))) {
- web_client_permission_denied(w);
- return;
- }
- w->response.code = rrdpush_receiver_thread_spawn(w, (char *)buffer_tostring(w->url_query_string_decoded), NULL);
- return;
- case HTTP_REQUEST_MODE_OPTIONS:
- if(unlikely(
- !http_can_access_dashboard(w) &&
- !http_can_access_registry(w) &&
- !http_can_access_badges(w) &&
- !http_can_access_mgmt(w) &&
- !http_can_access_netdataconf(w)
- )) {
- web_client_permission_denied(w);
- break;
- }
- w->response.data->content_type = CT_TEXT_PLAIN;
- buffer_flush(w->response.data);
- buffer_strcat(w->response.data, "OK");
- w->response.code = HTTP_RESP_OK;
- break;
- case HTTP_REQUEST_MODE_FILECOPY:
- case HTTP_REQUEST_MODE_POST:
- case HTTP_REQUEST_MODE_GET:
- case HTTP_REQUEST_MODE_PUT:
- case HTTP_REQUEST_MODE_DELETE:
- if(unlikely(
- !http_can_access_dashboard(w) &&
- !http_can_access_registry(w) &&
- !http_can_access_badges(w) &&
- !http_can_access_mgmt(w) &&
- !http_can_access_netdataconf(w)
- )) {
- web_client_permission_denied(w);
- break;
- }
- web_client_reset_path_flags(w);
- // find if the URL path has a filename extension
- char path[FILENAME_MAX + 1];
- strncpyz(path, buffer_tostring(w->url_path_decoded), FILENAME_MAX);
- char *s = path, *e = path;
- // remove the query string and find the last char
- for (; *e ; e++) {
- if (*e == '?')
- break;
- }
- if(e == s || (*(e - 1) == '/'))
- web_client_flag_set(w, WEB_CLIENT_FLAG_PATH_HAS_TRAILING_SLASH);
- // check if there is a filename extension
- while (--e > s) {
- if (*e == '/')
- break;
- if(*e == '.') {
- web_client_flag_set(w, WEB_CLIENT_FLAG_PATH_HAS_FILE_EXTENSION);
- break;
- }
- }
- w->response.code = (short)web_client_process_url(localhost, w, path);
- break;
- default:
- web_client_permission_denied(w);
- return;
- }
- break;
- case HTTP_VALIDATION_INCOMPLETE:
- if(w->response.data->len > NETDATA_WEB_REQUEST_MAX_SIZE) {
- buffer_flush(w->url_as_received);
- buffer_strcat(w->url_as_received, "too big request");
- netdata_log_debug(D_WEB_CLIENT_ACCESS, "%llu: Received request is too big (%zu bytes).", w->id, w->response.data->len);
- size_t len = w->response.data->len;
- buffer_flush(w->response.data);
- buffer_sprintf(w->response.data, "Received request is too big (received %zu bytes, max is %zu bytes).\r\n", len, (size_t)NETDATA_WEB_REQUEST_MAX_SIZE);
- w->response.code = HTTP_RESP_BAD_REQUEST;
- }
- else {
- // wait for more data
- // set to normal to prevent web_server_rcv_callback
- // from going into stream mode
- if (w->mode == HTTP_REQUEST_MODE_STREAM)
- w->mode = HTTP_REQUEST_MODE_GET;
- return;
- }
- break;
- #ifdef ENABLE_HTTPS
- case HTTP_VALIDATION_REDIRECT:
- {
- buffer_flush(w->response.data);
- w->response.data->content_type = CT_TEXT_HTML;
- 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 + window.location.search\">"
- "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 + window.location.search\">here</a>."
- "</body></html>");
- w->response.code = HTTP_RESP_HTTPS_UPGRADE;
- break;
- }
- #endif
- case HTTP_VALIDATION_MALFORMED_URL:
- netdata_log_debug(D_WEB_CLIENT_ACCESS, "%llu: Malformed URL '%s'.", w->id, w->response.data->buffer);
- buffer_flush(w->response.data);
- buffer_strcat(w->response.data, "Malformed URL...\r\n");
- w->response.code = HTTP_RESP_BAD_REQUEST;
- break;
- case HTTP_VALIDATION_EXCESS_REQUEST_DATA:
- netdata_log_debug(D_WEB_CLIENT_ACCESS, "%llu: Excess data in request '%s'.", w->id, w->response.data->buffer);
- buffer_flush(w->response.data);
- buffer_strcat(w->response.data, "Excess data in request.\r\n");
- w->response.code = HTTP_RESP_BAD_REQUEST;
- break;
- case HTTP_VALIDATION_TOO_MANY_READ_RETRIES:
- netdata_log_debug(D_WEB_CLIENT_ACCESS, "%llu: Too many retries to read request '%s'.", w->id, w->response.data->buffer);
- buffer_flush(w->response.data);
- buffer_strcat(w->response.data, "Too many retries to read request.\r\n");
- w->response.code = HTTP_RESP_BAD_REQUEST;
- break;
- case HTTP_VALIDATION_NOT_SUPPORTED:
- netdata_log_debug(D_WEB_CLIENT_ACCESS, "%llu: HTTP method requested is not supported '%s'.", w->id, w->response.data->buffer);
- buffer_flush(w->response.data);
- buffer_strcat(w->response.data, "HTTP method requested is not supported...\r\n");
- w->response.code = HTTP_RESP_BAD_REQUEST;
- break;
- }
- // keep track of the processing time
- web_client_timeout_checkpoint_response_ready(w, NULL);
- w->response.sent = 0;
- web_client_send_http_header(w);
- // enable sending immediately if we have data
- if(w->response.data->len) web_client_enable_wait_send(w);
- else web_client_disable_wait_send(w);
- switch(w->mode) {
- case HTTP_REQUEST_MODE_STREAM:
- netdata_log_debug(D_WEB_CLIENT, "%llu: STREAM done.", w->id);
- break;
- case HTTP_REQUEST_MODE_OPTIONS:
- netdata_log_debug(D_WEB_CLIENT, "%llu: Done preparing the OPTIONS response. Sending data (%zu bytes) to client.", w->id, w->response.data->len);
- break;
- case HTTP_REQUEST_MODE_POST:
- case HTTP_REQUEST_MODE_GET:
- case HTTP_REQUEST_MODE_PUT:
- case HTTP_REQUEST_MODE_DELETE:
- netdata_log_debug(D_WEB_CLIENT, "%llu: Done preparing the response. Sending data (%zu bytes) to client.", w->id, w->response.data->len);
- break;
- case HTTP_REQUEST_MODE_FILECOPY:
- if(w->response.rlen) {
- netdata_log_debug(D_WEB_CLIENT, "%llu: Done preparing the response. Will be sending data file of %zu bytes to client.", w->id, w->response.rlen);
- web_client_enable_wait_receive(w);
- /*
- // utilize the kernel sendfile() for copying the file to the socket.
- // this block of code can be commented, without anything missing.
- // when it is commented, the program will copy the data using async I/O.
- {
- long len = sendfile(w->ofd, w->ifd, NULL, w->response.data->rbytes);
- if(len != w->response.data->rbytes)
- netdata_log_error("%llu: sendfile() should copy %ld bytes, but copied %ld. Falling back to manual copy.", w->id, w->response.data->rbytes, len);
- else
- web_client_request_done(w);
- }
- */
- }
- else
- netdata_log_debug(D_WEB_CLIENT, "%llu: Done preparing the response. Will be sending an unknown amount of bytes to client.", w->id);
- break;
- default:
- fatal("%llu: Unknown client mode %u.", w->id, w->mode);
- break;
- }
- }
- ssize_t web_client_send_chunk_header(struct web_client *w, size_t len)
- {
- netdata_log_debug(D_DEFLATE, "%llu: OPEN CHUNK of %zu bytes (hex: %zx).", w->id, len, len);
- char buf[24];
- ssize_t bytes;
- bytes = (ssize_t)sprintf(buf, "%zX\r\n", len);
- buf[bytes] = 0x00;
- bytes = web_client_send_data(w,buf,strlen(buf),0);
- if(bytes > 0) {
- netdata_log_debug(D_DEFLATE, "%llu: Sent chunk header %zd bytes.", w->id, bytes);
- w->statistics.sent_bytes += bytes;
- }
- else if(bytes == 0) {
- netdata_log_debug(D_WEB_CLIENT, "%llu: Did not send chunk header to the client.", w->id);
- }
- else {
- netdata_log_debug(D_WEB_CLIENT, "%llu: Failed to send chunk header to client.", w->id);
- WEB_CLIENT_IS_DEAD(w);
- }
- return bytes;
- }
- ssize_t web_client_send_chunk_close(struct web_client *w)
- {
- //debug(D_DEFLATE, "%llu: CLOSE CHUNK.", w->id);
- ssize_t bytes;
- bytes = web_client_send_data(w,"\r\n",2,0);
- if(bytes > 0) {
- netdata_log_debug(D_DEFLATE, "%llu: Sent chunk suffix %zd bytes.", w->id, bytes);
- w->statistics.sent_bytes += bytes;
- }
- else if(bytes == 0) {
- netdata_log_debug(D_WEB_CLIENT, "%llu: Did not send chunk suffix to the client.", w->id);
- }
- else {
- netdata_log_debug(D_WEB_CLIENT, "%llu: Failed to send chunk suffix to client.", w->id);
- WEB_CLIENT_IS_DEAD(w);
- }
- return bytes;
- }
- ssize_t web_client_send_chunk_finalize(struct web_client *w)
- {
- //debug(D_DEFLATE, "%llu: FINALIZE CHUNK.", w->id);
- ssize_t bytes;
- bytes = web_client_send_data(w,"\r\n0\r\n\r\n",7,0);
- if(bytes > 0) {
- netdata_log_debug(D_DEFLATE, "%llu: Sent chunk suffix %zd bytes.", w->id, bytes);
- w->statistics.sent_bytes += bytes;
- }
- else if(bytes == 0) {
- netdata_log_debug(D_WEB_CLIENT, "%llu: Did not send chunk finalize suffix to the client.", w->id);
- }
- else {
- netdata_log_debug(D_WEB_CLIENT, "%llu: Failed to send chunk finalize suffix to client.", w->id);
- WEB_CLIENT_IS_DEAD(w);
- }
- return bytes;
- }
- ssize_t web_client_send_deflate(struct web_client *w)
- {
- ssize_t len = 0, t = 0;
- // when using compression,
- // w->response.sent is the amount of bytes passed through compression
- netdata_log_debug(D_DEFLATE, "%llu: web_client_send_deflate(): w->response.data->len = %zu, w->response.sent = %zu, w->response.zhave = %zu, w->response.zsent = %zu, w->response.zstream.avail_in = %u, w->response.zstream.avail_out = %u, w->response.zstream.total_in = %lu, w->response.zstream.total_out = %lu.",
- w->id, w->response.data->len, w->response.sent, w->response.zhave, w->response.zsent, w->response.zstream.avail_in, w->response.zstream.avail_out, w->response.zstream.total_in, w->response.zstream.total_out);
- if(w->response.data->len - w->response.sent == 0 && w->response.zstream.avail_in == 0 && w->response.zhave == w->response.zsent && w->response.zstream.avail_out != 0) {
- // there is nothing to send
- netdata_log_debug(D_WEB_CLIENT, "%llu: Out of output data.", w->id);
- // finalize the chunk
- if(w->response.sent != 0) {
- t = web_client_send_chunk_finalize(w);
- if(t < 0) return t;
- }
- if(w->mode == HTTP_REQUEST_MODE_FILECOPY && web_client_has_wait_receive(w) && w->response.rlen && w->response.rlen > w->response.data->len) {
- // we have to wait, more data will come
- netdata_log_debug(D_WEB_CLIENT, "%llu: Waiting for more data to become available.", w->id);
- web_client_disable_wait_send(w);
- return t;
- }
- if(unlikely(!web_client_has_keepalive(w))) {
- netdata_log_debug(D_WEB_CLIENT, "%llu: Closing (keep-alive is not enabled). %zu bytes sent.", w->id, w->response.sent);
- WEB_CLIENT_IS_DEAD(w);
- return t;
- }
- // reset the client
- web_client_request_done(w);
- netdata_log_debug(D_WEB_CLIENT, "%llu: Done sending all data on socket.", w->id);
- return t;
- }
- if(w->response.zhave == w->response.zsent) {
- // compress more input data
- // close the previous open chunk
- if(w->response.sent != 0) {
- t = web_client_send_chunk_close(w);
- if(t < 0) return t;
- }
- netdata_log_debug(D_DEFLATE, "%llu: Compressing %zu new bytes starting from %zu (and %u left behind).", w->id, (w->response.data->len - w->response.sent), w->response.sent, w->response.zstream.avail_in);
- // give the compressor all the data not passed through the compressor yet
- if(w->response.data->len > w->response.sent) {
- w->response.zstream.next_in = (Bytef *)&w->response.data->buffer[w->response.sent - w->response.zstream.avail_in];
- w->response.zstream.avail_in += (uInt) (w->response.data->len - w->response.sent);
- }
- // reset the compressor output buffer
- w->response.zstream.next_out = w->response.zbuffer;
- w->response.zstream.avail_out = NETDATA_WEB_RESPONSE_ZLIB_CHUNK_SIZE;
- // ask for FINISH if we have all the input
- int flush = Z_SYNC_FLUSH;
- if((w->mode == HTTP_REQUEST_MODE_GET || w->mode == HTTP_REQUEST_MODE_POST || w->mode == HTTP_REQUEST_MODE_PUT || w->mode == HTTP_REQUEST_MODE_DELETE)
- || (w->mode == HTTP_REQUEST_MODE_FILECOPY && !web_client_has_wait_receive(w) && w->response.data->len == w->response.rlen)) {
- flush = Z_FINISH;
- netdata_log_debug(D_DEFLATE, "%llu: Requesting Z_FINISH, if possible.", w->id);
- }
- else {
- netdata_log_debug(D_DEFLATE, "%llu: Requesting Z_SYNC_FLUSH.", w->id);
- }
- // compress
- if(deflate(&w->response.zstream, flush) == Z_STREAM_ERROR) {
- netdata_log_error("%llu: Compression failed. Closing down client.", w->id);
- web_client_request_done(w);
- return(-1);
- }
- w->response.zhave = NETDATA_WEB_RESPONSE_ZLIB_CHUNK_SIZE - w->response.zstream.avail_out;
- w->response.zsent = 0;
- // keep track of the bytes passed through the compressor
- w->response.sent = w->response.data->len;
- netdata_log_debug(D_DEFLATE, "%llu: Compression produced %zu bytes.", w->id, w->response.zhave);
- // open a new chunk
- ssize_t t2 = web_client_send_chunk_header(w, w->response.zhave);
- if(t2 < 0) return t2;
- t += t2;
- }
- netdata_log_debug(D_WEB_CLIENT, "%llu: Sending %zu bytes of data (+%zd of chunk header).", w->id, w->response.zhave - w->response.zsent, t);
- len = web_client_send_data(w,&w->response.zbuffer[w->response.zsent], (size_t) (w->response.zhave - w->response.zsent), MSG_DONTWAIT);
- if(len > 0) {
- w->statistics.sent_bytes += len;
- w->response.zsent += len;
- len += t;
- netdata_log_debug(D_WEB_CLIENT, "%llu: Sent %zd bytes.", w->id, len);
- }
- else if(len == 0) {
- netdata_log_debug(D_WEB_CLIENT, "%llu: Did not send any bytes to the client (zhave = %zu, zsent = %zu, need to send = %zu).",
- w->id, w->response.zhave, w->response.zsent, w->response.zhave - w->response.zsent);
- }
- else {
- netdata_log_debug(D_WEB_CLIENT, "%llu: Failed to send data to client.", w->id);
- WEB_CLIENT_IS_DEAD(w);
- }
- return(len);
- }
- ssize_t web_client_send(struct web_client *w) {
- if(likely(w->response.zoutput)) return web_client_send_deflate(w);
- ssize_t bytes;
- if(unlikely(w->response.data->len - w->response.sent == 0)) {
- // there is nothing to send
- netdata_log_debug(D_WEB_CLIENT, "%llu: Out of output data.", w->id);
- // there can be two cases for this
- // A. we have done everything
- // B. we temporarily have nothing to send, waiting for the buffer to be filled by ifd
- if(w->mode == HTTP_REQUEST_MODE_FILECOPY && web_client_has_wait_receive(w) && w->response.rlen && w->response.rlen > w->response.data->len) {
- // we have to wait, more data will come
- netdata_log_debug(D_WEB_CLIENT, "%llu: Waiting for more data to become available.", w->id);
- web_client_disable_wait_send(w);
- return 0;
- }
- if(unlikely(!web_client_has_keepalive(w))) {
- netdata_log_debug(D_WEB_CLIENT, "%llu: Closing (keep-alive is not enabled). %zu bytes sent.", w->id, w->response.sent);
- WEB_CLIENT_IS_DEAD(w);
- return 0;
- }
- web_client_request_done(w);
- netdata_log_debug(D_WEB_CLIENT, "%llu: Done sending all data on socket. Waiting for next request on the same socket.", w->id);
- return 0;
- }
- bytes = web_client_send_data(w,&w->response.data->buffer[w->response.sent], w->response.data->len - w->response.sent, MSG_DONTWAIT);
- if(likely(bytes > 0)) {
- w->statistics.sent_bytes += bytes;
- w->response.sent += bytes;
- netdata_log_debug(D_WEB_CLIENT, "%llu: Sent %zd bytes.", w->id, bytes);
- }
- else if(likely(bytes == 0)) {
- netdata_log_debug(D_WEB_CLIENT, "%llu: Did not send any bytes to the client.", w->id);
- }
- else {
- netdata_log_debug(D_WEB_CLIENT, "%llu: Failed to send data to client.", w->id);
- WEB_CLIENT_IS_DEAD(w);
- }
- return(bytes);
- }
- ssize_t web_client_read_file(struct web_client *w)
- {
- if(unlikely(w->response.rlen > w->response.data->size))
- buffer_need_bytes(w->response.data, w->response.rlen - w->response.data->size);
- if(unlikely(w->response.rlen <= w->response.data->len))
- return 0;
- ssize_t left = (ssize_t)(w->response.rlen - w->response.data->len);
- ssize_t bytes = read(w->ifd, &w->response.data->buffer[w->response.data->len], (size_t)left);
- if(likely(bytes > 0)) {
- size_t old = w->response.data->len;
- (void)old;
- w->response.data->len += bytes;
- w->response.data->buffer[w->response.data->len] = '\0';
- netdata_log_debug(D_WEB_CLIENT, "%llu: Read %zd bytes.", w->id, bytes);
- netdata_log_debug(D_WEB_DATA, "%llu: Read data: '%s'.", w->id, &w->response.data->buffer[old]);
- web_client_enable_wait_send(w);
- if(w->response.rlen && w->response.data->len >= w->response.rlen)
- web_client_disable_wait_receive(w);
- }
- else if(likely(bytes == 0)) {
- netdata_log_debug(D_WEB_CLIENT, "%llu: Out of input file data.", w->id);
- // if we cannot read, it means we have an error on input.
- // if however, we are copying a file from ifd to ofd, we should not return an error.
- // in this case, the error should be generated when the file has been sent to the client.
- // we are copying data from ifd to ofd
- // let it finish copying...
- web_client_disable_wait_receive(w);
- netdata_log_debug(D_WEB_CLIENT, "%llu: Read the whole file.", w->id);
- if(web_server_mode != WEB_SERVER_MODE_STATIC_THREADED) {
- if (w->ifd != w->ofd) close(w->ifd);
- }
- w->ifd = w->ofd;
- }
- else {
- netdata_log_debug(D_WEB_CLIENT, "%llu: read data failed.", w->id);
- WEB_CLIENT_IS_DEAD(w);
- }
- return(bytes);
- }
- ssize_t web_client_receive(struct web_client *w)
- {
- if(unlikely(w->mode == HTTP_REQUEST_MODE_FILECOPY))
- return web_client_read_file(w);
- ssize_t bytes;
- ssize_t left = (ssize_t)(w->response.data->size - w->response.data->len);
- // do we have any space for more data?
- buffer_need_bytes(w->response.data, NETDATA_WEB_REQUEST_INITIAL_SIZE);
- errno = 0;
- #ifdef ENABLE_HTTPS
- if ( (web_client_check_conn_tcp(w)) && (netdata_ssl_web_server_ctx) ) {
- if (SSL_connection(&w->ssl)) {
- bytes = netdata_ssl_read(&w->ssl, &w->response.data->buffer[w->response.data->len], (size_t) (left - 1));
- web_client_enable_wait_from_ssl(w);
- }
- else {
- bytes = recv(w->ifd, &w->response.data->buffer[w->response.data->len], (size_t) (left - 1), MSG_DONTWAIT);
- }
- }
- else if(web_client_check_conn_tcp(w) || web_client_check_conn_unix(w)) {
- bytes = recv(w->ifd, &w->response.data->buffer[w->response.data->len], (size_t) (left - 1), MSG_DONTWAIT);
- }
- else // other connection methods
- bytes = -1;
- #else
- if(web_client_check_conn_tcp(w) || web_client_check_conn_unix(w))
- bytes = recv(w->ifd, &w->response.data->buffer[w->response.data->len], (size_t) (left - 1), MSG_DONTWAIT);
- else
- bytes = -1;
- #endif
- if(likely(bytes > 0)) {
- w->statistics.received_bytes += bytes;
- size_t old = w->response.data->len;
- (void)old;
- w->response.data->len += bytes;
- w->response.data->buffer[w->response.data->len] = '\0';
- netdata_log_debug(D_WEB_CLIENT, "%llu: Received %zd bytes.", w->id, bytes);
- netdata_log_debug(D_WEB_DATA, "%llu: Received data: '%s'.", w->id, &w->response.data->buffer[old]);
- }
- else if(unlikely(bytes < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR))) {
- web_client_enable_wait_receive(w);
- return 0;
- }
- else if (bytes < 0) {
- netdata_log_debug(D_WEB_CLIENT, "%llu: receive data failed.", w->id);
- WEB_CLIENT_IS_DEAD(w);
- } else
- netdata_log_debug(D_WEB_CLIENT, "%llu: Received %zd bytes.", w->id, bytes);
- return(bytes);
- }
- void web_client_decode_path_and_query_string(struct web_client *w, const char *path_and_query_string) {
- char buffer[NETDATA_WEB_REQUEST_URL_SIZE + 2];
- buffer[0] = '\0';
- buffer_flush(w->url_path_decoded);
- buffer_flush(w->url_query_string_decoded);
- if(buffer_strlen(w->url_as_received) == 0)
- // do not overwrite this if it is already filled
- buffer_strcat(w->url_as_received, path_and_query_string);
- if(w->mode == HTTP_REQUEST_MODE_STREAM) {
- // in stream mode, there is no path
- url_decode_r(buffer, path_and_query_string, NETDATA_WEB_REQUEST_URL_SIZE + 1);
- buffer[NETDATA_WEB_REQUEST_URL_SIZE + 1] = '\0';
- buffer_strcat(w->url_query_string_decoded, buffer);
- }
- else {
- // in non-stream mode, there is a path
- // FIXME - the way this is implemented, query string params never accept the symbol &, not even encoded as %26
- // To support the symbol & in query string params, we need to turn the url_query_string_decoded into a
- // dictionary and decode each of the parameters individually.
- // OR: in url_query_string_decoded use as separator a control character that cannot appear in the URL.
- url_decode_r(buffer, path_and_query_string, NETDATA_WEB_REQUEST_URL_SIZE + 1);
- char *question_mark_start = strchr(buffer, '?');
- if (question_mark_start) {
- buffer_strcat(w->url_query_string_decoded, question_mark_start);
- char c = *question_mark_start;
- *question_mark_start = '\0';
- buffer_strcat(w->url_path_decoded, buffer);
- *question_mark_start = c;
- } else {
- buffer_strcat(w->url_query_string_decoded, "");
- buffer_strcat(w->url_path_decoded, buffer);
- }
- }
- }
- void web_client_reuse_from_cache(struct web_client *w) {
- // zero everything about it - but keep the buffers
- web_client_reset_allocations(w, false);
- // remember the pointers to the buffers
- BUFFER *b1 = w->response.data;
- BUFFER *b2 = w->response.header;
- BUFFER *b3 = w->response.header_output;
- BUFFER *b4 = w->url_path_decoded;
- BUFFER *b5 = w->url_as_received;
- BUFFER *b6 = w->url_query_string_decoded;
- BUFFER *b7 = w->payload;
- #ifdef ENABLE_HTTPS
- NETDATA_SSL ssl = w->ssl;
- #endif
- size_t use_count = w->use_count;
- size_t *statistics_memory_accounting = w->statistics.memory_accounting;
- // zero everything
- memset(w, 0, sizeof(struct web_client));
- w->ifd = w->ofd = -1;
- w->statistics.memory_accounting = statistics_memory_accounting;
- w->use_count = use_count;
- #ifdef ENABLE_HTTPS
- w->ssl = ssl;
- #endif
- // restore the pointers of the buffers
- w->response.data = b1;
- w->response.header = b2;
- w->response.header_output = b3;
- w->url_path_decoded = b4;
- w->url_as_received = b5;
- w->url_query_string_decoded = b6;
- w->payload = b7;
- }
- struct web_client *web_client_create(size_t *statistics_memory_accounting) {
- struct web_client *w = (struct web_client *)callocz(1, sizeof(struct web_client));
- #ifdef ENABLE_HTTPS
- w->ssl = NETDATA_SSL_UNSET_CONNECTION;
- #endif
- w->use_count = 1;
- w->statistics.memory_accounting = statistics_memory_accounting;
- w->url_as_received = buffer_create(NETDATA_WEB_DECODED_URL_INITIAL_SIZE, w->statistics.memory_accounting);
- w->url_path_decoded = buffer_create(NETDATA_WEB_DECODED_URL_INITIAL_SIZE, w->statistics.memory_accounting);
- w->url_query_string_decoded = buffer_create(NETDATA_WEB_DECODED_URL_INITIAL_SIZE, w->statistics.memory_accounting);
- w->response.data = buffer_create(NETDATA_WEB_RESPONSE_INITIAL_SIZE, w->statistics.memory_accounting);
- w->response.header = buffer_create(NETDATA_WEB_RESPONSE_HEADER_INITIAL_SIZE, w->statistics.memory_accounting);
- w->response.header_output = buffer_create(NETDATA_WEB_RESPONSE_HEADER_INITIAL_SIZE, w->statistics.memory_accounting);
- __atomic_add_fetch(w->statistics.memory_accounting, sizeof(struct web_client), __ATOMIC_RELAXED);
- return w;
- }
- void web_client_free(struct web_client *w) {
- #ifdef ENABLE_HTTPS
- netdata_ssl_close(&w->ssl);
- #endif
- web_client_reset_allocations(w, true);
- __atomic_sub_fetch(w->statistics.memory_accounting, sizeof(struct web_client), __ATOMIC_RELAXED);
- freez(w);
- }
- inline void web_client_timeout_checkpoint_init(struct web_client *w) {
- now_monotonic_high_precision_timeval(&w->timings.tv_in);
- }
- inline void web_client_timeout_checkpoint_set(struct web_client *w, int timeout_ms) {
- w->timings.timeout_ut = timeout_ms * USEC_PER_MS;
- if(!w->timings.tv_in.tv_sec)
- web_client_timeout_checkpoint_init(w);
- if(!w->timings.tv_timeout_last_checkpoint.tv_sec)
- w->timings.tv_timeout_last_checkpoint = w->timings.tv_in;
- }
- inline usec_t web_client_timeout_checkpoint(struct web_client *w) {
- struct timeval now;
- now_monotonic_high_precision_timeval(&now);
- if (!w->timings.tv_timeout_last_checkpoint.tv_sec)
- w->timings.tv_timeout_last_checkpoint = w->timings.tv_in;
- usec_t since_last_check_ut = dt_usec(&w->timings.tv_timeout_last_checkpoint, &now);
- w->timings.tv_timeout_last_checkpoint = now;
- return since_last_check_ut;
- }
- inline usec_t web_client_timeout_checkpoint_response_ready(struct web_client *w, usec_t *usec_since_last_checkpoint) {
- usec_t since_last_check_ut = web_client_timeout_checkpoint(w);
- if(usec_since_last_checkpoint)
- *usec_since_last_checkpoint = since_last_check_ut;
- w->timings.tv_ready = w->timings.tv_timeout_last_checkpoint;
- // return the total time of the query
- return dt_usec(&w->timings.tv_in, &w->timings.tv_ready);
- }
- inline bool web_client_timeout_checkpoint_and_check(struct web_client *w, usec_t *usec_since_last_checkpoint) {
- usec_t since_last_check_ut = web_client_timeout_checkpoint(w);
- if(usec_since_last_checkpoint)
- *usec_since_last_checkpoint = since_last_check_ut;
- if(!w->timings.timeout_ut)
- return false;
- usec_t since_reception_ut = dt_usec(&w->timings.tv_in, &w->timings.tv_timeout_last_checkpoint);
- if (since_reception_ut >= w->timings.timeout_ut) {
- buffer_flush(w->response.data);
- buffer_strcat(w->response.data, "Query timeout exceeded");
- w->response.code = HTTP_RESP_GATEWAY_TIMEOUT;
- return true;
- }
- return false;
- }
|