Parses and processes multipart/form-data uploads over HTTP. More...
#include <MultipartParser.h>
Public Member Functions | |
MultipartParser () | |
Construct a new Multipart Parser object. | |
bool | handleMultipart (HttpRequest &req, HttpResponse &res) |
Begin processing the multipart upload from the socket. | |
void | setBoundaryFromContentType (const std::string &contentType) |
sets the boundary from the Content-Type header. | |
Private Member Functions | |
bool | extractFilename (const std::string &contentDisposition) |
Extract the filename from a Content-Disposition header. | |
bool | processFileData (const std::string &fileData) |
Append a chunk of file data to the output file. | |
bool | handleChunk (std::string &chunkData) |
Handle an incoming chunk of multipart data. | |
int | appendFileToSD (const char *filename, const char *data, size_t size) |
Append file data to storage (e.g., SD card). | |
int | file_exists (const std::string &filename) |
Check if a file already exists in storage. | |
int | findDataStart (std::string ¤tData) |
Find the start of the file content in a chunk (after headers). | |
bool | handleFinalBoundary (std::string &fileData) |
Detect and handle the final multipart boundary. | |
void | sendHttpResponse (int statusCode, const std::string &message) |
Send a simple HTTP response to the client. | |
Private Attributes | |
Tcp * | tcp |
The client TCP connection. | |
std::string | boundary |
std::string | filename |
std::string | leftoverData |
std::string | buffer |
Static Private Attributes | |
static State | currentState = SEARCHING_FOR_BOUNDARY |
MultipartParser is responsible for:
Definition at line 43 of file MultipartParser.h.
MultipartParser::MultipartParser | ( | ) |
Construct a new Multipart Parser object.
clientSocket | The socket for the HTTP client connection. |
request | The full HTTP request object. |
clientSocket | The socket for the HTTP client connection. |
request | The full HTTP request object. |
Definition at line 49 of file MultipartParser.cpp.
|
private |
filename | Name of the file to write to. |
data | Pointer to raw data. |
size | Length of the data buffer. |
|
private |
Extract the filename from a Content-Disposition header.
contentDisposition | The raw header line. |
contentDisposition | The raw header line. |
Definition at line 259 of file MultipartParser.cpp.
References file_exists(), filename, AppContext::getTypeKey(), MULTIPART_UPLOAD_PATH, sendHttpResponse(), and TRACE.
Referenced by handleChunk().
|
private |
Check if a file already exists in storage.
filename | Name of the file to check. |
filename | Name of the file to check. |
Definition at line 320 of file MultipartParser.cpp.
References StorageManager::exists(), filename, AppContext::getTypeKey(), and TRACE.
Referenced by extractFilename().
|
private |
Find the start of the file content in a chunk (after headers).
currentData | The buffer containing headers + data. |
currentData | The buffer containing headers + data. |
Definition at line 328 of file MultipartParser.cpp.
|
private |
Handle an incoming chunk of multipart data.
chunkData | The raw data received. |
chunkData | The raw data received. |
Definition at line 131 of file MultipartParser.cpp.
References boundary, COMPLETE, currentState, extractFilename(), filename, FOUND_BOUNDARY, FOUND_DATA_START, processFileData(), SEARCHING_FOR_BOUNDARY, sendHttpResponse(), and TRACE.
Referenced by handleMultipart().
|
private |
Detect and handle the final multipart boundary.
fileData | The current chunk of file data. |
fileData | The current chunk of file data. |
Definition at line 335 of file MultipartParser.cpp.
References boundary.
bool MultipartParser::handleMultipart | ( | HttpRequest & | req, |
HttpResponse & | res | ||
) |
This method reads from the client socket in chunks, processes headers and boundaries, and writes file contents to storage.
Definition at line 69 of file MultipartParser.cpp.
References boundary, COMPLETE, currentState, filename, HttpRequest::getBody(), HttpRequest::getHeader(), HttpRequest::getTcp(), handleChunk(), HTTP_RECEIVE_TIMEOUT, Tcp::recv(), SEARCHING_FOR_BOUNDARY, HttpResponse::send(), sendHttpResponse(), setBoundaryFromContentType(), HttpResponse::status(), tcp, and TRACE.
Referenced by HttpRequest::handle_multipart().
|
private |
Append a chunk of file data to the output file.
fileData | Data to append. |
fileData | Data to append. |
Definition at line 298 of file MultipartParser.cpp.
References filename, AppContext::getTypeKey(), sendHttpResponse(), and TRACE.
Referenced by handleChunk().
|
private |
Send a simple HTTP response to the client.
statusCode | HTTP status code. |
message | Message body to send. |
statusCode | HTTP status code. |
message | Message body to send. |
Definition at line 347 of file MultipartParser.cpp.
References Tcp::isConnected(), Tcp::send(), and tcp.
Referenced by extractFilename(), handleChunk(), handleMultipart(), and processFileData().
void MultipartParser::setBoundaryFromContentType | ( | const std::string & | contentType | ) |
contentType | The Content-Type header value. |
Definition at line 55 of file MultipartParser.cpp.
References boundary, and TRACE.
Referenced by handleMultipart().
|
private |
Definition at line 73 of file MultipartParser.h.
Referenced by handleChunk(), handleFinalBoundary(), handleMultipart(), and setBoundaryFromContentType().
|
private |
Definition at line 76 of file MultipartParser.h.
|
staticprivate |
Definition at line 79 of file MultipartParser.h.
Referenced by handleChunk(), and handleMultipart().
|
private |
Definition at line 74 of file MultipartParser.h.
Referenced by extractFilename(), file_exists(), handleChunk(), handleMultipart(), and processFileData().
|
private |
Definition at line 75 of file MultipartParser.h.
|
private |
Definition at line 72 of file MultipartParser.h.
Referenced by handleMultipart(), and sendHttpResponse().