Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access to HTTP POST body #272

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 64 additions & 24 deletions Sming/SmingCore/Network/HttpRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ HttpRequest::HttpRequest()
cookies = NULL;
postDataProcessed = 0;
combinePostFrag = false;
bodyBuf = NULL;
}

HttpRequest::~HttpRequest()
Expand All @@ -28,19 +29,25 @@ HttpRequest::~HttpRequest()
delete requestPostParameters;
delete cookies;
postDataProcessed = 0;
if (bodyBuf != NULL)
{
os_free(bodyBuf);
}
}

String HttpRequest::getQueryParameter(String parameterName, String defaultValue /* = "" */)
String HttpRequest::getQueryParameter(String parameterName,
String defaultValue /* = "" */)
{
if (requestGetParameters && requestGetParameters->contains(parameterName))
return (*requestGetParameters)[parameterName];
return (*requestGetParameters)[parameterName];

return defaultValue;
}
String HttpRequest::getPostParameter(String parameterName, String defaultValue /* = "" */)
String HttpRequest::getPostParameter(String parameterName,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't like that short lines, it's harder to read. No reason in actual monitors to fit in 80 chars.
Very long lines should be splited, of course.

String defaultValue /* = "" */)
{
if (requestPostParameters && requestPostParameters->contains(parameterName))
return (*requestPostParameters)[parameterName];
return (*requestPostParameters)[parameterName];

return defaultValue;
}
Expand All @@ -64,7 +71,8 @@ String HttpRequest::getCookie(String name, String defaultValue /* = "" */)
int HttpRequest::getContentLength()
{
String len = getHeader("Content-Length");
if (len.length() == 0) return -1;
if (len.length() == 0)
return -1;

return len.toInt();
}
Expand All @@ -77,7 +85,8 @@ String HttpRequest::getContentType()
HttpParseResult HttpRequest::parseHeader(HttpServer *server, pbuf* buf)
{
int headerEnd = NetUtils::pbufFindStr(buf, "\r\n\r\n");
if (headerEnd == -1) return eHPR_Wait;
if (headerEnd == -1)
return eHPR_Wait;
if (headerEnd > NETWORK_MAX_HTTP_PARSING_LEN)
{
debugf("NETWORK_MAX_HTTP_PARSING_LEN");
Expand All @@ -97,15 +106,17 @@ HttpParseResult HttpRequest::parseHeader(HttpServer *server, pbuf* buf)
if (urlParamsStart != -1 && urlParamsStart < urlEnd)
{
path = NetUtils::pbufStrCopy(buf, urlStart, urlParamsStart - urlStart);
if (requestGetParameters == NULL) requestGetParameters = new HashMap<String, String>();
extractParsingItemsList(buf, urlParamsStart + 1, urlEnd, '&', ' ', requestGetParameters);
if (requestGetParameters == NULL)
requestGetParameters = new HashMap<String, String>();
extractParsingItemsList(buf, urlParamsStart + 1, urlEnd, '&', ' ',
requestGetParameters);
}
else
path = NetUtils::pbufStrCopy(buf, urlStart, urlEnd - urlStart);
debugf("path=%s", path.c_str());

int line, nextLine;
line = NetUtils::pbufFindStr(buf, "\r\n", urlEnd) + 2;
line = NetUtils::pbufFindStr(buf, "\r\n", urlEnd) + 2;
do
{
nextLine = NetUtils::pbufFindStr(buf, "\r\n", line);
Expand All @@ -119,22 +130,26 @@ HttpParseResult HttpRequest::parseHeader(HttpServer *server, pbuf* buf)
{
if (name == "Cookie")
{
if (cookies == NULL) cookies = new HashMap<String, String>();
extractParsingItemsList(buf, delim + 1, nextLine, ';', '\r', cookies);
if (cookies == NULL)
cookies = new HashMap<String, String>();
extractParsingItemsList(buf, delim + 1, nextLine, ';',
'\r', cookies);
}
else
{
String value = NetUtils::pbufStrCopy(buf, delim + 1, nextLine - (delim + 1));
String value = NetUtils::pbufStrCopy(buf, delim + 1,
nextLine - (delim + 1));
value.trim();
if (requestHeaders == NULL) requestHeaders = new HashMap<String, String>();
if (requestHeaders == NULL)
requestHeaders = new HashMap<String, String>();
(*requestHeaders)[name] = value;
debugf("%s === %s", name.c_str(), value.c_str());
}
}
}
}
line = nextLine + 2;
} while(nextLine != -1);
} while (nextLine != -1);

