github.com/blend/go-sdk@v1.20220411.3/reverseproxy/redirect_http.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package reverseproxy
     9  
    10  import (
    11  	"net/http"
    12  
    13  	"github.com/blend/go-sdk/webutil"
    14  )
    15  
    16  // HTTPRedirect redirects HTTP to HTTPS
    17  type HTTPRedirect struct {
    18  	RedirectScheme string
    19  	RedirectHost   string
    20  }
    21  
    22  // ServeHTTP redirects HTTP to HTTPS
    23  func (hr HTTPRedirect) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
    24  	if hr.RedirectScheme != "" {
    25  		req.URL.Scheme = hr.RedirectScheme
    26  	} else {
    27  		req.URL.Scheme = webutil.SchemeHTTPS
    28  	}
    29  	if hr.RedirectHost != "" {
    30  		req.URL.Host = hr.RedirectHost
    31  	}
    32  	if req.URL.Host == "" {
    33  		req.URL.Host = req.Host
    34  	}
    35  	http.Redirect(rw, req, req.URL.String(), http.StatusMovedPermanently)
    36  }