This repository has been archived by the owner on Mar 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfer.go
146 lines (126 loc) · 3.17 KB
/
transfer.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
139
140
141
142
143
144
145
146
package httpsession
import (
"net/http"
"net/url"
"sync"
"time"
)
// Transfer provide and set sessionid
type Transfer interface {
SetMaxAge(maxAge time.Duration)
Get(req *http.Request) (Id, error)
Set(req *http.Request, rw http.ResponseWriter, id Id)
Clear(rw http.ResponseWriter)
}
// CookieRetriever provide sessionid from cookie
type CookieTransfer struct {
Name string
MaxAge time.Duration
Lock sync.Mutex
Secure bool
RootPath string
Domain string
}
func NewCookieTransfer(name string, maxAge time.Duration, secure bool, rootPath string) *CookieTransfer {
return &CookieTransfer{
Name: name,
MaxAge: maxAge,
Secure: secure,
RootPath: rootPath,
}
}
func (transfer *CookieTransfer) SetMaxAge(maxAge time.Duration) {
transfer.MaxAge = maxAge
}
func (transfer *CookieTransfer) Get(req *http.Request) (Id, error) {
cookie, err := req.Cookie(transfer.Name)
if err != nil {
if err == http.ErrNoCookie {
return "", nil
}
return "", err
}
if cookie.Value == "" {
return Id(""), nil
}
id, _ := url.QueryUnescape(cookie.Value)
return Id(id), nil
}
func (transfer *CookieTransfer) Set(req *http.Request, rw http.ResponseWriter, id Id) {
sid := url.QueryEscape(string(id))
transfer.Lock.Lock()
defer transfer.Lock.Unlock()
cookie, _ := req.Cookie(transfer.Name)
if cookie == nil {
cookie = &http.Cookie{
Name: transfer.Name,
Value: sid,
Path: transfer.RootPath,
Domain: transfer.Domain,
HttpOnly: true,
Secure: transfer.Secure,
}
if transfer.MaxAge > 0 {
cookie.MaxAge = int(transfer.MaxAge / time.Second)
//cookie.Expires = time.Now().Add(transfer.maxAge).UTC()
}
req.AddCookie(cookie)
} else {
cookie.Value = sid
if transfer.MaxAge > 0 {
cookie.MaxAge = int(transfer.MaxAge / time.Second)
//cookie.Expires = time.Now().Add(transfer.maxAge)
}
}
http.SetCookie(rw, cookie)
}
func (transfer *CookieTransfer) Clear(rw http.ResponseWriter) {
cookie := http.Cookie{
Name: transfer.Name,
Path: transfer.RootPath,
Domain: transfer.Domain,
HttpOnly: true,
Secure: transfer.Secure,
Expires: time.Date(0, 1, 1, 0, 0, 0, 0, time.Local),
MaxAge: -1,
}
http.SetCookie(rw, &cookie)
}
var _ Transfer = NewCookieTransfer("test", 0, false, "/")
// CookieRetriever provide sessionid from url
/*type UrlTransfer struct {
}
func NewUrlTransfer() *UrlTransfer {
return &UrlTransfer{}
}
func (transfer *UrlTransfer) Get(req *http.Request) (string, error) {
return "", nil
}
func (transfer *UrlTransfer) Set(rw http.ResponseWriter, id Id) {
}
var (
_ Transfer = NewUrlTransfer()
)
*/
//for SWFUpload ...
func NewCookieUrlTransfer(name string, maxAge time.Duration, secure bool, rootPath string) *CookieUrlTransfer {
return &CookieUrlTransfer{
CookieTransfer: CookieTransfer{
Name: name,
MaxAge: maxAge,
Secure: secure,
RootPath: rootPath,
},
}
}
type CookieUrlTransfer struct {
CookieTransfer
}
func (transfer *CookieUrlTransfer) Get(req *http.Request) (Id, error) {
sessionId := req.URL.Query().Get(transfer.Name)
if sessionId != "" {
sessionId, _ = url.QueryUnescape(sessionId)
return Id(sessionId), nil
}
return transfer.CookieTransfer.Get(req)
}