From a2ac18d515cd39ac34e951360f27a566e5ddc53f Mon Sep 17 00:00:00 2001 From: "martin.ubl" Date: Thu, 23 Jan 2014 17:25:02 +0100 Subject: [PATCH] Implement content-type and accept headers option in web scenarios --- database/ibm_db2/schema.sql | 2 + database/mysql/schema.sql | 2 + database/oracle/schema.sql | 2 + database/postgresql/schema.sql | 2 + database/sqlite3/schema.sql | 2 + frontends/php/include/schema.inc.php | 12 ++++++ .../include/views/configuration.httpconf.popup.php | 10 ++++- .../views/js/configuration.httpconf.edit.js.php | 10 +++++ .../views/js/configuration.httpconf.popup.js.php | 8 +++- frontends/php/popup_httpstep.php | 2 + include/db.h | 2 + src/zabbix_server/httppoller/httptest.c | 41 +++++++++++++++++++- 12 files changed, 90 insertions(+), 5 deletions(-) diff --git a/database/ibm_db2/schema.sql b/database/ibm_db2/schema.sql index 1051757..d4a17b3 100644 --- a/database/ibm_db2/schema.sql +++ b/database/ibm_db2/schema.sql @@ -189,6 +189,8 @@ CREATE TABLE httpstep ( required varchar(255) WITH DEFAULT '' NOT NULL, status_codes varchar(255) WITH DEFAULT '' NOT NULL, variables varchar(2048) WITH DEFAULT '' NOT NULL, + content_type varchar(60) WITH DEFAULT '' NOT NULL, + accept_type varchar(60) WITH DEFAULT '' NOT NULL, PRIMARY KEY (httpstepid) ); CREATE INDEX httpstep_1 ON httpstep (httptestid); diff --git a/database/mysql/schema.sql b/database/mysql/schema.sql index 785b2db..740b9e8 100644 --- a/database/mysql/schema.sql +++ b/database/mysql/schema.sql @@ -189,6 +189,8 @@ CREATE TABLE `httpstep` ( `required` varchar(255) DEFAULT '' NOT NULL, `status_codes` varchar(255) DEFAULT '' NOT NULL, `variables` text NOT NULL, + `content_type` varchar(60) DEFAULT '' NOT NULL, + `accept_type` varchar(60) DEFAULT '' NOT NULL, PRIMARY KEY (httpstepid) ) ENGINE=InnoDB; CREATE INDEX `httpstep_1` ON `httpstep` (`httptestid`); diff --git a/database/oracle/schema.sql b/database/oracle/schema.sql index c5e6c52..187baa5 100644 --- a/database/oracle/schema.sql +++ b/database/oracle/schema.sql @@ -189,6 +189,8 @@ CREATE TABLE httpstep ( required nvarchar2(255) DEFAULT '' , status_codes nvarchar2(255) DEFAULT '' , variables nvarchar2(2048) DEFAULT '' , + content_type nvarchar2(60) DEFAULT '' , + accept_type nvarchar2(60) DEFAULT '' , PRIMARY KEY (httpstepid) ); CREATE INDEX httpstep_1 ON httpstep (httptestid); diff --git a/database/postgresql/schema.sql b/database/postgresql/schema.sql index d621cc5..6f6b0bb 100644 --- a/database/postgresql/schema.sql +++ b/database/postgresql/schema.sql @@ -189,6 +189,8 @@ CREATE TABLE httpstep ( required varchar(255) DEFAULT '' NOT NULL, status_codes varchar(255) DEFAULT '' NOT NULL, variables text DEFAULT '' NOT NULL, + content_type varchar(60) DEFAULT '' NOT NULL, + accept_type varchar(60) DEFAULT '' NOT NULL, PRIMARY KEY (httpstepid) ); CREATE INDEX httpstep_1 ON httpstep (httptestid); diff --git a/database/sqlite3/schema.sql b/database/sqlite3/schema.sql index d9d7710..0c8988c 100644 --- a/database/sqlite3/schema.sql +++ b/database/sqlite3/schema.sql @@ -189,6 +189,8 @@ CREATE TABLE httpstep ( required varchar(255) DEFAULT '' NOT NULL, status_codes varchar(255) DEFAULT '' NOT NULL, variables text DEFAULT '' NOT NULL, + content_type varchar(60) DEFAULT '' NOT NULL, + accept_type varchar(60) DEFAULT '' NOT NULL, PRIMARY KEY (httpstepid) ); CREATE INDEX httpstep_1 ON httpstep (httptestid); diff --git a/frontends/php/include/schema.inc.php b/frontends/php/include/schema.inc.php index a83037a..79ca528 100644 --- a/frontends/php/include/schema.inc.php +++ b/frontends/php/include/schema.inc.php @@ -875,6 +875,18 @@ return array( 'type' => DB::FIELD_TYPE_TEXT, 'default' => '', ), + 'content_type' => array( + 'null' => false, + 'type' => DB::FIELD_TYPE_CHAR, + 'length' => 60, + 'default' => '', + ), + 'accept_type' => array( + 'null' => false, + 'type' => DB::FIELD_TYPE_CHAR, + 'length' => 60, + 'default' => '', + ), ), ), 'interface' => array( diff --git a/frontends/php/include/views/configuration.httpconf.popup.php b/frontends/php/include/views/configuration.httpconf.popup.php index d0ff330..3003eab 100644 --- a/frontends/php/include/views/configuration.httpconf.popup.php +++ b/frontends/php/include/views/configuration.httpconf.popup.php @@ -46,7 +46,9 @@ if (isset($_REQUEST['save']) && $result) { zbx_jsvalue($_REQUEST['posts']).','. zbx_jsvalue($_REQUEST['variables']).','. zbx_jsvalue($_REQUEST['required']).','. - zbx_jsvalue($_REQUEST['status_codes']).");\n" + zbx_jsvalue($_REQUEST['status_codes']).','. + zbx_jsvalue($_REQUEST['content_type']).','. + zbx_jsvalue($_REQUEST['accept_type']).");\n" ); } else { @@ -60,7 +62,9 @@ if (isset($_REQUEST['save']) && $result) { zbx_jsvalue($_REQUEST['posts']).','. zbx_jsvalue($_REQUEST['variables']).','. zbx_jsvalue($_REQUEST['required']).','. - zbx_jsvalue($_REQUEST['status_codes']).");\n" + zbx_jsvalue($_REQUEST['status_codes']).','. + zbx_jsvalue($_REQUEST['content_type']).','. + zbx_jsvalue($_REQUEST['accept_type']).");\n" ); } } @@ -81,6 +85,8 @@ else { $httpPopupFormList->addRow(_('Timeout'), new CNumericBox('timeout', get_request('timeout', 15), 5)); $httpPopupFormList->addRow(_('Required string'), new CTextBox('required', get_request('required', ''), ZBX_TEXTBOX_STANDARD_SIZE)); $httpPopupFormList->addRow(_('Required status codes'), new CTextBox('status_codes', get_request('status_codes', ''), ZBX_TEXTBOX_STANDARD_SIZE)); + $httpPopupFormList->addRow(_('Content-type'), new CTextBox('content_type', get_request('content_type',''), ZBX_TEXTBOX_STANDARD_SIZE)); + $httpPopupFormList->addRow(_('Accept type'), new CTextBox('accept_type', get_request('accept_type',''), ZBX_TEXTBOX_STANDARD_SIZE)); // append tabs to form $httpPopupTab = new CTabView(); diff --git a/frontends/php/include/views/js/configuration.httpconf.edit.js.php b/frontends/php/include/views/js/configuration.httpconf.edit.js.php index 912d655..37526d7 100644 --- a/frontends/php/include/views/js/configuration.httpconf.edit.js.php +++ b/frontends/php/include/views/js/configuration.httpconf.edit.js.php @@ -14,6 +14,8 @@ jQuery('#steps_' + step + '_variables').remove(); jQuery('#steps_' + step + '_required').remove(); jQuery('#steps_' + step + '_status_codes').remove(); + jQuery('#steps_' + step + '_content_type').remove(); + jQuery('#steps_' + step + '_accept_type').remove(); if (table.find('tr.sortable').length <= 1) { table.sortable('disable'); @@ -41,6 +43,8 @@ jQuery('#steps_' + step + '_variables').attr('id', 'tmp_steps_' + step + '_variables'); jQuery('#steps_' + step + '_required').attr('id', 'tmp_steps_' + step + '_required'); jQuery('#steps_' + step + '_status_codes').attr('id', 'tmp_steps_' + step + '_status_codes'); + jQuery('#steps_' + step + '_content_type').attr('id', 'tmp_steps_' + step + '_content_type'); + jQuery('#steps_' + step + '_accept_type').attr('id', 'tmp_steps_' + step + '_accept_type'); jQuery('#current_step_' + step).attr('id', 'tmp_current_step_' + step); // set order number @@ -67,6 +71,8 @@ jQuery('#tmp_steps_' + n + '_variables').attr('id', 'steps_' + newStep + '_variables'); jQuery('#tmp_steps_' + n + '_required').attr('id', 'steps_' + newStep + '_required'); jQuery('#tmp_steps_' + n + '_status_codes').attr('id', 'steps_' + newStep + '_status_codes'); + jQuery('#tmp_steps_' + n + '_content_type').attr('id', 'steps_' + newStep + '_content_type'); + jQuery('#tmp_steps_' + n + '_accept_type').attr('id', 'steps_' + newStep + '_accept_type'); jQuery('#remove_' + newStep).attr('remove_step', newStep); jQuery('#name_' + newStep).attr('name_step', newStep); @@ -82,6 +88,8 @@ jQuery('#steps_' + newStep + '_variables').attr('name', 'steps[' + newStep + '][variables]'); jQuery('#steps_' + newStep + '_required').attr('name', 'steps[' + newStep + '][required]'); jQuery('#steps_' + newStep + '_status_codes').attr('name', 'steps[' + newStep + '][status_codes]'); + jQuery('#steps_' + newStep + '_content_type').attr('name', 'steps[' + newStep + '][content_type]'); + jQuery('#steps_' + newStep + '_accept_type').attr('name', 'steps[' + newStep + '][accept_type]'); // set new step order position currStep.attr('id', 'current_step_' + newStep); @@ -145,6 +153,8 @@ + '' + '' + '' + + '' + + '' + '' + stepNames, 600, 510); }); diff --git a/frontends/php/include/views/js/configuration.httpconf.popup.js.php b/frontends/php/include/views/js/configuration.httpconf.popup.js.php index 9f10cfe..6978f54 100644 --- a/frontends/php/include/views/js/configuration.httpconf.popup.js.php +++ b/frontends/php/include/views/js/configuration.httpconf.popup.js.php @@ -7,7 +7,7 @@ obj.appendChild(new_variable); } - function add_httpstep(formname, name, timeout, url, posts, variables, required, status_codes) { + function add_httpstep(formname, name, timeout, url, posts, variables, required, status_codes, content_type, accept_type) { var form = window.opener.document.forms[formname]; if (!form) { close_window(); @@ -21,13 +21,15 @@ add_var_to_opener_obj(form, 'new_httpstep[variables]', variables); add_var_to_opener_obj(form, 'new_httpstep[required]', required); add_var_to_opener_obj(form, 'new_httpstep[status_codes]', status_codes); + add_var_to_opener_obj(form, 'new_httpstep[content_type]', content_type); + add_var_to_opener_obj(form, 'new_httpstep[accept_type]', accept_type); form.submit(); close_window(); return true; } - function update_httpstep(formname, list_name, stepid, name, timeout, url, posts, variables, required, status_codes) { + function update_httpstep(formname, list_name, stepid, name, timeout, url, posts, variables, required, status_codes, content_type, accept_type) { var form = window.opener.document.forms[formname]; if (!form) { close_window(); @@ -41,6 +43,8 @@ add_var_to_opener_obj(form, list_name + '[' + stepid + '][variables]', variables); add_var_to_opener_obj(form, list_name + '[' + stepid + '][required]', required); add_var_to_opener_obj(form, list_name + '[' + stepid + '][status_codes]', status_codes); + add_var_to_opener_obj(form, list_name + '[' + stepid + '][content_type]', content_type); + add_var_to_opener_obj(form, list_name + '[' + stepid + '][accept_type]', accept_type); form.submit(); close_window(); diff --git a/frontends/php/popup_httpstep.php b/frontends/php/popup_httpstep.php index 65a4ebc..9da4917 100644 --- a/frontends/php/popup_httpstep.php +++ b/frontends/php/popup_httpstep.php @@ -40,6 +40,8 @@ $fields = array( 'timeout' => array(T_ZBX_INT, O_OPT, null, BETWEEN(0,65535), 'isset({save})', _('Timeout')), 'required' => array(T_ZBX_STR, O_OPT, null, null, 'isset({save})'), 'status_codes' => array(T_ZBX_INT_RANGE, O_OPT, null, null, 'isset({save})'), + 'content_type' => array(T_ZBX_STR, O_OPT, null, null, 'isset({save})'), + 'accept_type' => array(T_ZBX_STR, O_OPT, null, null, 'isset({save})'), 'templated' => array(T_ZBX_STR, O_OPT, null, null, null), 'old_name'=> array(T_ZBX_STR, O_OPT, null, null, null), 'steps_names'=> array(T_ZBX_STR, O_OPT, null, null, null), diff --git a/include/db.h b/include/db.h index c972a05..b306754 100644 --- a/include/db.h +++ b/include/db.h @@ -400,6 +400,8 @@ typedef struct int no; int timeout; char *variables; + char *content_type; + char *accept_type; } DB_HTTPSTEP; diff --git a/src/zabbix_server/httppoller/httptest.c b/src/zabbix_server/httppoller/httptest.c index 6f98ee2..955565a 100644 --- a/src/zabbix_server/httppoller/httptest.c +++ b/src/zabbix_server/httppoller/httptest.c @@ -311,7 +311,7 @@ static void process_httptest(DC_HOST *host, zbx_httptest_t *httptest) lastfailedstep = 0; result = DBselect( - "select httpstepid,no,name,url,timeout,posts,required,status_codes,variables" + "select httpstepid,no,name,url,timeout,posts,required,status_codes,variables,content_type,accept_type" " from httpstep" " where httptestid=" ZBX_FS_UI64 " order by no", @@ -367,10 +367,15 @@ static void process_httptest(DC_HOST *host, zbx_httptest_t *httptest) httpstep.variables = row[8]; + httpstep.content_type = row[9]; + httpstep.accept_type = row[10]; + memset(&stat, 0, sizeof(stat)); http_substitute_variables(httptest, &httpstep.url); http_substitute_variables(httptest, &httpstep.posts); + http_substitute_variables(httptest, &httpstep.content_type); + http_substitute_variables(httptest, &httpstep.accept_type); zabbix_log(LOG_LEVEL_DEBUG, "%s() use step \"%s\"", __function_name, httpstep.name); @@ -389,6 +394,40 @@ static void process_httptest(DC_HOST *host, zbx_httptest_t *httptest) goto httpstep_error; } + if ((NULL != httpstep.content_type && '\0' != *httpstep.content_type) || (NULL != httpstep.accept_type && '\0' != *httpstep.accept_type)) + { + struct curl_slist *headers = NULL; + char *headeropt = NULL; + int len = 0; + if (NULL != httpstep.content_type && '\0' != *httpstep.content_type) + { + len = 14+strlen(httpstep.content_type)+1; + headeropt = (char*)malloc(len*sizeof(char)); + zbx_snprintf(headeropt, len, "Content-type: %s", httpstep.content_type); + headers = curl_slist_append(headers, headeropt); + free(headeropt); + } + + if (NULL != httpstep.accept_type && '\0' != *httpstep.accept_type) + { + len = 8+strlen(httpstep.accept_type)+1; + headeropt = (char*)malloc(len*sizeof(char)); + zbx_snprintf(headeropt, len, "Accept: %s", httpstep.accept_type); + headers = curl_slist_append(headers, headeropt); + free(headeropt); + } + + if (NULL != headers) + { + if (CURLE_OK != (err = curl_easy_setopt(easyhandle, CURLOPT_HTTPHEADER, headers))) + { + err_str = zbx_strdup(err_str, curl_easy_strerror(err)); + goto httpstep_error; + } + curl_slist_free(headers); + } + } + if (HTTPTEST_AUTH_NONE != httptest->httptest.authentication) { long curlauth = 0; -- 1.7.10.4