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

Extend POST parameter handling to PUT requests as well #1081

Closed
wants to merge 1 commit 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
4 changes: 2 additions & 2 deletions Sming/SmingCore/DataSourceStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ void TemplateFileStream::setVarsFromRequest(const HttpRequest& request)
{
if (request.requestGetParameters != NULL)
templateData.setMultiple(*request.requestGetParameters);
if (request.requestPostParameters != NULL)
templateData.setMultiple(*request.requestPostParameters);
if (request.requestPostPutParameters != NULL)
templateData.setMultiple(*request.requestPostPutParameters);
}

///////////////////////////////////////////////////////////////////////////
Expand Down
28 changes: 14 additions & 14 deletions Sming/SmingCore/Network/HttpRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ HttpRequest::HttpRequest()
{
requestHeaders = NULL;
requestGetParameters = NULL;
requestPostParameters = NULL;
requestPostPutParameters = NULL;
cookies = NULL;
headerDataProcessed = 0;
postDataProcessed = 0;
postPutDataProcessed = 0;
bodyBuf = NULL;
tmpbuf = "";
}
Expand All @@ -27,9 +27,9 @@ HttpRequest::~HttpRequest()
{
delete requestHeaders;
delete requestGetParameters;
delete requestPostParameters;
delete requestPostPutParameters;
delete cookies;
postDataProcessed = 0;
postPutDataProcessed = 0;
if (bodyBuf != NULL)
{
os_free(bodyBuf);
Expand All @@ -43,10 +43,10 @@ String HttpRequest::getQueryParameter(String parameterName, String defaultValue

return defaultValue;
}
String HttpRequest::getPostParameter(String parameterName, String defaultValue /* = "" */)
String HttpRequest::getPostPutParameter(String parameterName, String defaultValue /* = "" */)
{
if (requestPostParameters && requestPostParameters->contains(parameterName))
return (*requestPostParameters)[parameterName];
if (requestPostPutParameters && requestPostPutParameters->contains(parameterName))
return (*requestPostPutParameters)[parameterName];

return defaultValue;
}
Expand Down Expand Up @@ -169,12 +169,12 @@ HttpParseResult HttpRequest::parseHeader(HttpServer *server, pbuf* buf)
return eHPR_Wait;
}

HttpParseResult HttpRequest::parsePostData(HttpServer *server, pbuf* buf)
HttpParseResult HttpRequest::parsePostPutData(HttpServer *server, pbuf* buf)
{
int start = 0;
tmpbuf += NetUtils::pbufStrCopy(buf, 0, buf->tot_len);
// First enter
if (requestPostParameters == NULL)
if (requestPostPutParameters == NULL)
{
int headerEnd = NetUtils::pbufFindStr(buf, "\r\n\r\n");
if (headerEnd == -1) return eHPR_Failed;
Expand All @@ -183,7 +183,7 @@ HttpParseResult HttpRequest::parsePostData(HttpServer *server, pbuf* buf)
debugf("NETWORK_MAX_HTTP_PARSING_LEN");
return eHPR_Failed;
}
requestPostParameters = new HashMap<String, String>();
requestPostPutParameters = new HashMap<String, String>();
start = headerEnd + 4;
tmpbuf = tmpbuf.substring(start, tmpbuf.length());
}
Expand All @@ -193,16 +193,16 @@ HttpParseResult HttpRequest::parsePostData(HttpServer *server, pbuf* buf)
contType.toLowerCase();
if (contType.indexOf(ContentType::FormUrlEncoded) != -1)
{
tmpbuf = extractParsingItemsList(tmpbuf, 0, tmpbuf.length(), '&', ' ', requestPostParameters);
tmpbuf = extractParsingItemsList(tmpbuf, 0, tmpbuf.length(), '&', ' ', requestPostPutParameters);
}

postDataProcessed += buf->tot_len - start ;
postPutDataProcessed += buf->tot_len - start ;

if (postDataProcessed == getContentLength())
if (postPutDataProcessed == getContentLength())
{
return eHPR_Successful;
}
else if (postDataProcessed > getContentLength())
else if (postPutDataProcessed > getContentLength())
{
//avoid bufferoverflow if client announces non-correct content-length
debugf("NETWORK_MAX_HTTP_PARSING_LEN");
Expand Down
8 changes: 4 additions & 4 deletions Sming/SmingCore/Network/HttpRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ class HttpRequest
bool isWebSocket();

String getQueryParameter(String parameterName, String defaultValue = "");
String getPostParameter(String parameterName, String defaultValue = "");
String getPostPutParameter(String parameterName, String defaultValue = "");
String getHeader(String headerName, String defaultValue = "");
String getCookie(String cookieName, String defaultValue = "");
String getBody();

public:
HttpParseResult parseHeader(HttpServer *server, pbuf* buf);
HttpParseResult parsePostData(HttpServer *server, pbuf* buf);
HttpParseResult parsePostPutData(HttpServer *server, pbuf* buf);
String extractParsingItemsList(String& buf, int startPos, int endPos,
char delimChar, char endChar,
HashMap<String, String>* resultItems);
Expand All @@ -57,9 +57,9 @@ class HttpRequest
String tmpbuf;
HashMap<String, String> *requestHeaders;
HashMap<String, String> *requestGetParameters;
HashMap<String, String> *requestPostParameters;
HashMap<String, String> *requestPostPutParameters;
HashMap<String, String> *cookies;
int postDataProcessed;
int postPutDataProcessed;
int headerDataProcessed;
char *bodyBuf;

Expand Down
15 changes: 8 additions & 7 deletions Sming/SmingCore/Network/HttpServerConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ err_t HttpServerConnection::onReceive(pbuf *buf)

String contType = request.getContentType();
contType.toLowerCase();
if (request.getContentLength() > 0 && request.getRequestMethod() == RequestMethod::POST)
state = eHCS_ParsePostData;
if (request.getContentLength() > 0 && (request.getRequestMethod() == RequestMethod::POST \
|| request.getRequestMethod() == RequestMethod::PUT))
state = eHCS_ParsePostPutData;
else
state = eHCS_ParsingCompleted;
}
Expand All @@ -69,20 +70,20 @@ err_t HttpServerConnection::onReceive(pbuf *buf)
server->processWebSocketFrame(buf, *this);
}

if (state == eHCS_ParsePostData)
if (state == eHCS_ParsePostPutData)
{
HttpParseResult res = request.parsePostData(server, buf);
HttpParseResult res = request.parsePostPutData(server, buf);
if (res == eHPR_Wait)
debugf("POST WAIT");
debugf("%s WAIT", request.getRequestMethod().c_str());
else if (res == eHPR_Failed)
{
debugf("POST FAILED");
debugf("%s FAILED", request.getRequestMethod().c_str());
response.badRequest();
sendError();
}
else if (res == eHPR_Successful)
{
debugf("POST Parsed");
debugf("%s Parsed", request.getRequestMethod().c_str());
state = eHCS_ParsingCompleted;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sming/SmingCore/Network/HttpServerConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class HttpServer;
enum HttpConnectionState
{
eHCS_Ready,
eHCS_ParsePostData,
eHCS_ParsePostPutData,
eHCS_ParsingCompleted,
eHCS_Sending,
eHCS_WebSocketFrames,
Expand Down