Skip to content

Commit 129cc3b

Browse files
SimulPiscatorSimulPiscator
andauthored
Merge commit from fork
Co-authored-by: SimulPiscator <simul.piscator@github.com>
1 parent c3ef018 commit 129cc3b

2 files changed

Lines changed: 18 additions & 12 deletions

File tree

web/httpserver.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
#include "httpserver.h"
2020

2121
#include <atomic>
22+
#include <cmath>
2223
#include <cstring>
2324
#include <ctime>
2425
#include <sstream>
@@ -44,6 +45,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
4445
#include "basic/fdbuf.h"
4546
#include "errorpage.h"
4647

48+
static const int kMaxRequestLength = 1024 * 1024; // bytes
49+
4750
const char* HttpServer::HTTP_GET = "GET";
4851
const char* HttpServer::HTTP_POST = "POST";
4952
const char* HttpServer::HTTP_DELETE = "DELETE";
@@ -386,7 +389,7 @@ struct HttpServer::Private
386389
fdbuf buf(fd);
387390
std::istream is(&buf);
388391
std::ostream os(&buf);
389-
Request request(is);
392+
Request request(is, kMaxRequestLength);
390393
Response response(os);
391394
if (!request.isValid()) {
392395
response.setStatus(HTTP_BAD_REQUEST);
@@ -722,9 +725,10 @@ HttpServer::Response::sendHeaders()
722725
return mpChunkstream ? *mpChunkstream : mStream;
723726
}
724727

725-
HttpServer::Request::Request(std::istream& is)
728+
HttpServer::Request::Request(std::istream& is, int maxLength)
726729
: mStream(is)
727730
, mValid(true)
731+
, mContentLength(-1)
728732
{
729733
std::string line;
730734
if (std::getline(is, line)) {
@@ -747,6 +751,15 @@ HttpServer::Request::Request(std::istream& is)
747751
}
748752
}
749753
}
754+
if (mHeaders.hasKey(HTTP_HEADER_CONTENT_LENGTH)) {
755+
double length = mHeaders.getNumber(HTTP_HEADER_CONTENT_LENGTH);
756+
if (std::isnan(length) || length > maxLength) {
757+
mValid = false;
758+
}
759+
else {
760+
mContentLength = length;
761+
}
762+
}
750763
}
751764

752765
const std::string&
@@ -784,14 +797,6 @@ HttpServer::Request::formData() const
784797
return mFormData;
785798
}
786799

787-
int
788-
HttpServer::Request::contentLength() const
789-
{
790-
return mHeaders.hasKey(HTTP_HEADER_CONTENT_LENGTH)
791-
? mHeaders.getNumber(HTTP_HEADER_CONTENT_LENGTH)
792-
: -1;
793-
}
794-
795800
std::ostream&
796801
HttpServer::Request::print(std::ostream& os) const
797802
{

web/httpserver.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ class HttpServer
9898
class Request
9999
{
100100
public:
101-
explicit Request(std::istream&);
101+
Request(std::istream&, int maxLength);
102102
bool isValid() const { return mValid; }
103103
const std::string& uri() const { return mUri; }
104104
const std::string& method() const { return mMethod; }
105105
const std::string& protocol() const { return mProtocol; }
106106
const std::string& header(const std::string& s) const;
107107
const Dictionary& headers() const { return mHeaders; }
108-
int contentLength() const;
108+
int contentLength() const { return mContentLength; }
109109
const std::string& content() const;
110110
bool hasFormData() const;
111111
const Dictionary& formData() const;
@@ -118,6 +118,7 @@ class HttpServer
118118
bool mValid;
119119
std::string mUri, mMethod, mProtocol, mLogInfo;
120120
Dictionary mHeaders;
121+
int mContentLength;
121122
mutable std::string mContent;
122123
mutable Dictionary mFormData;
123124
};

0 commit comments

Comments
 (0)