if (headerEnd != -1)
{
Expand All @@ -152,7 +167,8 @@ HttpParseResult HttpRequest::parsePostData(HttpServer *server, pbuf* buf)
if (requestPostParameters == NULL)
{
int headerEnd = NetUtils::pbufFindStr(buf, "\r\n\r\n");
if (headerEnd == -1) return eHPR_Failed;
if (headerEnd == -1)
return eHPR_Failed;
if (headerEnd + getContentLength() > NETWORK_MAX_HTTP_PARSING_LEN)
{
debugf("NETWORK_MAX_HTTP_PARSING_LEN");
Expand All @@ -164,7 +180,8 @@ HttpParseResult HttpRequest::parsePostData(HttpServer *server, pbuf* buf)
}
else if (combinePostFrag)
{
String cur = requestPostParameters->keyAt(requestPostParameters->count() - 1);
String cur = requestPostParameters->keyAt(
requestPostParameters->count() - 1);
debugf("Continue POST frag %s", cur.c_str());
int delimItem = NetUtils::pbufFindChar(buf, '&', 0);
if (delimItem == -1)
Expand All @@ -181,7 +198,8 @@ HttpParseResult HttpRequest::parsePostData(HttpServer *server, pbuf* buf)
postDataProcessed += start;
}

bool notFinished = extractParsingItemsList(buf, start, buf->tot_len, '&', ' ', requestPostParameters);
bool notFinished = extractParsingItemsList(buf, start, buf->tot_len, '&',
' ', requestPostParameters);
if (notFinished)
combinePostFrag = true; // continue reading this parameter value
//TODO: continue for param name
Expand All @@ -193,43 +211,65 @@ HttpParseResult HttpRequest::parsePostData(HttpServer *server, pbuf* buf)
return eHPR_Wait;
}

bool HttpRequest::extractParsingItemsList(pbuf* buf, int startPos, int endPos, char delimChar, char endChar,
HashMap<String, String>* resultItems)
bool HttpRequest::extractParsingItemsList(pbuf* buf, int startPos, int endPos,
char delimChar, char endChar, HashMap<String, String>* resultItems)
{
bool continued = false;
int delimItem, nextItem, startItem = startPos;
while (startItem < endPos)
{
delimItem = NetUtils::pbufFindStr(buf, "=", startItem);
if (delimItem == -1 || delimItem > endPos) break;
if (delimItem == -1 || delimItem > endPos)
break;
nextItem = NetUtils::pbufFindChar(buf, delimChar, delimItem + 1);
if (nextItem == -1)
nextItem = NetUtils::pbufFindChar(buf, endChar, delimItem + 1);
if (nextItem > endPos) break;
if (nextItem > endPos)
break;

if (nextItem == -1)
{
nextItem = endPos;
continued = true;
}

String ItemName = NetUtils::pbufStrCopy(buf, startItem, delimItem - startItem);
String ItemValue = NetUtils::pbufStrCopy(buf, delimItem + 1, nextItem - delimItem - 1);
String ItemName = NetUtils::pbufStrCopy(buf, startItem,
delimItem - startItem);
String ItemValue = NetUtils::pbufStrCopy(buf, delimItem + 1,
nextItem - delimItem - 1);
char* nam = uri_unescape(NULL, 0, ItemName.c_str(), -1);
ItemName = nam;
free(nam);
char* val = uri_unescape(NULL, 0, ItemValue.c_str(), -1);
ItemValue = val;
free(val);
ItemName.trim();
if (!continued) ItemValue.trim();
if (!continued)
ItemValue.trim();
debugf("Item: %s = %s", ItemName.c_str(), ItemValue.c_str());
(*resultItems)[ItemName] = ItemValue;
startItem = nextItem + 1;
}
return continued;
}

void HttpRequest::parseRawData(HttpServer *server, pbuf* buf)
{
bodyBuf = (char *) os_zalloc(sizeof(char) * buf->tot_len);
int headerEnd = NetUtils::pbufFindStr(buf, "\r\n\r\n");
if (headerEnd + getContentLength() > NETWORK_MAX_HTTP_PARSING_LEN)
{
debugf("NETWORK_MAX_HTTP_PARSING_LEN");
return;
}
pbuf_copy_partial(buf, bodyBuf, buf->tot_len, headerEnd + 4);
}

char* HttpRequest::getBody()
{
return bodyBuf;
}

bool HttpRequest::isAjax()
{
String req = getHeader("HTTP_X_REQUESTED_WITH");
Expand Down
3 changes: 3 additions & 0 deletions Sming/SmingCore/Network/HttpRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ class HttpRequest
String getPostParameter(String parameterName, String defaultValue = "");
String getHeader(String headerName, String defaultValue = "");
String getCookie(String cookieName, String defaultValue = "");
char* getBody();

public:
HttpParseResult parseHeader(HttpServer *server, pbuf* buf);
HttpParseResult parsePostData(HttpServer *server, pbuf* buf);
bool extractParsingItemsList(pbuf* buf, int startPos, int endPos,
char delimChar, char endChar,
HashMap<String, String> *resultItems);
void parseRawData(HttpServer *server, pbuf* buf);

private:
String method;
Expand All @@ -59,6 +61,7 @@ class HttpRequest
HashMap<String, String> *cookies;
int postDataProcessed;
bool combinePostFrag;
char *bodyBuf;

friend class TemplateFileStream;
};
Expand Down
36 changes: 23 additions & 13 deletions Sming/SmingCore/Network/HttpServerConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
#include "TcpServer.h"
#include "../../Services/cWebsocket/websocket.h"

HttpServerConnection::HttpServerConnection(HttpServer *parentServer, tcp_pcb *clientTcp)
: TcpConnection(clientTcp, true), server(parentServer), state(eHCS_Ready)
HttpServerConnection::HttpServerConnection(HttpServer *parentServer,
tcp_pcb *clientTcp) :
TcpConnection(clientTcp, true), server(parentServer), state(eHCS_Ready)
{
TcpServer::totalConnections++;
response.setHeader("Connection", "close");
Expand All @@ -32,13 +33,13 @@ err_t HttpServerConnection::onReceive(pbuf *buf)
}

/*{
// split.buf.test
pbuf* dbghack = new pbuf();
dbghack->payload = (char*)buf->payload + 100;
dbghack->len = buf->len - 100;
buf->len = 100;
buf->next = dbghack;
}*/
// split.buf.test
pbuf* dbghack = new pbuf();
dbghack->payload = (char*)buf->payload + 100;
dbghack->len = buf->len - 100;
buf->len = 100;
buf->next = dbghack;
}*/

