Skip to content

Fix issue with duplicate parsing of Cache-Control header#424

Closed
arturobernalg wants to merge 1 commit intoapache:5.3.xfrom
arturobernalg:feature/redundant-cache-control-validations
Closed

Fix issue with duplicate parsing of Cache-Control header#424
arturobernalg wants to merge 1 commit intoapache:5.3.xfrom
arturobernalg:feature/redundant-cache-control-validations

Conversation

@arturobernalg
Copy link
Copy Markdown
Member

This PR fixes a bug where the same Cache-Control f3f07a3 header is parsed twice, leading to incorrect performance issues. The parsing code has been extracted from the calculateFreshnessLifetime method and is now enhanced to include the main cache control directive that the isExplicitlyNonCacheable method uses to make its decision. This makes the decision based on the main cache control directive instead of parsing the header twice.

Additionally, this PR adds a new method parseCacheControlHeader to the CacheControlParser class which parses the Cache-Control header and returns a CacheControl instance. This new method improves the parsing of the Cache-Control header and provides more accurate parsing of the different directives.

This PR also includes some code cleanup and refactoring.

@arturobernalg arturobernalg force-pushed the feature/redundant-cache-control-validations branch from 4ad2ad5 to 3a4c595 Compare March 13, 2023 22:19
Copy link
Copy Markdown
Member

@ok2c ok2c left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arturobernalg Two directives: no-cache and private should not be represented as a boolean as they may have a list of field names as a parameter. For now, if we do not use those parameters fro caching policies we can keep it like that.

* The set of characters that can delimit a value in the header.
*/
private static final BitSet VALUE_DELIMS = Tokenizer.INIT_BITSET(EQUAL_CHAR, ',');
private static final BitSet VALUE_DELIMS = Tokenizer.INIT_BITSET(EQUAL_CHAR, ',', SEMICOLON_CHAR);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arturobernalg Why? Why did you put it back? Where is it being used?


