Skip to content

Commit 0e5f54a

Browse files
committed
Throw ISE from Servlet parameter methods
Fixes #13670 as an alternate to #13678 Fully deprecate and replace BadMessageException
1 parent 35ca941 commit 0e5f54a

File tree

49 files changed

+171
-165
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+171
-165
lines changed

jetty-core/jetty-client/src/test/java/org/eclipse/jetty/client/http/HttpReceiverOverHTTPTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
import org.eclipse.jetty.client.transport.internal.HttpChannelOverHTTP;
3535
import org.eclipse.jetty.client.transport.internal.HttpConnectionOverHTTP;
3636
import org.eclipse.jetty.client.transport.internal.HttpReceiverOverHTTP;
37-
import org.eclipse.jetty.http.BadMessageException;
3837
import org.eclipse.jetty.http.HttpCompliance;
38+
import org.eclipse.jetty.http.HttpException;
3939
import org.eclipse.jetty.http.HttpFields;
4040
import org.eclipse.jetty.http.HttpHeader;
4141
import org.eclipse.jetty.http.HttpVersion;
@@ -226,7 +226,7 @@ public void testReceiveBadResponse(HttpCompliance compliance) throws Exception
226226

227227
ExecutionException e = assertThrows(ExecutionException.class, () -> completable.get(5, TimeUnit.SECONDS));
228228
assertThat(e.getCause(), instanceOf(HttpResponseException.class));
229-
assertThat(e.getCause().getCause(), instanceOf(BadMessageException.class));
229+
assertThat(e.getCause().getCause(), instanceOf(HttpException.class));
230230
assertThat(e.getCause().getCause().getCause(), instanceOf(NumberFormatException.class));
231231
}
232232

jetty-core/jetty-compression/jetty-compression-server/src/main/java/org/eclipse/jetty/compression/server/CompressionHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.eclipse.jetty.compression.Compression;
2222
import org.eclipse.jetty.compression.server.internal.CompressionResponse;
2323
import org.eclipse.jetty.compression.server.internal.DecompressionRequest;
24-
import org.eclipse.jetty.http.BadMessageException;
2524
import org.eclipse.jetty.http.ComplianceViolation;
2625
import org.eclipse.jetty.http.HttpException;
2726
import org.eclipse.jetty.http.HttpField;
@@ -280,7 +279,7 @@ protected void onComplianceViolation(ComplianceViolation violation, String value
280279
if (httpConfiguration.getHttpCompliance().allows(violation))
281280
httpConfiguration.notifyViolation(violation, value);
282281
else
283-
throw new BadMessageException(violation.toString());
282+
throw new HttpException.IllegalStateException(HttpStatus.BAD_REQUEST_400, violation.toString());
284283
}
285284
};
286285
}

jetty-core/jetty-fcgi/jetty-fcgi-server/src/main/java/org/eclipse/jetty/fcgi/server/internal/ServerFCGIConnection.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
import org.eclipse.jetty.fcgi.generator.Flusher;
2222
import org.eclipse.jetty.fcgi.generator.ServerGenerator;
2323
import org.eclipse.jetty.fcgi.parser.ServerParser;
24-
import org.eclipse.jetty.http.BadMessageException;
24+
import org.eclipse.jetty.http.HttpException;
2525
import org.eclipse.jetty.http.HttpField;
26+
import org.eclipse.jetty.http.HttpStatus;
2627
import org.eclipse.jetty.http.HttpVersion;
2728
import org.eclipse.jetty.io.ByteBufferPool;
2829
import org.eclipse.jetty.io.Content;
@@ -401,7 +402,7 @@ public void onFailure(int request, Throwable failure)
401402
if (LOG.isDebugEnabled())
402403
LOG.atDebug().setCause(failure).log("Request {} failure on {}", request, stream);
403404
if (stream != null)
404-
ThreadPool.executeImmediately(getExecutor(), stream.getHttpChannel().onFailure(new BadMessageException(null, failure)));
405+
ThreadPool.executeImmediately(getExecutor(), stream.getHttpChannel().onFailure(new HttpException.IllegalStateException(HttpStatus.BAD_REQUEST_400, null, failure)));
405406
stream = null;
406407
}
407408
}

jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/BadMessageException.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515

1616
/**
1717
* <p>Exception thrown to indicate a Bad HTTP Message has either been received
18-
* or attempted to be generated. Typically, these are handled with either 400
18+
* or attempted to be generated. Typically, these are handled with {@code 4xx} or {@code 5xx}
1919
* responses.</p>
20-
* @see HttpException
20+
* @deprecated use HttpException
2121
*/
22+
@Deprecated(since = "12.1.6", forRemoval = true)
2223
public class BadMessageException extends HttpException.RuntimeException
2324
{
2425
public BadMessageException()

jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/CookieCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public List<HttpCookie> getCookies(HttpFields headers, ComplianceViolation.Liste
159159
}
160160
catch (CookieParser.InvalidCookieException invalidCookieException)
161161
{
162-
throw new BadMessageException(invalidCookieException.getMessage(), invalidCookieException);
162+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, invalidCookieException.getMessage(), invalidCookieException);
163163
}
164164
}
165165

jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/HostPortHttpField.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected HostPortHttpField(HttpHeader header, String name, String authority)
3838
}
3939
catch (Exception e)
4040
{
41-
throw new BadMessageException("Bad HostPort", e);
41+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "Bad HostPort", e);
4242
}
4343
}
4444

jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/HttpCompliance.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,6 @@ private static void assertAllowed(Violation violation, HttpCompliance mode, Comp
456456
mode, violation, violation.getDescription()
457457
));
458458
else
459-
throw new BadMessageException(violation.getDescription());
459+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, violation.getDescription());
460460
}
461461
}

jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,12 @@ public void setHeaderCacheCaseSensitive(boolean headerCacheCaseSensitive)
364364
_fieldCache.setCaseSensitive(headerCacheCaseSensitive);
365365
}
366366

367-
protected void checkViolation(Violation violation) throws BadMessageException
367+
protected void checkViolation(Violation violation) throws HttpException.RuntimeException
368368
{
369369
if (violation.isAllowedBy(_complianceMode))
370370
reportComplianceViolation(violation, violation.getDescription());
371371
else
372-
throw new BadMessageException(violation.getDescription());
372+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, violation.getDescription());
373373
}
374374

375375
protected void reportComplianceViolation(Violation violation)
@@ -503,7 +503,7 @@ private HttpTokens.Token next(ByteBuffer buffer)
503503

504504
case CR:
505505
if (_cr)
506-
throw new BadMessageException("Bad EOL");
506+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "Bad EOL");
507507

508508
if (buffer.hasRemaining())
509509
{
@@ -514,7 +514,7 @@ private HttpTokens.Token next(ByteBuffer buffer)
514514
{
515515
case CNTL -> throw new IllegalCharacterException(_state, t, buffer);
516516
case LF -> EOL_CRLF;
517-
default -> throw new BadMessageException("Bad EOL");
517+
default -> throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "Bad EOL");
518518
};
519519
}
520520
_cr = true;
@@ -529,7 +529,7 @@ private HttpTokens.Token next(ByteBuffer buffer)
529529
case OTEXT:
530530
case COLON:
531531
if (_cr)
532-
throw new BadMessageException("Bad EOL");
532+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "Bad EOL");
533533
break;
534534

535535
default:
@@ -547,9 +547,9 @@ private void addAndCheckHeadersSize(int delta)
547547
if (_headerBytes > _maxHeaderBytes)
548548
{
549549
if (_state == State.URI)
550-
throw new BadMessageException(HttpStatus.URI_TOO_LONG_414);
550+
throw new HttpException.RuntimeException(HttpStatus.URI_TOO_LONG_414);
551551
if (_requestParser)
552-
throw new BadMessageException(HttpStatus.REQUEST_HEADER_FIELDS_TOO_LARGE_431);
552+
throw new HttpException.RuntimeException(HttpStatus.REQUEST_HEADER_FIELDS_TOO_LARGE_431);
553553
throw new HttpException.RuntimeException(_responseStatus, "Response Header Bytes Too Large");
554554
}
555555
}
@@ -786,7 +786,7 @@ else if (Violation.CASE_INSENSITIVE_METHOD.isAllowedBy(_complianceMode))
786786
break;
787787

788788
case EOL:
789-
throw new BadMessageException("No URI");
789+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "No URI");
790790

