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

Feat/proxy preserve host params or not #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ Nova Proxy needs a configuration file:
{
"path": "/",
"host": "http://blog:8000",
"modifyResponse": true
"modifyResponse": true,
"proxyPreserveHost": true
}
]
}
Expand All @@ -30,6 +31,8 @@ The `locations` items require the `path` and `host` to let know to Nova Proxy wh

The `modifyResponse` enable the serve-side includes to that location.

The `proxyPreserveHost` is used to preserve and retain the original Host: header from the client browser when constructing the proxied request to send to the target server.

## Using Nova Proxy with [Ara CLI](https://github.com/ara-framework/ara-cli)

Before to run the command we need to set the `HYPERNOVA_BATCH` variable using the Nova service endpoint.
Expand Down
16 changes: 10 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import (
)

type location struct {
Path string
Host string
ModifyResponse bool
Path string
Host string
ModifyResponse bool
ProxyPreserveHost bool
}

type configuration struct {
Expand All @@ -40,7 +41,7 @@ func ReadConfigFile() {
logger.Error(err, "Config file not found")

err = json.Unmarshal(e, &jsonConfig)
logger.Fatal(err, "Unable to parse " + os.Getenv("CONFIG_FILE"))
logger.Fatal(err, "Unable to parse "+os.Getenv("CONFIG_FILE"))

}

Expand All @@ -53,7 +54,7 @@ func SetUpLocations() error {
proxy := httputil.NewSingleHostReverseProxy(origin)
if location.ModifyResponse {
proxy.ModifyResponse = modifyResponse
proxy.Director = modifyRequest(origin)
proxy.Director = modifyRequest(origin, location.ProxyPreserveHost)
}
http.Handle(location.Path, proxy)
}
Expand Down Expand Up @@ -84,12 +85,15 @@ func modifyResponse(r *http.Response) error {
return nil
}

func modifyRequest(origin *url.URL) func(req *http.Request) {
func modifyRequest(origin *url.URL, proxyPreserveHost bool) func(req *http.Request) {
return func(req *http.Request) {
req.Header.Add("X-Forwarded-Host", req.Host)
req.Header.Add("X-Origin-Host", origin.Host)
req.Header.Del("Accept-Encoding")
req.URL.Scheme = "http"
req.URL.Host = origin.Host
if !proxyPreserveHost {
req.Host = origin.Host
}
}
}