uri_port returns integer

URI syntax only supports numeric ports.
This commit is contained in:
Peter Eisentraut 2015-02-18 22:10:05 -05:00
parent 67a513742a
commit ad14a83d51
4 changed files with 31 additions and 12 deletions

12
pguri.c
View File

@ -172,15 +172,15 @@ uri_port(PG_FUNCTION_ARGS)
Datum arg = PG_GETARG_DATUM(0); Datum arg = PG_GETARG_DATUM(0);
char *s = TextDatumGetCString(arg); char *s = TextDatumGetCString(arg);
UriUriA uri; UriUriA uri;
text *result; const char *p;
parse_uri(s, &uri); parse_uri(s, &uri);
result = uri_text_range_to_text(uri.portText); if (!uri.portText.first || !uri.portText.afterLast
uriFreeUriMembersA(&uri); || uri.portText.afterLast == uri.portText.first)
if (result)
PG_RETURN_TEXT_P(result);
else
PG_RETURN_NULL(); 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); PG_FUNCTION_INFO_V1(uri_query);

View File

@ -50,7 +50,7 @@ CREATE FUNCTION uri_host_inet(uri) RETURNS inet
LANGUAGE C LANGUAGE C
AS '$libdir/pguri'; AS '$libdir/pguri';
CREATE FUNCTION uri_port(uri) RETURNS text CREATE FUNCTION uri_port(uri) RETURNS integer
IMMUTABLE IMMUTABLE
STRICT STRICT
LANGUAGE C LANGUAGE C

View File

@ -10,6 +10,7 @@ VALUES ('http://www.postgresql.org/'),
('http://admin:password@192.168.0.1'), ('http://admin:password@192.168.0.1'),
('http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html'), ('http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html'),
('http://[1080::8:800:200C:417A]/foo'), ('http://[1080::8:800:200C:417A]/foo'),
('http://host:'),
(''), (''),
('foobar'); ('foobar');
SELECT * FROM test; SELECT * FROM test;
@ -24,9 +25,10 @@ SELECT * FROM test;
7 | http://admin:password@192.168.0.1 7 | http://admin:password@192.168.0.1
8 | http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html 8 | http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html
9 | http://[1080::8:800:200C:417A]/foo 9 | http://[1080::8:800:200C:417A]/foo
10 | 10 | http://host:
11 | foobar 11 |
(11 rows) 12 | foobar
(12 rows)
-- error cases -- error cases
SELECT uri ':'; SELECT uri ':';
@ -37,6 +39,10 @@ SELECT uri 'foo bar';
ERROR: invalid input syntax for type uri at or near " bar" ERROR: invalid input syntax for type uri at or near " bar"
LINE 1: SELECT uri 'foo 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 \x on
SELECT b AS uri, SELECT b AS uri,
uri_scheme(b), uri_scheme(b),
@ -139,6 +145,16 @@ uri_path | {foo}
uri_query | _null_ uri_query | _null_
uri_fragment | _null_ uri_fragment | _null_
-[ RECORD 10 ]+---------------------------------------------------------------------------------------- -[ 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 |
uri_scheme | _null_ uri_scheme | _null_
uri_userinfo | _null_ uri_userinfo | _null_
@ -148,7 +164,7 @@ uri_port | _null_
uri_path | {} uri_path | {}
uri_query | _null_ uri_query | _null_
uri_fragment | _null_ uri_fragment | _null_
-[ RECORD 11 ]+---------------------------------------------------------------------------------------- -[ RECORD 12 ]+----------------------------------------------------------------------------------------
uri | foobar uri | foobar
uri_scheme | _null_ uri_scheme | _null_
uri_userinfo | _null_ uri_userinfo | _null_
@ -169,10 +185,11 @@ SELECT DISTINCT b FROM test ORDER BY b;
http://[1080::8:800:200C:417A]/foo http://[1080::8:800:200C:417A]/foo
http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html
http://admin:password@192.168.0.1 http://admin:password@192.168.0.1
http://host:
http://www.postgresql.org/ http://www.postgresql.org/
http://www.postgresql.org/docs/devel/static/xfunc-sql.html#XFUNC-SQL-FUNCTION-ARGUMENTS http://www.postgresql.org/docs/devel/static/xfunc-sql.html#XFUNC-SQL-FUNCTION-ARGUMENTS
https://duckduckgo.com/?q=postgresql&ia=about https://duckduckgo.com/?q=postgresql&ia=about
mailto:foo@example.com mailto:foo@example.com
ssh://username@review.openstack.org:29418/openstack/nova.git ssh://username@review.openstack.org:29418/openstack/nova.git
(11 rows) (12 rows)

View File

@ -12,6 +12,7 @@ VALUES ('http://www.postgresql.org/'),
('http://admin:password@192.168.0.1'), ('http://admin:password@192.168.0.1'),
('http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html'), ('http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html'),
('http://[1080::8:800:200C:417A]/foo'), ('http://[1080::8:800:200C:417A]/foo'),
('http://host:'),
(''), (''),
('foobar'); ('foobar');
@ -20,6 +21,7 @@ SELECT * FROM test;
-- error cases -- error cases
SELECT uri ':'; SELECT uri ':';
SELECT uri 'foo bar'; SELECT uri 'foo bar';
SELECT uri 'http://host:port/';
\x on \x on