gin icon indicating copy to clipboard operation
gin copied to clipboard

Automatic HEAD handling

Open rickb777 opened this issue 5 years ago • 3 comments

HTTP HEAD requests are intended to be identical to the corresponding GET requests, except that there is no response body (https://tools.ietf.org/html/rfc7231#section-4.3.2). It is a considerable convenience to Gin users if this behavior is provided automatically, so that's the purpose of this PR.

  • Whenever a GET handler is registered, the equivalent HEAD handler will exist automatically.
  • Extra HEAD handlers can be added but will rarely be needed.

This PR

  • refactored Engine.handleHTTPRequest so that two passes are made if the request is HEAD and there isn't a matching HEAD handler - this is the important part of the PR
  • suppresses rendering for HEAD request in Context.Render, which means that wasteful processing is avoided
  • re-ordered GET and HEAD methods to be adjacent
  • added test cases for the new code
  • added more documentation

The standard Go api net/http also has automatic behavior for HEAD requests such that the response body is discarded. This PR doesn't rely on this (although it could), because rendering the unwanted HEAD response can be avoided.

rickb777 avatar Nov 07 '20 10:11 rickb777

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 98.48%. Comparing base (65ed60e) to head (bfbe3c3). :warning: Report is 499 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2550   +/-   ##
=======================================
  Coverage   98.48%   98.48%           
=======================================
  Files          41       41           
  Lines        1974     1975    +1     
=======================================
+ Hits         1944     1945    +1     
  Misses         17       17           
  Partials       13       13           

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar Nov 07 '20 10:11 codecov[bot]

anyone know why this was never merged?

dickeyy avatar Dec 22 '24 16:12 dickeyy

Please resolve conflicting files first.

appleboy avatar Dec 28 '24 12:12 appleboy