791791
case ALPHA:
792792
case DIGIT:
@@ -818,7 +818,7 @@ else if (Violation.CASE_INSENSITIVE_METHOD.isAllowedBy(_complianceMode))
818818
_string.append(t.getChar());
819819
break;
820820
case EOL:
821-
throw new BadMessageException("No Status");
821+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "No Status");
822822
default:
823823
throw new IllegalCharacterException(_state, t, buffer);
824824
}
@@ -838,7 +838,7 @@ else if (Violation.CASE_INSENSITIVE_METHOD.isAllowedBy(_complianceMode))
838838
if (!_requestParser)
839839
{
840840
if (t.getType() != HttpTokens.Type.DIGIT || t.getByte() == '0')
841-
throw new BadMessageException("Bad status");
841+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "Bad status");
842842
setState(State.STATUS);
843843
setResponseStatus(t.getByte() - '0');
844844
}
@@ -873,7 +873,7 @@ else if (Violation.CASE_INSENSITIVE_METHOD.isAllowedBy(_complianceMode))
873873

874874
default:
875875
if (_requestParser)
876-
throw new BadMessageException("No URI");
876+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "No URI");
877877
else
878878
throw new HttpException.RuntimeException(_responseStatus, "No Status");
879879
}
@@ -885,14 +885,14 @@ else if (Violation.CASE_INSENSITIVE_METHOD.isAllowedBy(_complianceMode))
885885
{
886886
case SPACE:
887887
if (_responseStatus < 100)
888-
throw new BadMessageException("Bad status");
888+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "Bad status");
889889
setState(State.SPACE2);
890890
break;
891891

892892
case DIGIT:
893893
_responseStatus = _responseStatus * 10 + (t.getByte() - '0');
894894
if (_responseStatus >= 1000)
895-
throw new BadMessageException("Bad status");
895+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "Bad status");
896896
break;
897897

898898
case EOL:
@@ -902,7 +902,7 @@ else if (Violation.CASE_INSENSITIVE_METHOD.isAllowedBy(_complianceMode))
902902
break;
903903

904904
default:
905-
throw new BadMessageException("Bad status");
905+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "Bad status");
906906
}
907907
break;
908908

@@ -1154,7 +1154,7 @@ private void parsedHeader()
11541154
{
11551155
checkViolation(MULTIPLE_CONTENT_LENGTHS);
11561156
if (contentLength != _contentLength)
1157-
throw new BadMessageException(MULTIPLE_CONTENT_LENGTHS.getDescription());
1157+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, MULTIPLE_CONTENT_LENGTHS.getDescription());
11581158
}
11591159
_hasContentLength = true;
11601160

@@ -1173,7 +1173,7 @@ private void parsedHeader()
11731173

11741174
// we encountered another Transfer-Encoding header, but chunked was already set
11751175
if (_endOfContent == EndOfContent.CHUNKED_CONTENT)
1176-
throw new BadMessageException("Bad Transfer-Encoding, chunked not last");
1176+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "Bad Transfer-Encoding, chunked not last");
11771177

11781178
if (HttpHeaderValue.CHUNKED.is(_valueString))
11791179
{
@@ -1190,7 +1190,7 @@ private void parsedHeader()
11901190
if (HttpHeaderValue.CHUNKED.is(values.get(i)))
11911191
{
11921192
if (chunked != -1)
1193-
throw new BadMessageException("Bad Transfer-Encoding, multiple chunked tokens");
1193+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "Bad Transfer-Encoding, multiple chunked tokens");
11941194
chunked = i;
11951195
// declared chunked
11961196
_endOfContent = EndOfContent.CHUNKED_CONTENT;
@@ -1199,7 +1199,7 @@ private void parsedHeader()
11991199
// we have a non-chunked token after a declared chunked token
12001200
else if (_endOfContent == EndOfContent.CHUNKED_CONTENT)
12011201
{
1202-
throw new BadMessageException("Bad Transfer-Encoding, chunked not last");
1202+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "Bad Transfer-Encoding, chunked not last");
12031203
}
12041204
}
12051205
}
@@ -1287,7 +1287,7 @@ private void parsedTrailer()
12871287
private long convertContentLength(String valueString)
12881288
{
12891289
if (valueString == null || valueString.isEmpty())
1290-
throw new BadMessageException("Invalid Content-Length Value", new NumberFormatException());
1290+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "Invalid Content-Length Value", new NumberFormatException());
12911291

