Skip to content

rename.. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ type Options struct {
}

// New an operational cookie instance by cookie.GlobalOptions.
func New(res http.ResponseWriter, req *http.Request, options ...*GlobalOptions) (cookie *Cookies) {
func New(w http.ResponseWriter, r *http.Request, options ...*GlobalOptions) (cookie *Cookies) {
cookie = &Cookies{
request: req,
writer: res,
req: r,
w: w,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

w 写法改统一了,但是 reqr 依然有点不一样的样子

Copy link
Member

@DavidCai1111 DavidCai1111 Jan 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉要不和 w 一样都统一为 r ? (http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {}))

Copy link
Member Author

@zensh zensh Jan 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

小伙子你 simple 了,我特意用了 req,这样看源码方便理解。不信你看 golang 的 server 源码,都是req, res。另外还一个原因是 w 出现极少没关系,w 表明它是 writer,不是完整的 response,所以没用 res :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔🤔🤔

}
var opts *GlobalOptions
if len(options) > 0 {
Expand All @@ -55,9 +55,9 @@ func New(res http.ResponseWriter, req *http.Request, options ...*GlobalOptions)

// Cookies is secure cookie base on native cookie
type Cookies struct {
request *http.Request
writer http.ResponseWriter
opts *GlobalOptions
req *http.Request
w http.ResponseWriter
opts *GlobalOptions
}

// Get the cookie with the given name from the Cookie header in the request. If such a cookie exists, its value is returned. Otherwise, nothing is returned. { signed: true } can optionally be passed as the second parameter options. In this case, a signature cookie (a cookie of same name ending with the .sig suffix appended) is fetched. If the signature cookie does exist, cookie will check the hash of cookie-value whether matches registered keys.
Expand All @@ -67,10 +67,10 @@ func (c *Cookies) Get(name string, options ...*Options) (value string, err error
opts := options[0]
signed = opts.Signed
if signed && len(c.opts.Keys) == 0 {
panic("Required key for signed cookies")
panic("key required for signed cookies")
}
}
val, err := c.request.Cookie(name)
val, err := c.req.Cookie(name)
if val == nil {
return
}
Expand All @@ -79,7 +79,7 @@ func (c *Cookies) Get(name string, options ...*Options) (value string, err error
value = val.Value
if signed {
var sigName = name + ".sig"
oldsignval, _ := c.request.Cookie(sigName)
oldsignval, _ := c.req.Cookie(sigName)
for _, key := range c.opts.Keys {
newsignval := Sign(key, val.Value)
if oldsignval != nil && (newsignval == oldsignval.Value || signSha1(key, val.Value) == oldsignval.Value) {
Expand All @@ -88,7 +88,7 @@ func (c *Cookies) Get(name string, options ...*Options) (value string, err error
break
} else {
value = ""
err = errors.New("The cookie's value have different sign")
err = errors.New("invalid signed cookie")
}
}
}
Expand Down Expand Up @@ -132,12 +132,12 @@ func (c *Cookies) Set(name string, val string, options ...*Options) *Cookies {
} else if maxAge < 0 {
cookie.Expires = time.Unix(1, 0)
}
http.SetCookie(c.writer, cookie)
http.SetCookie(c.w, cookie)
if Signed {
signcookie := *cookie
signcookie.Value = Sign(key, val)
signcookie.Name = signcookie.Name + ".sig"
http.SetCookie(c.writer, &signcookie)
http.SetCookie(c.w, &signcookie)
}
return c
}
Expand All @@ -149,6 +149,8 @@ func Sign(key string, data string) (sign string) {
sign = base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
return
}

// compatibility for https://github.com/pillarjs/cookies v1.0.x
func signSha1(key string, data string) (sign string) {
mac := hmac.New(sha1.New, []byte(key))
mac.Write([]byte(data))
Expand Down
4 changes: 2 additions & 2 deletions cookie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func TestCookie(t *testing.T) {
Signed: true,
}
val, err := cookies.Get(cookiekey, opts)
assert.Equal(err.Error(), "The cookie's value have different sign")
assert.Equal(err.Error(), "invalid signed cookie")
assert.NotEqual(val, cookievalue)
assert.Equal(val, "")
})
Expand Down Expand Up @@ -293,7 +293,7 @@ func TestCookie(t *testing.T) {
Signed: true,
}
val, err := cookies.Get(cookiekey, opts)
assert.Equal(err.Error(), "The cookie's value have different sign")
assert.Equal(err.Error(), "invalid signed cookie")
assert.NotEqual(val, cookievalue)
assert.Equal(val, "")
})
Expand Down
File renamed without changes.