if (state == eHCS_Ready)
{
Expand All @@ -54,11 +55,14 @@ err_t HttpServerConnection::onReceive(pbuf *buf)
else if (res == eHPR_Successful)
{
debugf("Request: %s, %s", request.getRequestMethod().c_str(),
(request.getContentLength() > 0 ? (String(request.getContentLength()) + " bytes").c_str() : "nodata"));
(request.getContentLength() > 0 ?
(String(request.getContentLength()) + " bytes").c_str() :
"nodata"));

String contType = request.getContentType();
contType.toLowerCase();
if (request.getContentLength() > 0 && contType.indexOf(ContentType::FormUrlEncoded) != -1)
if (request.getContentLength() > 0
&& contType.indexOf(ContentType::FormUrlEncoded) != -1)
state = eHCS_ParsePostData;
else
state = eHCS_ParsingCompleted;
Expand Down Expand Up @@ -86,6 +90,10 @@ err_t HttpServerConnection::onReceive(pbuf *buf)
state = eHCS_ParsingCompleted;
}
}
else if (state == eHCS_ParsingCompleted && request.getContentLength() > 0)
{
request.parseRawData(server, buf);
}

// Fire callbacks
TcpConnection::onReceive(buf);
Expand All @@ -102,7 +110,8 @@ void HttpServerConnection::beginSendData()
return;
}

if (!response.hasBody() && (response.getStatusCode() < 100 || response.getStatusCode() > 399))
if (!response.hasBody()
&& (response.getStatusCode() < 100 || response.getStatusCode() > 399))
{
// Show default error message
sendError();
Expand Down Expand Up @@ -173,7 +182,8 @@ void HttpServerConnection::onError(err_t err)
TcpConnection::onError(err);
}

void HttpServerConnection::setDisconnectionHandler(HttpServerConnectionDelegate handler)
void HttpServerConnection::setDisconnectionHandler(
HttpServerConnectionDelegate handler)
{
disconnection = handler;
}