From bdd84303c71b9f85dca4a3b986f77113912d6307 Mon Sep 17 00:00:00 2001 From: Regina Obe Date: Fri, 24 Jan 2025 20:08:36 -0500 Subject: [PATCH 1/2] TEST HARNESS CHANGES AND gitignore - Change to test against httpbin.org, if local httpbin not available - Add tests for new GUC feature - ignore .dll files --- .gitignore | 1 + expected/http.out | 164 +++++++++++++++++++++++++++++++--------------- sql/http.sql | 87 ++++++++++++++++++------ 3 files changed, 180 insertions(+), 72 deletions(-) diff --git a/.gitignore b/.gitignore index 370a59a..0c450ae 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ *.a *.pc *.dylib +*.dll regression.diffs regression.out results/ diff --git a/expected/http.out b/expected/http.out index a779748..364baf1 100644 --- a/expected/http.out +++ b/expected/http.out @@ -1,4 +1,5 @@ CREATE EXTENSION http; +SET http.server_host = 'http://localhost:9080'; set http.timeout_msec = 10000; SELECT http_set_curlopt('CURLOPT_TIMEOUT', '10'); http_set_curlopt @@ -6,9 +7,19 @@ SELECT http_set_curlopt('CURLOPT_TIMEOUT', '10'); t (1 row) +-- if local server not up use global one +DO language plpgsql $$ +BEGIN + BEGIN + PERFORM http_get(current_setting('http.server_host') || '/status/202'); + EXCEPTION WHEN OTHERS THEN + SET http.server_host = 'http://httpbin.org'; + END; +END; +$$; -- Status code SELECT status -FROM http_get('http://localhost:9080/status/202'); +FROM http_get(current_setting('http.server_host') || '/status/202'); status -------- 202 @@ -18,7 +29,7 @@ FROM http_get('http://localhost:9080/status/202'); SELECT lower(field) AS field, value FROM ( SELECT (unnest(headers)).* - FROM http_get('http://localhost:9080/response-headers?Abcde=abcde') + FROM http_get(current_setting('http.server_host') || '/response-headers?Abcde=abcde') ) a WHERE field ILIKE 'Abcde'; field | value @@ -31,10 +42,10 @@ SELECT status, content::json->'args'->>'foo' AS args, content::json->>'url' AS url, content::json->>'method' AS method -FROM http_get('http://localhost:9080/anything?foo=bar'); - status | args | url | method ---------+------+----------------------------------------+-------- - 200 | bar | http://localhost:9080/anything?foo=bar | GET +FROM http_get(current_setting('http.server_host') || '/anything?foo=bar'); + status | args | url | method +--------+------+-------------------------------------+-------- + 200 | bar | http://httpbin.org/anything?foo=bar | GET (1 row) -- GET with data @@ -42,10 +53,10 @@ SELECT status, content::json->'args'->>'this' AS args, content::json->>'url' AS url, content::json->>'method' AS method -FROM http_get('http://localhost:9080/anything', jsonb_build_object('this', 'that')); - status | args | url | method ---------+------+------------------------------------------+-------- - 200 | that | http://localhost:9080/anything?this=that | GET +FROM http_get(current_setting('http.server_host') || '/anything', jsonb_build_object('this', 'that')); + status | args | url | method +--------+------+---------------------------------------+-------- + 200 | that | http://httpbin.org/anything?this=that | GET (1 row) -- GET with data @@ -54,10 +65,10 @@ content::json->>'args' as args, (content::json)->>'data' as data, content::json->>'url' as url, content::json->>'method' as method -FROM http(('GET', 'http://localhost:9080/anything', NULL, 'application/json', '{"search": "toto"}')); - status | args | data | url | method ---------+------+--------------------+--------------------------------+-------- - 200 | {} | {"search": "toto"} | http://localhost:9080/anything | GET +FROM http(('GET', current_setting('http.server_host') || '/anything', NULL, 'application/json', '{"search": "toto"}')); + status | args | data | url | method +--------+------+--------------------+-----------------------------+-------- + 200 | {} | {"search": "toto"} | http://httpbin.org/anything | GET (1 row) -- DELETE @@ -65,10 +76,10 @@ SELECT status, content::json->'args'->>'foo' AS args, content::json->>'url' AS url, content::json->>'method' AS method -FROM http_delete('http://localhost:9080/anything?foo=bar'); - status | args | url | method ---------+------+----------------------------------------+-------- - 200 | bar | http://localhost:9080/anything?foo=bar | DELETE +FROM http_delete(current_setting('http.server_host') || '/anything?foo=bar'); + status | args | url | method +--------+------+-------------------------------------+-------- + 200 | bar | http://httpbin.org/anything?foo=bar | DELETE (1 row) -- DELETE with payload @@ -77,10 +88,10 @@ content::json->'args'->>'foo' AS args, content::json->>'url' AS url, content::json->>'method' AS method, content::json->>'data' AS data -FROM http_delete('http://localhost:9080/anything?foo=bar', 'payload', 'text/plain'); - status | args | url | method | data ---------+------+----------------------------------------+--------+--------- - 200 | bar | http://localhost:9080/anything?foo=bar | DELETE | payload +FROM http_delete(current_setting('http.server_host') || '/anything?foo=bar', 'payload', 'text/plain'); + status | args | url | method | data +--------+------+-------------------------------------+--------+--------- + 200 | bar | http://httpbin.org/anything?foo=bar | DELETE | payload (1 row) -- PUT @@ -89,10 +100,10 @@ content::json->>'data' AS data, content::json->'args'->>'foo' AS args, content::json->>'url' AS url, content::json->>'method' AS method -FROM http_put('http://localhost:9080/anything?foo=bar','payload','text/plain'); - status | data | args | url | method ---------+---------+------+----------------------------------------+-------- - 200 | payload | bar | http://localhost:9080/anything?foo=bar | PUT +FROM http_put(current_setting('http.server_host') || '/anything?foo=bar','payload','text/plain'); + status | data | args | url | method +--------+---------+------+-------------------------------------+-------- + 200 | payload | bar | http://httpbin.org/anything?foo=bar | PUT (1 row) -- PATCH @@ -101,10 +112,10 @@ content::json->>'data' AS data, content::json->'args'->>'foo' AS args, content::json->>'url' AS url, content::json->>'method' AS method -FROM http_patch('http://localhost:9080/anything?foo=bar','{"this":"that"}','application/json'); - status | data | args | url | method ---------+-----------------+------+----------------------------------------+-------- - 200 | {"this":"that"} | bar | http://localhost:9080/anything?foo=bar | PATCH +FROM http_patch(current_setting('http.server_host') || '/anything?foo=bar','{"this":"that"}','application/json'); + status | data | args | url | method +--------+-----------------+------+-------------------------------------+-------- + 200 | {"this":"that"} | bar | http://httpbin.org/anything?foo=bar | PATCH (1 row) -- POST @@ -113,10 +124,10 @@ content::json->>'data' AS data, content::json->'args'->>'foo' AS args, content::json->>'url' AS url, content::json->>'method' AS method -FROM http_post('http://localhost:9080/anything?foo=bar','payload','text/plain'); - status | data | args | url | method ---------+---------+------+----------------------------------------+-------- - 200 | payload | bar | http://localhost:9080/anything?foo=bar | POST +FROM http_post(current_setting('http.server_host') || '/anything?foo=bar','payload','text/plain'); + status | data | args | url | method +--------+---------+------+-------------------------------------+-------- + 200 | payload | bar | http://httpbin.org/anything?foo=bar | POST (1 row) -- POST with json data @@ -124,10 +135,10 @@ SELECT status, content::json->'form'->>'this' AS args, content::json->>'url' AS url, content::json->>'method' AS method -FROM http_post('http://localhost:9080/anything', jsonb_build_object('this', 'that')); - status | args | url | method ---------+------+--------------------------------+-------- - 200 | that | http://localhost:9080/anything | POST +FROM http_post(current_setting('http.server_host') || '/anything', jsonb_build_object('this', 'that')); + status | args | url | method +--------+------+-----------------------------+-------- + 200 | that | http://httpbin.org/anything | POST (1 row) -- POST with data @@ -136,17 +147,17 @@ content::json->'form'->>'key1' AS key1, content::json->'form'->>'key2' AS key2, content::json->>'url' AS url, content::json->>'method' AS method -FROM http_post('http://localhost:9080/anything', 'key1=value1&key2=value2','application/x-www-form-urlencoded'); - status | key1 | key2 | url | method ---------+--------+--------+--------------------------------+-------- - 200 | value1 | value2 | http://localhost:9080/anything | POST +FROM http_post(current_setting('http.server_host') || '/anything', 'key1=value1&key2=value2','application/x-www-form-urlencoded'); + status | key1 | key2 | url | method +--------+--------+--------+-----------------------------+-------- + 200 | value1 | value2 | http://httpbin.org/anything | POST (1 row) -- HEAD SELECT lower(field) AS field, value FROM ( SELECT (unnest(headers)).* - FROM http_head('http://localhost:9080/response-headers?Abcde=abcde') + FROM http_head(current_setting('http.server_host') || '/response-headers?Abcde=abcde') ) a WHERE field ILIKE 'Abcde'; field | value @@ -157,16 +168,16 @@ WHERE field ILIKE 'Abcde'; -- Follow redirect SELECT status, (content::json)->>'url' AS url -FROM http_get('http://localhost:9080/redirect-to?url=get'); - status | url ---------+--------------------------- - 200 | http://localhost:9080/get +FROM http_get(current_setting('http.server_host') || '/redirect-to?url=get'); + status | url +--------+------------------------ + 200 | http://httpbin.org/get (1 row) -- Request image WITH http AS ( - SELECT * FROM http_get('http://localhost:9080/image/png') + SELECT * FROM http_get(current_setting('http.server_host') || '/image/png') ), headers AS ( SELECT (unnest(headers)).* FROM http @@ -191,7 +202,7 @@ SELECT http_set_curlopt('CURLOPT_PROXY', '127.0.0.1'); -- Error because proxy is not there DO $$ BEGIN - SELECT status FROM http_get('http://localhost:9080/status/555'); + SELECT status FROM http_get(current_setting('http.server_host') || '/status/555'); EXCEPTION WHEN OTHERS THEN RAISE WARNING 'Failed to connect'; @@ -201,7 +212,7 @@ WARNING: Failed to connect -- Still an error DO $$ BEGIN - SELECT status FROM http_get('http://localhost:9080/status/555'); + SELECT status FROM http_get(current_setting('http.server_host') || '/status/555'); EXCEPTION WHEN OTHERS THEN RAISE WARNING 'Failed to connect'; @@ -216,7 +227,7 @@ SELECT http_reset_curlopt(); (1 row) -- Now it should work -SELECT status FROM http_get('http://localhost:9080/status/555'); +SELECT status FROM http_get(current_setting('http.server_host') || '/status/555'); status -------- 555 @@ -230,9 +241,58 @@ SELECT http_set_curlopt('CURLOPT_TIMEOUT_MS', '10000'); t (1 row) -SELECT status FROM http_get('http://localhost:9080/delay/7'); +SELECT status FROM http_get(current_setting('http.server_host') || '/delay/7'); + status +-------- + 200 +(1 row) + +-- Test new GUC feature +SET http.CURLOPT_TIMEOUT_MS = '10'; +-- should fail +-- Still an error +DO $$ +BEGIN + SELECT status FROM http_get(current_setting('http.server_host') || '/delay/7'); +EXCEPTION + WHEN OTHERS THEN + RAISE WARNING 'Failed to connect'; +END; +$$; +WARNING: Failed to connect +SET http.CURLOPT_TIMEOUT_MS = '10000'; +--should pass +SELECT status FROM http_get(current_setting('http.server_host') || '/delay/7'); + status +-------- + 200 +(1 row) + +-- SET to bogus file +SET http.CURLOPT_CAINFO = '/path/to/somebundle.crt'; +-- should fail +SELECT status FROM http_get('https://postgis.net'); +ERROR: error setting certificate file: /path/to/somebundle.crt +-- set to ignore cert +SET http.CURLOPT_SSL_VERIFYPEER = '0'; +-- should pass +SELECT status FROM http_get('https://postgis.net'); status -------- 200 (1 row) +SHOW http.CURLOPT_CAINFO; + http.curlopt_cainfo +------------------------- + /path/to/somebundle.crt +(1 row) + +-- reset it +RESET http.CURLOPT_CAINFO; +SHOW http.CURLOPT_CAINFO; + http.curlopt_cainfo +--------------------- + +(1 row) + diff --git a/sql/http.sql b/sql/http.sql index 7274104..3e2e0e0 100644 --- a/sql/http.sql +++ b/sql/http.sql @@ -1,17 +1,27 @@ CREATE EXTENSION http; - +SET http.server_host = 'http://localhost:9080'; set http.timeout_msec = 10000; SELECT http_set_curlopt('CURLOPT_TIMEOUT', '10'); +-- if local server not up use global one +DO language plpgsql $$ +BEGIN + BEGIN + PERFORM http_get(current_setting('http.server_host') || '/status/202'); + EXCEPTION WHEN OTHERS THEN + SET http.server_host = 'http://httpbin.org'; + END; +END; +$$; -- Status code SELECT status -FROM http_get('http://localhost:9080/status/202'); +FROM http_get(current_setting('http.server_host') || '/status/202'); -- Headers SELECT lower(field) AS field, value FROM ( SELECT (unnest(headers)).* - FROM http_get('http://localhost:9080/response-headers?Abcde=abcde') + FROM http_get(current_setting('http.server_host') || '/response-headers?Abcde=abcde') ) a WHERE field ILIKE 'Abcde'; @@ -20,14 +30,14 @@ SELECT status, content::json->'args'->>'foo' AS args, content::json->>'url' AS url, content::json->>'method' AS method -FROM http_get('http://localhost:9080/anything?foo=bar'); +FROM http_get(current_setting('http.server_host') || '/anything?foo=bar'); -- GET with data SELECT status, content::json->'args'->>'this' AS args, content::json->>'url' AS url, content::json->>'method' AS method -FROM http_get('http://localhost:9080/anything', jsonb_build_object('this', 'that')); +FROM http_get(current_setting('http.server_host') || '/anything', jsonb_build_object('this', 'that')); -- GET with data SELECT status, @@ -35,14 +45,14 @@ content::json->>'args' as args, (content::json)->>'data' as data, content::json->>'url' as url, content::json->>'method' as method -FROM http(('GET', 'http://localhost:9080/anything', NULL, 'application/json', '{"search": "toto"}')); +FROM http(('GET', current_setting('http.server_host') || '/anything', NULL, 'application/json', '{"search": "toto"}')); -- DELETE SELECT status, content::json->'args'->>'foo' AS args, content::json->>'url' AS url, content::json->>'method' AS method -FROM http_delete('http://localhost:9080/anything?foo=bar'); +FROM http_delete(current_setting('http.server_host') || '/anything?foo=bar'); -- DELETE with payload SELECT status, @@ -50,7 +60,7 @@ content::json->'args'->>'foo' AS args, content::json->>'url' AS url, content::json->>'method' AS method, content::json->>'data' AS data -FROM http_delete('http://localhost:9080/anything?foo=bar', 'payload', 'text/plain'); +FROM http_delete(current_setting('http.server_host') || '/anything?foo=bar', 'payload', 'text/plain'); -- PUT SELECT status, @@ -58,7 +68,7 @@ content::json->>'data' AS data, content::json->'args'->>'foo' AS args, content::json->>'url' AS url, content::json->>'method' AS method -FROM http_put('http://localhost:9080/anything?foo=bar','payload','text/plain'); +FROM http_put(current_setting('http.server_host') || '/anything?foo=bar','payload','text/plain'); -- PATCH SELECT status, @@ -66,7 +76,7 @@ content::json->>'data' AS data, content::json->'args'->>'foo' AS args, content::json->>'url' AS url, content::json->>'method' AS method -FROM http_patch('http://localhost:9080/anything?foo=bar','{"this":"that"}','application/json'); +FROM http_patch(current_setting('http.server_host') || '/anything?foo=bar','{"this":"that"}','application/json'); -- POST SELECT status, @@ -74,14 +84,14 @@ content::json->>'data' AS data, content::json->'args'->>'foo' AS args, content::json->>'url' AS url, content::json->>'method' AS method -FROM http_post('http://localhost:9080/anything?foo=bar','payload','text/plain'); +FROM http_post(current_setting('http.server_host') || '/anything?foo=bar','payload','text/plain'); -- POST with json data SELECT status, content::json->'form'->>'this' AS args, content::json->>'url' AS url, content::json->>'method' AS method -FROM http_post('http://localhost:9080/anything', jsonb_build_object('this', 'that')); +FROM http_post(current_setting('http.server_host') || '/anything', jsonb_build_object('this', 'that')); -- POST with data SELECT status, @@ -89,25 +99,25 @@ content::json->'form'->>'key1' AS key1, content::json->'form'->>'key2' AS key2, content::json->>'url' AS url, content::json->>'method' AS method -FROM http_post('http://localhost:9080/anything', 'key1=value1&key2=value2','application/x-www-form-urlencoded'); +FROM http_post(current_setting('http.server_host') || '/anything', 'key1=value1&key2=value2','application/x-www-form-urlencoded'); -- HEAD SELECT lower(field) AS field, value FROM ( SELECT (unnest(headers)).* - FROM http_head('http://localhost:9080/response-headers?Abcde=abcde') + FROM http_head(current_setting('http.server_host') || '/response-headers?Abcde=abcde') ) a WHERE field ILIKE 'Abcde'; -- Follow redirect SELECT status, (content::json)->>'url' AS url -FROM http_get('http://localhost:9080/redirect-to?url=get'); +FROM http_get(current_setting('http.server_host') || '/redirect-to?url=get'); -- Request image WITH http AS ( - SELECT * FROM http_get('http://localhost:9080/image/png') + SELECT * FROM http_get(current_setting('http.server_host') || '/image/png') ), headers AS ( SELECT (unnest(headers)).* FROM http @@ -123,7 +133,7 @@ SELECT http_set_curlopt('CURLOPT_PROXY', '127.0.0.1'); -- Error because proxy is not there DO $$ BEGIN - SELECT status FROM http_get('http://localhost:9080/status/555'); + SELECT status FROM http_get(current_setting('http.server_host') || '/status/555'); EXCEPTION WHEN OTHERS THEN RAISE WARNING 'Failed to connect'; @@ -132,7 +142,7 @@ $$; -- Still an error DO $$ BEGIN - SELECT status FROM http_get('http://localhost:9080/status/555'); + SELECT status FROM http_get(current_setting('http.server_host') || '/status/555'); EXCEPTION WHEN OTHERS THEN RAISE WARNING 'Failed to connect'; @@ -141,9 +151,46 @@ $$; -- Reset options SELECT http_reset_curlopt(); -- Now it should work -SELECT status FROM http_get('http://localhost:9080/status/555'); +SELECT status FROM http_get(current_setting('http.server_host') || '/status/555'); -- Alter the default timeout and then run a query that is longer than -- the default (5s), but shorter than the new timeout SELECT http_set_curlopt('CURLOPT_TIMEOUT_MS', '10000'); -SELECT status FROM http_get('http://localhost:9080/delay/7'); +SELECT status FROM http_get(current_setting('http.server_host') || '/delay/7'); + +-- Test new GUC feature +SET http.CURLOPT_TIMEOUT_MS = '10'; +-- should fail +-- Still an error +DO $$ +BEGIN + SELECT status FROM http_get(current_setting('http.server_host') || '/delay/7'); +EXCEPTION + WHEN OTHERS THEN + RAISE WARNING 'Failed to connect'; +END; +$$; + +SET http.CURLOPT_TIMEOUT_MS = '10000'; +--should pass +SELECT status FROM http_get(current_setting('http.server_host') || '/delay/7'); + +-- SET to bogus file +SET http.CURLOPT_CAINFO = '/path/to/somebundle.crt'; + +-- should fail +SELECT status FROM http_get('https://postgis.net'); + +-- set to ignore cert +SET http.CURLOPT_SSL_VERIFYPEER = '0'; + +-- should pass +SELECT status FROM http_get('https://postgis.net'); + +SHOW http.CURLOPT_CAINFO; + +-- reset it +RESET http.CURLOPT_CAINFO; + +SHOW http.CURLOPT_CAINFO; + From 251ad8cc6eba6dfce17045f8906e839faaa8d539 Mon Sep 17 00:00:00 2001 From: Regina Obe Date: Fri, 24 Jan 2025 20:33:18 -0500 Subject: [PATCH 2/2] Get rid of url as different based on host Fail cert error --- expected/http.out | 97 +++++++++++++++++++++++++---------------------- sql/http.sql | 29 ++++++++------ 2 files changed, 68 insertions(+), 58 deletions(-) diff --git a/expected/http.out b/expected/http.out index 364baf1..570dcbd 100644 --- a/expected/http.out +++ b/expected/http.out @@ -40,117 +40,115 @@ WHERE field ILIKE 'Abcde'; -- GET SELECT status, content::json->'args'->>'foo' AS args, -content::json->>'url' AS url, content::json->>'method' AS method FROM http_get(current_setting('http.server_host') || '/anything?foo=bar'); - status | args | url | method ---------+------+-------------------------------------+-------- - 200 | bar | http://httpbin.org/anything?foo=bar | GET + status | args | method +--------+------+-------- + 200 | bar | GET (1 row) -- GET with data SELECT status, content::json->'args'->>'this' AS args, -content::json->>'url' AS url, +replace(content::json->>'url',current_setting('http.server_host'),'') AS path, content::json->>'method' AS method FROM http_get(current_setting('http.server_host') || '/anything', jsonb_build_object('this', 'that')); - status | args | url | method ---------+------+---------------------------------------+-------- - 200 | that | http://httpbin.org/anything?this=that | GET + status | args | path | method +--------+------+---------------------+-------- + 200 | that | /anything?this=that | GET (1 row) -- GET with data SELECT status, content::json->>'args' as args, (content::json)->>'data' as data, -content::json->>'url' as url, content::json->>'method' as method FROM http(('GET', current_setting('http.server_host') || '/anything', NULL, 'application/json', '{"search": "toto"}')); - status | args | data | url | method ---------+------+--------------------+-----------------------------+-------- - 200 | {} | {"search": "toto"} | http://httpbin.org/anything | GET + status | args | data | method +--------+------+--------------------+-------- + 200 | {} | {"search": "toto"} | GET (1 row) -- DELETE SELECT status, content::json->'args'->>'foo' AS args, -content::json->>'url' AS url, +replace(content::json->>'url',current_setting('http.server_host'),'') AS path, content::json->>'method' AS method FROM http_delete(current_setting('http.server_host') || '/anything?foo=bar'); - status | args | url | method ---------+------+-------------------------------------+-------- - 200 | bar | http://httpbin.org/anything?foo=bar | DELETE + status | args | path | method +--------+------+-------------------+-------- + 200 | bar | /anything?foo=bar | DELETE (1 row) -- DELETE with payload SELECT status, content::json->'args'->>'foo' AS args, -content::json->>'url' AS url, +replace(content::json->>'url',current_setting('http.server_host'),'') AS path, content::json->>'method' AS method, content::json->>'data' AS data FROM http_delete(current_setting('http.server_host') || '/anything?foo=bar', 'payload', 'text/plain'); - status | args | url | method | data ---------+------+-------------------------------------+--------+--------- - 200 | bar | http://httpbin.org/anything?foo=bar | DELETE | payload + status | args | path | method | data +--------+------+-------------------+--------+--------- + 200 | bar | /anything?foo=bar | DELETE | payload (1 row) -- PUT SELECT status, content::json->>'data' AS data, content::json->'args'->>'foo' AS args, -content::json->>'url' AS url, +replace(content::json->>'url', current_setting('http.server_host'),'') AS path, content::json->>'method' AS method FROM http_put(current_setting('http.server_host') || '/anything?foo=bar','payload','text/plain'); - status | data | args | url | method ---------+---------+------+-------------------------------------+-------- - 200 | payload | bar | http://httpbin.org/anything?foo=bar | PUT + status | data | args | path | method +--------+---------+------+-------------------+-------- + 200 | payload | bar | /anything?foo=bar | PUT (1 row) -- PATCH SELECT status, content::json->>'data' AS data, content::json->'args'->>'foo' AS args, -content::json->>'url' AS url, +replace(content::json->>'url', current_setting('http.server_host'),'') AS path, content::json->>'method' AS method FROM http_patch(current_setting('http.server_host') || '/anything?foo=bar','{"this":"that"}','application/json'); - status | data | args | url | method ---------+-----------------+------+-------------------------------------+-------- - 200 | {"this":"that"} | bar | http://httpbin.org/anything?foo=bar | PATCH + status | data | args | path | method +--------+-----------------+------+-------------------+-------- + 200 | {"this":"that"} | bar | /anything?foo=bar | PATCH (1 row) -- POST SELECT status, content::json->>'data' AS data, content::json->'args'->>'foo' AS args, -content::json->>'url' AS url, +replace(content::json->>'url', current_setting('http.server_host'),'') AS path, content::json->>'method' AS method FROM http_post(current_setting('http.server_host') || '/anything?foo=bar','payload','text/plain'); - status | data | args | url | method ---------+---------+------+-------------------------------------+-------- - 200 | payload | bar | http://httpbin.org/anything?foo=bar | POST + status | data | args | path | method +--------+---------+------+-------------------+-------- + 200 | payload | bar | /anything?foo=bar | POST (1 row) -- POST with json data SELECT status, content::json->'form'->>'this' AS args, -content::json->>'url' AS url, +replace(content::json->>'url', current_setting('http.server_host'),'') AS path, content::json->>'method' AS method FROM http_post(current_setting('http.server_host') || '/anything', jsonb_build_object('this', 'that')); - status | args | url | method ---------+------+-----------------------------+-------- - 200 | that | http://httpbin.org/anything | POST + status | args | path | method +--------+------+-----------+-------- + 200 | that | /anything | POST (1 row) -- POST with data SELECT status, content::json->'form'->>'key1' AS key1, content::json->'form'->>'key2' AS key2, -content::json->>'url' AS url, +replace(content::json->>'url', current_setting('http.server_host'),'') AS path, content::json->>'method' AS method FROM http_post(current_setting('http.server_host') || '/anything', 'key1=value1&key2=value2','application/x-www-form-urlencoded'); - status | key1 | key2 | url | method ---------+--------+--------+-----------------------------+-------- - 200 | value1 | value2 | http://httpbin.org/anything | POST + status | key1 | key2 | path | method +--------+--------+--------+-----------+-------- + 200 | value1 | value2 | /anything | POST (1 row) -- HEAD @@ -167,11 +165,11 @@ WHERE field ILIKE 'Abcde'; -- Follow redirect SELECT status, -(content::json)->>'url' AS url +replace((content::json)->>'url', current_setting('http.server_host'),'') AS path FROM http_get(current_setting('http.server_host') || '/redirect-to?url=get'); - status | url ---------+------------------------ - 200 | http://httpbin.org/get + status | path +--------+------ + 200 | /get (1 row) -- Request image @@ -271,8 +269,15 @@ SELECT status FROM http_get(current_setting('http.server_host') || '/delay/7'); -- SET to bogus file SET http.CURLOPT_CAINFO = '/path/to/somebundle.crt'; -- should fail -SELECT status FROM http_get('https://postgis.net'); -ERROR: error setting certificate file: /path/to/somebundle.crt +DO $$ +BEGIN + SELECT status FROM http_get('https://postgis.net'); +EXCEPTION + WHEN OTHERS THEN + RAISE WARNING 'Invalid cert file'; +END; +$$; +WARNING: Invalid cert file -- set to ignore cert SET http.CURLOPT_SSL_VERIFYPEER = '0'; -- should pass diff --git a/sql/http.sql b/sql/http.sql index 3e2e0e0..2fc6a03 100644 --- a/sql/http.sql +++ b/sql/http.sql @@ -28,14 +28,13 @@ WHERE field ILIKE 'Abcde'; -- GET SELECT status, content::json->'args'->>'foo' AS args, -content::json->>'url' AS url, content::json->>'method' AS method FROM http_get(current_setting('http.server_host') || '/anything?foo=bar'); -- GET with data SELECT status, content::json->'args'->>'this' AS args, -content::json->>'url' AS url, +replace(content::json->>'url',current_setting('http.server_host'),'') AS path, content::json->>'method' AS method FROM http_get(current_setting('http.server_host') || '/anything', jsonb_build_object('this', 'that')); @@ -43,21 +42,20 @@ FROM http_get(current_setting('http.server_host') || '/anything', jsonb_build_ob SELECT status, content::json->>'args' as args, (content::json)->>'data' as data, -content::json->>'url' as url, content::json->>'method' as method FROM http(('GET', current_setting('http.server_host') || '/anything', NULL, 'application/json', '{"search": "toto"}')); -- DELETE SELECT status, content::json->'args'->>'foo' AS args, -content::json->>'url' AS url, +replace(content::json->>'url',current_setting('http.server_host'),'') AS path, content::json->>'method' AS method FROM http_delete(current_setting('http.server_host') || '/anything?foo=bar'); -- DELETE with payload SELECT status, content::json->'args'->>'foo' AS args, -content::json->>'url' AS url, +replace(content::json->>'url',current_setting('http.server_host'),'') AS path, content::json->>'method' AS method, content::json->>'data' AS data FROM http_delete(current_setting('http.server_host') || '/anything?foo=bar', 'payload', 'text/plain'); @@ -66,7 +64,7 @@ FROM http_delete(current_setting('http.server_host') || '/anything?foo=bar', 'pa SELECT status, content::json->>'data' AS data, content::json->'args'->>'foo' AS args, -content::json->>'url' AS url, +replace(content::json->>'url', current_setting('http.server_host'),'') AS path, content::json->>'method' AS method FROM http_put(current_setting('http.server_host') || '/anything?foo=bar','payload','text/plain'); @@ -74,7 +72,7 @@ FROM http_put(current_setting('http.server_host') || '/anything?foo=bar','payloa SELECT status, content::json->>'data' AS data, content::json->'args'->>'foo' AS args, -content::json->>'url' AS url, +replace(content::json->>'url', current_setting('http.server_host'),'') AS path, content::json->>'method' AS method FROM http_patch(current_setting('http.server_host') || '/anything?foo=bar','{"this":"that"}','application/json'); @@ -82,14 +80,14 @@ FROM http_patch(current_setting('http.server_host') || '/anything?foo=bar','{"th SELECT status, content::json->>'data' AS data, content::json->'args'->>'foo' AS args, -content::json->>'url' AS url, +replace(content::json->>'url', current_setting('http.server_host'),'') AS path, content::json->>'method' AS method FROM http_post(current_setting('http.server_host') || '/anything?foo=bar','payload','text/plain'); -- POST with json data SELECT status, content::json->'form'->>'this' AS args, -content::json->>'url' AS url, +replace(content::json->>'url', current_setting('http.server_host'),'') AS path, content::json->>'method' AS method FROM http_post(current_setting('http.server_host') || '/anything', jsonb_build_object('this', 'that')); @@ -97,7 +95,7 @@ FROM http_post(current_setting('http.server_host') || '/anything', jsonb_build_o SELECT status, content::json->'form'->>'key1' AS key1, content::json->'form'->>'key2' AS key2, -content::json->>'url' AS url, +replace(content::json->>'url', current_setting('http.server_host'),'') AS path, content::json->>'method' AS method FROM http_post(current_setting('http.server_host') || '/anything', 'key1=value1&key2=value2','application/x-www-form-urlencoded'); @@ -111,7 +109,7 @@ WHERE field ILIKE 'Abcde'; -- Follow redirect SELECT status, -(content::json)->>'url' AS url +replace((content::json)->>'url', current_setting('http.server_host'),'') AS path FROM http_get(current_setting('http.server_host') || '/redirect-to?url=get'); -- Request image @@ -179,7 +177,14 @@ SELECT status FROM http_get(current_setting('http.server_host') || '/delay/7'); SET http.CURLOPT_CAINFO = '/path/to/somebundle.crt'; -- should fail -SELECT status FROM http_get('https://postgis.net'); +DO $$ +BEGIN + SELECT status FROM http_get('https://postgis.net'); +EXCEPTION + WHEN OTHERS THEN + RAISE WARNING 'Invalid cert file'; +END; +$$; -- set to ignore cert SET http.CURLOPT_SSL_VERIFYPEER = '0';