【Gin-v1.9.0源码阅读】render/redirect.go

// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package render

import (
	"fmt"
	"net/http"
)

// Redirect contains the http request reference and redirects status code and location.
// Redirect包含http请求引用,并重定向状态代码和位置。
type Redirect struct {
	Code     int
	Request  *http.Request
	Location string
}

// Render (Redirect) redirects the http request to new location and writes redirect response.
// Render(Redirect)将http请求重定向到新位置,并写入重定向响应。
func (r Redirect) Render(w http.ResponseWriter) error {
	if (r.Code < http.StatusMultipleChoices || r.Code > http.StatusPermanentRedirect) && r.Code != http.StatusCreated {
		panic(fmt.Sprintf("Cannot redirect with status code %d", r.Code))
	}
	http.Redirect(w, r.Request, r.Location, r.Code)
	return nil
}

// WriteContentType (Redirect) don't write any ContentType.
// WriteContentType(重定向)不写入任何ContentType。
func (r Redirect) WriteContentType(http.ResponseWriter) {}

猜你喜欢

转载自blog.csdn.net/qq2942713658/article/details/131056188
今日推荐