Rework the multipart parser code to also work with binary files.

This commit is contained in:
Relintai 2022-07-20 22:53:55 +02:00
parent 25dda93577
commit 55eaf521b4
6 changed files with 36 additions and 20 deletions

View File

@ -1,6 +1,7 @@
#include "web_server_request.h"
#include "core/object.h"
#include "core/variant.h"
#include "web_server.h"
#include "web_server_cookie.h"
@ -176,8 +177,8 @@ String WebServerRequest::get_file_key(const int index) const {
int WebServerRequest::get_file_length(const int index) const {
return 0;
}
const uint8_t *WebServerRequest::get_file_data(const int index) const {
return nullptr;
PoolByteArray WebServerRequest::get_file_data(const int index) const {
return PoolByteArray();
}
String WebServerRequest::get_file_data_str(const int index) const {
return "";
@ -504,12 +505,12 @@ void WebServerRequest::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_method"), &WebServerRequest::get_method);
//virtual const uint8_t *get_file_data(const int index) const;
ClassDB::bind_method(D_METHOD("parse_files"), &WebServerRequest::parse_files);
ClassDB::bind_method(D_METHOD("get_file_count"), &WebServerRequest::get_file_count);
ClassDB::bind_method(D_METHOD("get_file_file_name", "index"), &WebServerRequest::get_file_file_name);
ClassDB::bind_method(D_METHOD("get_file_key", "index"), &WebServerRequest::get_file_key);
ClassDB::bind_method(D_METHOD("get_file_length", "index"), &WebServerRequest::get_file_length);
ClassDB::bind_method(D_METHOD("get_file_data"), &WebServerRequest::get_file_data);
ClassDB::bind_method(D_METHOD("get_file_data_str"), &WebServerRequest::get_file_data_str);
ClassDB::bind_method(D_METHOD("get_parameter", "key"), &WebServerRequest::get_parameter);

View File

@ -70,7 +70,7 @@ public:
virtual String get_file_file_name(const int index) const;
virtual String get_file_key(const int index) const;
virtual int get_file_length(const int index) const;
virtual const uint8_t *get_file_data(const int index) const;
virtual PoolByteArray get_file_data(const int index) const;
virtual String get_file_data_str(const int index) const;
virtual String get_parameter(const String &key) const;

View File

@ -147,19 +147,31 @@ void HTTPParser::HTTPParser::process_multipart_data() {
}
//ERR_PRINT("BODY");
_multipart_form_data = _partial_data.substr_index(0, boundary_index - 4); //to strip the 2 \r\n from before the boundary
String data = _partial_data.substr_index(0, boundary_index - 4); //to strip the 2 \r\n from before the boundary
//ERR_PRINT(data);
if (_multipart_form_is_file) {
if (_multipart_form_data == "") {
if (data == "") {
_in_boundary_header = true;
continue;
}
_request->add_file(_multipart_form_name, _multipart_form_filename, _multipart_form_data);
CharString cs = data.ascii();
PoolByteArray file_data;
file_data.resize(cs.length());
PoolByteArray::Write w = file_data.write();
for (int i = 0; i < cs.length(); i++) {
w[i] = cs[i];
}
w.release();
_request->add_file(_multipart_form_name, _multipart_form_filename, file_data);
} else {
_request->add_parameter(_multipart_form_name, _multipart_form_data);
_request->add_parameter(_multipart_form_name, data);
}
boundary_index += _multipart_boundary.size();
@ -182,7 +194,6 @@ void HTTPParser::_process_multipart_header(const String &header) {
_multipart_form_name = "";
_multipart_form_filename = "";
_multipart_form_content_type = "";
_multipart_form_data = "";
_multipart_form_is_file = false;
int nlc = header.get_slice_count("\r\n");

View File

@ -85,7 +85,6 @@ private:
String _multipart_form_name;
String _multipart_form_filename;
String _multipart_form_content_type;
String _multipart_form_data;
bool _multipart_form_is_file;
};

View File

@ -44,18 +44,23 @@ String SimpleWebServerRequest::get_file_key(const int index) const {
int SimpleWebServerRequest::get_file_length(const int index) const {
ERR_FAIL_INDEX_V(index, _files.size(), 0);
return _files[index].data.length();
return _files[index].data.size();
}
const uint8_t *SimpleWebServerRequest::get_file_data(const int index) const {
ERR_FAIL_INDEX_V(index, _files.size(), nullptr);
PoolByteArray SimpleWebServerRequest::get_file_data(const int index) const {
ERR_FAIL_INDEX_V(index, _files.size(), PoolByteArray());
//return _files[index].data.ptr();
return nullptr;
return _files[index].data;
}
String SimpleWebServerRequest::get_file_data_str(const int index) const {
ERR_FAIL_INDEX_V(index, _files.size(), "");
return _files[index].data;
PoolByteArray::Read r = _files[index].data.read();
String ret = reinterpret_cast<const char *>(r.ptr());
r.release();
return ret;
}
String SimpleWebServerRequest::get_parameter(const String &key) const {
@ -113,7 +118,7 @@ void SimpleWebServerRequest::set_host(const String &value) {
_host = value;
}
void SimpleWebServerRequest::add_file(const String &key, const String &file_name, const String &data) {
void SimpleWebServerRequest::add_file(const String &key, const String &file_name, const PoolByteArray &data) {
FileEntry e;
e.key = key;
e.file_name = file_name;

View File

@ -32,7 +32,7 @@ public:
virtual String get_file_file_name(const int index) const;
virtual String get_file_key(const int index) const;
virtual int get_file_length(const int index) const;
virtual const uint8_t *get_file_data(const int index) const;
virtual PoolByteArray get_file_data(const int index) const;
virtual String get_file_data_str(const int index) const;
virtual String get_parameter(const String &key) const;
@ -47,7 +47,7 @@ public:
void set_parser_path(const String &value);
void set_host(const String &value);
void add_file(const String &key, const String &file_name, const String &data);
void add_file(const String &key, const String &file_name, const PoolByteArray &data);
//virtual String get_path_full() const;
@ -65,7 +65,7 @@ protected:
struct FileEntry {
String file_name;
String data;
PoolByteArray data;
String key; //form name key
};