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

net/http: ResponseWriter render an avif image always set the content-type to "application/octet-stream" instead of the right one "image/avif" #53873

Closed
yourchanges opened this issue Jul 14, 2022 · 4 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@yourchanges
Copy link

What version of Go are you using (go version)?

$ go version
go 1.17.x 
go 1.18.x
linux or mac

What did you do?

here is the test code:

package main

import (
	"bytes"
	"net/http"
	"os"
	"strconv"
	"time"
)

func work(w http.ResponseWriter, req *http.Request) {
	imgBytes, _ := os.ReadFile("test.avif")

	http.ServeContent(w, req, "test.avif", time.Now(), bytes.NewReader(imgBytes))
	return
}

//not work with image/avif, but works with image/png image/jpg image/webp
func notwork(w http.ResponseWriter, req *http.Request) {
	imgBytes, _ := os.ReadFile("test.avif")

	w.WriteHeader(http.StatusOK)
	w.Header().Set("Content-Type", "image/avif")
	w.Header().Set("Content-Length", strconv.Itoa(len(imgBytes)))
	w.Header().Set("Content-Disposition", `inline;filename="test.avif"`)
	w.Write(imgBytes)
	return
}

func main() {
	http.HandleFunc("/work", work)
	http.HandleFunc("/notwork", notwork)

	http.ListenAndServe(":8090", nil)
}

here is the test result output:

(base) leedeMacBook-Pro:~ lee$ curl -v http://localhost:8090/work
*   Trying ::1:8090...
* TCP_NODELAY set
* Connected to localhost (::1) port 8090 (#0)
> GET /work HTTP/1.1
> Host: localhost:8090
> User-Agent: curl/7.65.3
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Content-Length: 72291
< Content-Type: image/avif
< Last-Modified: Thu, 14 Jul 2022 13:58:34 GMT
< Date: Thu, 14 Jul 2022 13:58:34 GMT
< 



(base) leedeMacBook-Pro:~ lee$ curl -v http://localhost:8090/notwork
*   Trying ::1:8090...
* TCP_NODELAY set
* Connected to localhost (::1) port 8090 (#0)
> GET /notwork HTTP/1.1
> Host: localhost:8090
> User-Agent: curl/7.65.3
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Thu, 14 Jul 2022 13:58:44 GMT
< Content-Type: application/octet-stream
< Transfer-Encoding: chunked
< 

What did you expect to see?

both "work" and "notwork" methods give the client browser content-type: "image/avif"

What did you see instead?

"notwork" method give the client browser content-type: "application/octet-stream"

@renthraysk
Copy link

notwork calls w.WriteHeader() too soon. Should happen after setting the headers and before the w.Write().

@mknyszek mknyszek changed the title http.ResponseWriter render an avif image always set the content-type to "application/octet-stream" instead of the right one "image/avif" net/http: ResponseWriter render an avif image always set the content-type to "application/octet-stream" instead of the right one "image/avif" Jul 14, 2022
@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 14, 2022
@mknyszek mknyszek added this to the Backlog milestone Jul 14, 2022
@mknyszek
Copy link
Contributor

CC @neild

@seankhliao
Copy link
Member

working as intended. headers must be set before WriteHeader

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jul 14, 2022
@yourchanges
Copy link
Author

Thanks, but the method name WriteHeader and Header.Set() misleading

@golang golang locked and limited conversation to collaborators Jul 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants