@@ -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+
4750const char * HttpServer::HTTP_GET = " GET" ;
4851const char * HttpServer::HTTP_POST = " POST" ;
4952const 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
752765const 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-
795800std::ostream&
796801HttpServer::Request::print (std::ostream& os) const
797802{
0 commit comments