Skip to content

Commit 6a26052

Browse files
committed
Post type / format CuRL
1 parent ef9d61a commit 6a26052

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

libs/openFrameworks/utils/ofURLFileLoader.cpp

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ using std::string;
1313
#include "ofThreadChannel.h"
1414
#include "ofThread.h"
1515
static bool curlInited = false;
16+
#define MAX_POSTFIELDS_SIZE (1024 * 1024)
1617
#if !defined(NO_OPENSSL)
1718
#include <openssl/evp.h>
1819
#include <openssl/pem.h>
@@ -327,11 +328,11 @@ ofHttpResponse ofURLFileLoaderImpl::handleRequest(const ofHttpRequest & request)
327328
#else
328329
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, 1L);
329330
#endif
330-
curl_easy_setopt(curl.get(), CURLOPT_MAXREDIRS, 50L);
331331
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYHOST, 2);
332332
}
333333
curl_easy_setopt(curl.get(), CURLOPT_URL, request.url.c_str());
334334
curl_easy_setopt(curl.get(), CURLOPT_FOLLOWLOCATION, 1L);
335+
curl_easy_setopt(curl.get(), CURLOPT_MAXREDIRS, 20L);
335336

336337
if (request.contentType != "") {
337338
headers = curl_slist_append(headers, ("Content-Type: " + request.contentType).c_str());
@@ -348,42 +349,48 @@ ofHttpResponse ofURLFileLoaderImpl::handleRequest(const ofHttpRequest & request)
348349
headers = curl_slist_append(headers, (it->first + ": " + it->second).c_str());
349350
}
350351

351-
curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, headers);
352+
if (headers) {
353+
curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, headers);
354+
}
352355
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+
}
361366
} 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, "");
365369
curl_easy_setopt(curl.get(), CURLOPT_READFUNCTION, nullptr);
366370
curl_easy_setopt(curl.get(), CURLOPT_READDATA, nullptr);
367371
}
368372
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);
372376
}
373377
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);
377381
}
378382
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);
382386
}
383387

384388
if (request.timeoutSeconds > 0) {
385389
curl_easy_setopt(curl.get(), CURLOPT_TIMEOUT, request.timeoutSeconds);
386390
}
391+
if (request.headerOnly) {
392+
curl_easy_setopt(curl.get(), CURLOPT_NOBODY, 1L);
393+
}
387394

388395
// start request and receive response
389396
ofHttpResponse response(request, 0, "");

libs/openFrameworks/utils/ofURLFileLoader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class ofHttpRequest {
2424
std::string contentType; ///< POST data mime type
2525
std::function<void(const ofHttpResponse &)> done;
2626
size_t timeoutSeconds = 0;
27+
bool headerOnly = false;
2728

2829
/// \return the unique id for this request
2930
int getId() const;

0 commit comments

Comments
 (0)