Home  >  Article  >  Backend Development  >  An article analyzing how to determine whether a request is http or https in go

An article analyzing how to determine whether a request is http or https in go

藏色散人
藏色散人forward
2023-03-02 17:11:023644browse

This article brings you relevant knowledge about go. It mainly talks about judging whether the request is http or https (used to obtain the current access address) in golang. Interested friends should take a look. I hope everyone has to help.

Determine whether the request is http or https in golang - used to obtain the current access address

A freelance independent developer, the development log of the online customer service system

Today, a configuration was added to the customer service system, configuring the enterprise WeChat internal group notification robot webhook. When new messages arrive, the webhook is called and pushed to the enterprise WeChat group. The content of new messages supports markdown and can bring links. If you want to bring the link of the current URL, you can click directly to reply to the message. The following is a summary of the technical knowledge points involved.

If reverse proxy such as nginx is not used

Then you can directly use the following code to confirm. The TLS field of the http.Request structure determines whether the request is used. HTTPS protocol. If this field is not nil, it means that the request uses the HTTPS protocol; otherwise, it means that the request uses the HTTP protocol

package main
import (
    "fmt"
    "net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
    if r.TLS != nil {
        fmt.Println("HTTPS request")
    } else {
        fmt.Println("HTTP request")
    }
}
func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

In the case of reverse proxy

Above The code is invalid, you can use the following method

If you use nginx reverse proxy, you need to ensure that the following headers parameter X-Forwarded-Proto is passed in the reverse proxy

location / {
    proxy_pass http://your_upstream_server;
    proxy_set_header X-Forwarded-Proto $scheme;
}

You can judge this header to confirm whether https

package main
import (
    "fmt"
    "net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
    proto := r.Header.Get("X-Forwarded-Proto")
    if proto == "https" {
        fmt.Println("HTTPS request")
    } else {
        fmt.Println("HTTP request")
    }
}
func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

To summarize, the function to obtain the current access address

//获取当前访问的Host
func GetHost(r *http.Request) (Url string) {
    scheme := "http://"
    if r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https" {
        scheme = "https://"
    }
    return strings.Join([]string{scheme, r.Host}, "")}

[Related recommendations: Go video tutorial

The above is the detailed content of An article analyzing how to determine whether a request is http or https in go. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete