@@ -13,6 +13,7 @@ using std::string;
13
13
#include " ofThreadChannel.h"
14
14
#include " ofThread.h"
15
15
static bool curlInited = false ;
16
+ #define MAX_POSTFIELDS_SIZE (1024 * 1024 )
16
17
#if !defined(NO_OPENSSL)
17
18
#include < openssl/evp.h>
18
19
#include < openssl/pem.h>
@@ -327,11 +328,11 @@ ofHttpResponse ofURLFileLoaderImpl::handleRequest(const ofHttpRequest & request)
327
328
#else
328
329
curl_easy_setopt (curl.get (), CURLOPT_SSL_VERIFYPEER, 1L );
329
330
#endif
330
- curl_easy_setopt (curl.get (), CURLOPT_MAXREDIRS, 50L );
331
331
curl_easy_setopt (curl.get (), CURLOPT_SSL_VERIFYHOST, 2 );
332
332
}
333
333
curl_easy_setopt (curl.get (), CURLOPT_URL, request.url .c_str ());
334
334
curl_easy_setopt (curl.get (), CURLOPT_FOLLOWLOCATION, 1L );
335
+ curl_easy_setopt (curl.get (), CURLOPT_MAXREDIRS, 20L );
335
336
336
337
if (request.contentType != " " ) {
337
338
headers = curl_slist_append (headers, (" Content-Type: " + request.contentType ).c_str ());
@@ -348,42 +349,48 @@ ofHttpResponse ofURLFileLoaderImpl::handleRequest(const ofHttpRequest & request)
348
349
headers = curl_slist_append (headers, (it->first + " : " + it->second ).c_str ());
349
350
}
350
351
351
- curl_easy_setopt (curl.get (), CURLOPT_HTTPHEADER, headers);
352
+ if (headers) {
353
+ curl_easy_setopt (curl.get (), CURLOPT_HTTPHEADER, headers);
354
+ }
352
355
std::string body = request.body ;
353
- // set body if there's any
354
- if (request.body != " " ) {
355
- // curl_easy_setopt(curl.get(), CURLOPT_UPLOAD, 1L); // Tis does PUT instead of POST
356
- curl_easy_setopt (curl.get (), CURLOPT_POSTFIELDSIZE, request.body .size ());
357
- curl_easy_setopt (curl.get (), CURLOPT_POSTFIELDS, nullptr );
358
- // curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, request.body.c_str());
359
- curl_easy_setopt (curl.get (), CURLOPT_READFUNCTION, readBody_cb);
360
- curl_easy_setopt (curl.get (), CURLOPT_READDATA, &body);
356
+ if (!request.body .empty ()) {
357
+ if (request.method == ofHttpRequest::PUT || request.body .size () > MAX_POSTFIELDS_SIZE) { // If request is an upload (e.g., file upload)
358
+ curl_easy_setopt (curl.get (), CURLOPT_UPLOAD, 1L );
359
+ curl_easy_setopt (curl.get (), CURLOPT_READFUNCTION, readBody_cb);
360
+ curl_easy_setopt (curl.get (), CURLOPT_READDATA, &body);
361
+ curl_easy_setopt (curl.get (), CURLOPT_POSTFIELDSIZE, 0L );
362
+ } else { // If request is a normal POST
363
+ curl_easy_setopt (curl.get (), CURLOPT_POSTFIELDSIZE, request.body .size ());
364
+ curl_easy_setopt (curl.get (), CURLOPT_POSTFIELDS, request.body .c_str ());
365
+ }
361
366
} else {
362
- // curl_easy_setopt(curl.get(), CURLOPT_UPLOAD, 0L);
363
- curl_easy_setopt (curl.get (), CURLOPT_POSTFIELDSIZE, 0 );
364
- // curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, nullptr);
367
+ curl_easy_setopt (curl.get (), CURLOPT_POSTFIELDSIZE, 0L );
368
+ curl_easy_setopt (curl.get (), CURLOPT_POSTFIELDS, " " );
365
369
curl_easy_setopt (curl.get (), CURLOPT_READFUNCTION, nullptr );
366
370
curl_easy_setopt (curl.get (), CURLOPT_READDATA, nullptr );
367
371
}
368
372
if (request.method == ofHttpRequest::GET) {
369
- curl_easy_setopt (curl.get (), CURLOPT_HTTPGET, 1 );
370
- curl_easy_setopt (curl.get (), CURLOPT_POST, 0 );
371
- curl_easy_setopt (curl.get (), CURLOPT_UPLOAD, 0 );
373
+ curl_easy_setopt (curl.get (), CURLOPT_HTTPGET, 1L );
374
+ curl_easy_setopt (curl.get (), CURLOPT_POST, 0L );
375
+ curl_easy_setopt (curl.get (), CURLOPT_UPLOAD, 0L );
372
376
}
373
377
else if (request.method == ofHttpRequest::PUT) {
374
- curl_easy_setopt (curl.get (), CURLOPT_UPLOAD, 1 );
375
- curl_easy_setopt (curl.get (), CURLOPT_POST, 0 );
376
- curl_easy_setopt (curl.get (), CURLOPT_HTTPGET, 0 );
378
+ curl_easy_setopt (curl.get (), CURLOPT_UPLOAD, 1L );
379
+ curl_easy_setopt (curl.get (), CURLOPT_POST, 0L );
380
+ curl_easy_setopt (curl.get (), CURLOPT_HTTPGET, 0L );
377
381
}
378
382
else if (request.method == ofHttpRequest::POST) {
379
- curl_easy_setopt (curl.get (), CURLOPT_POST, 1 );
380
- curl_easy_setopt (curl.get (), CURLOPT_UPLOAD, 0 );
381
- curl_easy_setopt (curl.get (), CURLOPT_HTTPGET, 0 );
383
+ curl_easy_setopt (curl.get (), CURLOPT_POST, 1L );
384
+ curl_easy_setopt (curl.get (), CURLOPT_UPLOAD, 0L );
385
+ curl_easy_setopt (curl.get (), CURLOPT_HTTPGET, 0L );
382
386
}
383
387
384
388
if (request.timeoutSeconds > 0 ) {
385
389
curl_easy_setopt (curl.get (), CURLOPT_TIMEOUT, request.timeoutSeconds );
386
390
}
391
+ if (request.headerOnly ) {
392
+ curl_easy_setopt (curl.get (), CURLOPT_NOBODY, 1L );
393
+ }
387
394
388
395
// start request and receive response
389
396
ofHttpResponse response (request, 0 , " " );
0 commit comments