labstack / echo

High performance, minimalist Go web framework
https://echo.labstack.com
MIT License
29.62k stars 2.22k forks source link

`Host` header always blank #2561

Closed danthegoodman1 closed 9 months ago

danthegoodman1 commented 9 months ago

Issue Description

ctx.Request().Header.Get("host") is always a blank string (at least testing against my local server). I see

Checklist

Expected behaviour

It's the value of the Host header

Actual behaviour

$ curl -vvv localhost:8080                                                                 2:30PM
*   Trying [::1]:8080...
* Connected to localhost (::1) port 8080
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.4.0-DEV
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/plain; charset=UTF-8
< Date: Sun, 17 Dec 2023 19:30:31 GMT
< Content-Length: 15
<

* Connection #0 to host localhost left intact
localhost:8080

You can see that curl is setting the host header properly.

$ curl localhost:8080

localhost:8080

note the newline before indicating it's a blank string

Steps to reproduce

curl localhost:8080

Working code to debug

package main

import (
    "fmt"
    "github.com/labstack/echo/v4"
    "net/http"
)

func main() {
    // Echo instance
    e := echo.New()

    // Routes
    e.GET("/", hello)

    // Start server
    e.Logger.Fatal(e.Start(":8080"))
}

// Handler
func hello(c echo.Context) error {
    return c.String(http.StatusOK, fmt.Sprintf("%s\n%s", c.Request().Header.Get("host"), c.Request().Host))
}

Version/commit

github.com/labstack/echo/v4 v4.9.1

aldas commented 9 months ago

c.Request().Header.Get("Host") is not something that Echo touches. I assume that Go standard library HTTP server does not set Host into header as that same thing is in Request.Host field

danthegoodman1 commented 9 months ago

I guess we could add middleware to actually set it back, but it seems incorrect (whether at the go level or echo) that it wouldn’t be set properly?

aldas commented 9 months ago

If you search Golang github you probably find answer there. I doubt you are first person to ask the same question. Also Request.Host field has comments. Probably in some situations you can do request without host header (probably HTTP/1.0) and it is deduced from the request url.

Hades32 commented 9 months ago

https://github.com/golang/go/issues/14239 just "won't fix" weird...

aldas commented 9 months ago

probably some optimization etc. As Request.Host is always filled there is no point to populate Request.Headers map with that value and use additional memory. Also if it is not guaranteed to exists as HTTP/1.0 does not require that header.

aldas commented 9 months ago

I am closing this issue as I believe there nothing to fix/change from Echo side.