diff --git a/http--1.0--1.1.sql b/http--1.0--1.1.sql index 9841d7e..3fbbd7b 100644 --- a/http--1.0--1.1.sql +++ b/http--1.0--1.1.sql @@ -22,18 +22,18 @@ CREATE TYPE http_header AS ( ); CREATE TYPE http_request AS ( - method http_method, + method http_method, uri VARCHAR, - headers http_header[], - content_type VARCHAR, - content VARCHAR + headers http_header[], + content_type VARCHAR, + content VARCHAR ); ALTER TYPE http_response ALTER ATTRIBUTE headers TYPE http_header[]; CREATE OR REPLACE FUNCTION http_header (field VARCHAR, value VARCHAR) RETURNS http_header - AS $$ SELECT field, value $$ + AS $$ SELECT $1, $2 $$ LANGUAGE 'sql'; CREATE OR REPLACE FUNCTION http(request http_request) @@ -42,23 +42,23 @@ CREATE OR REPLACE FUNCTION http(request http_request) LANGUAGE 'c'; CREATE OR REPLACE FUNCTION http_get(uri VARCHAR) - RETURNS http_response - AS $$ SELECT http(('GET', uri, NULL, NULL, NULL)::http_request) $$ + RETURNS http_response + AS $$ SELECT http(('GET', $1, NULL, NULL, NULL)::http_request) $$ LANGUAGE 'sql'; CREATE OR REPLACE FUNCTION http_post(uri VARCHAR, content VARCHAR, content_type VARCHAR) - RETURNS http_response - AS $$ SELECT http(('POST', uri, NULL, content_type, content)::http_request) $$ + RETURNS http_response + AS $$ SELECT http(('POST', $1, NULL, $2, $3)::http_request) $$ LANGUAGE 'sql'; CREATE OR REPLACE FUNCTION http_put(uri VARCHAR, content VARCHAR, content_type VARCHAR) - RETURNS http_response - AS $$ SELECT http(('PUT', uri, NULL, content_type, content)::http_request) $$ + RETURNS http_response + AS $$ SELECT http(('PUT', $1, NULL, $2, $3)::http_request) $$ LANGUAGE 'sql'; CREATE OR REPLACE FUNCTION http_delete(uri VARCHAR) RETURNS http_response - AS $$ SELECT http(('DELETE', uri, NULL, NULL, NULL)::http_request) $$ + AS $$ SELECT http(('DELETE', $1, NULL, NULL, NULL)::http_request) $$ LANGUAGE 'sql'; CREATE OR REPLACE FUNCTION urlencode(string VARCHAR) diff --git a/http--1.1.sql b/http--1.1.sql index 044c31d..8e8adea 100644 --- a/http--1.1.sql +++ b/http--1.1.sql @@ -36,7 +36,7 @@ CREATE TYPE http_request AS ( CREATE OR REPLACE FUNCTION http_header (field VARCHAR, value VARCHAR) RETURNS http_header - AS $$ SELECT field, value $$ + AS $$ SELECT $1, $2 $$ LANGUAGE 'sql'; CREATE OR REPLACE FUNCTION http(request http_request) @@ -46,22 +46,22 @@ CREATE OR REPLACE FUNCTION http(request http_request) CREATE OR REPLACE FUNCTION http_get(uri VARCHAR) RETURNS http_response - AS $$ SELECT http(('GET', uri, NULL, NULL, NULL)::http_request) $$ + AS $$ SELECT http(('GET', $1, NULL, NULL, NULL)::http_request) $$ LANGUAGE 'sql'; CREATE OR REPLACE FUNCTION http_post(uri VARCHAR, content VARCHAR, content_type VARCHAR) RETURNS http_response - AS $$ SELECT http(('POST', uri, NULL, content_type, content)::http_request) $$ + AS $$ SELECT http(('POST', $1, NULL, $2, $3)::http_request) $$ LANGUAGE 'sql'; CREATE OR REPLACE FUNCTION http_put(uri VARCHAR, content VARCHAR, content_type VARCHAR) RETURNS http_response - AS $$ SELECT http(('PUT', uri, NULL, content_type, content)::http_request) $$ + AS $$ SELECT http(('PUT', $1, NULL, $2, $3)::http_request) $$ LANGUAGE 'sql'; CREATE OR REPLACE FUNCTION http_delete(uri VARCHAR) RETURNS http_response - AS $$ SELECT http(('DELETE', uri, NULL, NULL, NULL)::http_request) $$ + AS $$ SELECT http(('DELETE', $1, NULL, NULL, NULL)::http_request) $$ LANGUAGE 'sql'; CREATE OR REPLACE FUNCTION urlencode(string VARCHAR) diff --git a/http.c b/http.c index eaee3cc..751ac7a 100644 --- a/http.c +++ b/http.c @@ -404,7 +404,8 @@ Datum http_request(PG_FUNCTION_ARGS) /* Processing */ CURL *http_handle = NULL; CURLcode err; - char *http_error_buffer = NULL; + char http_error_buffer[CURL_ERROR_SIZE]; + struct curl_slist *headers = NULL; StringInfoData si_data; StringInfoData si_headers; @@ -430,6 +431,9 @@ Datum http_request(PG_FUNCTION_ARGS) * Build and run a curl request from the http_request argument *************************************************************************/ + /* Zero out static memory */ + memset(http_error_buffer, 0, sizeof(http_error_buffer)); + /* Extract type info from the tuple itself */ tup_type = HeapTupleHeaderGetTypeId(rec); tup_typmod = HeapTupleHeaderGetTypMod(rec); @@ -473,7 +477,6 @@ Datum http_request(PG_FUNCTION_ARGS) CURL_SETOPT(http_handle, CURLOPT_FORBID_REUSE, 1); /* Set up the error buffer */ - http_error_buffer = palloc0(CURL_ERROR_SIZE); CURL_SETOPT(http_handle, CURLOPT_ERRORBUFFER, http_error_buffer); /* Set up the write-back function */ @@ -574,19 +577,23 @@ Datum http_request(PG_FUNCTION_ARGS) *************************************************************************/ /* Write out an error on failure */ - if ( http_return ) + if ( http_return != CURLE_OK ) { - curl_easy_cleanup(http_handle); curl_slist_free_all(headers); - ereport(ERROR, (errmsg("CURL: %s", http_error_buffer))); + curl_easy_cleanup(http_handle); + + if ( strlen(http_error_buffer) > 0 ) + ereport(ERROR, (errmsg("%s", http_error_buffer))); + else + ereport(ERROR, (errmsg("%s", curl_easy_strerror(http_return)))); } /* Read the metadata from the handle directly */ if ( (CURLE_OK != curl_easy_getinfo(http_handle, CURLINFO_RESPONSE_CODE, &status)) || (CURLE_OK != curl_easy_getinfo(http_handle, CURLINFO_CONTENT_TYPE, &content_type)) ) { - curl_easy_cleanup(http_handle); curl_slist_free_all(headers); + curl_easy_cleanup(http_handle); ereport(ERROR, (errmsg("CURL: Error in curl_easy_getinfo"))); } @@ -646,7 +653,6 @@ Datum http_request(PG_FUNCTION_ARGS) ReleaseTupleDesc(tup_desc); curl_easy_cleanup(http_handle); curl_slist_free_all(headers); - pfree(http_error_buffer); pfree(si_headers.data); pfree(si_data.data); pfree(values);