OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::Http::priv Struct Reference
Collaboration diagram for Slic3r::Http::priv:

Public Types

enum  { DEFAULT_TIMEOUT_CONNECT = 10 , DEFAULT_TIMEOUT_MAX = 0 , DEFAULT_SIZE_LIMIT = 1024 * 1024 * 1024 }

Public Member Functions

 priv (const std::string &url)
 ~priv ()
void set_timeout_connect (long timeout)
void set_timeout_max (long timeout)
void form_add_file (const char *name, const fs::path &path, const char *filename, boost::filesystem::ifstream::off_type offset, size_t length)
void mime_form_add_text (const char *name, const char *value)
void mime_form_add_file (const char *name, const char *path)
void set_post_body (const fs::path &path)
void set_post_body (const std::string &body)
void set_put_body (const fs::path &path)
void set_del_body (const std::string &body)
void set_range (const std::string &range)
std::string curl_error (CURLcode curlcode)
std::string body_size_error ()
void http_perform ()

Static Public Member Functions

static bool ca_file_supported (::CURL *curl)
static size_t writecb (void *data, size_t size, size_t nmemb, void *userp)
static int xfercb (void *userp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
static int xfercb_legacy (void *userp, double dltotal, double dlnow, double ultotal, double ulnow)
static size_t form_file_read_cb (char *buffer, size_t size, size_t nitems, void *userp)
static size_t headers_cb (char *buffer, size_t size, size_t nitems, void *userp)

Public Attributes

::CURL * curl
::curl_httppost * form
::curl_httppost * form_end
::curl_mime * mime
::curl_slist * headerlist
std::string url
std::string method
std::string buffer
std::deque< form_file > form_files
std::string postfields
std::string error_buffer
std::string headers
size_t limit
std::atomic_bool cancel
std::unique_ptr< form_file > putFile
std::thread io_thread
Http::CompleteFn completefn
Http::ErrorFn errorfn
Http::ProgressFn progressfn
Http::IPResolveFn ipresolvefn
Http::HeaderCallbackFn headerfn

Detailed Description

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
DEFAULT_TIMEOUT_CONNECT 
DEFAULT_TIMEOUT_MAX 
DEFAULT_SIZE_LIMIT 
122 {
125 DEFAULT_SIZE_LIMIT = 1024 * 1024 * 1024,
126 };
@ DEFAULT_SIZE_LIMIT
Definition Http.cpp:125
@ DEFAULT_TIMEOUT_MAX
Definition Http.cpp:124
@ DEFAULT_TIMEOUT_CONNECT
Definition Http.cpp:123

Constructor & Destructor Documentation

◆ priv()

Slic3r::Http::priv::priv ( const std::string & url)
191 : curl(::curl_easy_init())
192 , form(nullptr)
193 , form_end(nullptr)
194 , mime(nullptr)
195 , headerlist(nullptr)
196 , url(url)
197 , method("GET")
198 , error_buffer(CURL_ERROR_SIZE + 1, '\0')
199 , limit(0)
200 , cancel(false)
201{
203
204 if (curl == nullptr) {
205 throw Slic3r::RuntimeError(std::string("Could not construct Curl object"));
206 }
207
210 ::curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, log_trace);
211 ::curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // curl makes a copy internally
212 ::curl_easy_setopt(curl, CURLOPT_USERAGENT, SLIC3R_APP_NAME "/" SoftFever_VERSION);
213 ::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error_buffer.front());
214#ifdef __WINDOWS__
215 ::curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_MAX_TLSv1_2);
216#endif
217 ::curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
218 ::curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
219
220 // https://everything.curl.dev/http/post/expect100.html
221 // remove the Expect: header, it will add a second delay to each request,
222 // if the file is uploaded in packets, it will cause the upload time to be longer
223 headerlist = curl_slist_append(headerlist, "Expect:");
224}
#define L(s)
Definition ExtrusionEntity.cpp:25
static std::string tls_global_init()
Definition Http.cpp:951
static int log_trace(CURL *handle, curl_infotype type, char *data, size_t size, void *userp)
Definition Http.cpp:183
std::string method
Definition Http.cpp:135
size_t limit
Definition Http.cpp:144
::curl_mime * mime
Definition Http.cpp:131
void set_timeout_connect(long timeout)
Definition Http.cpp:329
::CURL * curl
Definition Http.cpp:128
::curl_httppost * form
Definition Http.cpp:129
std::atomic_bool cancel
Definition Http.cpp:145
::curl_httppost * form_end
Definition Http.cpp:130
::curl_slist * headerlist
Definition Http.cpp:132
std::string error_buffer
Definition Http.cpp:142
std::string url
Definition Http.cpp:134
void set_timeout_max(long timeout)
Definition Http.cpp:334

