Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Always add Content-Length header #648

Merged
merged 2 commits into from
Jan 31, 2019

Conversation

andreamlin
Copy link
Contributor

Fixes googleapis/google-cloud-java#3735 and prevents 411 HTTP error codes.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Jan 29, 2019
@andreamlin
Copy link
Contributor Author

PTAL

@@ -143,6 +143,9 @@ public String getFieldValue(String s) {
HttpRequest httpRequest = httpRequestRunnable.createHttpRequest();
String expectedUrl = ENDPOINT + "name/tree_frog" + "?requestId=request57";
Truth.assertThat(httpRequest.getUrl().toString()).isEqualTo(expectedUrl);
Truth.assertThat(httpRequest.getHeaders().getContentLength())
.isEqualTo(httpRequest.getContent().getLength());
Truth.assertThat(httpRequest.getHeaders().getContentLength()).isGreaterThan((long) 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use 0L literal instead of (long) 0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed this .

@@ -110,10 +112,32 @@ HttpRequest createHttpRequest() throws IOException {
for (HttpJsonHeaderEnhancer enhancer : getHeaderEnhancers()) {
enhancer.enhance(httpRequest.getHeaders());
}

// Set Content-Length header to avoid 411s.
HttpJsonHeaderEnhancer contentLengthHeader = getContentLengthHeader(jsonHttpContent);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a little bit odd place for this. Few line earlier (L113) we already call the actual enhancers, but then, right after that we create one more on the fly just to call it once. Ideally we would want to have this enhancer as a default one, created in InstantiatingHttpJsonChannelProvider#createChannel() method (together with the other, static enhancers). The difference between this enhancer and the other ones (returned by getHeaderEnhancers() is that the other ones are static (do not depend on the actual body of the request) and this one is dynamic (does depend on the body, specifically on the size of the body).

I would suggest changing the HttpJsonHeaderEnhancer interface (it is @BetaApi, so should be ok) to accept the actual HttpRequest instead of HttpHeaders object.

Then create two enhancer implementations, one, which will be doing static headers (i.e. one single enhancer for all static headers) and one for dynamic headers, and instantiate both of those in InstantiatingHttpJsonChannelProvider#createChannel().

Then here, just call the enhancers normally in the loop (like on line 112), but pass the httpRequest as an argument (instead of httpRequest.getHeaders() as it is done now).

With the above suggestion in place, the HeaderEnhancers get their fair chunk of responsibility: setting headers (bot static and dynamic). Now they seem like be just doing a simple call to HttpHeadersUtils.setHeader() which is too little to justify existence of the small "family" of classes (the Enhancers class hierarchy).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed all of this.

@codecov
Copy link

codecov bot commented Jan 30, 2019

Codecov Report

Merging #648 into master will increase coverage by 0.05%.
The diff coverage is 70%.

Impacted file tree graph

@@             Coverage Diff             @@
##             master    #648      +/-   ##
===========================================
+ Coverage     75.45%   75.5%   +0.05%     
- Complexity     1000    1003       +3     
===========================================
  Files           185     185              
  Lines          4322    4332      +10     
  Branches        343     345       +2     
===========================================
+ Hits           3261    3271      +10     
+ Misses          906     905       -1     
- Partials        155     156       +1
Impacted Files Coverage Δ Complexity Δ
...m/google/api/gax/httpjson/HttpRequestRunnable.java 56.89% <70%> (+2.72%) 7 <2> (+2) ⬆️
...ogle/api/gax/httpjson/HttpJsonHeaderEnhancers.java 100% <0%> (+100%) 1% <0%> (+1%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b8923fa...c10d38b. Read the comment docs.

@codecov-io
Copy link

Codecov Report

Merging #648 into master will increase coverage by 0.05%.
The diff coverage is 70%.

Impacted file tree graph

@@             Coverage Diff             @@
##             master    #648      +/-   ##
===========================================
+ Coverage     75.45%   75.5%   +0.05%     
- Complexity     1000    1003       +3     
===========================================
  Files           185     185              
  Lines          4322    4332      +10     
  Branches        343     345       +2     
===========================================
+ Hits           3261    3271      +10     
+ Misses          906     905       -1     
- Partials        155     156       +1
Impacted Files Coverage Δ Complexity Δ
...m/google/api/gax/httpjson/HttpRequestRunnable.java 56.89% <70%> (+2.72%) 7 <2> (+2) ⬆️
...ogle/api/gax/httpjson/HttpJsonHeaderEnhancers.java 100% <0%> (+100%) 1% <0%> (+1%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b8923fa...257ba13. Read the comment docs.

1 similar comment
@codecov-io
Copy link

codecov-io commented Jan 30, 2019

Codecov Report

Merging #648 into master will increase coverage by 0.05%.
The diff coverage is 70%.

Impacted file tree graph

@@             Coverage Diff             @@
##             master    #648      +/-   ##
===========================================
+ Coverage     75.45%   75.5%   +0.05%     
- Complexity     1000    1003       +3     
===========================================
  Files           185     185              
  Lines          4322    4332      +10     
  Branches        343     345       +2     
===========================================
+ Hits           3261    3271      +10     
+ Misses          906     905       -1     
- Partials        155     156       +1
Impacted Files Coverage Δ Complexity Δ
...m/google/api/gax/httpjson/HttpRequestRunnable.java 56.89% <70%> (+2.72%) 7 <2> (+2) ⬆️
...ogle/api/gax/httpjson/HttpJsonHeaderEnhancers.java 100% <0%> (+100%) 1% <0%> (+1%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b8923fa...257ba13. Read the comment docs.

@andreamlin
Copy link
Contributor Author

@vam-google Hey Vadym, the previous rendition of the PR didn't actually work against my handwritten google-cloud-compute calls. I've scrapped all of that and made a simpler PR that does work against the repro steps listed in the google-cloud-java issue.

PTAL

Copy link
Contributor

@vam-google vam-google left a comment

Choose a reason for hiding this comment

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

LGTM, under assumption that it actually works =).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants