Skip to content

Commit

Permalink
Merge pull request #1531 from dilanSachi/header-size
Browse files Browse the repository at this point in the history
Add `maxHeaderSize` config for server side
  • Loading branch information
dilanSachi committed Feb 1, 2024
2 parents 55f05ed + c93685e commit 38cec89
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 13 deletions.
11 changes: 11 additions & 0 deletions ballerina-tests/tests/08_unary_client_with_headers.bal
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,14 @@ function testHeadersInBlockingClient() returns grpc:Error? {
map<string|string[]> resHeaders = response.headers;
test:assertEquals(check grpc:getHeaders(resHeaders, "x-id"), ["0987654321", "1234567890", "2233445677"]);
}

@test:Config {enable: true}
function testLargeHeaderSize() returns grpc:Error? {
wrappers:ContextString requestMessage = {content: "WSO2", headers: {"large-header": "09876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210987654321"}};
wrappers:ContextString|grpc:Error response = helloWorld8BlockingEp->helloContext(requestMessage);
if response is grpc:Error {
test:assertEquals(response.message(), "Header size exceeded max allowed size (600)");
} else {
test:assertFail("Expected an error");
}
}
3 changes: 2 additions & 1 deletion ballerina-tests/tests/08_unary_service_with_headers.bal
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import ballerina/protobuf.types.wrappers;

// Server endpoint configuration
listener grpc:Listener ep8 = new (9098, {
host: "localhost"
host: "localhost",
maxHeaderSize: 600
});

@grpc:Descriptor {
Expand Down
12 changes: 6 additions & 6 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "ballerina"
name = "grpc"
version = "1.10.5"
version = "1.10.6"
distribution = "2201.8.0"
authors = ["Ballerina"]
keywords = ["network", "grpc", "protobuf", "server-streaming", "client-streaming", "bidirectional-streaming"]
Expand All @@ -16,11 +16,11 @@ graalvmCompatible = true
[[platform.java17.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "grpc-native"
version = "1.10.5"
path = "../native/build/libs/grpc-native-1.10.5.jar"
version = "1.10.6"
path = "../native/build/libs/grpc-native-1.10.6-SNAPSHOT.jar"

[[platform.java17.dependency]]
path = "../test-utils/build/libs/grpc-test-utils-1.10.5.jar"
path = "../test-utils/build/libs/grpc-test-utils-1.10.6-SNAPSHOT.jar"
scope = "testOnly"

[[platform.java17.dependency]]
Expand All @@ -34,8 +34,8 @@ scope = "testOnly"
[[platform.java17.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "http-native"
version = "2.10.3"
path = "./lib/http-native-2.10.3.jar"
version = "2.10.6"
path = "./lib/http-native-2.10.6.jar"

[[platform.java17.dependency]]
groupId = "io.netty"
Expand Down
2 changes: 1 addition & 1 deletion ballerina/CompilerPlugin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ id = "grpc-compiler-plugin"
class = "io.ballerina.stdlib.grpc.plugin.GrpcCompilerPlugin"

[[dependency]]
path = "../compiler-plugin/build/libs/grpc-compiler-plugin-1.10.5.jar"
path = "../compiler-plugin/build/libs/grpc-compiler-plugin-1.10.6-SNAPSHOT.jar"
4 changes: 2 additions & 2 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "grpc"
version = "1.10.5"
version = "1.10.6"
dependencies = [
{org = "ballerina", name = "auth"},
{org = "ballerina", name = "crypto"},
Expand All @@ -94,7 +94,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http"
version = "2.10.3"
version = "2.10.6"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "auth"},
Expand Down
2 changes: 2 additions & 0 deletions ballerina/service_endpoint.bal
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,14 @@ const decimal DEFAULT_LISTENER_TIMEOUT = 120; //2 mins
# + timeout - Period of time in seconds that a connection waits for a read/write operation. Use value 0 to
# disable the timeout
# + maxInboundMessageSize - The maximum message size to be permitted for inbound messages. Default value is 4 MB
# + maxHeaderSize - The maximum size of the headers to be permitted for inbound messages. Default value is 8 KB
# + reflectionEnabled - Support reflection
public type ListenerConfiguration record {|
string host = "0.0.0.0";
ListenerSecureSocket? secureSocket = ();
decimal timeout = DEFAULT_LISTENER_TIMEOUT;
int maxInboundMessageSize = 4194304;
int maxHeaderSize = 8192;
boolean reflectionEnabled = false;
|};

Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This file contains all the notable changes done to the Ballerina gRPC package th
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Added
- [Added `maxHeaderSize` in `grpc:ListenerConfiguration`](https://github.com/ballerina-platform/ballerina-library/issues/5969)

## [1.10.5] - 2024-01-22
### Fixed
- [Fixed the way of handling `grpc:RetryConfiguration`](https://github.com/ballerina-platform/ballerina-library/issues/5970)
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ stdlibAuthVersion=2.10.0
stdlibJwtVersion=2.10.0
stdlibOAuth2Version=2.10.0

stdlibHttpVersion=2.10.6-20240130-162500-8913531
stdlibHttpVersion=2.10.6

# Ballerinax Observer
observeVersion=1.2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static io.ballerina.stdlib.grpc.GrpcConstants.GRPC_MESSAGE_KEY;
import static io.ballerina.stdlib.grpc.GrpcConstants.GRPC_STATUS_KEY;
import static io.ballerina.stdlib.grpc.GrpcConstants.MESSAGE_ENCODING;
import static io.ballerina.stdlib.grpc.MessageUtils.httpStatusToGrpcCode;
import static io.ballerina.stdlib.grpc.MessageUtils.readAsString;

/**
Expand Down Expand Up @@ -161,9 +162,13 @@ private boolean isValid(InboundMessage inboundMessage) {
// Ignore the headers. See RFC 7540 §8.1
// 1xx (Informational): The request was received, continuing process
return false;
} else if (httpStatus == 431) {
transportError = httpStatusToGrpcCode(httpStatus).toStatus()
.withDescription("Exceeded the maximum header size allowed");
} else {
transportError = validateInitialMetadata(inboundMessage);
}
headersReceived = true;
transportError = validateInitialMetadata(inboundMessage);
return transportError == null;
} finally {
if (transportError != null) {
Expand Down
4 changes: 4 additions & 0 deletions native/src/main/java/io/ballerina/stdlib/grpc/GrpcUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import static io.ballerina.stdlib.http.api.HttpConstants.CONNECTION_MANAGER;
import static io.ballerina.stdlib.http.api.HttpConstants.CONNECTION_POOLING_MAX_ACTIVE_STREAMS_PER_CONNECTION;
import static io.ballerina.stdlib.http.api.HttpConstants.LISTENER_CONFIGURATION;
import static io.ballerina.stdlib.http.api.HttpConstants.MAX_HEADER_SIZE;
import static io.ballerina.stdlib.http.api.HttpConstants.PKCS_STORE_TYPE;
import static io.ballerina.stdlib.http.api.HttpConstants.PROTOCOL_HTTPS;
import static io.ballerina.stdlib.http.api.HttpConstants.SERVER_NAME;
Expand Down Expand Up @@ -233,6 +234,9 @@ public static ListenerConfiguration getListenerConfig(long port, BMap endpointCo
return setSslConfig(sslConfig, listenerConfiguration);
}

listenerConfiguration.getMsgSizeValidationConfig().setMaxHeaderSize(endpointConfig.getIntValue(MAX_HEADER_SIZE)
.intValue());

listenerConfiguration.setPipeliningEnabled(true); //Pipelining is enabled all the time
listenerConfiguration.setSocketReuse(true);
listenerConfiguration.setTcpNoDelay(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ static Status httpStatusToGrpcStatus(int httpStatusCode) {
.withDescription("HTTP status code " + httpStatusCode);
}

private static Status.Code httpStatusToGrpcCode(int httpStatusCode) {
static Status.Code httpStatusToGrpcCode(int httpStatusCode) {
if (httpStatusCode >= 100 && httpStatusCode < 200) {
// 1xx. These headers should have been ignored.
return Status.Code.INTERNAL;
Expand Down

0 comments on commit 38cec89

Please sign in to comment.