References cancel, curl, DEFAULT_TIMEOUT_CONNECT, DEFAULT_TIMEOUT_MAX, error_buffer, form, form_end, headerlist, L, limit, Slic3r::log_trace(), method, mime, set_timeout_connect(), set_timeout_max(), Slic3r::Http::tls_global_init(), and url.

Here is the call graph for this function:

◆ ~priv()

Slic3r::Http::priv::~priv ( )
227{
228 ::curl_easy_cleanup(curl);
229 ::curl_formfree(form);
230 ::curl_mime_free(mime);
231 ::curl_slist_free_all(headerlist);
232}

References curl, form, headerlist, and mime.

Member Function Documentation

◆ body_size_error()

std::string Slic3r::Http::priv::body_size_error ( )
444{
445 return (boost::format("HTTP body data size exceeded limit (%1% bytes)") % limit).str();
446}

References limit.

Referenced by http_perform().

Here is the caller graph for this function:

◆ ca_file_supported()

bool Slic3r::Http::priv::ca_file_supported ( ::CURL * curl)
static
235{
236 //BBS support set ca file by default
237 bool res = true;
238
239 if (curl == nullptr) { return res; }
240
241#if LIBCURL_VERSION_MAJOR >= 7 && LIBCURL_VERSION_MINOR >= 48
242 ::curl_tlssessioninfo *tls;
243 if (::curl_easy_getinfo(curl, CURLINFO_TLS_SSL_PTR, &tls) == CURLE_OK) {
244 if (tls->backend == CURLSSLBACKEND_SCHANNEL || tls->backend == CURLSSLBACKEND_DARWINSSL) {
245 // With Windows and OS X native SSL support, cert files cannot be set
246 res = false;
247 }
248 }
249#endif
250
251 return res;
252}

References curl.

Referenced by Slic3r::Http::ca_file(), and Slic3r::Http::ca_file_supported().

Here is the caller graph for this function:

◆ curl_error()

std::string Slic3r::Http::priv::curl_error ( CURLcode curlcode)
435{
436 return (boost::format("curl:%1%:\n%2%\n[Error %3%]")
437 % ::curl_easy_strerror(curlcode)
438 % error_buffer.c_str()
439 % curlcode
440 ).str();
441}

References error_buffer.

Referenced by http_perform().

Here is the caller graph for this function:

◆ form_add_file()

void Slic3r::Http::priv::form_add_file ( const char * name,
const fs::path & path,
const char * filename,
boost::filesystem::ifstream::off_type offset,
size_t length )
340{
341 // We can't use CURLFORM_FILECONTENT, because curl doesn't support Unicode filenames on Windows
342 // and so we use CURLFORM_STREAM with boost ifstream to read the file.
343
344 std::string filename_str;
345 if (filename == nullptr) {
346 filename_str = path.string();
347 filename = filename_str.c_str();
348 }
349
350 form_files.emplace_back(path, offset, length);
351 auto &f = form_files.back();
352 size_t size = length;
353 if (length == 0) {
354 f.ifs.seekg(0, std::ios::end);
355 size = f.ifs.tellg() - offset;
356 }
357 f.ifs.seekg(offset);
358
359 if (filename != nullptr) {
360 ::curl_formadd(&form, &form_end,
361 CURLFORM_COPYNAME, name,
362 CURLFORM_FILENAME, filename,
363 CURLFORM_CONTENTTYPE, "application/octet-stream",
364 CURLFORM_STREAM, static_cast<void*>(&f),
365 CURLFORM_CONTENTSLENGTH, static_cast<long>(size),
366 CURLFORM_END
367 );
368 }
369}
double length(const Points &pts)
Definition MultiPoint.hpp:181
Slic3r::Polygons offset(const Slic3r::Polygon &polygon, const float delta, JoinType joinType, double miterLimit)
Definition ClipperUtils.cpp:582
static double f(double x, double z_sin, double z_cos, bool vertical, bool flip)
Definition FillGyroid.cpp:124
std::deque< form_file > form_files
Definition Http.cpp:140

References Slic3r::f(), form, form_end, form_files, Slic3r::length(), and Slic3r::offset().

Here is the call graph for this function:

◆ form_file_read_cb()

size_t Slic3r::Http::priv::form_file_read_cb ( char * buffer,
size_t size,
size_t nitems,
void * userp )
static
294{
295 auto f = reinterpret_cast<form_file*>(userp);
296
297 try {
298 size_t max_read_size = size * nitems;
299 if (f->content_length == 0) {
300 // Unlimited
301 f->ifs.read(buffer, max_read_size);
302 } else {
303 unsigned long long read_size = f->ifs.tellg() - f->init_offset;
304 if (read_size >= f->content_length) {
305 return 0;
306 }
307
308 max_read_size = std::min(max_read_size, size_t(f->content_length - read_size));
309 f->ifs.read(buffer, max_read_size);
310 }
311 } catch (const std::exception &) {
312 return CURL_READFUNC_ABORT;
313 }
314
315 return f->ifs.gcount();
316}
std::string buffer
Definition Http.cpp:137

References buffer, and Slic3r::f().

Referenced by http_perform().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ headers_cb()

size_t Slic3r::Http::priv::headers_cb ( char * buffer,
size_t size,
size_t nitems,
void * userp )
static
319{
320 auto self = static_cast<priv*>(userp);
321
322 if (self->headerfn) {
323 self->headers.append(buffer, nitems * size);
324 self->headerfn(self->headers);
325 }
326 return nitems * size;
327}
priv(const std::string &url)
Definition Http.cpp:190

References buffer.

Referenced by http_perform().

Here is the caller graph for this function:

◆ http_perform()

