Added Cookie.ParseBytes to be consistent with Args

This commit is contained in:
Aliaksandr Valialkin
2015-11-05 12:54:10 +02:00
parent 6fb8b96152
commit 236d4bd461
5 changed files with 17 additions and 8 deletions
+10 -1
View File
@@ -36,6 +36,7 @@ type Cookie struct {
Path []byte
bufKV argsKV
buf []byte
}
var zeroTime time.Time
@@ -77,7 +78,15 @@ func (c *Cookie) AppendBytes(dst []byte) []byte {
var errNoCookies = errors.New("no cookies found")
// Parse parses Set-Cookie header.
func (c *Cookie) Parse(src []byte) error {
func (c *Cookie) Parse(src string) error {
c.buf = AppendBytesStr(c.buf[:0], src)
return c.ParseBytes(c.buf)
}
// ParseBytes parses Set-Cookie header.
//
// It is safe modifying src buffer after function return.
func (c *Cookie) ParseBytes(src []byte) error {
c.Clear()
var s cookieScanner
+1 -1
View File
@@ -16,7 +16,7 @@ func TestCookieParse(t *testing.T) {
func testCookieParse(t *testing.T, s, expectedS string) {
var c Cookie
if err := c.Parse([]byte(s)); err != nil {
if err := c.Parse(s); err != nil {
t.Fatalf("unexpected error: %s", err)
}
result := string(c.AppendBytes(nil))
+3 -3
View File
@@ -8,7 +8,7 @@ func BenchmarkCookieParseMin(b *testing.B) {
var c Cookie
s := []byte("xxx=yyy")
for i := 0; i < b.N; i++ {
if err := c.Parse(s); err != nil {
if err := c.ParseBytes(s); err != nil {
b.Fatalf("unexpected error when parsing cookies: %s", err)
}
}
@@ -18,7 +18,7 @@ func BenchmarkCookieParseNoExpires(b *testing.B) {
var c Cookie
s := []byte("xxx=yyy; domain=foobar.com; path=/a/b")
for i := 0; i < b.N; i++ {
if err := c.Parse(s); err != nil {
if err := c.ParseBytes(s); err != nil {
b.Fatalf("unexpected error when parsing cookies: %s", err)
}
}
@@ -28,7 +28,7 @@ func BenchmarkCookieParseFull(b *testing.B) {
var c Cookie
s := []byte("xxx=yyy; expires=Tue, 10 Nov 2009 23:00:00 GMT; domain=foobar.com; path=/a/b")
for i := 0; i < b.N; i++ {
if err := c.Parse(s); err != nil {
if err := c.ParseBytes(s); err != nil {
b.Fatalf("unexpected error when parsing cookies: %s", err)
}
}
+2 -2
View File
@@ -142,7 +142,7 @@ func (h *ResponseHeader) VisitAll(f func(key, value []byte)) {
//
// Cookie name is passed in key and the whole Set-Cookie header value
// is passed in value on each f invocation. Value may be parsed
// with Cookie.Parse().
// with Cookie.ParseBytes().
//
// f must not retain references to key and/or value after returning.
func (h *ResponseHeader) VisitAllCookie(f func(key, value []byte)) {
@@ -458,7 +458,7 @@ func (h *ResponseHeader) GetCookie(cookie *Cookie) bool {
if v == nil {
return false
}
cookie.Parse(v)
cookie.ParseBytes(v)
return true
}
+1 -1
View File
@@ -50,7 +50,7 @@ func TestResponseHeaderCookie(t *testing.T) {
h.VisitAllCookie(func(key, value []byte) {
var cc Cookie
cc.Parse(value)
cc.ParseBytes(value)
if !bytes.Equal(key, cc.Key) {
t.Fatalf("Unexpected cookie key %q. Expected %q", key, cc.Key)
}