12921292
long value = 0;
12931293
int length = valueString.length();
@@ -1296,7 +1296,7 @@ private long convertContentLength(String valueString)
12961296
{
12971297
char c = valueString.charAt(i);
12981298
if (c < '0' || c > '9')
1299-
throw new BadMessageException("Invalid Content-Length Value", new NumberFormatException());
1299+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "Invalid Content-Length Value", new NumberFormatException());
13001300

13011301
value = Math.addExact(Math.multiplyExact(value, 10), c - '0');
13021302
}
@@ -1370,14 +1370,14 @@ protected boolean parseFields(ByteBuffer buffer)
13701370
{
13711371
// Transfer-Encoding chunked not specified
13721372
// https://tools.ietf.org/html/rfc7230#section-3.3.1
1373-
throw new BadMessageException("Bad Transfer-Encoding, chunked not last");
1373+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "Bad Transfer-Encoding, chunked not last");
13741374
}
13751375
}
13761376

13771377
// Was there a required host header?
13781378
if (_parsedHost == null && _version == HttpVersion.HTTP_1_1 && _requestParser)
13791379
{
1380-
throw new BadMessageException("No Host");
1380+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "No Host");
13811381
}
13821382

13831383
// is it a response that cannot have a body?
@@ -1803,7 +1803,7 @@ else if (isTerminated())
18031803
LOG.debug("{} EOF in {}", this, _state);
18041804
setState(State.CLOSED);
18051805
if (_requestParser)
1806-
_handler.badMessage(new BadMessageException(HttpStatus.BAD_REQUEST_400, "Early EOF"));
1806+
_handler.badMessage(new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "Early EOF"));
18071807
else
18081808
_handler.badMessage(new HttpException.RuntimeException(_responseStatus, "Early EOF"));
18091809
break;
@@ -1821,7 +1821,7 @@ else if (isTerminated())
18211821
else
18221822
{
18231823
if (_requestParser)
1824-
bad = new BadMessageException("Bad Request", x);
1824+
bad = new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "Bad Request", x);
18251825
else
18261826
bad = new HttpException.RuntimeException(_responseStatus, "Bad Response", x);
18271827
}
@@ -1968,7 +1968,7 @@ protected boolean parseContent(ByteBuffer buffer)
19681968
if (t.isHexDigit())
19691969
{
19701970
if (_chunkLength > MAX_CHUNK_LENGTH)
1971-
throw new BadMessageException(HttpStatus.PAYLOAD_TOO_LARGE_413);
1971+
throw new HttpException.RuntimeException(HttpStatus.PAYLOAD_TOO_LARGE_413);
19721972
_chunkLength = _chunkLength * 16 + t.getHexDigit();
19731973
}
19741974
else if (t.getChar() == ';')
@@ -2256,11 +2256,11 @@ public interface ResponseHandler extends HttpHandler
22562256
void startResponse(HttpVersion version, int status, String reason);
22572257
}
22582258

2259-
private static class IllegalCharacterException extends BadMessageException
2259+
private static class IllegalCharacterException extends HttpException.RuntimeException
22602260
{
22612261
private IllegalCharacterException(State state, HttpTokens.Token token, ByteBuffer buffer)
22622262
{
2263-
super(String.format("Illegal character %s", token));
2263+
super(HttpStatus.BAD_REQUEST_400, String.format("Illegal character %s", token));
22642264
if (LOG.isDebugEnabled())
22652265
LOG.debug(String.format("Illegal character %s in state=%s for buffer %s", token, state, BufferUtil.toDetailString(buffer)));
22662266
}
@@ -2381,7 +2381,7 @@ protected void onComplianceViolation(ComplianceViolation violation, String value
23812381
if (_complianceMode.allows(violation))
23822382
_handler.onViolation(new ComplianceViolation.Event(_complianceMode, violation, value));
23832383
else
2384-
throw new BadMessageException(violation.toString());
2384+
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, violation.toString());
23852385
}
23862386
};
23872387
}

jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/HttpTester.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ public ByteBuffer generate()
491491

492492
case HEADER_OVERFLOW:
493493
if (header.capacity() >= 32 * 1024)
494-
throw new BadMessageException(500, "Header too large");
494+
throw new HttpException.RuntimeException(HttpStatus.INTERNAL_SERVER_ERROR_500, "Header too large");
495495
header = BufferUtil.allocate(32 * 1024);
496496
continue;
497497

0 commit comments

Comments
 (0)