-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshorturl.go
138 lines (125 loc) · 2.69 KB
/
shorturl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package shorturl
import (
_ "embed"
"fmt"
"net/http"
"net/url"
"strings"
"golang.org/x/net/idna"
"github.com/wzshiming/shorturl/storage"
)
type Handler struct {
storage storage.Storage
baseURL string
}
func WithBaseURL(baseURL string) func(*Handler) {
return func(h *Handler) {
h.baseURL = baseURL
}
}
func NewHandler(storage storage.Storage, opts ...func(*Handler)) *Handler {
h := &Handler{
storage: storage,
}
for _, opt := range opts {
opt(h)
}
return h
}
func errorResponse(w http.ResponseWriter, status int, msg ...string) {
m := ""
if len(msg) > 0 {
m = strings.Join(msg, " ")
} else {
m = http.StatusText(status)
}
http.Error(w, m, status)
}
//go:embed page.html
var page string
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet, http.MethodHead:
h.get(w, r)
case http.MethodPost:
h.post(w, r)
default:
errorResponse(w, http.StatusMethodNotAllowed)
}
}
func (h *Handler) get(w http.ResponseWriter, r *http.Request) {
code := r.URL.Path[1:]
if code == "" {
if r.Method == http.MethodGet {
d, err := h.basePrefix(r)
if err != nil {
errorResponse(w, http.StatusBadRequest, "invalid domain")
return
}
w.Write([]byte(fmt.Sprintf(page, d, d)))
}
return
}
if !Validate(code) {
errorResponse(w, http.StatusBadRequest, "invalid code")
return
}
url, err := h.storage.Decode(r.Context(), code)
if err != nil {
errorResponse(w, http.StatusNotFound)
return
}
http.Redirect(w, r, url, http.StatusFound)
}
func (h *Handler) post(w http.ResponseWriter, r *http.Request) {
u := r.FormValue("url")
if len(u) == 0 {
errorResponse(w, http.StatusBadRequest, "empty url")
return
}
if len(u) > 2048 {
errorResponse(w, http.StatusRequestURITooLong, "url too long")
return
}
url, err := url.Parse(u)
if err != nil || url.Scheme == "" || url.Host == "" {
errorResponse(w, http.StatusBadRequest, "invalid url")
return
}
host, err := idna.ToASCII(url.Host)
if err != nil {
errorResponse(w, http.StatusBadRequest, "invalid host")
return
}
url.Host = host
url.ForceQuery = false
if url.Path == "" {
url.Path = "/"
}
code, err := h.storage.Encode(r.Context(), url.String())
if err != nil {
errorResponse(w, http.StatusInternalServerError, "storage error")
return
}
d, err := h.basePrefix(r)
if err != nil {
errorResponse(w, http.StatusInternalServerError)
return
}
w.Write([]byte(d + code))
}
func (h *Handler) basePrefix(r *http.Request) (string, error) {
if h.baseURL != "" {
return h.baseURL, nil
}
u, err := r.URL.Parse("/")
if err != nil {
return "", err
}
u.Host = r.Host
u.Scheme = "http"
if r.TLS != nil {
u.Scheme = "https"
}
return u.String(), nil
}