-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
50 lines (43 loc) · 1009 Bytes
/
http.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
package ohauth
import (
"encoding/json"
"net/http"
"net/url"
"time"
)
func mergeValues(vs ...url.Values) url.Values {
out := url.Values{}
for _, v := range vs {
for key, val := range v {
out[key] = val
}
}
return out
}
type context struct {
provider *Provider
writer http.ResponseWriter
request *http.Request
timestamp time.Time
}
func (c *context) redirect(u string) {
http.Redirect(c.writer, c.request, u, http.StatusFound)
}
func (c *context) fail(ru *StrictURL, e *Error, state string) {
v := mergeValues(url.Values{}, e.Values())
v.Set("state", state)
c.redirect(ru.StringWithParams(v))
}
func (c *context) json(s int, o interface{}) {
c.writer.Header().Set("Content-Type", "application/json; charset=utf-8")
c.writer.WriteHeader(s)
if err := json.NewEncoder(c.writer).Encode(o); err != nil {
panic(err)
}
}
func (c *context) abort(status int, msg string) {
c.writer.WriteHeader(status)
if _, err := c.writer.Write([]byte(msg)); err != nil {
panic(err)
}
}