Skip to content

Commit

Permalink
Initial version of the file upload example.
Browse files Browse the repository at this point in the history
The most important code works but the sample needs some HTML magic to be easier to understand and use.
  • Loading branch information
slav-at-attachix committed Aug 1, 2019
1 parent 6aaeff9 commit 03fdcd2
Show file tree
Hide file tree
Showing 27 changed files with 1,607 additions and 133 deletions.
225 changes: 104 additions & 121 deletions Sming/Core/Network/Http/HttpBodyParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,184 +125,167 @@ void bodyToStringParser(HttpRequest& request, const char* at, int length)
#ifdef ENABLE_HTTP_SERVER_MULTIPART
#include "multipart-parser/multipart_parser.h"

/** @brief Boilerplate code for multipart_parser callbacks
* @note Obtain parser object and check it
*/
#define GET_PARSER() \
auto parser = static_cast<MultipartParser*>(p->data); \
if(parser == nullptr) { \
return -1; \
}

class MultipartParser
{
public:
MultipartParser(HttpRequest* request);
~MultipartParser();

public:
MultipartParser(HttpRequest* request);
~MultipartParser();
void execute(const char* at, size_t length);

void execute(const char* at, size_t length);
static int readHeaderName(multipart_parser_t* p, const char* at, size_t length);
static int readHeaderValue(multipart_parser_t* p, const char* at, size_t length);
static int partBegin(multipart_parser_t* p);
static int partData(multipart_parser_t* p, const char* at, size_t length);
static int partEnd(multipart_parser_t* p);
static int bodyEnd(multipart_parser_t* p);

static int readHeaderName(multipart_parser_t* p, const char* at,
size_t length);
static int readHeaderValue(multipart_parser_t* p, const char* at,
size_t length);
static int partBegin(multipart_parser_t* p);
static int partData(multipart_parser_t* p, const char* at, size_t length);
static int partEnd(multipart_parser_t* p);
static int bodyEnd(multipart_parser_t* p);
private:
multipart_parser_settings_t settings;

private:
multipart_parser_settings_t settings;
bool useValue = false;

bool useValue = false;
HttpRequest* request;

HttpRequest* request;

multipart_parser_t* parser;
String name; // current parameter name
multipart_parser_t* parser;
String name; // current parameter name
};

MultipartParser::MultipartParser(HttpRequest* request)
{
memset(&settings, 0, sizeof(settings));
settings.on_header_field = readHeaderName;
settings.on_header_value = readHeaderValue;
settings.on_part_data_begin = partBegin;
settings.on_part_data = partData;
settings.on_part_data_end = partEnd;
settings.on_body_end = bodyEnd;

if (request->headers.contains(HTTP_HEADER_CONTENT_TYPE)) {
// Content-Type: multipart/form-data; boundary=------------------------a48863c0572edce6
int startPost = request->headers[HTTP_HEADER_CONTENT_TYPE].indexOf("boundary=");
if (startPost == -1) {
return;
}

startPost += 9;
String boundary =
"--" + request->headers[HTTP_HEADER_CONTENT_TYPE].substring(startPost);
parser = multipart_parser_init(boundary.c_str(), &settings);
}

this->request = request;
parser->data = this;
memset(&settings, 0, sizeof(settings));
settings.on_header_field = readHeaderName;
settings.on_header_value = readHeaderValue;
settings.on_part_data_begin = partBegin;
settings.on_part_data = partData;
settings.on_part_data_end = partEnd;
settings.on_body_end = bodyEnd;

if(request->headers.contains(HTTP_HEADER_CONTENT_TYPE)) {
// Content-Type: multipart/form-data; boundary=------------------------a48863c0572edce6
int startPost = request->headers[HTTP_HEADER_CONTENT_TYPE].indexOf("boundary=");
if(startPost == -1) {
return;
}

startPost += 9;
String boundary = "--" + request->headers[HTTP_HEADER_CONTENT_TYPE].substring(startPost);
parser = multipart_parser_init(boundary.c_str(), &settings);
}

this->request = request;
parser->data = this;
}

MultipartParser::~MultipartParser()
{
multipart_parser_free(parser);
parser = nullptr;
multipart_parser_free(parser);
parser = nullptr;
}

void MultipartParser::execute(const char* at, size_t length)
{
multipart_parser_execute(parser, at, length);
multipart_parser_execute(parser, at, length);
}

int MultipartParser::partBegin(multipart_parser_t* p)
{
MultipartParser* parser = (MultipartParser*)p->data;
if (parser == nullptr) {
return -1;
}
GET_PARSER();

parser->useValue = false;
return 0;
parser->useValue = false;
return 0;
}

int MultipartParser::readHeaderName(multipart_parser_t* p, const char* at,
size_t length)
int MultipartParser::readHeaderName(multipart_parser_t* p, const char* at, size_t length)
{
MultipartParser* parser = (MultipartParser*)p->data;
if (parser == nullptr) {
return -1;
}
GET_PARSER();

if (memcmp(at, "Content-Disposition", length) == 0) {
parser->useValue = true;
}
if(memcmp(at, "Content-Disposition", length) == 0) {
parser->useValue = true;
}

return 0;
return 0;
}

int MultipartParser::readHeaderValue(multipart_parser_t* p, const char* at,
size_t length)
int MultipartParser::readHeaderValue(multipart_parser_t* p, const char* at, size_t length)
{
MultipartParser* parser = (MultipartParser*)p->data;
if (parser == nullptr) {
return -1;
}

if (parser->useValue) {
// Content-Disposition: form-data; name="image"; filename=".gitignore"
// Content-Disposition: form-data; name="data"
String value = String(at, length);
int startPos = value.indexOf("name=");
if (startPos == -1) {
return -1; // Invalid header content
}
startPos += 6; // name="
int endPos = value.indexOf(';', startPos);
if (endPos == -1) {
parser->name = value.substring(startPos, value.length() - 1);
} else {
parser->name = value.substring(startPos, endPos - 1);
}
}

return 0;
GET_PARSER();

if(parser->useValue) {
// Content-Disposition: form-data; name="image"; filename=".gitignore"
// Content-Disposition: form-data; name="data"
String value = String(at, length);
int startPos = value.indexOf("name=");
if(startPos == -1) {
return -1; // Invalid header content
}
startPos += 6; // name="
int endPos = value.indexOf(';', startPos);
if(endPos == -1) {
parser->name = value.substring(startPos, value.length() - 1);
} else {
parser->name = value.substring(startPos, endPos - 1);
}
}

return 0;
}

int MultipartParser::partData(multipart_parser_t* p, const char* at,
size_t length)
int MultipartParser::partData(multipart_parser_t* p, const char* at, size_t length)
{
MultipartParser* parser = (MultipartParser*)p->data;
if (parser == nullptr) {
return -1;
}
GET_PARSER();

ReadWriteStream* stream = parser->request->files[parser->name];
if(stream != nullptr) {
stream->write((uint8_t*)at, length);
}
ReadWriteStream* stream = parser->request->files[parser->name];
if(stream != nullptr) {
stream->write((uint8_t*)at, length);
}

return 0;
return 0;
}

int MultipartParser::partEnd(multipart_parser_t* p)
{
MultipartParser* parser = (MultipartParser*)p->data;
if (parser == nullptr) {
return -1;
}
GET_PARSER();

return 0;
return 0;
}

int MultipartParser::bodyEnd(multipart_parser_t* p)
{
MultipartParser* parser = (MultipartParser*)p->data;
if (parser == nullptr) {
return -1;
}
GET_PARSER();

return 0;
return 0;
}

void formMultipartParser(HttpRequest& request, const char* at, int length)
{
MultipartParser* parser = (MultipartParser*)request.args;
MultipartParser* parser = (MultipartParser*)request.args;

if (length == -1) {
delete parser;
if(length == -1) {
delete parser;

parser = new MultipartParser(&request);
request.args = parser;
parser = new MultipartParser(&request);
request.args = parser;

return;
}
return;
}

if (length == -2) {
delete parser;
request.args = nullptr;
if(length == -2) {
delete parser;
request.args = nullptr;

return;
}
return;
}

parser->execute(at, length);
parser->execute(at, length);
}
#endif /* ENABLE_HTTP_SERVER_MULTIPART */

2 changes: 1 addition & 1 deletion Sming/Core/Network/Http/HttpBodyParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ void formUrlParser(HttpRequest& request, const char* at, int length);
void bodyToStringParser(HttpRequest& request, const char* at, int length);

#ifdef ENABLE_HTTP_SERVER_MULTIPART
void formMultipartParser(HttpRequest& request, const char *at, int length);
void formMultipartParser(HttpRequest& request, const char* at, int length);
#endif
10 changes: 5 additions & 5 deletions Sming/Core/Network/Http/HttpMultipartResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
* http://github.com/SmingHub/Sming
* All files of the Sming Core are provided under the LGPL v3 license.
*
* WebsocketResource.cpp
* HttpMultipartResource.cpp
*
* @author: 2017 - Slavey Karadzhov <[email protected]>
* @author: 2019 - Slavey Karadzhov <[email protected]>
*
****/

#include "HttpMultipartResource.h"

int HttpMultipartResource::setFileMap(HttpServerConnection& connection, HttpRequest& request, HttpResponse& response)
{
const String& contentType = request.headers[HTTP_HEADER_CONTENT_TYPE];
if(request.method != HTTP_POST /* || !contentType.startsWith(String(MIME_FORM_MULTIPART)) */) {
String contentType = request.headers[HTTP_HEADER_CONTENT_TYPE];
String mimeType = ContentType::toString(MIME_FORM_MULTIPART);
if(!(request.method == HTTP_POST && contentType.startsWith(mimeType))) {
return 0;
}

Expand All @@ -26,5 +27,4 @@ int HttpMultipartResource::setFileMap(HttpServerConnection& connection, HttpRequ

void HttpMultipartResource::shutdown(HttpServerConnection& connection)
{

}
21 changes: 16 additions & 5 deletions Sming/Core/Network/Http/HttpMultipartResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* http://github.com/SmingHub/Sming
* All files of the Sming Core are provided under the LGPL v3 license.
*
* WebsocketResource.h
* HttpMultipartResource.h
*
* @author: 2017 - Slavey Karadzhov <[email protected]>
* @author: 2019 - Slavey Karadzhov <[email protected]>
*
****/

Expand All @@ -15,17 +15,28 @@
#include "HttpServerConnection.h"
#include "HttpResource.h"
#include "WString.h"
#include "WHashMap.h"

typedef Delegate<void(HttpFiles&)> HttpFilesMapper;

class HttpMultipartResource : public HttpResource
{
public:
HttpMultipartResource(const HttpFilesMapper& mapper, HttpResourceDelegate process)
/**
* @brief HttpResource that allows handling of HTTP file upload.
* @param mapper callback that provides information where the desired upload fields will be stored.
* @param complete callback that will be called after the request has completed.
* @note:
* On a normal computer the file uploads are usually using
* temporary space on the hard disk or in memory to store the incoming data.
* On an embedded device that is a luxury that we can hardly afford.
* Therefore we should define a `map` that specifies explicitly
* where every form field will be stored.
* If a field is not specified then its content will be discarded.
*/
HttpMultipartResource(const HttpFilesMapper& mapper, HttpResourceDelegate complete)
{
onHeadersComplete = HttpResourceDelegate(&HttpMultipartResource::setFileMap, this);
onRequestComplete = process;
onRequestComplete = complete;
this->mapper = mapper;
}

Expand Down
2 changes: 1 addition & 1 deletion Sming/Core/Network/HttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void HttpServer::configure(const HttpServerSettings& settings)
if(settings.useDefaultBodyParsers) {
setBodyParser(ContentType::toString(MIME_FORM_URL_ENCODED), formUrlParser);
#ifdef ENABLE_HTTP_SERVER_MULTIPART
setBodyParser(ContentType::toString(MIME_FORM_MULTIPART), formMultipartParser);
setBodyParser(ContentType::toString(MIME_FORM_MULTIPART), formMultipartParser);
#endif
}

Expand Down
Loading

0 comments on commit 03fdcd2

Please sign in to comment.