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);
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);

View File

@ -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

View File

@ -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)

View File

@ -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