Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Long to represent Content-Length #357

Merged
merged 5 commits into from
Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ default List<String> headers(CharSequence name) {
*
* @return the content-length if present
*/
default Optional<Integer> contentLength() {
return header(CONTENT_LENGTH).map(Integer::valueOf);
default Optional<Long> contentLength() {
return header(CONTENT_LENGTH).map(Long::valueOf);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import static com.hotels.styx.api.RequestCookie.decode;
import static com.hotels.styx.api.RequestCookie.encode;
import static io.netty.buffer.Unpooled.copiedBuffer;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.util.Arrays.asList;
import static java.util.Objects.requireNonNull;
import static java.util.UUID.randomUUID;
Expand Down Expand Up @@ -716,7 +716,7 @@ private boolean isMethodValid() {

private void ensureContentLengthIsValid() {
requireNotDuplicatedHeader(CONTENT_LENGTH).ifPresent(contentLength ->
checkArgument(isInteger(contentLength), "Invalid Content-Length found. %s", contentLength)
checkArgument(isNonNegativeInteger(contentLength), "Invalid Content-Length found. %s", contentLength)
);
}

Expand All @@ -728,10 +728,10 @@ private Optional<String> requireNotDuplicatedHeader(CharSequence headerName) {
return headerValues.isEmpty() ? Optional.empty() : Optional.of(headerValues.get(0));
}

private static boolean isInteger(String contentLength) {
private static boolean isNonNegativeInteger(String contentLength) {
try {
parseInt(contentLength);
return true;
long value = parseLong(contentLength);
return value >= 0;
} catch (NumberFormatException e) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import static com.hotels.styx.api.ResponseCookie.decode;
import static com.hotels.styx.api.ResponseCookie.encode;
import static io.netty.buffer.Unpooled.copiedBuffer;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.util.Arrays.asList;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
Expand Down Expand Up @@ -564,15 +564,15 @@ Builder ensureContentLengthIsValid() {
checkArgument(contentLengths.size() <= 1, "Duplicate Content-Length found. %s", contentLengths);

if (contentLengths.size() == 1) {
checkArgument(isInteger(contentLengths.get(0)), "Invalid Content-Length found. %s", contentLengths.get(0));
checkArgument(isNonNegativeInteger(contentLengths.get(0)), "Invalid Content-Length found. %s", contentLengths.get(0));
}
return this;
}

private static boolean isInteger(String contentLength) {
private static boolean isNonNegativeInteger(String contentLength) {
try {
parseInt(contentLength);
return true;
long value = parseLong(contentLength);
return value >= 0;
} catch (NumberFormatException e) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ default List<String> headers(CharSequence name) {
*
* @return the content-length if present
*/
default Optional<Integer> contentLength() {
return header(CONTENT_LENGTH).map(Integer::valueOf);
default Optional<Long> contentLength() {
return header(CONTENT_LENGTH).map(Long::valueOf);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import static com.hotels.styx.api.RequestCookie.encode;
import static io.netty.buffer.ByteBufUtil.getBytes;
import static io.netty.buffer.Unpooled.copiedBuffer;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.util.Arrays.asList;
import static java.util.Objects.requireNonNull;
import static java.util.UUID.randomUUID;
Expand Down Expand Up @@ -1003,7 +1003,7 @@ private boolean isMethodValid() {

private void ensureContentLengthIsValid() {
requireNotDuplicatedHeader(CONTENT_LENGTH).ifPresent(contentLength ->
checkArgument(isInteger(contentLength), "Invalid Content-Length found. %s", contentLength)
checkArgument(isNonNegativeInteger(contentLength), "Invalid Content-Length found. %s", contentLength)
);
}

Expand All @@ -1015,10 +1015,10 @@ private Optional<String> requireNotDuplicatedHeader(CharSequence headerName) {
return headerValues.isEmpty() ? Optional.empty() : Optional.of(headerValues.get(0));
}

private static boolean isInteger(String contentLength) {
private static boolean isNonNegativeInteger(String contentLength) {
try {
parseInt(contentLength);
return true;
long value = parseLong(contentLength);
return value >= 0;
} catch (NumberFormatException e) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import static com.hotels.styx.api.ResponseCookie.decode;
import static com.hotels.styx.api.ResponseCookie.encode;
import static io.netty.buffer.ByteBufUtil.getBytes;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.util.Arrays.asList;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
Expand Down Expand Up @@ -864,15 +864,15 @@ Builder ensureContentLengthIsValid() {
checkArgument(contentLengths.size() <= 1, "Duplicate Content-Length found. %s", contentLengths);

if (contentLengths.size() == 1) {
checkArgument(isInteger(contentLengths.get(0)), "Invalid Content-Length found. %s", contentLengths.get(0));
checkArgument(isNonNegativeInteger(contentLengths.get(0)), "Invalid Content-Length found. %s", contentLengths.get(0));
}
return this;
}

private static boolean isInteger(String contentLength) {
private static boolean isNonNegativeInteger(String contentLength) {
try {
parseInt(contentLength);
return true;
long value = parseLong(contentLength);
return value >= 0;
} catch (NumberFormatException e) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,4 +523,11 @@ public void removesCookiesInSameBuilder() {

assertThat(r1.cookie("x"), isAbsent());
}

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Invalid Content-Length found. -3")
public void ensuresContentLengthIsPositive() {
HttpRequest.post("/y")
.header("Content-Length", -3)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,11 @@ public void removesCookiesInSameBuilder() {

assertThat(r1.cookie("x"), isAbsent());
}

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Invalid Content-Length found. -3")
public void ensuresContentLengthIsPositive() {
response()
.header("Content-Length", -3)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,13 @@ public void transformsByRemovingCookieList() {
assertEquals(request.cookie("cookie"), Optional.empty());
}

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Invalid Content-Length found. -3")
public void ensuresContentLengthIsPositive() {
LiveHttpRequest.post("/y")
.header("Content-Length", -3)
.build();
}

private static ByteStream body(String... contents) {

return new ByteStream(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,13 @@ public void transformerReplacesBody() {
assertEquals(buf2.delegate().refCnt(), 0);
}

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Invalid Content-Length found. -3")
public void ensuresContentLengthIsPositive() {
response(OK)
.header("Content-Length", -3)
.build();
}

private static LiveHttpResponse.Builder response() {
return LiveHttpResponse.response();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public void respondsWithStaticBody() {
assertThat(fullResponse.bodyAs(UTF_8), is("foo"));
}

private Integer length(String string) {
return string.getBytes().length;
private Long length(String string) {
return (long) string.getBytes().length;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
*/
package com.hotels.styx.proxy.interceptors;

import com.hotels.styx.api.Eventual;
import com.hotels.styx.api.HttpInterceptor;
import com.hotels.styx.api.LiveHttpRequest;
import com.hotels.styx.api.LiveHttpResponse;
import com.hotels.styx.api.Eventual;

import java.util.Optional;

import static com.hotels.styx.api.HttpHeaderNames.CONTENT_LENGTH;
import com.hotels.styx.api.LiveHttpRequest;

/**
* Fixes bad content length headers.
Expand All @@ -34,7 +34,7 @@ public Eventual<LiveHttpResponse> intercept(LiveHttpRequest request, Chain chain
}

private static LiveHttpRequest removeBadContentLength(LiveHttpRequest request) {
Optional<Integer> contentLength = request.contentLength();
Optional<Long> contentLength = request.contentLength();
if (contentLength.isPresent() && request.chunked()) {
return request.newBuilder()
.removeHeader(CONTENT_LENGTH)
Expand Down