Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

head method in health endpoint #688

Merged
merged 2 commits into from
Sep 22, 2024

Conversation

moinologics
Copy link
Contributor

@moinologics moinologics commented Sep 18, 2024

Fixes #687

Copy link
Member

@eternal-flame-AD eternal-flame-AD left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. It is weird some company would do a HEAD preflight for a health check service, but good detail to include.

@@ -104,7 +104,7 @@ func Create(db *database.GormDatabase, vInfo *model.VersionInfo, conf *config.Co

ui.Register(g, *vInfo, conf.Registration)

g.GET("/health", healthHandler.Health)
g.Match([]string{"GET", "HEAD"}, "/health", healthHandler.Health)
Copy link
Member

Choose a reason for hiding this comment

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

This is not compliant with HTTP specs. Please refer to the MDN docs and not return a body. Thanks!

https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD

Copy link
Member

Choose a reason for hiding this comment

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

@eternal-flame-AD I would merge this as is. The RFC states:

The server SHOULD send the same header fields in response to a HEAD request as it would have sent if the request method had been GET. However, a server MAY omit header fields for which a value is determined only while generating the content.

I'd see it as recommendation to include content-length, but you can/MAY optionally omit it in some circumstances. So the current implementation complies with the recommendation.

I tested this with socat, seems like gin internally strips the body if the verb was HEAD

I think it's actually the native go http server, and it makes sense, as you need the body to get the content-length and given HEAD requests must not have a body I'd see this as working as intended and not as error-catching feature.

Copy link
Member

Choose a reason for hiding this comment

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

It's not clearly stated that how acceptable possible inconsistencies are but I agree it seems to imply it's not super important, and in the remote possibility they are doing it in C we still have an always correct length in the actual GET.
Hyper/axum seem to do it too it seems. I just think the semantics looks like GET and HEAD would produce similar results because you just matched string-wise on the method and I'm not sure it's well-accepted for an http library to internally handle this for you.

@eternal-flame-AD
Copy link
Member

eternal-flame-AD commented Sep 20, 2024

@moinologics I tested this with socat, seems like gin internally strips the body if the verb was HEAD, so this code produces a compliant behavior (except dynamically generated JSON should not have a content-length on HEAD). However I still think this is not ideal, we shouldn't rely on undocumented error-catching features and write code with a looks-wrong semantics.

Feel free to @ jmattheis for a second opinion :) Everybody has different styles and preferences

@eternal-flame-AD
Copy link
Member

eternal-flame-AD commented Sep 20, 2024

Here's a solution I think is robust, (maybe even a little overkill?) , but at the very least I think connection, content type and content-encoding must be the same, and content length must not be present.

b43e7cd

Copy link

codecov bot commented Sep 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.46%. Comparing base (a58b0be) to head (2e0210d).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #688   +/-   ##
=======================================
  Coverage   80.46%   80.46%           
=======================================
  Files          56       56           
  Lines        2191     2191           
=======================================
  Hits         1763     1763           
  Misses        337      337           
  Partials       91       91           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@eternal-flame-AD eternal-flame-AD left a comment

Choose a reason for hiding this comment

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

Thanks.

Cc @moinologics

@@ -104,7 +104,7 @@ func Create(db *database.GormDatabase, vInfo *model.VersionInfo, conf *config.Co

ui.Register(g, *vInfo, conf.Registration)

g.GET("/health", healthHandler.Health)
g.Match([]string{"GET", "HEAD"}, "/health", healthHandler.Health)
Copy link
Member

Choose a reason for hiding this comment

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

It's not clearly stated that how acceptable possible inconsistencies are but I agree it seems to imply it's not super important, and in the remote possibility they are doing it in C we still have an always correct length in the actual GET.
Hyper/axum seem to do it too it seems. I just think the semantics looks like GET and HEAD would produce similar results because you just matched string-wise on the method and I'm not sure it's well-accepted for an http library to internally handle this for you.

@eternal-flame-AD eternal-flame-AD merged commit 58084c8 into gotify:master Sep 22, 2024
3 checks passed
eternal-flame-AD added a commit that referenced this pull request Oct 16, 2024
Co-authored-by: 饺子w (Yumechi) <yume@yumechi.jp>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

HEAD method on /health endpoint throw 404 error
3 participants