void Slic3r::Http::priv::http_perform ( )
449{
450 ::curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
451 ::curl_easy_setopt(curl, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
452 ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writecb);
453 ::curl_easy_setopt(curl, CURLOPT_WRITEDATA, static_cast<void*>(this));
454 ::curl_easy_setopt(curl, CURLOPT_READFUNCTION, form_file_read_cb);
455 //BBS set header functions
456 ::curl_easy_setopt(curl, CURLOPT_HEADERDATA, static_cast<void *>(this));
457 ::curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, headers_cb);
458
459 ::curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
460#if LIBCURL_VERSION_MAJOR >= 7 && LIBCURL_VERSION_MINOR >= 32
461 ::curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xfercb);
462 ::curl_easy_setopt(curl, CURLOPT_XFERINFODATA, static_cast<void*>(this));
463#ifndef _WIN32
464 (void)xfercb_legacy; // prevent unused function warning
465#endif
466#else
467 ::curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, xfercb);
468 ::curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, static_cast<void*>(this));
469#endif
470
471 ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
472
473 if (headerlist != nullptr) {
474 ::curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
475 }
476
477 if (form != nullptr) {
478 ::curl_easy_setopt(curl, CURLOPT_HTTPPOST, form);
479 }
480
481 if (mime != nullptr) {
482 ::curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
483 }
484
485 if (!postfields.empty()) {
486 ::curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postfields.c_str());
487 ::curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, postfields.size());
488 } else if (method == "POST" && form == nullptr && mime == nullptr) {
489 // A POST with no body. Without CURLOPT_POSTFIELDS, curl takes the request body from
490 // the read callback, but form_file_read_cb only has a usable user pointer for a PUT
491 // (set_put_body) or a streamed multipart part (form_add_file); for an empty POST it
492 // dereferences libcurl's default READDATA as a form_file and crashes. Point curl at a
493 // zero-length body so the read callback is never used.
494 ::curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postfields.c_str());
495 ::curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, static_cast<curl_off_t>(0));
496 }
497
498 CURLcode res = ::curl_easy_perform(curl);
499
500 putFile.reset();
501
502 if (res != CURLE_OK) {
503 if (res == CURLE_ABORTED_BY_CALLBACK) {
504 if (cancel.load()) {
505 // The abort comes from the request being cancelled programatically
506 Progress dummyprogress(0, 0, 0, 0, std::string());
507 bool cancel = true;
508 if (progressfn) { progressfn(dummyprogress, cancel); }
509 } else {
510 // The abort comes from the CURLOPT_READFUNCTION callback, which means reading file failed
511 if (errorfn) { errorfn(std::move(buffer), "Error reading file for file upload", 0); }
512 }
513 }
514 else if (res == CURLE_WRITE_ERROR) {
515 if (errorfn) { errorfn(std::move(buffer), body_size_error(), 0); }
516 } else {
517 if (errorfn) { errorfn(std::move(buffer), curl_error(res), 0); }
518 };
519 } else {
520 long http_status = 0;
521 ::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_status);
522
523 //BBS check success http status code
524 if (http_status >= 200 && http_status < 300) {
525 if (completefn) { completefn(std::move(buffer), http_status); }
526 if (ipresolvefn) {
527 char* ct;
528 res = curl_easy_getinfo(curl, CURLINFO_PRIMARY_IP, &ct);
529 if ((CURLE_OK == res) && ct) {
530 ipresolvefn(ct);
531 }
532 }
533 }
534 //BBS check error http status code
535 else if (http_status >= 400) {
536 if (errorfn) { errorfn(std::move(buffer), std::string(), http_status); }
537 }
538 }
539}
Http::CompleteFn completefn
Definition Http.cpp:149
std::string curl_error(CURLcode curlcode)
Definition Http.cpp:434
Http::ProgressFn progressfn
Definition Http.cpp:151
static size_t form_file_read_cb(char *buffer, size_t size, size_t nitems, void *userp)
Definition Http.cpp:293
std::string postfields
Definition Http.cpp:141
static size_t headers_cb(char *buffer, size_t size, size_t nitems, void *userp)
Definition Http.cpp:318
static size_t writecb(void *data, size_t size, size_t nmemb, void *userp)
Definition Http.cpp:254
static int xfercb(void *userp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
Definition Http.cpp:271
Http::ErrorFn errorfn
Definition Http.cpp:150
static int xfercb_legacy(void *userp, double dltotal, double dlnow, double ultotal, double ulnow)
Definition Http.cpp:288
std::unique_ptr< form_file > putFile
Definition Http.cpp:146
Http::IPResolveFn ipresolvefn
Definition Http.cpp:152
std::string body_size_error()
Definition Http.cpp:443

References body_size_error(), buffer, cancel, completefn, curl, curl_error(), errorfn, form, form_file_read_cb(), headerlist, headers_cb(), ipresolvefn, L, method, mime, postfields, progressfn, putFile, writecb(), xfercb(), and xfercb_legacy().

Here is the call graph for this function:

◆ mime_form_add_file()

void Slic3r::Http::priv::mime_form_add_file ( const char * name,
const char * path )
385{
386 if (!mime) {
387 mime = curl_mime_init(curl);
388 }
389
390 curl_mimepart* part;
391 part = curl_mime_addpart(mime);
392 curl_mime_name(part, "file");
393 curl_mime_type(part, "multipart/form-data");
394 curl_mime_filedata(part, path);
395 // BBS specify filename after filedata
396 curl_mime_filename(part, name);
397}

References curl, and mime.

◆ mime_form_add_text()

void Slic3r::Http::priv::mime_form_add_text ( const char * name,
const char * value )
372{
373 if (!mime) {
374 mime = curl_mime_init(curl);
375 }
376
377 curl_mimepart *part;
378 part = curl_mime_addpart(mime);
379 curl_mime_name(part, name);
380 curl_mime_type(part, "multipart/form-data");
381 curl_mime_data(part, value, CURL_ZERO_TERMINATED);
382}

References curl, and mime.

◆ set_del_body()

void Slic3r::Http::priv::set_del_body ( const std::string & body)
425{
426 postfields = body;
427}

References postfields.

◆ set_post_body() [1/2]

void Slic3r::Http::priv::set_post_body ( const fs::path & path)
401{
402 std::ifstream file(path.string());
403 std::string file_content { std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>() };
404 postfields = std::move(file_content);
405}

References postfields.

◆ set_post_body() [2/2]

void Slic3r::Http::priv::set_post_body ( const std::string & body)
408{
409 postfields = body;
410}

References postfields.

◆ set_put_body()

void Slic3r::Http::priv::set_put_body ( const fs::path & path)
413{
414 boost::system::error_code ec;
415 boost::uintmax_t filesize = file_size(path, ec);
416 if (!ec) {
417 putFile = std::make_unique<form_file>(path, 0, 0);
418 ::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
419 ::curl_easy_setopt(curl, CURLOPT_READDATA, (void *) (putFile.get()));
420 ::curl_easy_setopt(curl, CURLOPT_INFILESIZE, filesize);
421 }
422}

References curl, and putFile.

◆ set_range()

void Slic3r::Http::priv::set_range ( const std::string & range)
430{
431 ::curl_easy_setopt(curl, CURLOPT_RANGE, range.c_str());
432}
auto range(Cont &&cont)
Definition libslic3r.h:405

References curl, and Slic3r::range().

Here is the call graph for this function:

◆ set_timeout_connect()

void Slic3r::Http::priv::set_timeout_connect ( long timeout)
330{
331 ::curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);
332}

