Home > Backend Development > Golang > 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

藏色散人
Release: 2023-03-02 19:56:18
forward
3730 people have browsed it

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)
}
Copy after login

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;
}
Copy after login

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)
}
Copy after login

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}, "")}
Copy after login

[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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template