-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow chunked responses without payload body (#1060)
Motivation: Some older implementations may use connection closures as a marker of the end of HTTP/1.1 messages if the receiver did not receive any part of the payload body before the connection closure. Modifications: - Introduce `H1SpecExceptions` that allow configuring special exceptions for HTTP/1.1 specification on `H1ProtocolConfig`; - Add a `H1SpecExceptions#allowPrematureClosureBeforePayloadBody()` option; - Add tests to verify that the new exception works as expected on the client side; Result: `HttpObjectDecoder` marks the chunked response as complete if server closes the connection right after the headers.
- Loading branch information
1 parent
1a7498d
commit 97f0d60
Showing
10 changed files
with
556 additions
and
14 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
servicetalk-http-api/src/testFixtures/java/io/servicetalk/http/api/Matchers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright © 2020 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.servicetalk.http.api; | ||
|
||
import org.hamcrest.Description; | ||
import org.hamcrest.Matcher; | ||
import org.hamcrest.TypeSafeMatcher; | ||
|
||
import static io.servicetalk.http.api.CharSequences.contentEquals; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* Custom {@link Matcher}s specific to http-api. | ||
*/ | ||
public final class Matchers { | ||
|
||
private Matchers() { | ||
// No instances | ||
} | ||
|
||
/** | ||
* {@link Matcher} representation for {@link CharSequences#contentEquals(CharSequence, CharSequence)}. | ||
* | ||
* @param expected expected {@link CharSequence} value | ||
* @return a {@link Matcher} to verify content equality of two {@link CharSequence}s | ||
*/ | ||
public static Matcher<CharSequence> contentEqualTo(final CharSequence expected) { | ||
requireNonNull(expected); | ||
return new TypeSafeMatcher<CharSequence>() { | ||
|
||
@Override | ||
protected boolean matchesSafely(final CharSequence item) { | ||
if (item == null) { | ||
return false; | ||
} | ||
return contentEquals(expected, item); | ||
} | ||
|
||
@Override | ||
public void describeTo(final Description description) { | ||
description.appendValue(expected); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
servicetalk-http-netty/src/main/java/io/servicetalk/http/netty/H1SpecExceptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright © 2020 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.servicetalk.http.netty; | ||
|
||
/** | ||
* Additional exceptions for <a href="https://tools.ietf.org/html/rfc7230">HTTP/1.1</a> specification. | ||
*/ | ||
public final class H1SpecExceptions { | ||
|
||
private final boolean allowPrematureClosureBeforePayloadBody; | ||
|
||
private H1SpecExceptions(final boolean allowPrematureClosureBeforePayloadBody) { | ||
this.allowPrematureClosureBeforePayloadBody = allowPrematureClosureBeforePayloadBody; | ||
} | ||
|
||
/** | ||
* Allows interpreting connection closures as the end of HTTP/1.1 messages if the receiver did not receive any part | ||
* of the payload body before the connection closure. | ||
* | ||
* @return {@code true} if the receiver should interpret connection closures as the end of HTTP/1.1 messages if it | ||
* did not receive any part of the payload body before the connection closure | ||
*/ | ||
public boolean allowPrematureClosureBeforePayloadBody() { | ||
return allowPrematureClosureBeforePayloadBody; | ||
} | ||
|
||
/** | ||
* Builder for {@link H1SpecExceptions}. | ||
*/ | ||
public static final class Builder { | ||
|
||
private boolean allowPrematureClosureBeforePayloadBody; | ||
|
||
/** | ||
* Allows interpreting connection closures as the end of HTTP/1.1 messages if the receiver did not receive any | ||
* part of the payload body before the connection closure. | ||
* | ||
* @return {@code this} | ||
*/ | ||
public Builder allowPrematureClosureBeforePayloadBody() { | ||
this.allowPrematureClosureBeforePayloadBody = true; | ||
return this; | ||
} | ||
|
||
/** | ||
* Builds {@link H1SpecExceptions}. | ||
* | ||
* @return a new {@link H1SpecExceptions} | ||
*/ | ||
public H1SpecExceptions build() { | ||
return new H1SpecExceptions(allowPrematureClosureBeforePayloadBody); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.