References curl.

Referenced by priv().

Here is the caller graph for this function:

◆ set_timeout_max()

void Slic3r::Http::priv::set_timeout_max ( long timeout)
335{
336 ::curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
337}

References curl.

Referenced by priv().

Here is the caller graph for this function:

◆ writecb()

size_t Slic3r::Http::priv::writecb ( void * data,
size_t size,
size_t nmemb,
void * userp )
static
255{
256 auto self = static_cast<priv*>(userp);
257 const char *cdata = static_cast<char*>(data);
258 const size_t realsize = size * nmemb;
259
260 const size_t limit = self->limit > 0 ? self->limit : DEFAULT_SIZE_LIMIT;
261 if (self->buffer.size() + realsize > limit) {
262 // This makes curl_easy_perform return CURLE_WRITE_ERROR
263 return 0;
264 }
265
266 self->buffer.append(cdata, realsize);
267
268 return realsize;
269}

References DEFAULT_SIZE_LIMIT, and limit.

Referenced by http_perform().

Here is the caller graph for this function:

◆ xfercb()

int Slic3r::Http::priv::xfercb ( void * userp,
curl_off_t dltotal,
curl_off_t dlnow,
curl_off_t ultotal,
curl_off_t ulnow )
static
272{
273 auto self = static_cast<priv*>(userp);
274 bool cb_cancel = false;
275
276 if (self->progressfn) {
277 double speed = 0.;
278 curl_easy_getinfo(self->curl, CURLINFO_SPEED_UPLOAD, &speed);
279 Progress progress(dltotal, dlnow, ultotal, ulnow, self->buffer, speed);
280 self->progressfn(progress, cb_cancel);
281 }
282
283 if (cb_cancel) { self->cancel.store(true); }
284
285 return self->cancel.load();
286}

Referenced by http_perform(), and xfercb_legacy().

Here is the caller graph for this function:

◆ xfercb_legacy()

int Slic3r::Http::priv::xfercb_legacy ( void * userp,
double dltotal,
double dlnow,
double ultotal,
double ulnow )
static
289{
290 return xfercb(userp, dltotal, dlnow, ultotal, ulnow);
291}

References xfercb().

Referenced by http_perform().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ buffer

std::string Slic3r::Http::priv::buffer

◆ cancel

std::atomic_bool Slic3r::Http::priv::cancel

Referenced by priv(), and http_perform().

◆ completefn

Http::CompleteFn Slic3r::Http::priv::completefn

Referenced by http_perform().

◆ curl

◆ error_buffer

std::string Slic3r::Http::priv::error_buffer

Referenced by priv(), and curl_error().

◆ errorfn

Http::ErrorFn Slic3r::Http::priv::errorfn

Referenced by http_perform().

◆ form

::curl_httppost* Slic3r::Http::priv::form

◆ form_end

::curl_httppost* Slic3r::Http::priv::form_end

Referenced by priv(), and form_add_file().

◆ form_files

std::deque<form_file> Slic3r::Http::priv::form_files

Referenced by form_add_file().

◆ headerfn

Http::HeaderCallbackFn Slic3r::Http::priv::headerfn

◆ headerlist

::curl_slist* Slic3r::Http::priv::headerlist

Referenced by priv(), ~priv(), and http_perform().

◆ headers

std::string Slic3r::Http::priv::headers

◆ io_thread

std::thread Slic3r::Http::priv::io_thread

◆ ipresolvefn

Http::IPResolveFn Slic3r::Http::priv::ipresolvefn

Referenced by http_perform().

◆ limit

size_t Slic3r::Http::priv::limit

Referenced by priv(), body_size_error(), and writecb().

◆ method

std::string Slic3r::Http::priv::method

Referenced by priv(), and http_perform().

◆ mime

::curl_mime* Slic3r::Http::priv::mime

◆ postfields

std::string Slic3r::Http::priv::postfields

◆ progressfn

Http::ProgressFn Slic3r::Http::priv::progressfn

Referenced by http_perform().

◆ putFile

std::unique_ptr<form_file> Slic3r::Http::priv::putFile

Referenced by http_perform(), and set_put_body().

◆ url

std::string Slic3r::Http::priv::url

Referenced by priv().


The documentation for this struct was generated from the following file:
  • /home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/Utils/Http.cpp