forked from UtrechtUniversity/davrods
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcurl_util.c
562 lines (422 loc) · 13.8 KB
/
curl_util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
** Copyright 2018 The Earlham Institute
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
/*
* curl_tool.c
*
* Created on: 13 Apr 2018
* Author: billy
*/
/*
** Copyright 2014-2016 The Earlham Institute
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
/*
* This code is based on the simplessl.c example code from http://curl.haxx.se/libcurl/c/simplessl.html
*/
/***************************************************************************/
#include "curl_util.h"
#include <string.h>
#include <stdlib.h>
#include <curl/easy.h>
#include "httpd.h"
#include "http_log.h"
#include "apr_strings.h"
#include "common.h"
/*
#ifdef _DEBUG
#define CURL_TOOLS_DEBUG (STM_LEVEL_FINE)
#else
#define CURL_TOOLS_DEBUG (STM_LEVEL_NONE)
#endif
*/
typedef struct CURLParam
{
CURLoption cp_opt;
const char *cp_value_s;
} CURLParam;
/**
* Allocate a new CURL object to a give CurlUtil and add optionally add a
* write to buffer callback.
*
* @param tool_p The CurlUtil to set up the CURL connection for.
* @param buffer_p If this is not NULL, then set the curl object
* to write its response to this buffer. This can be NULL <code>NULL</code>.
* @see AddCurlCallback
* @return <code>true</code> upon success or <code>false</code> upon error.
*/
static bool SetupCurl (CurlUtil *tool_p, apr_pool_t *pool_p);
/**
* Free a CURL object.
*
* @param curl_p The CURL object to free.
*/
static void FreeCurl (CURL *curl_p);
/**
* @brief Set the cryptographic engine to use.
*
* @param curl_p The CurlUtil instance to set the SSL engine for.
* @param cryptograph_engine_name_s The name of the cryptographic engine
* to use. For the valid names see the CURL documentation.
* @return <code>true</code> if the SSL engine name was set successfully,
* <code>false</code> otherwise.
* @memberof CurlUtil
*/
static bool SetSSLEngine (CurlUtil *curl_p, const char *cryptograph_engine_name_s);
/**
* Add a callback to write the HTTP response for this CURL object to
* the given buffer.
*
* @param buffer_p The ByteBuffer which will store the CURL object's response. *
* @param curl_p The CurlUtil object to add the callback for.
* @return <code>true</code> if the CurlUtil was updated successfully,
* <code>false</code> otherwise.
* @memberof CurlUtil
*/
static bool AddCurlCallback (CurlUtil *curl_tool_p, apr_pool_t *pool_p);
/**
* Set the URI that the CurlUtil will try to get the html data from.
*
* @param tool_p The CurlUtil to update.
* @param uri_s The URI to set the CurlUtil for.
* @return <code>true</code> if the CurlUtil was updated successfully,
* <code>false</code> otherwise.
* @memberof CurlUtil
*/
static bool SetUriForCurlUtil (CurlUtil *tool_p, const char * const uri_s);
/**
* @brief Run a CurlUtil.
* This will get the CurlUtil to get all of the data from its given URI.
*
* @param tool_p The CurlUtil to run.
* @return CURLE_OK if successful or an error code upon failure.
* @memberof CurlUtil
*/
static CURLcode RunCurlUtil (CurlUtil *tool_p);
/**
* @brief Set up a CurlUtil to do a JSON post when it is run.
*
* @param tool_p The CurlUtil to update.
* @return <code>true</code> if the CurlUtil was updated successfully,
* <code>false</code> otherwise.
* @memberof CurlUtil
*/
static bool SetCurlUtilForJSONPost (CurlUtil *tool_p);
/**
* @brief Get the downloaded data from a CurlUtil.
*
* If the CurlUtil has been run successfully, this will get a read-only
* version of the downloaded data. RunCurlUtil() must have been
* run prior to this.
* @param tool_p The CurlUtil to get the data from.
* @return The downloaded data or <code>NULL</code> upon error.
* @see RunCurlUtil
* @memberof CurlUtil
*/
static char *GetCurlUtilData (CurlUtil *tool_p);
/**
* Add a key value pair to the request header that a CurlUtil will send when it is run.
*
* @param tool_p The CurlUtil to add the header to
* @param key_s The key to add. This will be copied so this value does not need to stay in scope.
* @param value_s The value to add. This will be copied so this value does not need to stay in scope.
* @return <code>true</code> if the header values were set successfully, <code>false</code>
* otherwise.
* @memberof CurlUtil
*/
static bool SetCurlUtilHeader (CurlUtil *tool_p, const char *key_s, const char *value_s);
static size_t WriteToMemoryCallback (char *response_data_p, size_t block_size, size_t num_blocks, void *store_p);
CurlUtil *AllocateCurlUtil (request_rec *req_p, apr_pool_t *pool_p)
{
apr_bucket_brigade *buffer_p = apr_brigade_create (pool_p, req_p -> connection -> bucket_alloc);
if (buffer_p)
{
CurlUtil *curl_tool_p = (CurlUtil *) malloc (sizeof (CurlUtil));
if (curl_tool_p)
{
curl_tool_p -> ct_buffer_p = buffer_p;
curl_tool_p -> ct_form_p = NULL;
curl_tool_p -> ct_last_field_p = NULL;
curl_tool_p -> ct_headers_list_p = NULL;
curl_tool_p -> ct_pool_p = pool_p;
curl_tool_p -> ct_req_p = req_p;
if (SetupCurl (curl_tool_p, pool_p))
{
return curl_tool_p;
}
free (curl_tool_p);
} /* if (curl_tool_p) */
} /* if (buffer_p) */
return NULL;
}
bool ResetCurlUtil (CurlUtil *tool_p)
{
bool success_flag = false;
apr_bucket_brigade *buffer_p = apr_brigade_create (tool_p -> ct_pool_p, tool_p -> ct_req_p -> connection -> bucket_alloc);
if (buffer_p)
{
tool_p -> ct_buffer_p = buffer_p;
success_flag = true;
}
return success_flag;
}
void FreeCurlUtil (CurlUtil *curl_tool_p)
{
FreeCurl (curl_tool_p -> ct_curl_p);
if (curl_tool_p -> ct_headers_list_p)
{
curl_slist_free_all (curl_tool_p -> ct_headers_list_p);
}
free (curl_tool_p);
}
static bool SetupCurl (CurlUtil *tool_p, apr_pool_t *pool_p)
{
tool_p -> ct_curl_p = curl_easy_init ();
if (tool_p -> ct_curl_p)
{
if (pool_p)
{
if (AddCurlCallback (tool_p, pool_p))
{
#if CURL_TOOLS_DEBUG >= STM_LEVEL_FINER
curl_easy_setopt (tool_p -> ct_curl_p, CURLOPT_VERBOSE, 1L);
#endif
return true;
}
else
{
ap_log_perror (__FILE__, __LINE__, APLOG_MODULE_INDEX, APLOG_ERR, APR_EGENERAL, pool_p, "Failed to add buffer callback for curl object\n");
}
}
FreeCurl (tool_p -> ct_curl_p);
tool_p -> ct_curl_p = NULL;
}
else
{
ap_log_perror (__FILE__, __LINE__, APLOG_MODULE_INDEX, APLOG_ERR, APR_ENOMEM, pool_p, "Failed to create curl object");
}
return false;
}
static void FreeCurl (CURL *curl_p)
{
curl_easy_cleanup (curl_p);
}
char *SimpleCallGetRequest (request_rec *req_p, apr_pool_t *pool_p, const char *uri_s)
{
char *result_s = NULL;
CurlUtil *tool_p = AllocateCurlUtil (req_p, pool_p);
if (tool_p)
{
result_s = CallGetRequest (tool_p, uri_s);
FreeCurlUtil (tool_p);
}
return result_s;
}
char *CallGetRequest (CurlUtil *tool_p, const char *uri_s)
{
char *result_s = NULL;
if (SetUriForCurlUtil (tool_p, uri_s))
{
CURLcode res = RunCurlUtil (tool_p);
if (res == CURLE_OK)
{
result_s = GetCurlUtilData (tool_p);
}
else
{
ap_log_perror (__FILE__, __LINE__, APLOG_MODULE_INDEX, APLOG_ERR, APR_EGENERAL, tool_p -> ct_pool_p, "Failed to get data for CurlUtil from \"%s\", %s", uri_s, curl_easy_strerror (res));
}
}
else
{
ap_log_perror (__FILE__, __LINE__, APLOG_MODULE_INDEX, APLOG_ERR, APR_EGENERAL, tool_p -> ct_pool_p, "Failed to set CurlUtil to call \"%s\"", uri_s);
}
return result_s;
}
bool SetUriForCurlUtil (CurlUtil *tool_p, const char * const uri_s)
{
bool success_flag = false;
CURLcode res = curl_easy_setopt (tool_p -> ct_curl_p, CURLOPT_URL, uri_s);
if (res == CURLE_OK)
{
success_flag = true;
}
return success_flag;
}
CURLcode RunCurlUtil (CurlUtil *tool_p)
{
CURLcode res = CURLE_OK;
if (tool_p -> ct_headers_list_p)
{
res = curl_easy_setopt (tool_p -> ct_curl_p, CURLOPT_HTTPHEADER, tool_p -> ct_headers_list_p);
}
if (res == CURLE_OK)
{
res = curl_easy_perform (tool_p -> ct_curl_p);
}
return res;
}
static bool SetSSLEngine (CurlUtil *curl_p, const char *cryptograph_engine_name_s)
{
bool success_flag = false;
/* load the crypto engine */
if (curl_easy_setopt (curl_p -> ct_curl_p, CURLOPT_SSLENGINE, cryptograph_engine_name_s) == CURLE_OK)
{
/* set the crypto engine as default */
/* only needed for the first time you load a engine in a curl object... */
if (curl_easy_setopt (curl_p -> ct_curl_p, CURLOPT_SSLENGINE_DEFAULT, 1L) == CURLE_OK)
{
success_flag = true;
}
else
{
ap_log_perror (__FILE__, __LINE__, APLOG_MODULE_INDEX, APLOG_ERR, APR_EGENERAL, curl_p -> ct_pool_p, "can't set crypto engine as default\n");
}
}
else
{
ap_log_perror (__FILE__, __LINE__, APLOG_MODULE_INDEX, APLOG_ERR, APR_EGENERAL, curl_p -> ct_pool_p, "can't set crypto engine %s\n", cryptograph_engine_name_s);
}
return success_flag;
}
static bool AddCurlCallback (CurlUtil *curl_tool_p, apr_pool_t *pool_p)
{
bool success_flag = true;
curl_write_callback callback_fn = WriteToMemoryCallback;
const CURLParam params [] =
{
{ CURLOPT_WRITEFUNCTION, (const char *) callback_fn },
{ CURLOPT_WRITEDATA, (const char *) (curl_tool_p -> ct_buffer_p) },
/* set default user agent */
{ CURLOPT_USERAGENT, "libcurl-agent/1.0" },
/* set timeout */
// { CURLOPT_TIMEOUT, (const char *) 5 },
/* enable location redirects */
{ CURLOPT_FOLLOWLOCATION, (const char *) 1},
/* set maximum allowed redirects */
{ CURLOPT_MAXREDIRS, (const char *) 1 },
{ CURLOPT_LASTENTRY, (const char *) NULL }
};
const CURLParam *param_p = params;
while (success_flag && (param_p -> cp_value_s))
{
CURLcode ret = curl_easy_setopt (curl_tool_p -> ct_curl_p, param_p -> cp_opt, (void *) param_p -> cp_value_s);
if (ret == CURLE_OK)
{
++ param_p;
}
else
{
success_flag = false;
ap_log_perror (__FILE__, __LINE__, APLOG_MODULE_INDEX, APLOG_ERR, APR_EGENERAL, curl_tool_p -> ct_pool_p, "Failed to to set CURL option \"%d\"\n", param_p -> cp_opt);
}
} /* while (continue_flag && param_p) */
return success_flag;
}
static bool SetCurlUtilHeader (CurlUtil *tool_p, const char *key_s, const char *value_s)
{
bool success_flag = false;
char *header_s = apr_pstrcat (tool_p -> ct_pool_p, key_s, ":", value_s, NULL);
if (header_s)
{
tool_p -> ct_headers_list_p = curl_slist_append (tool_p -> ct_headers_list_p, header_s);
success_flag = true;
}
else
{
ap_log_perror (__FILE__, __LINE__, APLOG_MODULE_INDEX, APLOG_ERR, APR_EGENERAL, tool_p -> ct_pool_p, "Failed to construct header for \"%s\" and \"%s\"", key_s, value_s);
}
return success_flag;
}
static bool SetCurlUtilForJSONPost (CurlUtil *tool_p)
{
bool success_flag = true;
const CURLParam params [] =
{
/// { CURLOPT_POST, 1 },
{ CURLOPT_HTTPHEADER, (const char *) (tool_p -> ct_headers_list_p) },
{ CURLOPT_LASTENTRY, (const char *) NULL }
};
const CURLParam *param_p = params;
tool_p -> ct_headers_list_p = curl_slist_append (tool_p -> ct_headers_list_p, "Accept: application/json");
tool_p -> ct_headers_list_p = curl_slist_append (tool_p -> ct_headers_list_p, "Content-Type: application/json");
while (success_flag && (param_p -> cp_value_s))
{
CURLcode ret = curl_easy_setopt (tool_p, param_p -> cp_opt, param_p -> cp_value_s);
if (ret == CURLE_OK)
{
++ param_p;
}
else
{
success_flag = false;
ap_log_perror (__FILE__, __LINE__, APLOG_MODULE_INDEX, APLOG_ERR, APR_EGENERAL, tool_p -> ct_pool_p, "Failed to set CURL option \"%s\" to \"%s\"\n", param_p -> cp_opt, param_p -> cp_value_s);
}
} /* while (continue_flag && param_p) */
return success_flag;
}
char *GetURLEscapedString (CurlUtil *tool_p, const char *src_s)
{
char *escaped_s = curl_easy_escape (tool_p -> ct_curl_p, src_s, 0);
if (!escaped_s)
{
ap_log_perror (__FILE__, __LINE__, APLOG_MODULE_INDEX, APLOG_ERR, APR_EGENERAL, tool_p -> ct_pool_p, "Failed to created URL escaped version of \"%s\"\n", src_s);
}
return escaped_s;
}
void FreeURLEscapedString (char *value_s)
{
curl_free (value_s);
}
char *GetCurlUtilData (CurlUtil *tool_p)
{
char *result_s;
apr_size_t result_length;
apr_status_t status;
CloseBucketsStream (tool_p -> ct_buffer_p);
status = apr_brigade_pflatten (tool_p -> ct_buffer_p, &result_s, &result_length, tool_p -> ct_pool_p);
/*
* Sometimes there is garbage at the end of this, and I don't know which apr_brigade_...
* method I need to get the terminating '\0' so have to do it explicitly.
*/
if (* (result_s + result_length) != '\0')
{
* (result_s + result_length) = '\0';
}
return result_s;
}
static size_t WriteToMemoryCallback (char *response_data_s, size_t block_size, size_t num_blocks, void *store_p)
{
size_t total_size = block_size * num_blocks;
size_t result = total_size;
apr_bucket_brigade *buffer_p = (apr_bucket_brigade *) store_p;
apr_status_t status = apr_brigade_write (buffer_p, NULL, NULL, response_data_s, total_size);
if (status != APR_SUCCESS)
{
result = 0;
}
return result;
}