Skip to content

Commit

Permalink
Merge pull request #2085 from ballerina-platform/cache-fix
Browse files Browse the repository at this point in the history
Fix NumberFormatException when specifying cache configurations as decimal values
  • Loading branch information
TharmiganK authored Jul 24, 2024
2 parents 5620118 + ca1ca4f commit d483e9a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
12 changes: 6 additions & 6 deletions ballerina-tests/http-advanced-tests/tests/http2_caching_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ service /cachingBackend on http2CachingListener2 { //new http:Listener(9240) {
isolated resource function 'default .(http:Caller caller, http:Request req) returns error? {
http:Response res = new;
http:ResponseCacheControl resCC = new;
resCC.maxAge = 60;
resCC.maxAge = 60.54;
resCC.isPrivate = false;

res.cacheControl = resCC;
Expand Down Expand Up @@ -158,28 +158,28 @@ function testHttp2BasicCachingBehaviour() returns error? {
@test:Config {}
function testHttp2RequestCacheControlBuildCacheControlDirectives() {
http:RequestCacheControl reqCC = new;
reqCC.maxAge = 60;
reqCC.maxAge = 60.34;
reqCC.noCache = true;
reqCC.noStore = true;
reqCC.noTransform = true;
reqCC.onlyIfCached = true;
reqCC.maxStale = 120;
reqCC.minFresh = 6;
reqCC.maxStale = 120.04;
reqCC.minFresh = 6.0;
test:assertEquals(reqCC.buildCacheControlDirectives(),
"no-cache, no-store, no-transform, only-if-cached, max-age=60, max-stale=120, min-fresh=6");
}

@test:Config {}
function testHttp2ResponseCacheControlBuildCacheControlDirectives() {
http:ResponseCacheControl resCC = new;
resCC.maxAge = 60;
resCC.maxAge = 60.54;
resCC.isPrivate = false;
resCC.mustRevalidate = true;
resCC.noCache = true;
resCC.noStore = true;
resCC.noTransform = true;
resCC.proxyRevalidate = true;
resCC.sMaxAge = 60;
resCC.sMaxAge = 60.32;
test:assertEquals(resCC.buildCacheControlDirectives(),
"must-revalidate, no-cache, no-store, no-transform, public, proxy-revalidate, max-age=60, s-maxage=60");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ service / on new http:Listener(cacheAnnotationTestPort2, httpVersion = http:HTTP
}
}

resource function default maxAgeBE(http:Request req) returns @http:Cache {maxAge: 5, mustRevalidate: false} xml {
resource function default maxAgeBE(http:Request req) returns @http:Cache {maxAge: 5.32, mustRevalidate: false} xml {
int count = 0;
lock {
maxAgeHitCountNew += 1;
Expand Down
6 changes: 3 additions & 3 deletions ballerina/caching_request_cache_control.bal
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,20 @@ public class RequestCacheControl {
}

if self.maxAge >= 0d {
directives[i] = MAX_AGE + "=" + self.maxAge.toString();
directives[i] = string `${MAX_AGE}=${decimal:floor(self.maxAge)}`;
i += 1;
}

if self.maxStale == MAX_STALE_ANY_AGE {
directives[i] = MAX_STALE;
i += 1;
} else if self.maxStale >= 0d {
directives[i] = MAX_STALE + "=" + self.maxStale.toString();
directives[i] = string `${MAX_STALE}=${decimal:floor(self.maxStale)}`;
i += 1;
}

if self.minFresh >= 0d {
directives[i] = MIN_FRESH + "=" + self.minFresh.toString();
directives[i] = string `${MIN_FRESH}=${decimal:floor(self.minFresh)}`;
i += 1;
}

Expand Down
8 changes: 4 additions & 4 deletions ballerina/caching_response_cache_control.bal
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public class ResponseCacheControl {
self.noTransform = cacheConfig.noTransform;
self.isPrivate = cacheConfig.isPrivate;
self.proxyRevalidate = cacheConfig.proxyRevalidate;
self.maxAge = cacheConfig.maxAge;
self.sMaxAge = cacheConfig.sMaxAge;
self.maxAge = decimal:floor(cacheConfig.maxAge);
self.sMaxAge = decimal:floor(cacheConfig.sMaxAge);
self.noCacheFields = cacheConfig.noCacheFields;
self.privateFields = cacheConfig.privateFields;
}
Expand Down Expand Up @@ -94,12 +94,12 @@ public class ResponseCacheControl {
}

if self.maxAge >= 0d {
directives[i] = MAX_AGE + "=" + self.maxAge.toString();
directives[i] = string `${MAX_AGE}=${decimal:floor(self.maxAge)}`;
i += 1;
}

if self.sMaxAge >= 0d {
directives[i] = S_MAX_AGE + "=" + self.sMaxAge.toString();
directives[i] = string `${S_MAX_AGE}=${decimal:floor(self.sMaxAge)}`;
i += 1;
}

Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- [Add connection eviction feature to handle connections that receive GO_AWAY from the client](https://github.com/ballerina-platform/ballerina-library/issues/6734)
- [Enhanced the configurability of Ballerina access logging by introducing multiple configuration options.](https://github.com/ballerina-platform/ballerina-library/issues/6111)

### Fixed

- [Fix number format exception with decimal values for cache configuration](https://github.com/ballerina-platform/ballerina-library/issues/6765)

## [2.11.2] - 2024-06-14

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ private boolean getBooleanValue(BObject responseCacheControl, BString fieldName)
}

private long getIntValue(BObject responseCacheControl, BString fieldName) {
return Long.parseLong(responseCacheControl.get(fieldName).toString());
try {
String value = responseCacheControl.get(fieldName).toString();
// The value is decimal, and need to compute the floor value
return Long.parseLong(value.split("\\.")[0]);
} catch (NumberFormatException e) {
// Ignore the exception and set 0 as the value
return 0;
}
}
}

0 comments on commit d483e9a

Please sign in to comment.