while (!cursor.atEnd()) {
final String name = tokenParser.parseToken(buffer, cursor, TOKEN_DELIMS);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arturobernalg Oh, man. This looks ugly. There should be a better way of doing the same.

Copy link
Copy Markdown
Member Author

@arturobernalg arturobernalg Mar 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ok2c
Perhaps, however, I am open to any help or advice as it has been challenging to fit all the pieces together. Nevertheless, I am determined to find the optimal solution.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arturobernalg Oh, man. This looks ugly. There should be a better way of doing the same.

@ok2c what about now?

@arturobernalg arturobernalg requested a review from ok2c March 14, 2023 21:56
Previously, the same Cache-Control header was being parsed twice, once by isExplicitlyNonCacheable and again by calculateFreshnessLifetime. The parsing code was extracted from calculateFreshnessLifetime and enhanced to include the main cache control directive that isExplicitlyNonCacheable could use to make its decision. This improves the efficiency and accuracy of the caching logic.
@arturobernalg arturobernalg force-pushed the feature/redundant-cache-control-validations branch from d4995b6 to 10191fa Compare March 15, 2023 05:42
@ok2c
Copy link
Copy Markdown
Member

ok2c commented Mar 15, 2023

Committed as 8ba0b17 with some changes to the parsing code.

@arturobernalg The trick was to reverse the parsing flow and make it simple and linear. Fewer lines of code, fewer intermediate garbage as a result.
I committed by code under your name as going back and forth with each and every line of change would have taken too long. Please take a look at my changes.

And why on earth did you choose to use deprecated DateUtils methods in your tests?

@ok2c ok2c closed this Mar 15, 2023
@arturobernalg
Copy link
Copy Markdown
Member Author

8ba0b17

Hi @ok2c
The change look good.
now I'm lost here -->"And why on earth did you choose to use deprecated DateUtils methods in your tests?"

@ok2c
Copy link
Copy Markdown
Member

ok2c commented Mar 15, 2023

@arturobernalg

[INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ httpclient5-cache ---
[INFO] Toolchain in maven-compiler-plugin: JDK[/opt/java-1.8]
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 44 source files to /home/oleg/src/apache.org/httpcomponents/httpclient/httpclient5-cache/target/test-classes
[WARNING] /home/oleg/src/apache.org/httpcomponents/httpclient/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestResponseCachingPolicy.java:[920,54] [deprecation] formatDate(Date,String) in DateUtils has been deprecated
[WARNING] /home/oleg/src/apache.org/httpcomponents/httpclient/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestResponseCachingPolicy.java:[921,57] [deprecation] formatDate(Date,String) in DateUtils has been deprecated
[WARNING] /home/oleg/src/apache.org/httpcomponents/httpclient/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestResponseCachingPolicy.java:[942,54] [deprecation] formatDate(Date,String) in DateUtils has been deprecated
[WARNING] /home/oleg/src/apache.org/httpcomponents/httpclient/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestResponseCachingPolicy.java:[943,57] [deprecation] formatDate(Date,String) in DateUtils has been deprecated
[WARNING] /home/oleg/src/apache.org/httpcomponents/httpclient/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestResponseCachingPolicy.java:[964,54] [deprecation] formatDate(Date,String) in DateUtils has been deprecated
[WARNING] /home/oleg/src/apache.org/httpcomponents/httpclient/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestResponseCachingPolicy.java:[965,57] [deprecation] formatDate(Date,String) in DateUtils has been deprecated
[WARNING] /home/oleg/src/apache.org/httpcomponents/httpclient/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestResponseCachingPolicy.java:[986,54] [deprecation] formatDate(Date,String) in DateUtils has been deprecated
[WARNING] /home/oleg/src/apache.org/httpcomponents/httpclient/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestResponseCachingPolicy.java:[987,57] [deprecation] formatDate(Date,String) in DateUtils has been deprecated

@arturobernalg
Copy link
Copy Markdown
Member Author

@arturobernalg

[INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ httpclient5-cache ---
[INFO] Toolchain in maven-compiler-plugin: JDK[/opt/java-1.8]
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 44 source files to /home/oleg/src/apache.org/httpcomponents/httpclient/httpclient5-cache/target/test-classes
[WARNING] /home/oleg/src/apache.org/httpcomponents/httpclient/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestResponseCachingPolicy.java:[920,54] [deprecation] formatDate(Date,String) in DateUtils has been deprecated
[WARNING] /home/oleg/src/apache.org/httpcomponents/httpclient/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestResponseCachingPolicy.java:[921,57] [deprecation] formatDate(Date,String) in DateUtils has been deprecated
[WARNING] /home/oleg/src/apache.org/httpcomponents/httpclient/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestResponseCachingPolicy.java:[942,54] [deprecation] formatDate(Date,String) in DateUtils has been deprecated
[WARNING] /home/oleg/src/apache.org/httpcomponents/httpclient/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestResponseCachingPolicy.java:[943,57] [deprecation] formatDate(Date,String) in DateUtils has been deprecated
[WARNING] /home/oleg/src/apache.org/httpcomponents/httpclient/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestResponseCachingPolicy.java:[964,54] [deprecation] formatDate(Date,String) in DateUtils has been deprecated
[WARNING] /home/oleg/src/apache.org/httpcomponents/httpclient/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestResponseCachingPolicy.java:[965,57] [deprecation] formatDate(Date,String) in DateUtils has been deprecated
[WARNING] /home/oleg/src/apache.org/httpcomponents/httpclient/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestResponseCachingPolicy.java:[986,54] [deprecation] formatDate(Date,String) in DateUtils has been deprecated
[WARNING] /home/oleg/src/apache.org/httpcomponents/httpclient/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestResponseCachingPolicy.java:[987,57] [deprecation] formatDate(Date,String) in DateUtils has been deprecated

damn.
you right. I'll tackle that later.

TY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants