23
23
24
24
#include "utils/jsonb.h"
25
25
26
+ #include <curl/curl.h>
26
27
#include <curl/multi.h>
27
28
28
29
PG_MODULE_MAGIC ;
@@ -102,7 +103,7 @@ header_cb(void *contents, size_t size, size_t nmemb, void *userp)
102
103
return realsize ;
103
104
}
104
105
105
- static int init (CURLM * cm , char * url , int64 id , HTAB * curlDataMap )
106
+ static int init (CURLM * cm , char * method , char * url , char * contentType , char * reqBody , int64 id , HTAB * curlDataMap )
106
107
{
107
108
CURL * eh = curl_easy_init ();
108
109
@@ -120,6 +121,25 @@ static int init(CURLM *cm, char *url, int64 id, HTAB *curlDataMap)
120
121
cdata -> headers = headers ;
121
122
}
122
123
124
+ if (contentType ) {
125
+ struct curl_slist * reqHeaders = NULL ;
126
+ reqHeaders = curl_slist_append (reqHeaders , contentType );
127
+ curl_easy_setopt (eh , CURLOPT_HTTPHEADER , reqHeaders );
128
+ }
129
+
130
+ if (strcasecmp (method , "GET" ) == 0 ) {
131
+ if (reqBody ) {
132
+ curl_easy_setopt (eh , CURLOPT_POSTFIELDS , reqBody );
133
+ curl_easy_setopt (eh , CURLOPT_CUSTOMREQUEST , "GET" );
134
+ }
135
+ } else if (strcasecmp (method , "POST" ) == 0 ) {
136
+ if (reqBody ) {
137
+ curl_easy_setopt (eh , CURLOPT_POSTFIELDS , reqBody );
138
+ }
139
+ } else {
140
+ elog (ERROR , "error: Unsupported request method %s\n" , method );
141
+ }
142
+
123
143
curl_easy_setopt (eh , CURLOPT_WRITEFUNCTION , body_cb );
124
144
curl_easy_setopt (eh , CURLOPT_WRITEDATA , cdata -> body );
125
145
curl_easy_setopt (eh , CURLOPT_HEADERFUNCTION , header_cb );
@@ -204,7 +224,7 @@ worker_main(Datum main_arg)
204
224
205
225
appendStringInfo (& select_query , "\
206
226
SELECT\
207
- q.id, q.url\
227
+ q.id, q.method, q. url, q.content_type, q.body \
208
228
FROM net.http_request_queue q \
209
229
LEFT JOIN net.http_response r ON q.id = r.id \
210
230
WHERE r.id IS NULL" );
@@ -223,12 +243,30 @@ worker_main(Datum main_arg)
223
243
224
244
for (int j = 0 ; j < SPI_processed ; j ++ )
225
245
{
246
+ StringInfoData content_type ;
247
+
226
248
int64 id = DatumGetInt64 (SPI_getbinval (SPI_tuptable -> vals [j ], SPI_tuptable -> tupdesc , 1 , & tupIsNull ));
227
- char * url = TextDatumGetCString (SPI_getbinval (SPI_tuptable -> vals [j ], SPI_tuptable -> tupdesc , 2 , & tupIsNull ));
249
+ char * method = TextDatumGetCString (SPI_getbinval (SPI_tuptable -> vals [j ], SPI_tuptable -> tupdesc , 2 , & tupIsNull ));
250
+ char * url = TextDatumGetCString (SPI_getbinval (SPI_tuptable -> vals [j ], SPI_tuptable -> tupdesc , 3 , & tupIsNull ));
251
+ Datum contentTypeBin ;
252
+ Datum bodyBin ;
253
+ char * contentType = NULL ;
254
+ char * body = NULL ;
255
+
256
+ contentTypeBin = SPI_getbinval (SPI_tuptable -> vals [j ], SPI_tuptable -> tupdesc , 4 , & tupIsNull );
257
+ if (!tupIsNull ) {
258
+ initStringInfo (& content_type );
259
+ appendStringInfo (& content_type , "Content-Type: %s" , TextDatumGetCString (contentTypeBin ));
260
+ contentType = content_type .data ;
261
+ }
262
+ bodyBin = SPI_getbinval (SPI_tuptable -> vals [j ], SPI_tuptable -> tupdesc , 5 , & tupIsNull );
263
+ if (!tupIsNull ) body = TextDatumGetCString (bodyBin );
264
+
265
+ elog (DEBUG1 , "Making a %s request to %s with id %ld" , method , url , id );
228
266
229
- elog ( DEBUG1 , "Making a request to %s with id %ld" , url , id );
267
+ res = init ( cm , method , url , contentType , body , id , curlDataMap );
230
268
231
- res = init ( cm , url , id , curlDataMap );
269
+ /* pfree(content_type.data); */
232
270
233
271
if (res ) {
234
272
elog (ERROR , "error: init() returned %d\n" , res );
0 commit comments