Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Parse Accept-Encoding header, and set Content-Encoding correctly#305

Merged
mantzas merged 6 commits into
beatlabs:masterfrom
azarakovskiy:feature/accept-encoding-header
Dec 8, 2020
Merged

Parse Accept-Encoding header, and set Content-Encoding correctly#305
mantzas merged 6 commits into
beatlabs:masterfrom
azarakovskiy:feature/accept-encoding-header

Conversation

@azarakovskiy

Copy link
Copy Markdown

Issue #300, #301

Which problem is this PR solving?

Resolves issues #300 and #301

Short description of the changes

Accept-Encoding header is now parsed and respected in compression middleware
Content-Encoding header is not correctly set

@codecov

codecov Bot commented Dec 4, 2020

Copy link
Copy Markdown

Codecov Report

Merging #305 (b1dd88d) into master (6ad69ec) will increase coverage by 0.39%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #305      +/-   ##
==========================================
+ Coverage   72.29%   72.68%   +0.39%     
==========================================
  Files          61       61              
  Lines        3295     3332      +37     
==========================================
+ Hits         2382     2422      +40     
+ Misses        820      817       -3     
  Partials       93       93              
Impacted Files Coverage Δ
component/http/middleware.go 88.48% <100.00%> (+4.71%) ⬆️

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 6ad69ec...b1dd88d. Read the comment docs.

Comment thread component/http/middleware.go Outdated
Comment thread component/http/middleware.go Outdated

selectedEncoding, err := parseAcceptEncoding(hdr)
if err != nil {
// todo if a selectedEncoding is now known by a service, it should reply 406 Not Acceptable

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.

TODO???

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The proper decision of whether to respond 406 might be better done somewhere else. This middleware might be or might not be a good place for it. Ideally, there should be some kind of orchestrator that manages such "global" decisions for request.

This decision is definitely out of the scope of compression middleware change only. I believe it's better to have this changed incrementally and not inflate the current scope.

A practice that I am used to in order to postpone such decisions, is creating a todo and opening an issue after the change has been merged. Without a todo that you can refer to in that issue, this might get lost.

How do you recommend doing this?

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.

Why can this not be just an http status error returned here? Middlewares in Go just write directly to the response writer, that's why they have it also as an argument.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done e072242

Comment thread component/http/middleware_test.go Outdated
@azarakovskiy azarakovskiy requested a review from mantzas December 7, 2020 09:45
@azarakovskiy azarakovskiy force-pushed the feature/accept-encoding-header branch from 7b4d018 to 913f1b1 Compare December 7, 2020 14:34
Issue beatlabs#300, beatlabs#301

diff --git a/component/http/middleware.go b/component/http/middleware.go
index 70c9bff..ea4446b 100644
--- a/component/http/middleware.go
+++ b/component/http/middleware.go
@@ -322,7 +322,7 @@ func MiddlewareChain(f http.Handler, mm ...MiddlewareFunc) http.Handler {
 }

 func isCompressionHeader(h string) bool {
-	return strings.Contains(h, "gzip") || strings.Contains(h, "deflate")
+	return strings.Contains(h, gzipHeader) || strings.Contains(h, deflateHeader)
 }

 func logRequestResponse(corID string, w *responseWriter, r *http.Request) {
drakos74
drakos74 previously approved these changes Dec 7, 2020
@azarakovskiy azarakovskiy force-pushed the feature/accept-encoding-header branch from e072242 to d0ffa91 Compare December 7, 2020 16:30
mantzas
mantzas previously approved these changes Dec 7, 2020
Comment thread component/http/middleware.go Outdated
tpaschalis
tpaschalis previously approved these changes Dec 8, 2020

@tpaschalis tpaschalis left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM, thanks for your input Alexey!

@azarakovskiy azarakovskiy force-pushed the feature/accept-encoding-header branch 2 times, most recently from 08f5e2c to 08648d7 Compare December 8, 2020 11:52
@azarakovskiy azarakovskiy force-pushed the feature/accept-encoding-header branch from 08648d7 to b1dd88d Compare December 8, 2020 11:56
@mantzas mantzas merged commit bd955b1 into beatlabs:master Dec 8, 2020
@azarakovskiy azarakovskiy deleted the feature/accept-encoding-header branch December 8, 2020 12:30
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.

4 participants