Skip to content

Commit

Permalink
Merge pull request pramsey#35 from tbussmann/master
Browse files Browse the repository at this point in the history
Add HEAD method
  • Loading branch information
pramsey authored Jun 8, 2017
2 parents fd54772 + fcc35a5 commit 54da5c8
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 12 deletions.
6 changes: 3 additions & 3 deletions META.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "http",
"abstract": "HTTP client for PostgreSQL",
"description": "HTTP allows you to get the content of a web page in a SQL function call.",
"version": "1.1",
"version": "1.2",
"maintainer": [
"Paul Ramsey <[email protected]>"
],
Expand All @@ -21,9 +21,9 @@
},
"provides": {
"http": {
"file": "http--1.1.sql",
"file": "http--1.2.sql",
"docfile": "README.md",
"version": "1.1",
"version": "1.2",
"abstract": "HTTP client for PostgreSQL"
},
},
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
MODULE_big = http
OBJS = http.o
EXTENSION = http
DATA = http--1.1.sql http--1.0--1.1.sql
DATA = http--1.2.sql http--1.1--1.2.sql http--1.0--1.1.sql
REGRESS = http
EXTRA_CLEAN =

Expand Down
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ To access binary content, you must coerce the content from the default `varchar`
--------------+---------------+----------------
image/gif | 31958 | 31958

To access only the headers you can do a HEAD-Request. This will not follow redirections.

SELECT
http.status,
headers.value AS location
FROM
http_head('http://google.com') AS http
LEFT OUTER JOIN LATERAL (SELECT value
FROM unnest(http.headers)
WHERE field = 'Location') AS headers
ON true;

status | location
--------+-----------------------------------------------------------
302 | http://www.google.ch/?gfe_rd=cr&ei=ACESWLy_KuvI8zeghL64Ag

## Concepts

Every HTTP call is a made up of an `http_request` and an `http_response`.
Expand All @@ -144,7 +160,7 @@ Every HTTP call is a made up of an `http_request` and an `http_response`.
headers | http_header[] |
content | character varying |

The utility functions, `http_get()`, `http_post()`, `http_put()`, and `http_delete()` are just wrappers around a master function, `http(http_request)` that returns `http_response`.
The utility functions, `http_get()`, `http_post()`, `http_put()`, `http_delete()` and `http_head()` are just wrappers around a master function, `http(http_request)` that returns `http_response`.

The `headers` field for requests and response is a PostgreSQL array of type `http_header` which is just a simple tuple.

Expand Down Expand Up @@ -178,6 +194,7 @@ By default a 5 second timeout is set for the completion of a request. If a diff
* `http_post(uri VARCHAR, content VARCHAR, content_type VARCHAR)` returns `http_response`
* `http_put(uri VARCHAR, content VARCHAR, content_type VARCHAR)` returns `http_response`
* `http_delete(uri VARCHAR)` returns `http_resonse`
* `http_head(uri VARCHAR)` returns `http_resonse`
* `urlencode(string VARCHAR)` returns `text`

## Installation
Expand Down
14 changes: 14 additions & 0 deletions http--1.1--1.2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

ALTER DOMAIN http_method DROP CONSTRAINT http_method_check;
ALTER DOMAIN http_method ADD CHECK (
VALUE ILIKE 'get' OR
VALUE ILIKE 'post' OR
VALUE ILIKE 'put' OR
VALUE ILIKE 'delete' OR
VALUE ILIKE 'head'
);

CREATE OR REPLACE FUNCTION http_head(uri VARCHAR)
RETURNS http_response
AS $$ SELECT http(('HEAD', $1, NULL, NULL, NULL)::http_request) $$
LANGUAGE 'sql';
8 changes: 7 additions & 1 deletion http--1.1.sql → http--1.2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ CHECK (
VALUE ILIKE 'get' OR
VALUE ILIKE 'post' OR
VALUE ILIKE 'put' OR
VALUE ILIKE 'delete'
VALUE ILIKE 'delete' OR
VALUE ILIKE 'head'
);

CREATE DOMAIN content_type AS text
Expand Down Expand Up @@ -63,6 +64,11 @@ CREATE OR REPLACE FUNCTION http_delete(uri VARCHAR)
RETURNS http_response
AS $$ SELECT http(('DELETE', $1, NULL, NULL, NULL)::http_request) $$
LANGUAGE 'sql';

CREATE OR REPLACE FUNCTION http_head(uri VARCHAR)
RETURNS http_response
AS $$ SELECT http(('HEAD', $1, NULL, NULL, NULL)::http_request) $$
LANGUAGE 'sql';

CREATE OR REPLACE FUNCTION urlencode(string VARCHAR)
RETURNS TEXT
Expand Down
20 changes: 15 additions & 5 deletions http.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ typedef enum {
HTTP_GET,
HTTP_POST,
HTTP_DELETE,
HTTP_PUT
HTTP_PUT,
HTTP_HEAD
} http_method;

/* Components (and postitions) of the http_request tuple type */
Expand Down Expand Up @@ -213,6 +214,8 @@ request_type(const char *method)
return HTTP_PUT;
else if ( strcasecmp(method, "DELETE") == 0 )
return HTTP_DELETE;
else if ( strcasecmp(method, "HEAD") == 0 )
return HTTP_HEAD;
else
return HTTP_GET;
}
Expand Down Expand Up @@ -604,10 +607,13 @@ Datum http_request(PG_FUNCTION_ARGS)
/* Set the HTTP content encoding to gzip */
/*curl_easy_setopt(g_http_handle, CURLOPT_ACCEPT_ENCODING, HTTP_ENCODING);*/

/* Follow redirects, as many as 5 */
CURL_SETOPT(g_http_handle, CURLOPT_FOLLOWLOCATION, 1);
CURL_SETOPT(g_http_handle, CURLOPT_MAXREDIRS, 5);

if ( method != HTTP_HEAD )
{
/* Follow redirects, as many as 5 */
CURL_SETOPT(g_http_handle, CURLOPT_FOLLOWLOCATION, 1);
CURL_SETOPT(g_http_handle, CURLOPT_MAXREDIRS, 5);
}

if ( g_use_keepalive )
{
/* Add a keep alive option to the headers to reuse network sockets */
Expand Down Expand Up @@ -678,6 +684,10 @@ Datum http_request(PG_FUNCTION_ARGS)
{
CURL_SETOPT(g_http_handle, CURLOPT_CUSTOMREQUEST, "DELETE");
}
else if ( method == HTTP_HEAD )
{
CURL_SETOPT(g_http_handle, CURLOPT_NOBODY, 1);
}

/* Set the headers */
CURL_SETOPT(g_http_handle, CURLOPT_HTTPHEADER, headers);
Expand Down
2 changes: 1 addition & 1 deletion http.control
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
default_version = '1.1'
default_version = '1.2'
module_pathname = '$libdir/http'
relocatable = true
comment = 'HTTP client for PostgreSQL, allows web page retrieval inside the database.'

0 comments on commit 54da5c8

Please sign in to comment.