@@ -16,9 +16,9 @@ static SPIPlanPtr ins_response_plan = NULL;
1616static size_t
1717body_cb (void * contents , size_t size , size_t nmemb , void * userp )
1818{
19- CurlData * cdata = (CurlData * ) userp ;
19+ CurlHandle * handle = (CurlHandle * ) userp ;
2020 size_t realsize = size * nmemb ;
21- appendBinaryStringInfo (cdata -> body , (const char * )contents , (int )realsize );
21+ appendBinaryStringInfo (handle -> body , (const char * )contents , (int )realsize );
2222 return realsize ;
2323}
2424
@@ -45,12 +45,12 @@ static struct curl_slist *pg_text_array_to_slist(ArrayType *array,
4545 return headers ;
4646}
4747
48- void init_curl_handle (CurlData * cdata , RequestQueueRow row ){
49- cdata -> id = row .id ;
50- cdata -> body = makeStringInfo ();
51- cdata -> ez_handle = curl_easy_init ();
48+ void init_curl_handle (CurlHandle * handle , RequestQueueRow row ){
49+ handle -> id = row .id ;
50+ handle -> body = makeStringInfo ();
51+ handle -> ez_handle = curl_easy_init ();
5252
53- cdata -> timeout_milliseconds = row .timeout_milliseconds ;
53+ handle -> timeout_milliseconds = row .timeout_milliseconds ;
5454
5555 if (!row .headersBin .isnull ) {
5656 ArrayType * pgHeaders = DatumGetArrayTypeP (row .headersBin .value );
@@ -60,57 +60,57 @@ void init_curl_handle(CurlData *cdata, RequestQueueRow row){
6060
6161 EREPORT_CURL_SLIST_APPEND (request_headers , "User-Agent: pg_net/" EXTVERSION );
6262
63- cdata -> request_headers = request_headers ;
63+ handle -> request_headers = request_headers ;
6464 }
6565
66- cdata -> url = TextDatumGetCString (row .url );
66+ handle -> url = TextDatumGetCString (row .url );
6767
68- cdata -> req_body = !row .bodyBin .isnull ? TextDatumGetCString (row .bodyBin .value ) : NULL ;
68+ handle -> req_body = !row .bodyBin .isnull ? TextDatumGetCString (row .bodyBin .value ) : NULL ;
6969
70- cdata -> method = TextDatumGetCString (row .method );
70+ handle -> method = TextDatumGetCString (row .method );
7171
72- if (strcasecmp (cdata -> method , "GET" ) != 0 && strcasecmp (cdata -> method , "POST" ) != 0 && strcasecmp (cdata -> method , "DELETE" ) != 0 ) {
73- ereport (ERROR , errmsg ("Unsupported request method %s" , cdata -> method ));
72+ if (strcasecmp (handle -> method , "GET" ) != 0 && strcasecmp (handle -> method , "POST" ) != 0 && strcasecmp (handle -> method , "DELETE" ) != 0 ) {
73+ ereport (ERROR , errmsg ("Unsupported request method %s" , handle -> method ));
7474 }
7575
76- if (strcasecmp (cdata -> method , "GET" ) == 0 ) {
77- if (cdata -> req_body ) {
78- EREPORT_CURL_SETOPT (cdata -> ez_handle , CURLOPT_POSTFIELDS , cdata -> req_body );
79- EREPORT_CURL_SETOPT (cdata -> ez_handle , CURLOPT_CUSTOMREQUEST , "GET" );
76+ if (strcasecmp (handle -> method , "GET" ) == 0 ) {
77+ if (handle -> req_body ) {
78+ EREPORT_CURL_SETOPT (handle -> ez_handle , CURLOPT_POSTFIELDS , handle -> req_body );
79+ EREPORT_CURL_SETOPT (handle -> ez_handle , CURLOPT_CUSTOMREQUEST , "GET" );
8080 }
8181 }
8282
83- if (strcasecmp (cdata -> method , "POST" ) == 0 ) {
84- if (cdata -> req_body ) {
85- EREPORT_CURL_SETOPT (cdata -> ez_handle , CURLOPT_POSTFIELDS , cdata -> req_body );
83+ if (strcasecmp (handle -> method , "POST" ) == 0 ) {
84+ if (handle -> req_body ) {
85+ EREPORT_CURL_SETOPT (handle -> ez_handle , CURLOPT_POSTFIELDS , handle -> req_body );
8686 }
8787 else {
88- EREPORT_CURL_SETOPT (cdata -> ez_handle , CURLOPT_POST , 1L );
89- EREPORT_CURL_SETOPT (cdata -> ez_handle , CURLOPT_POSTFIELDSIZE , 0L );
88+ EREPORT_CURL_SETOPT (handle -> ez_handle , CURLOPT_POST , 1L );
89+ EREPORT_CURL_SETOPT (handle -> ez_handle , CURLOPT_POSTFIELDSIZE , 0L );
9090 }
9191 }
9292
93- if (strcasecmp (cdata -> method , "DELETE" ) == 0 ) {
94- EREPORT_CURL_SETOPT (cdata -> ez_handle , CURLOPT_CUSTOMREQUEST , "DELETE" );
95- if (cdata -> req_body ) {
96- EREPORT_CURL_SETOPT (cdata -> ez_handle , CURLOPT_POSTFIELDS , cdata -> req_body );
93+ if (strcasecmp (handle -> method , "DELETE" ) == 0 ) {
94+ EREPORT_CURL_SETOPT (handle -> ez_handle , CURLOPT_CUSTOMREQUEST , "DELETE" );
95+ if (handle -> req_body ) {
96+ EREPORT_CURL_SETOPT (handle -> ez_handle , CURLOPT_POSTFIELDS , handle -> req_body );
9797 }
9898 }
9999
100- EREPORT_CURL_SETOPT (cdata -> ez_handle , CURLOPT_WRITEFUNCTION , body_cb );
101- EREPORT_CURL_SETOPT (cdata -> ez_handle , CURLOPT_WRITEDATA , cdata );
102- EREPORT_CURL_SETOPT (cdata -> ez_handle , CURLOPT_HEADER , 0L );
103- EREPORT_CURL_SETOPT (cdata -> ez_handle , CURLOPT_URL , cdata -> url );
104- EREPORT_CURL_SETOPT (cdata -> ez_handle , CURLOPT_HTTPHEADER , cdata -> request_headers );
105- EREPORT_CURL_SETOPT (cdata -> ez_handle , CURLOPT_TIMEOUT_MS , (long ) cdata -> timeout_milliseconds );
106- EREPORT_CURL_SETOPT (cdata -> ez_handle , CURLOPT_PRIVATE , cdata );
107- EREPORT_CURL_SETOPT (cdata -> ez_handle , CURLOPT_FOLLOWLOCATION , (long ) true);
100+ EREPORT_CURL_SETOPT (handle -> ez_handle , CURLOPT_WRITEFUNCTION , body_cb );
101+ EREPORT_CURL_SETOPT (handle -> ez_handle , CURLOPT_WRITEDATA , handle );
102+ EREPORT_CURL_SETOPT (handle -> ez_handle , CURLOPT_HEADER , 0L );
103+ EREPORT_CURL_SETOPT (handle -> ez_handle , CURLOPT_URL , handle -> url );
104+ EREPORT_CURL_SETOPT (handle -> ez_handle , CURLOPT_HTTPHEADER , handle -> request_headers );
105+ EREPORT_CURL_SETOPT (handle -> ez_handle , CURLOPT_TIMEOUT_MS , (long ) handle -> timeout_milliseconds );
106+ EREPORT_CURL_SETOPT (handle -> ez_handle , CURLOPT_PRIVATE , handle );
107+ EREPORT_CURL_SETOPT (handle -> ez_handle , CURLOPT_FOLLOWLOCATION , (long ) true);
108108 if (log_min_messages <= DEBUG2 )
109- EREPORT_CURL_SETOPT (cdata -> ez_handle , CURLOPT_VERBOSE , 1L );
109+ EREPORT_CURL_SETOPT (handle -> ez_handle , CURLOPT_VERBOSE , 1L );
110110#if LIBCURL_VERSION_NUM >= 0x075500 /* libcurl 7.85.0 */
111- EREPORT_CURL_SETOPT (cdata -> ez_handle , CURLOPT_PROTOCOLS_STR , "http,https" );
111+ EREPORT_CURL_SETOPT (handle -> ez_handle , CURLOPT_PROTOCOLS_STR , "http,https" );
112112#else
113- EREPORT_CURL_SETOPT (cdata -> ez_handle , CURLOPT_PROTOCOLS , CURLPROTO_HTTP | CURLPROTO_HTTPS );
113+ EREPORT_CURL_SETOPT (handle -> ez_handle , CURLOPT_PROTOCOLS , CURLPROTO_HTTP | CURLPROTO_HTTPS );
114114#endif
115115}
116116
@@ -244,36 +244,33 @@ static Jsonb *jsonb_headers_from_curl_handle(CURL *ez_handle){
244244 return jsonb_headers ;
245245}
246246
247- void insert_response (CURL * ez_handle , CURLcode curl_return_code ){
247+ void insert_response (CurlHandle * handle , CURLcode curl_return_code ){
248248 enum { nparams = 7 }; // using an enum because const size_t nparams doesn't compile
249249 Datum vals [nparams ];
250250 char nulls [nparams ]; MemSet (nulls , 'n' , nparams );
251251
252- CurlData * cdata = NULL ;
253- EREPORT_CURL_GETINFO (ez_handle , CURLINFO_PRIVATE , & cdata );
254-
255- vals [0 ] = Int64GetDatum (cdata -> id );
252+ vals [0 ] = Int64GetDatum (handle -> id );
256253 nulls [0 ] = ' ' ;
257254
258255 if (curl_return_code == CURLE_OK ) {
259- Jsonb * jsonb_headers = jsonb_headers_from_curl_handle (ez_handle );
256+ Jsonb * jsonb_headers = jsonb_headers_from_curl_handle (handle -> ez_handle );
260257 long res_http_status_code = 0 ;
261258
262- EREPORT_CURL_GETINFO (ez_handle , CURLINFO_RESPONSE_CODE , & res_http_status_code );
259+ EREPORT_CURL_GETINFO (handle -> ez_handle , CURLINFO_RESPONSE_CODE , & res_http_status_code );
263260
264261 vals [1 ] = Int32GetDatum (res_http_status_code );
265262 nulls [1 ] = ' ' ;
266263
267- if (cdata -> body && cdata -> body -> data [0 ] != '\0' ){
268- vals [2 ] = CStringGetTextDatum (cdata -> body -> data );
264+ if (handle -> body && handle -> body -> data [0 ] != '\0' ){
265+ vals [2 ] = CStringGetTextDatum (handle -> body -> data );
269266 nulls [2 ] = ' ' ;
270267 }
271268
272269 vals [3 ] = JsonbPGetDatum (jsonb_headers );
273270 nulls [3 ] = ' ' ;
274271
275272 struct curl_header * hdr ;
276- if (curl_easy_header (ez_handle , "content-type" , 0 , CURLH_HEADER , -1 , & hdr ) == CURLHE_OK ){
273+ if (curl_easy_header (handle -> ez_handle , "content-type" , 0 , CURLH_HEADER , -1 , & hdr ) == CURLHE_OK ){
277274 vals [4 ] = CStringGetTextDatum (hdr -> value );
278275 nulls [4 ] = ' ' ;
279276 }
@@ -285,7 +282,7 @@ void insert_response(CURL *ez_handle, CURLcode curl_return_code){
285282 char * error_msg = NULL ;
286283
287284 if (timed_out ){
288- error_msg = detailed_timeout_strerror (ez_handle , cdata -> timeout_milliseconds ).msg ;
285+ error_msg = detailed_timeout_strerror (handle -> ez_handle , handle -> timeout_milliseconds ).msg ;
289286 } else {
290287 error_msg = (char * ) curl_easy_strerror (curl_return_code );
291288 }
@@ -323,15 +320,15 @@ void insert_response(CURL *ez_handle, CURLcode curl_return_code){
323320 }
324321}
325322
326- void pfree_curl_data ( CurlData * cdata ){
327- pfree (cdata -> url );
328- pfree (cdata -> method );
329- if (cdata -> req_body )
330- pfree (cdata -> req_body );
323+ void pfree_handle ( CurlHandle * handle ){
324+ pfree (handle -> url );
325+ pfree (handle -> method );
326+ if (handle -> req_body )
327+ pfree (handle -> req_body );
331328
332- if (cdata -> body )
333- destroyStringInfo (cdata -> body );
329+ if (handle -> body )
330+ destroyStringInfo (handle -> body );
334331
335- if (cdata -> request_headers ) //curl_slist_free_all already handles the NULL case, but be explicit about it
336- curl_slist_free_all (cdata -> request_headers );
332+ if (handle -> request_headers ) //curl_slist_free_all already handles the NULL case, but be explicit about it
333+ curl_slist_free_all (handle -> request_headers );
337334}
0 commit comments