Skip to content

Commit

Permalink
Backwards compatible SQL, more erporting
Browse files Browse the repository at this point in the history
  • Loading branch information
pramsey committed May 11, 2015
1 parent 59ad2e8 commit d9011f5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
24 changes: 12 additions & 12 deletions http--1.0--1.1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions http--1.1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
20 changes: 13 additions & 7 deletions http.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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")));
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d9011f5

Please sign in to comment.