Add optional fd parameter to sendHTTPRequest() in order to directly write received data in a buffer and not in an intermediate buffer

This commit is contained in:
2022-03-17 21:55:02 +01:00
parent 2ce6142596
commit 1221b2a95a
7 changed files with 55 additions and 18 deletions

View File

@@ -98,10 +98,11 @@ namespace gourou
* @param POSTData POST data if needed, if not set, a GET request is done
* @param contentType Optional content type of POST Data
* @param responseHeaders Optional Response headers of HTTP request
* @param fd Optional file descriptor to write request result
*
* @return data of HTTP response
*/
virtual std::string sendHTTPRequest(const std::string& URL, const std::string& POSTData=std::string(""), const std::string& contentType=std::string(""), std::map<std::string, std::string>* responseHeaders=0) = 0;
virtual std::string sendHTTPRequest(const std::string& URL, const std::string& POSTData=std::string(""), const std::string& contentType=std::string(""), std::map<std::string, std::string>* responseHeaders=0, int fd=0) = 0;
};
class RSAInterface

View File

@@ -130,14 +130,15 @@ namespace gourou
/**
* @brief Send HTTP (GET or POST) request
*
* @param URL HTTP URL
* @param POSTData POST data if needed, if not set, a GET request is done
* @param contentType Optional content type of POST Data
* @param URL HTTP URL
* @param POSTData POST data if needed, if not set, a GET request is done
* @param contentType Optional content type of POST Data
* @param responseHeaders Optional Response headers of HTTP request
* @param fd Optional File descriptor to write received data
*
* @return data of HTTP response
*/
ByteArray sendRequest(const std::string& URL, const std::string& POSTData=std::string(), const char* contentType=0, std::map<std::string, std::string>* responseHeaders=0);
ByteArray sendRequest(const std::string& URL, const std::string& POSTData=std::string(), const char* contentType=0, std::map<std::string, std::string>* responseHeaders=0, int fd=0);
/**
* @brief Send HTTP POST request to URL with document as POSTData

View File

@@ -287,15 +287,27 @@ namespace gourou
}
/**
* @brief Write data in a file. If it already exists, it's truncated
* @brief Open a file descriptor on path. If it already exists, it's truncated
*
* @return Created fd, must be closed
*/
static inline void writeFile(std::string path, const unsigned char* data, unsigned int length)
static inline int createNewFile(std::string path)
{
int fd = open(path.c_str(), O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
if (fd <= 0)
EXCEPTION(GOUROU_FILE_ERROR, "Unable to create " << path);
return fd;
}
/**
* @brief Write data in a file. If it already exists, it's truncated
*/
static inline void writeFile(std::string path, const unsigned char* data, unsigned int length)
{
int fd = createNewFile(path);
if (write(fd, data, length) != length)
EXCEPTION(GOUROU_FILE_ERROR, "Write error for file " << path);