cyga / www_fdw

fdw extension for postgres
http://wiki.postgresql.org/wiki/WWW_FDW
123 stars 21 forks source link

Does this support custom headers? #22

Closed varunkumar closed 8 years ago

varunkumar commented 8 years ago

Is there a way to send custom headers to web services?

cyga commented 8 years ago

no, it wasn't implemented.

if you want static configurable header, you can add it in the code:

  1. parse options, grep for "method_update", it has to be similar.
  2. setting headers section:
            /* in the src/www_fdw.c */
            curl_opts = curl_slist_append(curl_opts, "Content-type:");
            curl_opts = curl_slist_append(curl_opts, post.content_type.data);
KrzysztofCzajkaTURCOM commented 8 years ago

Introducing new parameter: request_user_header. Now you can push own HTTP header. Clickmeeting need to set X-Api-Key in header.


@@ -64,10 +64,11 @@ static struct WWW_fdw_option valid_options[] =
    { "method_insert",  ForeignServerRelationId },
    { "method_delete",  ForeignServerRelationId },
    { "method_update",  ForeignServerRelationId },

    { "request_user_agent", ForeignServerRelationId },
+        { "request_user_header",ForeignServerRelationId },
    { "request_serialize_callback", ForeignServerRelationId },
    { "request_serialize_type", ForeignServerRelationId },
    { "request_serialize_human_readable",   ForeignServerRelationId },

    { "response_type",  ForeignServerRelationId },
@@ -95,10 +96,11 @@ typedef struct  WWW_fdw_options
    char*   method_select;
    char*   method_insert;
    char*   method_delete;
    char*   method_update;
    char*   request_user_agent;
+        char*   request_user_header;
    char*   request_serialize_callback;
    char*   request_serialize_type;
    char*   request_serialize_human_readable;
    char*   response_type;
    char*   response_deserialize_callback;
@@ -220,10 +222,11 @@ www_fdw_validator(PG_FUNCTION_ARGS)
    char        *method_select  = NULL;
    char        *method_insert  = NULL;
    char        *method_delete  = NULL;
    char        *method_update  = NULL;
    char        *request_user_agent= NULL;
+        char            *request_user_header = NULL;
    char        *request_serialize_callback = NULL;
    char        *request_serialize_type = NULL;
    char        *request_serialize_human_readable   = NULL;
    char        *response_type  = NULL;
    char        *response_deserialize_callback  = NULL;
@@ -277,10 +280,11 @@ www_fdw_validator(PG_FUNCTION_ARGS)
        if(parse_parameter("method_select", &method_select, def)) continue;
        if(parse_parameter("method_insert", &method_insert, def)) continue;
        if(parse_parameter("method_delete", &method_delete, def)) continue;
        if(parse_parameter("method_update", &method_update, def)) continue;
        if(parse_parameter("request_user_agent", &request_user_agent, def)) continue;
+                if(parse_parameter("request_user_header", &request_user_header, def)) continue;
        if(parse_parameter("request_serialize_callback", &request_serialize_callback, def)) continue;
        if(parse_parameter("request_serialize_type", &request_serialize_type, def)) continue;
        if(parse_parameter("request_serialize_human_readable", &request_serialize_human_readable, def))
        {
            if(
@@ -1396,10 +1400,11 @@ get_www_fdw_options(WWW_fdw_options *opts, Oid *opts_type, Datum *opts_value)
        opts->method_insert,
        opts->method_delete,
        opts->method_update,

        opts->request_user_agent,
+                opts->request_user_header,
        opts->request_serialize_callback,
        opts->request_serialize_type,
        opts->request_serialize_human_readable,

        opts->response_type,
@@ -1729,10 +1734,11 @@ www_begin(ForeignScanState *node, int eflags)
    StringInfoData  buffer;
    Oid             opts_type   = 0;
    Datum           opts_value  = 0;
    PostParameters  post;
    struct curl_slist   *curl_opts = NULL;
+        int                     header_set = 0;

    d("www_begin routine");

    /*
     * Do nothing in EXPLAIN
@@ -1777,23 +1783,33 @@ www_begin(ForeignScanState *node, int eflags)

    /* interacting with the server */
    curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, url.data);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, opts->request_user_agent);
+        if(opts->request_user_header)
+        {
+            curl_opts = curl_slist_append(curl_opts, opts->request_user_header);
+        }
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_error_buffer);
    if(post.post || 0 == strcmp(opts->method_select, "POST"))
    {
        curl_easy_setopt(curl, CURLOPT_POST, 1);
        if(0 < post.content_type.len)
        {
            curl_opts = curl_slist_append(curl_opts, "Content-type:");
            curl_opts = curl_slist_append(curl_opts, post.content_type.data);
            curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_opts);
+                        header_set = 1;
        }
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post.data.data);
    }

+        if ((opts->request_user_header)&&(0 == header_set))
+        {
+            curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_opts);
+        }
+
    /* prepare parsers */
    if( 0 == strcmp(opts->response_type, "json") )
    {
        if(opts->response_deserialize_callback)
        {
@@ -2234,10 +2250,13 @@ get_options(Oid foreigntableid, WWW_fdw_options *opts)
            opts->method_update = defGetString(def);

        if (strcmp(def->defname, "request_user_agent") == 0)
            opts->request_user_agent    = defGetString(def);

+       if (strcmp(def->defname, "request_user_header") == 0)
+           opts->request_user_header   = defGetString(def);
+
        if (strcmp(def->defname, "request_serialize_callback") == 0)
            opts->request_serialize_callback    = defGetString(def);

        if (strcmp(def->defname, "request_serialize_type") == 0)
            opts->request_serialize_type    = defGetString(def);
@@ -2280,10 +2299,11 @@ get_options(Oid foreigntableid, WWW_fdw_options *opts)
    if (!opts->method_insert) opts->method_insert   = "PUT";
    if (!opts->method_delete) opts->method_delete   = "DELETE";
    if (!opts->method_update) opts->method_update   = "POST";

    if (!opts->request_user_agent) opts->request_user_agent = "www_fdw postgres extension";
+        if (!opts->request_user_header) opts->request_user_header = "";

    if (!opts->request_serialize_type) opts->request_serialize_type = "log";
    if (!opts->request_serialize_human_readable) opts->request_serialize_human_readable = "0";

    if (!opts->response_type) opts->response_type   = "json";
jmealo commented 8 years ago

@KrzysztofCzajkaTURCOM Any chance you can create a pull request with your changes?

cyga commented 8 years ago

@jmealo it's already in the HEAD