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

Host header always blank #2561

Closed
3 tasks done
danthegoodman1 opened this issue Dec 17, 2023 · 6 comments
Closed
3 tasks done

Host header always blank #2561

danthegoodman1 opened this issue Dec 17, 2023 · 6 comments

Comments

@danthegoodman1
Copy link

danthegoodman1 commented Dec 17, 2023

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

@aldas
Copy link
Contributor

aldas commented Dec 17, 2023

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
Copy link
Author

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
Copy link
Contributor

aldas commented Dec 17, 2023

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
Copy link

Hades32 commented Dec 18, 2023

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

@aldas
Copy link
Contributor

aldas commented Dec 18, 2023

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
Copy link
Contributor

aldas commented Dec 19, 2023

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

@aldas aldas closed this as completed Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants