mirror of
https://github.com/1f349/violet.git
synced 2024-11-09 22:22:50 +00:00
46 lines
699 B
Go
46 lines
699 B
Go
package target
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/MrMelon54/violet/utils"
|
|
"net/http"
|
|
"net/url"
|
|
"path"
|
|
)
|
|
|
|
type Redirect struct {
|
|
Pre bool
|
|
Host string
|
|
Port int
|
|
Path string
|
|
Abs bool
|
|
Code int
|
|
}
|
|
|
|
func (r Redirect) FullHost() string {
|
|
if r.Port == 0 {
|
|
return r.Host
|
|
}
|
|
return fmt.Sprintf("%s:%d", r.Host, r.Port)
|
|
}
|
|
|
|
func (r Redirect) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|
p := r.Path
|
|
if !r.Abs {
|
|
p = path.Join(r.Path, req.URL.Path)
|
|
}
|
|
u := &url.URL{
|
|
Scheme: req.URL.Scheme,
|
|
Host: r.FullHost(),
|
|
Path: p,
|
|
}
|
|
if u.Path == "/" {
|
|
u.Path = ""
|
|
}
|
|
utils.FastRedirect(rw, req, u.String(), r.Code)
|
|
}
|
|
|
|
func (r Redirect) String() string {
|
|
return fmt.Sprintf("%#v", r)
|
|
}
|