@@ -75,6 +75,10 @@ HttpConnection_t::HttpConnection_t()
75
75
// instead of buffering it here. To get the latter behavior, user code must call
76
76
// dont_accumulate_post.
77
77
bAccumulatePost = true ;
78
+
79
+ // By default this limit is initialized to 20 MiB, it could be changed at runtime
80
+ // by the user if needed
81
+ ContentLengthLimit = MaxContentLength;
78
82
}
79
83
80
84
@@ -140,6 +144,20 @@ void HttpConnection_t::ReceivePostData (const char *data, int len)
140
144
cerr << " UNIMPLEMENTED ReceivePostData" << endl;
141
145
}
142
146
147
+ /* ********************************
148
+ HttpConnection_t::Get/SetMaxContentLength
149
+ *********************************/
150
+
151
+ int HttpConnection_t::GetMaxContentLength ()
152
+ {
153
+ return ContentLengthLimit;
154
+ }
155
+
156
+ void HttpConnection_t::SetMaxContentLength (int len)
157
+ {
158
+ ContentLengthLimit = len;
159
+ }
160
+
143
161
/* ****************************
144
162
HttpConnection_t::ConsumeData
145
163
*****************************/
@@ -254,7 +272,7 @@ void HttpConnection_t::ConsumeData (const char *data, int length)
254
272
}
255
273
else {
256
274
const char *nl = strpbrk (data, " \r\n " );
257
- int len = nl ? (nl - data) : length;
275
+ int len = nl ? (int )( nl - data) : length;
258
276
if ((size_t )(HeaderLinePos + len) >= sizeof (HeaderLine)) {
259
277
// TODO, log this
260
278
goto fail_connection;
@@ -358,17 +376,17 @@ bool HttpConnection_t::_InterpretHeaderLine (const char *header)
358
376
if (bContentLengthSeen) {
359
377
// TODO, log this. There are some attacks that depend
360
378
// on sending more than one content-length header.
361
- _SendError (406 );
379
+ _SendError (400 , " Bad Request " );
362
380
return false ;
363
381
}
364
382
bContentLengthSeen = true ;
365
383
const char *s = header + 15 ;
366
384
while (*s && ((*s==' ' ) || (*s==' \t ' )))
367
385
s++;
368
386
ContentLength = atoi (s);
369
- if (ContentLength > MaxContentLength ) {
387
+ if (ContentLength > ContentLengthLimit ) {
370
388
// TODO, log this.
371
- _SendError (406 );
389
+ _SendError (413 , " Request Entity Too Large " );
372
390
return false ;
373
391
}
374
392
}
@@ -400,7 +418,7 @@ bool HttpConnection_t::_InterpretHeaderLine (const char *header)
400
418
401
419
// Copy the incoming header into a block
402
420
if ((HeaderBlockPos + strlen (header) + 1 ) < HeaderBlockSize) {
403
- int len = strlen (header);
421
+ int len = ( int ) strlen (header);
404
422
memcpy (HeaderBlock+HeaderBlockPos, header, len);
405
423
HeaderBlockPos += len;
406
424
HeaderBlock [HeaderBlockPos++] = 0 ;
@@ -439,26 +457,27 @@ bool HttpConnection_t::_InterpretRequest (const char *header)
439
457
440
458
const char *blank = strchr (header, ' ' );
441
459
if (!blank) {
442
- _SendError (406 );
460
+ _SendError (400 , " Bad Request " );
443
461
return false ;
444
462
}
445
463
446
- if (!_DetectVerbAndSetEnvString (header, blank - header))
464
+ if (!_DetectVerbAndSetEnvString (header, ( int )( blank - header) ))
447
465
return false ;
448
466
449
467
blank++;
450
468
if (*blank != ' /' ) {
451
- _SendError (406 );
469
+ _SendError (400 , " Bad Request " );
452
470
return false ;
453
471
}
454
472
455
473
const char *blank2 = strchr (blank, ' ' );
456
474
if (!blank2) {
457
- _SendError (406 );
475
+ _SendError (400 , " Bad Request " );
458
476
return false ;
459
477
}
478
+
460
479
if (strcasecmp (blank2 + 1 , " HTTP/1.0" ) && strcasecmp (blank2 + 1 , " HTTP/1.1" )) {
461
- _SendError (505 );
480
+ _SendError (505 , " HTTP Version Not Supported " );
462
481
return false ;
463
482
}
464
483
@@ -573,13 +592,18 @@ HttpConnection_t::_SendError
573
592
****************************/
574
593
575
594
void HttpConnection_t::_SendError (int code)
595
+ {
596
+ _SendError (code, " ..." );
597
+ }
598
+
599
+ void HttpConnection_t::_SendError (int code, const char *desc)
576
600
{
577
601
stringstream ss;
578
- ss << " HTTP/1.1 " << code << " ... \r\n " ;
602
+ ss << " HTTP/1.1 " << code << " " << desc << " \r\n " ;
579
603
ss << " Connection: close\r\n " ;
580
604
ss << " Content-type: text/plain\r\n " ;
581
605
ss << " \r\n " ;
582
606
ss << " Detected error: HTTP code " << code;
583
607
584
- SendData (ss.str ().c_str (), ss.str ().length ());
585
- }
608
+ SendData (ss.str ().c_str (), ( int ) ss.str ().length ());
609
+ }
0 commit comments