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

http to https redirect fails when not on test port #20

Closed
nullbio opened this issue Jul 25, 2017 · 1 comment
Closed

http to https redirect fails when not on test port #20

nullbio opened this issue Jul 25, 2017 · 1 comment

Comments

@nullbio
Copy link
Member

nullbio commented Jul 25, 2017

Review patch:

+++ b/vendor/github.com/volatiletech/abcweb/abcserver/server.go
@@ -1,6 +1,7 @@
 package abcserver

 import (
+       "bytes"
        "context"
        "crypto/tls"
        "fmt"
@@ -9,6 +10,7 @@ import (
        "net/http"
        "os"
        "os/signal"
+       "strings"

        "github.com/go-chi/chi"
        "github.com/pkg/errors"
@@ -108,13 +110,23 @@ func Redirect(cfg abcconfig.ServerConfig, logger *zap.Logger) {
                ErrorLog:     log.New(serverErrLogger{logger}, "", 0),
                Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
                        // Remove port if it exists so we can replace it with https port
-                       var httpHost string
-                       httpHost, _, err = net.SplitHostPort(r.Host)
-                       if err != nil {
-                               log.Fatal("failed to get http host from request", zap.Error(err))
+                       httpHost := r.Host
+                       if strings.ContainsRune(r.Host, ':') {
+                               httpHost, _, err = net.SplitHostPort(r.Host)
+                               if err != nil {
+                                       log.Fatal("failed to get http host from request", zap.Error(err))
+                               }
                        }
-
-                       url := fmt.Sprintf("https://%s:%s%s", httpHost, httpsPort, r.RequestURI)
+                       b := bytes.Buffer{}
+                       b.WriteString("https://")
+                       b.WriteString(httpHost)
+                       if httpsPort != "443" {
+                               b.WriteByte(':')
+                               b.WriteString(httpsPort)
+                       }
+                       b.WriteString(r.RequestURI)
+                       url := b.String()
+                       logger.Info("redirect", zap.String("origin", r.Host), zap.String("origin-path", r.URL.String()), zap.String("url", url))
                        http.Redirect(w, r, url, http.StatusMovedPermanently)
                }),
        }
@nullbio
Copy link
Member Author

nullbio commented Jul 26, 2017

Fixed in v3.0.2

@nullbio nullbio closed this as completed Jul 26, 2017
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

1 participant