Working on PR for "Missing HEAD method when the code moved to chi" #6793

Closed
opened 2025-11-02 07:06:43 -06:00 by GiteaMirror · 1 comment
Owner

Originally created by @faridtsl on GitHub (Jan 30, 2021).

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/data/testgitea(master*) # curl --head https://try.gitea.io/anyuser/anyrepo/raw/branch/master/anyfile.png                                                                                                                                                                                                                                                                                                                                        
HTTP/2 405
date: Sat, 30 Jan 2021 23:04:16 GMT
set-cookie: i_like_gitea=10985d2770de63e2; Path=/; HttpOnly
set-cookie: _csrf=kFz_ot6_LVT-Jsf6zgWksSWXVLU6MTYxMjA0Nzg1NjQ0NDU5MjM5MA; Path=/; Expires=Sun, 31 Jan 2021 23:04:16 GMT; HttpOnly
set-cookie: macaron_flash=; Path=/; Max-Age=0; HttpOnly
x-frame-options: SAMEORIGIN

Description

When I was trying to fix issue #8030 I had a look at the code base. Then two days later it moved from Macaron to Chi, the HEAD HTTP method no longer works automatically when GET is defined.

I already added and tested the code locally. Adding this line to WebRoutes function in 'routers/routes/web.go' fixes the issue.

r.Use(middleware.GetHead)

As for fixing issue #8030 :

This is a problem when trying to make a program download from Gitea. For example, Windows executables break when trying to download from Gitea /raw route as it does not give "Content-Length" in HEAD response.

The only function that needed to change is ServeData in 'routers/repo/download.go'. By changing the call to io.Copy to ioutil.ReadAll() + ctx.Resp.Write() . Then Writing the Content-Length explicitly by taking the length of the ioutil.ReadAll() byte array.

Questions :
1- Are the design decisions good ? should I make a PR ?
2- Should I make one or two PRs ?
3- I have no idea how to write tests these two functionalities ?

https://github.com/go-gitea/gitea/issues/8030

Originally created by @faridtsl on GitHub (Jan 30, 2021). <!-- NOTE: If your issue is a security concern, please send an email to security@gitea.io instead of opening a public issue --> <!-- 1. Please speak English, this is the language all maintainers can speak and write. 2. Please ask questions or configuration/deploy problems on our Discord server (https://discord.gg/gitea) or forum (https://discourse.gitea.io). 3. Please take a moment to check that your issue doesn't already exist. 4. Make sure it's not mentioned in the FAQ (https://docs.gitea.io/en-us/faq) 5. Please give all relevant information below for bug reports, because incomplete details will be handled as an invalid report. --> - Gitea version (or commit ref): 6064bfa3c3076254ad40f4d238d0c9c48d728e3d <!-- Please include information on whether you built gitea yourself, used one of our downloads or are using some other package --> <!-- Please also tell us how you are running gitea, e.g. if it is being run from docker, a command-line, systemd etc. ---> <!-- If you are using a package or systemd tell us what distribution you are using --> - Can you reproduce the bug at https://try.gitea.io: - [x] Yes (provide example URL) - [ ] No ``` ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- /data/testgitea(master*) # curl --head https://try.gitea.io/anyuser/anyrepo/raw/branch/master/anyfile.png HTTP/2 405 date: Sat, 30 Jan 2021 23:04:16 GMT set-cookie: i_like_gitea=10985d2770de63e2; Path=/; HttpOnly set-cookie: _csrf=kFz_ot6_LVT-Jsf6zgWksSWXVLU6MTYxMjA0Nzg1NjQ0NDU5MjM5MA; Path=/; Expires=Sun, 31 Jan 2021 23:04:16 GMT; HttpOnly set-cookie: macaron_flash=; Path=/; Max-Age=0; HttpOnly x-frame-options: SAMEORIGIN ``` <!-- It really is important to provide pertinent logs --> <!-- Please read https://docs.gitea.io/en-us/logging-configuration/#debugging-problems --> <!-- In addition, if your problem relates to git commands set `RUN_MODE=dev` at the top of app.ini --> ## Description When I was trying to fix issue #8030 I had a look at the code base. Then two days later it moved from Macaron to Chi, the HEAD HTTP method no longer works automatically when GET is defined. I already added and tested the code locally. Adding this line to WebRoutes function in 'routers/routes/web.go' fixes the issue. ```go r.Use(middleware.GetHead) ``` As for fixing issue #8030 : This is a problem when trying to make a program download from Gitea. For example, Windows executables break when trying to download from Gitea /raw route as it does not give "Content-Length" in HEAD response. The only function that needed to change is ServeData in 'routers/repo/download.go'. By changing the call to io.Copy to ioutil.ReadAll() + ctx.Resp.Write() . Then Writing the Content-Length explicitly by taking the length of the ioutil.ReadAll() byte array. Questions : 1- Are the design decisions good ? should I make a PR ? 2- Should I make one or two PRs ? 3- I have no idea how to write tests these two functionalities ? ## Related issues https://github.com/go-gitea/gitea/issues/8030
GiteaMirror added the issue/needs-feedback label 2025-11-02 07:06:43 -06:00
Author
Owner

@lunny commented on GitHub (Feb 1, 2021):

Should we really need an automatical head response?

For those file routes, I think we can add HEAD methods specially.

Please send PRs if you think it resolved something, so we can discuss there.

@lunny commented on GitHub (Feb 1, 2021): Should we really need an automatical head response? For those file routes, I think we can add `HEAD` methods specially. Please send PRs if you think it resolved something, so we can discuss there.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/gitea#6793