labstack / echo

High performance, minimalist Go web framework

Home Page:https://echo.labstack.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`Host` header always blank

danthegoodman1 opened this issue · comments

Issue Description

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

Checklist

  • Dependencies installed
  • No typos
  • Searched existing issues and docs

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

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

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?

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.

golang/go#14239 just "won't fix" weird...

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.

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