From ad14a83d5102203ed84c34c8da2c9334ca3e93b7 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Wed, 18 Feb 2015 22:10:05 -0500 Subject: [PATCH] uri_port returns integer URI syntax only supports numeric ports. --- pguri.c | 12 ++++++------ pguri.sql | 2 +- test/expected/test.out | 27 ++++++++++++++++++++++----- test/sql/test.sql | 2 ++ 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/pguri.c b/pguri.c index 17f7481..8d4d3cb 100644 --- a/pguri.c +++ b/pguri.c @@ -172,15 +172,15 @@ uri_port(PG_FUNCTION_ARGS) Datum arg = PG_GETARG_DATUM(0); char *s = TextDatumGetCString(arg); UriUriA uri; - text *result; + const char *p; parse_uri(s, &uri); - result = uri_text_range_to_text(uri.portText); - uriFreeUriMembersA(&uri); - if (result) - PG_RETURN_TEXT_P(result); - else + if (!uri.portText.first || !uri.portText.afterLast + || uri.portText.afterLast == uri.portText.first) PG_RETURN_NULL(); + p = pnstrdup(uri.portText.first, uri.portText.afterLast - uri.portText.first); + uriFreeUriMembersA(&uri); + PG_RETURN_INT32(strtol(p, NULL, 10)); } PG_FUNCTION_INFO_V1(uri_query); diff --git a/pguri.sql b/pguri.sql index 0fe9929..0881131 100644 --- a/pguri.sql +++ b/pguri.sql @@ -50,7 +50,7 @@ CREATE FUNCTION uri_host_inet(uri) RETURNS inet LANGUAGE C AS '$libdir/pguri'; -CREATE FUNCTION uri_port(uri) RETURNS text +CREATE FUNCTION uri_port(uri) RETURNS integer IMMUTABLE STRICT LANGUAGE C diff --git a/test/expected/test.out b/test/expected/test.out index 53a1c99..0e82ac7 100644 --- a/test/expected/test.out +++ b/test/expected/test.out @@ -10,6 +10,7 @@ VALUES ('http://www.postgresql.org/'), ('http://admin:password@192.168.0.1'), ('http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html'), ('http://[1080::8:800:200C:417A]/foo'), + ('http://host:'), (''), ('foobar'); SELECT * FROM test; @@ -24,9 +25,10 @@ SELECT * FROM test; 7 | http://admin:password@192.168.0.1 8 | http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html 9 | http://[1080::8:800:200C:417A]/foo - 10 | - 11 | foobar -(11 rows) + 10 | http://host: + 11 | + 12 | foobar +(12 rows) -- error cases SELECT uri ':'; @@ -37,6 +39,10 @@ SELECT uri 'foo bar'; ERROR: invalid input syntax for type uri at or near " bar" LINE 1: SELECT uri 'foo bar'; ^ +SELECT uri 'http://host:port/'; +ERROR: invalid input syntax for type uri at or near "/" +LINE 1: SELECT uri 'http://host:port/'; + ^ \x on SELECT b AS uri, uri_scheme(b), @@ -139,6 +145,16 @@ uri_path | {foo} uri_query | _null_ uri_fragment | _null_ -[ RECORD 10 ]+---------------------------------------------------------------------------------------- +uri | http://host: +uri_scheme | http +uri_userinfo | _null_ +uri_host | host +uri_host_inet | _null_ +uri_port | _null_ +uri_path | {} +uri_query | _null_ +uri_fragment | _null_ +-[ RECORD 11 ]+---------------------------------------------------------------------------------------- uri | uri_scheme | _null_ uri_userinfo | _null_ @@ -148,7 +164,7 @@ uri_port | _null_ uri_path | {} uri_query | _null_ uri_fragment | _null_ --[ RECORD 11 ]+---------------------------------------------------------------------------------------- +-[ RECORD 12 ]+---------------------------------------------------------------------------------------- uri | foobar uri_scheme | _null_ uri_userinfo | _null_ @@ -169,10 +185,11 @@ SELECT DISTINCT b FROM test ORDER BY b; http://[1080::8:800:200C:417A]/foo http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html http://admin:password@192.168.0.1 + http://host: http://www.postgresql.org/ http://www.postgresql.org/docs/devel/static/xfunc-sql.html#XFUNC-SQL-FUNCTION-ARGUMENTS https://duckduckgo.com/?q=postgresql&ia=about mailto:foo@example.com ssh://username@review.openstack.org:29418/openstack/nova.git -(11 rows) +(12 rows) diff --git a/test/sql/test.sql b/test/sql/test.sql index 0faa0cb..c113fd3 100644 --- a/test/sql/test.sql +++ b/test/sql/test.sql @@ -12,6 +12,7 @@ VALUES ('http://www.postgresql.org/'), ('http://admin:password@192.168.0.1'), ('http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html'), ('http://[1080::8:800:200C:417A]/foo'), + ('http://host:'), (''), ('foobar'); @@ -20,6 +21,7 @@ SELECT * FROM test; -- error cases SELECT uri ':'; SELECT uri 'foo bar'; +SELECT uri 'http://host:port/'; \x on