BACKENDS: NETWORKING: Use Path for cURL requests

This commit is contained in:
Le Philousophe 2023-09-21 20:32:33 +02:00 committed by Eugene Sandulenko
parent 01ebece807
commit d87571766e
4 changed files with 13 additions and 11 deletions

View File

@ -112,7 +112,7 @@ void CurlRequest::addFormField(const Common::String &name, const Common::String
_formFields[name] = value;
}
void CurlRequest::addFormFile(const Common::String &name, const Common::String &filename) {
void CurlRequest::addFormFile(const Common::String &name, const Common::Path &filename) {
if (_bytesBuffer)
warning("CurlRequest: added POST form files would be ignored, because there is buffer present");

View File

@ -23,6 +23,7 @@
#define BACKENDS_NETWORKING_CURL_CURLREQUEST_H
#include "backends/networking/curl/request.h"
#include "common/path.h"
#include "common/str.h"
#include "common/array.h"
#include "common/hashmap.h"
@ -44,7 +45,7 @@ protected:
curl_slist *_headersList;
Common::String _postFields;
Common::HashMap<Common::String, Common::String> _formFields;
Common::HashMap<Common::String, Common::String> _formFiles;
Common::HashMap<Common::String, Common::Path> _formFiles;
byte *_bytesBuffer;
uint32 _bytesBufferSize;
bool _uploading; //using PUT method
@ -75,7 +76,7 @@ public:
virtual void addFormField(const Common::String &name, const Common::String &value);
/** Adds a form/multipart file (field name, file name). */
virtual void addFormFile(const Common::String &name, const Common::String &filename);
virtual void addFormFile(const Common::String &name, const Common::Path &filename);
/** Sets bytes buffer. */
virtual void setBuffer(byte *buffer, uint32 size);

View File

@ -158,7 +158,7 @@ void NetworkReadStream::setupBufferContents(const byte *buffer, uint32 bufferSiz
ConnMan.registerEasyHandle(_easy);
}
void NetworkReadStream::setupFormMultipart(const Common::HashMap<Common::String, Common::String> &formFields, const Common::HashMap<Common::String, Common::String> &formFiles) {
void NetworkReadStream::setupFormMultipart(const Common::HashMap<Common::String, Common::String> &formFields, const Common::HashMap<Common::String, Common::Path> &formFiles) {
// set POST multipart upload form fields/files
struct curl_httppost *formpost = nullptr;
struct curl_httppost *lastptr = nullptr;
@ -176,12 +176,12 @@ void NetworkReadStream::setupFormMultipart(const Common::HashMap<Common::String,
warning("NetworkReadStream: field curl_formadd('%s') failed", i->_key.c_str());
}
for (Common::HashMap<Common::String, Common::String>::iterator i = formFiles.begin(); i != formFiles.end(); ++i) {
for (Common::HashMap<Common::String, Common::Path>::iterator i = formFiles.begin(); i != formFiles.end(); ++i) {
CURLFORMcode code = curl_formadd(
&formpost,
&lastptr,
CURLFORM_COPYNAME, i->_key.c_str(),
CURLFORM_FILE, i->_value.c_str(),
CURLFORM_FILE, i->_value.toString(Common::Path::kNativeSeparator).c_str(),
CURLFORM_END
);
@ -199,7 +199,7 @@ NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, c
setupBufferContents((const byte *)postFields.c_str(), postFields.size(), uploading, usingPatch, false);
}
NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, const Common::HashMap<Common::String, Common::String> &formFields, const Common::HashMap<Common::String, Common::String> &formFiles, bool keepAlive, long keepAliveIdle, long keepAliveInterval):
NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, const Common::HashMap<Common::String, Common::String> &formFields, const Common::HashMap<Common::String, Common::Path> &formFiles, bool keepAlive, long keepAliveIdle, long keepAliveInterval):
_backingStream(DisposeAfterUse::YES), _keepAlive(keepAlive), _keepAliveIdle(keepAliveIdle), _keepAliveInterval(keepAliveInterval), _errorBuffer(nullptr), _errorCode(CURLE_OK) {
initCurl(url, headersList);
setupFormMultipart(formFields, formFiles);
@ -220,7 +220,7 @@ bool NetworkReadStream::reuse(const char *url, curl_slist *headersList, const Co
return true;
}
bool NetworkReadStream::reuse(const char *url, curl_slist *headersList, const Common::HashMap<Common::String, Common::String> &formFields, const Common::HashMap<Common::String, Common::String> &formFiles) {
bool NetworkReadStream::reuse(const char *url, curl_slist *headersList, const Common::HashMap<Common::String, Common::String> &formFields, const Common::HashMap<Common::String, Common::Path> &formFiles) {
if (!reuseCurl(url, headersList))
return false;

View File

@ -23,6 +23,7 @@
#define BACKENDS_NETWORKING_CURL_NETWORKREADSTREAM_H
#include "common/memstream.h"
#include "common/path.h"
#include "common/stream.h"
#include "common/str.h"
#include "common/hashmap.h"
@ -52,7 +53,7 @@ class NetworkReadStream : public Common::ReadStream {
void initCurl(const char *url, curl_slist *headersList);
bool reuseCurl(const char *url, curl_slist *headersList);
void setupBufferContents(const byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post);
void setupFormMultipart(const Common::HashMap<Common::String, Common::String> &formFields, const Common::HashMap<Common::String, Common::String> &formFiles);
void setupFormMultipart(const Common::HashMap<Common::String, Common::String> &formFields, const Common::HashMap<Common::String, Common::Path> &formFiles);
/**
* Fills the passed buffer with _sendingContentsBuffer contents.
@ -81,7 +82,7 @@ public:
NetworkReadStream(
const char *url, curl_slist *headersList,
const Common::HashMap<Common::String, Common::String> &formFields,
const Common::HashMap<Common::String, Common::String> &formFiles,
const Common::HashMap<Common::String, Common::Path> &formFiles,
bool keepAlive = false, long keepAliveIdle = 120, long keepAliveInterval = 60);
/** Send <buffer>, using POST by default. */
NetworkReadStream(const char *url, curl_slist *headersList, const byte *buffer, uint32 bufferSize, bool uploading = false, bool usingPatch = false, bool post = true, bool keepAlive = false, long keepAliveIdle = 120, long keepAliveInterval = 60);
@ -93,7 +94,7 @@ public:
bool reuse(
const char *url, curl_slist *headersList,
const Common::HashMap<Common::String, Common::String> &formFields,
const Common::HashMap<Common::String, Common::String> &formFiles);
const Common::HashMap<Common::String, Common::Path> &formFiles);
/** Send <buffer>, using POST by default. */
bool reuse(const char *url, curl_slist *headersList, const byte *buffer, uint32 bufferSize, bool uploading = false, bool usingPatch = false, bool post = true);