github.com/TeaOSLab/EdgeNode@v1.3.8/internal/nodes/http_request_redirect_https.go (about)

     1  package nodes
     2  
     3  import (
     4  	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
     5  	"net/http"
     6  	"strconv"
     7  	"strings"
     8  )
     9  
    10  func (this *HTTPRequest) doRedirectToHTTPS(redirectToHTTPSConfig *serverconfigs.HTTPRedirectToHTTPSConfig) (shouldBreak bool) {
    11  	var host = this.RawReq.Host
    12  
    13  	// 检查域名是否匹配
    14  	if !redirectToHTTPSConfig.MatchDomain(host) {
    15  		return false
    16  	}
    17  
    18  	if len(redirectToHTTPSConfig.Host) > 0 {
    19  		if redirectToHTTPSConfig.Port > 0 && redirectToHTTPSConfig.Port != 443 {
    20  			host = redirectToHTTPSConfig.Host + ":" + strconv.Itoa(redirectToHTTPSConfig.Port)
    21  		} else {
    22  			host = redirectToHTTPSConfig.Host
    23  		}
    24  	} else if redirectToHTTPSConfig.Port > 0 {
    25  		var lastIndex = strings.LastIndex(host, ":")
    26  		if lastIndex > 0 {
    27  			host = host[:lastIndex]
    28  		}
    29  		if redirectToHTTPSConfig.Port != 443 {
    30  			host = host + ":" + strconv.Itoa(redirectToHTTPSConfig.Port)
    31  		}
    32  	} else {
    33  		var lastIndex = strings.LastIndex(host, ":")
    34  		if lastIndex > 0 {
    35  			host = host[:lastIndex]
    36  		}
    37  	}
    38  
    39  	var statusCode = http.StatusMovedPermanently
    40  	if redirectToHTTPSConfig.Status > 0 {
    41  		statusCode = redirectToHTTPSConfig.Status
    42  	}
    43  
    44  	var newURL = "https://" + host + this.RawReq.RequestURI
    45  	this.ProcessResponseHeaders(this.writer.Header(), statusCode)
    46  	httpRedirect(this.writer, this.RawReq, newURL, statusCode)
    47  
    48  	return true
    49  }