Skip to content

Commit 5a07b4b

Browse files
committed
fix: bad error message on invalid URL scheme
Closes #243
1 parent 3dac34f commit 5a07b4b

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

src/errors.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@
2626
list = new_list; \
2727
} while (0)
2828

29+
// curl_url_strerror is available starting from libcurl 7.80.0
30+
#define EREPORT_CURL_URL_SET(hdl, part, str, flags) \
31+
do { \
32+
CURLUcode rc = curl_url_set(hdl, part, str, flags); \
33+
if (rc != CURLUE_OK) \
34+
ereport(ERROR, errmsg("invalid URL \"%s\": %s", str, curl_url_strerror(rc))); \
35+
} while (0)
36+
37+
#define EREPORT_CURL_URL_GET(hdl, part, out, flags, value) \
38+
do { \
39+
CURLUcode rc = curl_url_get(hdl, part, out, flags); \
40+
if (rc != CURLUE_OK) \
41+
ereport(ERROR, errmsg("failed to encode URL \"%s\": %s", value, curl_url_strerror(rc))); \
42+
} while (0)
43+
2944
#define EREPORT_NULL_ATTR(tupIsNull, attr) \
3045
do { \
3146
if (tupIsNull) ereport(ERROR, errmsg("%s cannot be null", #attr)); \

src/util.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "curl_prelude.h"
44

5+
#include "errors.h"
56
#include "util.h"
67

78
PG_FUNCTION_INFO_V1(_urlencode_string);
@@ -33,30 +34,20 @@ Datum _encode_url_with_params_array(PG_FUNCTION_ARGS) {
3334
bool isnull;
3435
char *param;
3536

36-
CURLU *h = curl_url();
37-
CURLUcode rc = curl_url_set(h, CURLUPART_URL, url, 0);
38-
if (rc != CURLUE_OK) {
39-
// TODO: Use curl_url_strerror once released.
40-
elog(ERROR, "%s", curl_easy_strerror((CURLcode)rc));
41-
}
37+
CURLU *h = curl_url();
38+
EREPORT_CURL_URL_SET(h, CURLUPART_URL, url, 0);
4239

4340
iterator = array_create_iterator(params, 0, NULL);
4441
while (array_iterate(iterator, &value, &isnull)) {
4542
if (isnull) continue;
4643

4744
param = TextDatumGetCString(value);
48-
rc = curl_url_set(h, CURLUPART_QUERY, param, CURLU_APPENDQUERY);
49-
if (rc != CURLUE_OK) {
50-
elog(ERROR, "curl_url returned: %d", rc);
51-
}
45+
EREPORT_CURL_URL_SET(h, CURLUPART_QUERY, param, CURLU_APPENDQUERY);
5246
pfree(param);
5347
}
5448
array_free_iterator(iterator);
5549

56-
rc = curl_url_get(h, CURLUPART_URL, &full_url, 0);
57-
if (rc != CURLUE_OK) {
58-
elog(ERROR, "curl_url returned: %d", rc);
59-
}
50+
EREPORT_CURL_URL_GET(h, CURLUPART_URL, &full_url, 0, url);
6051

6152
pfree(url);
6253
curl_url_cleanup(h);

test/test_http_errors.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,20 @@ def test_get_bad_url(sess):
1515
"""
1616
))
1717

18-
assert r"resolve proxy name" in str(execinfo)
18+
assert 'Unsupported URL scheme' in str(execinfo.value)
19+
20+
21+
def test_http_get_rejects_relative_url(sess):
22+
"""net.http_get with a correct error when given a relative url"""
23+
24+
with pytest.raises(Exception) as execinfo:
25+
sess.execute(text(
26+
"""
27+
select net.http_get('/malformed_url');
28+
"""
29+
))
30+
31+
assert 'invalid URL "/malformed_url"' in str(execinfo.value)
1932

2033

2134
def test_bad_post(sess):

0 commit comments

Comments
 (0)