Skip to content

Commit

Permalink
Fix missing protocol header (#46)
Browse files Browse the repository at this point in the history
Co-authored-by: jopoly <jopoly>
  • Loading branch information
jopoly authored Jan 31, 2024
1 parent b64b1b4 commit 054821e
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 78 deletions.
21 changes: 13 additions & 8 deletions expected/12.16/option.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ERROR: InfluxDB only support versions from v1.x to 2.x. "version" must be 1 or
--Testcase 5:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (dbname 'optiondb', host 'http://localhost', port '38086', version '-1', retention_policy '');
ERROR: InfluxDB only support versions from v1.x to 2.x. "version" must be 1 or 2.
-- host must be not NULL or not empty
-- host must be not NULL
--Testcase 6:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (dbname 'optiondb', port '38086', version '2', retention_policy '');
--Testcase 7:
Expand All @@ -27,20 +27,25 @@ DROP SERVER influxdb_svr CASCADE;
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to user mapping for vagrant on server influxdb_svr
drop cascades to foreign table optiontbl
-- host must start with either http:// or https://
--Testcase 11:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (dbname 'optiondb', host '', port '38086', version '2', retention_policy '');
ERROR: influxdb_fdw: Host address must start with either http:// or https://
--Testcase 12:
CREATE USER MAPPING FOR CURRENT_USER SERVER influxdb_svr OPTIONS (auth_token 'mytoken');
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (dbname 'optiondb', host 'localhost', port '38086', version '2', retention_policy '');
ERROR: influxdb_fdw: Host address must start with either http:// or https://
--Testcase 13:
CREATE FOREIGN TABLE optiontbl (tag1 text, version text, value2 int) SERVER influxdb_svr;
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'HTTP://localhost', port '38086', version '2', retention_policy '');
DROP SERVER influxdb_svr CASCADE;
--Testcase 14:
SELECT * FROM optiontbl;
ERROR: influxdb_fdw: Server Host not specified
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'HTTPS://localhost', port '38086', version '2', retention_policy '');
DROP SERVER influxdb_svr CASCADE;
--Testcase 15:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'https://localhost', port '38086', version '2', retention_policy '');
DROP SERVER influxdb_svr CASCADE;
--Testcase 92:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'hTTpS://localhost', port '38086', version '2', retention_policy '');
DROP SERVER influxdb_svr CASCADE;
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to user mapping for vagrant on server influxdb_svr
drop cascades to foreign table optiontbl
-- dbname must be not NULL or not empty
--Testcase 16:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'http://localhost', port '38086', version '2', retention_policy '');
Expand Down
21 changes: 13 additions & 8 deletions expected/13.12/option.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ERROR: InfluxDB only support versions from v1.x to 2.x. "version" must be 1 or
--Testcase 5:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (dbname 'optiondb', host 'http://localhost', port '38086', version '-1', retention_policy '');
ERROR: InfluxDB only support versions from v1.x to 2.x. "version" must be 1 or 2.
-- host must be not NULL or not empty
-- host must be not NULL
--Testcase 6:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (dbname 'optiondb', port '38086', version '2', retention_policy '');
--Testcase 7:
Expand All @@ -27,20 +27,25 @@ DROP SERVER influxdb_svr CASCADE;
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to user mapping for vagrant on server influxdb_svr
drop cascades to foreign table optiontbl
-- host must start with either http:// or https://
--Testcase 11:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (dbname 'optiondb', host '', port '38086', version '2', retention_policy '');
ERROR: influxdb_fdw: Host address must start with either http:// or https://
--Testcase 12:
CREATE USER MAPPING FOR CURRENT_USER SERVER influxdb_svr OPTIONS (auth_token 'mytoken');
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (dbname 'optiondb', host 'localhost', port '38086', version '2', retention_policy '');
ERROR: influxdb_fdw: Host address must start with either http:// or https://
--Testcase 13:
CREATE FOREIGN TABLE optiontbl (tag1 text, version text, value2 int) SERVER influxdb_svr;
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'HTTP://localhost', port '38086', version '2', retention_policy '');
DROP SERVER influxdb_svr CASCADE;
--Testcase 14:
SELECT * FROM optiontbl;
ERROR: influxdb_fdw: Server Host not specified
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'HTTPS://localhost', port '38086', version '2', retention_policy '');
DROP SERVER influxdb_svr CASCADE;
--Testcase 15:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'https://localhost', port '38086', version '2', retention_policy '');
DROP SERVER influxdb_svr CASCADE;
--Testcase 92:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'hTTpS://localhost', port '38086', version '2', retention_policy '');
DROP SERVER influxdb_svr CASCADE;
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to user mapping for vagrant on server influxdb_svr
drop cascades to foreign table optiontbl
-- dbname must be not NULL or not empty
--Testcase 16:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'http://localhost', port '38086', version '2', retention_policy '');
Expand Down
21 changes: 13 additions & 8 deletions expected/14.9/option.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ERROR: InfluxDB only support versions from v1.x to 2.x. "version" must be 1 or
--Testcase 5:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (dbname 'optiondb', host 'http://localhost', port '38086', version '-1', retention_policy '');
ERROR: InfluxDB only support versions from v1.x to 2.x. "version" must be 1 or 2.
-- host must be not NULL or not empty
-- host must be not NULL
--Testcase 6:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (dbname 'optiondb', port '38086', version '2', retention_policy '');
--Testcase 7:
Expand All @@ -27,20 +27,25 @@ DROP SERVER influxdb_svr CASCADE;
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to user mapping for vagrant on server influxdb_svr
drop cascades to foreign table optiontbl
-- host must start with either http:// or https://
--Testcase 11:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (dbname 'optiondb', host '', port '38086', version '2', retention_policy '');
ERROR: influxdb_fdw: Host address must start with either http:// or https://
--Testcase 12:
CREATE USER MAPPING FOR CURRENT_USER SERVER influxdb_svr OPTIONS (auth_token 'mytoken');
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (dbname 'optiondb', host 'localhost', port '38086', version '2', retention_policy '');
ERROR: influxdb_fdw: Host address must start with either http:// or https://
--Testcase 13:
CREATE FOREIGN TABLE optiontbl (tag1 text, version text, value2 int) SERVER influxdb_svr;
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'HTTP://localhost', port '38086', version '2', retention_policy '');
DROP SERVER influxdb_svr CASCADE;
--Testcase 14:
SELECT * FROM optiontbl;
ERROR: influxdb_fdw: Server Host not specified
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'HTTPS://localhost', port '38086', version '2', retention_policy '');
DROP SERVER influxdb_svr CASCADE;
--Testcase 15:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'https://localhost', port '38086', version '2', retention_policy '');
DROP SERVER influxdb_svr CASCADE;
--Testcase 92:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'hTTpS://localhost', port '38086', version '2', retention_policy '');
DROP SERVER influxdb_svr CASCADE;
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to user mapping for vagrant on server influxdb_svr
drop cascades to foreign table optiontbl
-- dbname must be not NULL or not empty
--Testcase 16:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'http://localhost', port '38086', version '2', retention_policy '');
Expand Down
21 changes: 13 additions & 8 deletions expected/15.4/option.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ERROR: InfluxDB only support versions from v1.x to 2.x. "version" must be 1 or
--Testcase 5:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (dbname 'optiondb', host 'http://localhost', port '38086', version '-1', retention_policy '');
ERROR: InfluxDB only support versions from v1.x to 2.x. "version" must be 1 or 2.
-- host must be not NULL or not empty
-- host must be not NULL
--Testcase 6:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (dbname 'optiondb', port '38086', version '2', retention_policy '');
--Testcase 7:
Expand All @@ -27,20 +27,25 @@ DROP SERVER influxdb_svr CASCADE;
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to user mapping for vagrant on server influxdb_svr
drop cascades to foreign table optiontbl
-- host must start with either http:// or https://
--Testcase 11:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (dbname 'optiondb', host '', port '38086', version '2', retention_policy '');
ERROR: influxdb_fdw: Host address must start with either http:// or https://
--Testcase 12:
CREATE USER MAPPING FOR CURRENT_USER SERVER influxdb_svr OPTIONS (auth_token 'mytoken');
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (dbname 'optiondb', host 'localhost', port '38086', version '2', retention_policy '');
ERROR: influxdb_fdw: Host address must start with either http:// or https://
--Testcase 13:
CREATE FOREIGN TABLE optiontbl (tag1 text, version text, value2 int) SERVER influxdb_svr;
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'HTTP://localhost', port '38086', version '2', retention_policy '');
DROP SERVER influxdb_svr CASCADE;
--Testcase 14:
SELECT * FROM optiontbl;
ERROR: influxdb_fdw: Server Host not specified
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'HTTPS://localhost', port '38086', version '2', retention_policy '');
DROP SERVER influxdb_svr CASCADE;
--Testcase 15:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'https://localhost', port '38086', version '2', retention_policy '');
DROP SERVER influxdb_svr CASCADE;
--Testcase 92:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'hTTpS://localhost', port '38086', version '2', retention_policy '');
DROP SERVER influxdb_svr CASCADE;
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to user mapping for vagrant on server influxdb_svr
drop cascades to foreign table optiontbl
-- dbname must be not NULL or not empty
--Testcase 16:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'http://localhost', port '38086', version '2', retention_policy '');
Expand Down
21 changes: 13 additions & 8 deletions expected/16.0/option.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ERROR: InfluxDB only support versions from v1.x to 2.x. "version" must be 1 or
--Testcase 5:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (dbname 'optiondb', host 'http://localhost', port '38086', version '-1', retention_policy '');
ERROR: InfluxDB only support versions from v1.x to 2.x. "version" must be 1 or 2.
-- host must be not NULL or not empty
-- host must be not NULL
--Testcase 6:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (dbname 'optiondb', port '38086', version '2', retention_policy '');
--Testcase 7:
Expand All @@ -27,20 +27,25 @@ DROP SERVER influxdb_svr CASCADE;
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to user mapping for vagrant on server influxdb_svr
drop cascades to foreign table optiontbl
-- host must start with either http:// or https://
--Testcase 11:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (dbname 'optiondb', host '', port '38086', version '2', retention_policy '');
ERROR: influxdb_fdw: Host address must start with either http:// or https://
--Testcase 12:
CREATE USER MAPPING FOR CURRENT_USER SERVER influxdb_svr OPTIONS (auth_token 'mytoken');
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (dbname 'optiondb', host 'localhost', port '38086', version '2', retention_policy '');
ERROR: influxdb_fdw: Host address must start with either http:// or https://
--Testcase 13:
CREATE FOREIGN TABLE optiontbl (tag1 text, version text, value2 int) SERVER influxdb_svr;
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'HTTP://localhost', port '38086', version '2', retention_policy '');
DROP SERVER influxdb_svr CASCADE;
--Testcase 14:
SELECT * FROM optiontbl;
ERROR: influxdb_fdw: Server Host not specified
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'HTTPS://localhost', port '38086', version '2', retention_policy '');
DROP SERVER influxdb_svr CASCADE;
--Testcase 15:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'https://localhost', port '38086', version '2', retention_policy '');
DROP SERVER influxdb_svr CASCADE;
--Testcase 92:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'hTTpS://localhost', port '38086', version '2', retention_policy '');
DROP SERVER influxdb_svr CASCADE;
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to user mapping for vagrant on server influxdb_svr
drop cascades to foreign table optiontbl
-- dbname must be not NULL or not empty
--Testcase 16:
CREATE SERVER influxdb_svr FOREIGN DATA WRAPPER influxdb_fdw OPTIONS (host 'http://localhost', port '38086', version '2', retention_policy '');
Expand Down
25 changes: 24 additions & 1 deletion option.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <regex.h>

#include "funcapi.h"
#include "access/reloptions.h"
#include "catalog/pg_foreign_server.h"
#include "catalog/pg_foreign_table.h"
#include "catalog/pg_user_mapping.h"
#include "catalog/pg_type.h"
#include "catalog/pg_collation.h"
#include "commands/defrem.h"
#include "commands/explain.h"
#include "foreign/fdwapi.h"
Expand All @@ -41,6 +43,7 @@
#include "optimizer/restrictinfo.h"
#include "optimizer/planmain.h"
#include "utils/varlena.h"
#include "utils/formatting.h"

#ifdef CXX_CLIENT
#include "query_cxx.h"
Expand Down Expand Up @@ -212,6 +215,26 @@ influxdb_fdw_validator(PG_FUNCTION_ARGS)
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("InfluxDB only support versions from v1.x to 2.x. \"%s\" must be 1 or 2.", def->defname)));
}

/* Validate host address if it is set */
if (strcmp(def->defname, "host") == 0)
{
regex_t regex;
int ret;
char *host = str_tolower(defGetString(def), strlen(defGetString(def)), C_COLLATION_OID);

/* Must start with either http:// or https:// */
ret = regcomp(&regex, "^((http|https)://)", REG_EXTENDED);
Assert(ret == 0); /*for debug invalid regex pattern */
ret = regexec(&regex, host, 0, NULL, 0);
if (ret == REG_NOMATCH)
{
regfree(&regex);
elog(ERROR, "influxdb_fdw: Host address must start with either http:// or https://");
}

regfree(&regex);
}
#endif
}
PG_RETURN_VOID();
Expand Down Expand Up @@ -322,7 +345,7 @@ influxdb_get_options(Oid foreignoid, Oid userid)

