|
| 1 | +package web |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "net/http" |
| 6 | + "net/url" |
| 7 | + "os" |
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +// internal utility methods |
| 14 | +func webTime(t time.Time) string { |
| 15 | + ftime := t.Format(time.RFC1123) |
| 16 | + if strings.HasSuffix(ftime, "UTC") { |
| 17 | + ftime = ftime[0:len(ftime)-3] + "GMT" |
| 18 | + } |
| 19 | + return ftime |
| 20 | +} |
| 21 | + |
| 22 | +func dirExists(dir string) bool { |
| 23 | + d, e := os.Stat(dir) |
| 24 | + switch { |
| 25 | + case e != nil: |
| 26 | + return false |
| 27 | + case !d.IsDir(): |
| 28 | + return false |
| 29 | + } |
| 30 | + |
| 31 | + return true |
| 32 | +} |
| 33 | + |
| 34 | +func fileExists(dir string) bool { |
| 35 | + info, err := os.Stat(dir) |
| 36 | + if err != nil { |
| 37 | + return false |
| 38 | + } |
| 39 | + |
| 40 | + return !info.IsDir() |
| 41 | +} |
| 42 | + |
| 43 | +// Urlencode is a helper method that converts a map into URL-encoded form data. |
| 44 | +// It is a useful when constructing HTTP POST requests. |
| 45 | +func Urlencode(data map[string]string) string { |
| 46 | + var buf bytes.Buffer |
| 47 | + for k, v := range data { |
| 48 | + buf.WriteString(url.QueryEscape(k)) |
| 49 | + buf.WriteByte('=') |
| 50 | + buf.WriteString(url.QueryEscape(v)) |
| 51 | + buf.WriteByte('&') |
| 52 | + } |
| 53 | + s := buf.String() |
| 54 | + return s[0 : len(s)-1] |
| 55 | +} |
| 56 | + |
| 57 | +var slugRegex = regexp.MustCompile(`(?i:[^a-z0-9\-_])`) |
| 58 | + |
| 59 | +// Slug is a helper function that returns the URL slug for string s. |
| 60 | +// It's used to return clean, URL-friendly strings that can be |
| 61 | +// used in routing. |
| 62 | +func Slug(s string, sep string) string { |
| 63 | + if s == "" { |
| 64 | + return "" |
| 65 | + } |
| 66 | + slug := slugRegex.ReplaceAllString(s, sep) |
| 67 | + if slug == "" { |
| 68 | + return "" |
| 69 | + } |
| 70 | + quoted := regexp.QuoteMeta(sep) |
| 71 | + sepRegex := regexp.MustCompile("(" + quoted + "){2,}") |
| 72 | + slug = sepRegex.ReplaceAllString(slug, sep) |
| 73 | + sepEnds := regexp.MustCompile("^" + quoted + "|" + quoted + "$") |
| 74 | + slug = sepEnds.ReplaceAllString(slug, "") |
| 75 | + return strings.ToLower(slug) |
| 76 | +} |
| 77 | + |
| 78 | +// NewCookie is a helper method that returns a new http.Cookie object. |
| 79 | +// Duration is specified in seconds. If the duration is zero, the cookie is permanent. |
| 80 | +// This can be used in conjunction with ctx.SetCookie. |
| 81 | +func NewCookie(name string, value string, age int64) *http.Cookie { |
| 82 | + var utctime time.Time |
| 83 | + if age == 0 { |
| 84 | + // 2^31 - 1 seconds (roughly 2038) |
| 85 | + utctime = time.Unix(2147483647, 0) |
| 86 | + } else { |
| 87 | + utctime = time.Unix(time.Now().Unix()+age, 0) |
| 88 | + } |
| 89 | + return &http.Cookie{Name: name, Value: value, Expires: utctime} |
| 90 | +} |
0 commit comments