Skip to content

Instantly share code, notes, and snippets.

@mkrautz
Created September 14, 2011 23:53
Show Gist options
  • Save mkrautz/1218145 to your computer and use it in GitHub Desktop.
Save mkrautz/1218145 to your computer and use it in GitHub Desktop.
Gorilla AppEngine cookies
--- ./sessions.go
+++ ./sessions.go
@@ -18,7 +18,6 @@ import (
"http"
"os"
"strconv"
- "strings"
"time"
"gorilla.googlecode.com/hg/gorilla/context"
)
@@ -848,124 +847,14 @@ func decode(value []byte) ([]byte, os.Er
// We can't call request.Cookie(name) with r58 so we re-implement this. :-/
func getCookie(r *http.Request, name string) (*http.Cookie, os.Error) {
- cookies := readCookies(r.Header, name)
- if len(cookies) == 0 {
- return nil, ErrNoCookie
- }
- return cookies[len(cookies)-1], nil
-}
-
-// readCookies parses all "Cookie" values from the header h and
-// returns the successfully parsed Cookies.
-//
-// if filter isn't empty, only cookies of that name are returned
-func readCookies(h http.Header, filter string) []*http.Cookie {
cookies := []*http.Cookie{}
- lines, ok := h["Cookie"]
- if !ok {
- return cookies
- }
-
- for _, line := range lines {
- parts := stringsSplit(strings.TrimSpace(line), ";")
- if len(parts) == 1 && parts[0] == "" {
- continue
- }
- // Per-line attributes
- parsedPairs := 0
- for i := 0; i < len(parts); i++ {
- parts[i] = strings.TrimSpace(parts[i])
- if len(parts[i]) == 0 {
- continue
- }
- name, val := parts[i], ""
- if j := strings.Index(name, "="); j >= 0 {
- name, val = name[:j], name[j+1:]
- }
- if !isCookieNameValid(name) {
- continue
- }
- if filter != "" && filter != name {
- continue
- }
- val, success := parseCookieValue(val)
- if !success {
- continue
- }
- cookies = append(cookies, &http.Cookie{Name: name, Value: val})
- parsedPairs++
- }
- }
- return cookies
-}
-
-func stringsSplit(s, sep string) []string {
- n := strings.Count(s, sep) + 1
- c := sep[0]
- start := 0
- a := make([]string, n)
- na := 0
- for i := 0; i+len(sep) <= len(s) && na+1 < n; i++ {
- if s[i] == c && (len(sep) == 1 || s[i:i+len(sep)] == sep) {
- a[na] = s[start : i]
- na++
- start = i + len(sep)
- i += len(sep) - 1
- }
- }
- a[na] = s[start:]
- return a[0 : na+1]
-}
-
-func isCookieNameValid(raw string) bool {
- for _, c := range raw {
- if !isToken(byte(c)) {
- return false
- }
- }
- return true
-}
-
-func isSeparator(c byte) bool {
- switch c {
- case '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '/', '[', ']', '?', '=', '{', '}', ' ', '\t':
- return true
- }
- return false
-}
-
-func isCtl(c byte) bool { return (0 <= c && c <= 31) || c == 127 }
-
-func isChar(c byte) bool { return 0 <= c && c <= 127 }
-
-func isToken(c byte) bool { return isChar(c) && !isCtl(c) && !isSeparator(c) }
-
-func parseCookieValue(raw string) (string, bool) {
- return parseCookieValueUsing(raw, isCookieByte)
-}
-
-func parseCookieValueUsing(raw string, validByte func(byte) bool) (string, bool) {
- raw = unquoteCookieValue(raw)
- for i := 0; i < len(raw); i++ {
- if !validByte(raw[i]) {
- return "", false
+ for _, c := range r.Cookie {
+ if c.Name == name {
+ cookies = append(cookies, c)
}
}
- return raw, true
-}
-
-func unquoteCookieValue(v string) string {
- if len(v) > 1 && v[0] == '"' && v[len(v)-1] == '"' {
- return v[1 : len(v)-1]
- }
- return v
-}
-
-func isCookieByte(c byte) bool {
- switch {
- case c == 0x21, 0x23 <= c && c <= 0x2b, 0x2d <= c && c <= 0x3a,
- 0x3c <= c && c <= 0x5b, 0x5d <= c && c <= 0x7e:
- return true
+ if len(cookies) == 0 {
+ return nil, ErrNoCookie
}
- return false
-}
+ return cookies[len(cookies)-1], nil
+}
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment