From 1012fd82fda9e9bc75d2308a3c68cb3b3738bf1b Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 4 Oct 2013 13:07:57 +0200 Subject: HTTP Server can now parse multipart/form-data forms; better architecture. --- source/HTTPServer/HTTPConnection.cpp | 38 ++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'source/HTTPServer/HTTPConnection.cpp') diff --git a/source/HTTPServer/HTTPConnection.cpp b/source/HTTPServer/HTTPConnection.cpp index c36b07d3d..61f4b3a31 100644 --- a/source/HTTPServer/HTTPConnection.cpp +++ b/source/HTTPServer/HTTPConnection.cpp @@ -108,22 +108,13 @@ void cHTTPConnection::DataReceived(const char * a_Data, int a_Size) { case wcsRecvHeaders: { - ASSERT(m_CurrentRequest == NULL); - - // Start searching 3 chars from the end of the already received data, if available: - size_t SearchStart = m_IncomingHeaderData.size(); - SearchStart = (SearchStart > 3) ? SearchStart - 3 : 0; - - m_IncomingHeaderData.append(a_Data, a_Size); - - // Parse the header, if it is complete: - size_t idxEnd = m_IncomingHeaderData.find("\r\n\r\n", SearchStart); - if (idxEnd == AString::npos) + if (m_CurrentRequest == NULL) { - return; + m_CurrentRequest = new cHTTPRequest; } - m_CurrentRequest = new cHTTPRequest; - if (!m_CurrentRequest->ParseHeaders(m_IncomingHeaderData.c_str(), idxEnd + 2)) + + int BytesConsumed = m_CurrentRequest->ParseHeaders(a_Data, a_Size); + if (BytesConsumed < 0) { delete m_CurrentRequest; m_CurrentRequest = NULL; @@ -131,20 +122,29 @@ void cHTTPConnection::DataReceived(const char * a_Data, int a_Size) m_HTTPServer.CloseConnection(*this); return; } + if (m_CurrentRequest->IsInHeaders()) + { + // The request headers are not yet complete + return; + } + + // The request has finished parsing its headers successfully, notify of it: m_State = wcsRecvBody; m_HTTPServer.NewRequest(*this, *m_CurrentRequest); m_CurrentRequestBodyRemaining = m_CurrentRequest->GetContentLength(); + if (m_CurrentRequestBodyRemaining < 0) + { + // The body length was not specified in the request, assume zero + m_CurrentRequestBodyRemaining = 0; + } // Process the rest of the incoming data into the request body: - if (m_IncomingHeaderData.size() > idxEnd + 4) + if (a_Size > BytesConsumed) { - m_IncomingHeaderData.erase(0, idxEnd + 4); - DataReceived(m_IncomingHeaderData.c_str(), m_IncomingHeaderData.size()); - m_IncomingHeaderData.clear(); + DataReceived(a_Data + BytesConsumed, a_Size - BytesConsumed); } else { - m_IncomingHeaderData.clear(); DataReceived("", 0); // If the request has zero body length, let it be processed right-away } break; -- cgit v1.2.3