#ifdef CXX_CLIENT
/* When using the influxdb-cxx API client, c++ not allow std::string(NULL). */
if (opt->svr_address == NULL || strcmp(opt->svr_address, "") == 0)
if (opt->svr_address == NULL)
elog(ERROR, "influxdb_fdw: Server Host not specified");

if (opt->svr_database == NULL || strcmp(opt->svr_database, "") == 0)
Expand Down
33 changes: 16 additions & 17 deletions query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,28 +488,26 @@ check_connected_influxdb_version(char* addr, int port, char* user, char* pass, c

query = "SHOW MEASUREMENTS ON " + std::string(db);

influx = influxdb::InfluxDBFactory::GetV2(std::string(addr), port, std::string(db), std::string(auth_token), std::string(retention_policy));

try
{
influx = influxdb::InfluxDBFactory::GetV2(std::string(addr), port, std::string(db), std::string(auth_token), std::string(retention_policy));
auto measurements = influx->query(query);
return INFLUXDB_VERSION_2;
}
catch(const std::exception& e)
catch (const std::exception &e)
{
/* do nothing */
}

influx = influxdb::InfluxDBFactory::GetV1(std::string(addr), port, std::string(db), std::string(user), std::string(pass));

try
{
influx = influxdb::InfluxDBFactory::GetV1(std::string(addr), port, std::string(db), std::string(user), std::string(pass));
auto measurements = influx->query(query);
return INFLUXDB_VERSION_1;
}
catch(const std::exception& e)
{
elog(ERROR, "Could not connect to InfluxDB.");
elog(ERROR, "Could not connect to InfluxDB: %s", e.what());
}

elog(ERROR, "Could not connect to InfluxDB.");
Expand Down Expand Up @@ -539,10 +537,10 @@ InfluxDBExecDDLCommand(char* addr, int port, char* user, char* pass, char* db, c

if (version != INFLUXDB_VERSION_1 && version != INFLUXDB_VERSION_2)
{
/* Automatically detect InfluxDB version 1.x and 2.x: trying to connect InfluxDB v2.x first */
influx = influxdb::InfluxDBFactory::GetV2(std::string(addr), port, std::string(db), std::string(auth_token), std::string(retention_policy));
try
{
/* Automatically detect InfluxDB version 1.x and 2.x: trying to connect InfluxDB v2.x first */
influx = influxdb::InfluxDBFactory::GetV2(std::string(addr), port, std::string(db), std::string(auth_token), std::string(retention_policy));
auto err = influx->query(cquery);
}
catch(const std::exception& e)
Expand All @@ -554,27 +552,28 @@ InfluxDBExecDDLCommand(char* addr, int port, char* user, char* pass, char* db, c
if (!retry_connect)
return NULL; /* Query successfully */

influx = influxdb::InfluxDBFactory::GetV1(std::string(addr), port, std::string(db), std::string(user), std::string(pass));
try
{
influx = influxdb::InfluxDBFactory::GetV1(std::string(addr), port, std::string(db), std::string(user), std::string(pass));
auto err = influx->query(cquery);
}
catch(const std::exception& e)
{
elog(ERROR, "influxdb_fdw: could not execute query: %s", cquery);
}
}
/* InfluxDB version is specified */
else if (version == INFLUXDB_VERSION_1)
influx = influxdb::InfluxDBFactory::GetV1(std::string(addr), port, std::string(db), std::string(user), std::string(pass));
else if (version == INFLUXDB_VERSION_2)
influx = influxdb::InfluxDBFactory::GetV2(std::string(addr), port, std::string(db), std::string(auth_token), std::string(retention_policy));

if (!influx)
elog(ERROR, "Fail to create influxDB client");

try
{
/* InfluxDB version is specified */
if (version == INFLUXDB_VERSION_1)
influx = influxdb::InfluxDBFactory::GetV1(std::string(addr), port, std::string(db), std::string(user), std::string(pass));
else if (version == INFLUXDB_VERSION_2)
influx = influxdb::InfluxDBFactory::GetV2(std::string(addr), port, std::string(db), std::string(auth_token), std::string(retention_policy));

if (!influx)
elog(ERROR, "Fail to create influxDB client");

auto err = influx->query(cquery);
}
catch (const std::exception& e)
Expand Down
Loading

0 comments on commit 054821e

Please sign in to comment.