d80ba7b0 by astaxie

when Redirect delete output

1 parent 2417464c
Showing 1 changed file with 71 additions and 72 deletions
1 package beego 1 package beego
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "mime" 5 "mime"
6 "net/http" 6 "net/http"
7 "strings" 7 "strings"
8 "time" 8 "time"
9 ) 9 )
10 10
11 type Context struct { 11 type Context struct {
12 ResponseWriter http.ResponseWriter 12 ResponseWriter http.ResponseWriter
13 Request *http.Request 13 Request *http.Request
14 Params map[string]string 14 Params map[string]string
15 } 15 }
16 16
17 func (ctx *Context) WriteString(content string) { 17 func (ctx *Context) WriteString(content string) {
18 ctx.ResponseWriter.Write([]byte(content)) 18 ctx.ResponseWriter.Write([]byte(content))
19 } 19 }
20 20
21 func (ctx *Context) Abort(status int, body string) { 21 func (ctx *Context) Abort(status int, body string) {
22 ctx.ResponseWriter.WriteHeader(status) 22 ctx.ResponseWriter.WriteHeader(status)
23 ctx.ResponseWriter.Write([]byte(body)) 23 ctx.ResponseWriter.Write([]byte(body))
24 } 24 }
25 25
26 func (ctx *Context) Redirect(status int, url_ string) { 26 func (ctx *Context) Redirect(status int, url_ string) {
27 ctx.ResponseWriter.Header().Set("Location", url_) 27 ctx.ResponseWriter.Header().Set("Location", url_)
28 ctx.ResponseWriter.WriteHeader(status) 28 ctx.ResponseWriter.WriteHeader(status)
29 ctx.ResponseWriter.Write([]byte("Redirecting to: " + url_)) 29 }
30 } 30
31 31 func (ctx *Context) NotModified() {
32 func (ctx *Context) NotModified() { 32 ctx.ResponseWriter.WriteHeader(304)
33 ctx.ResponseWriter.WriteHeader(304) 33 }
34 } 34
35 35 func (ctx *Context) NotFound(message string) {
36 func (ctx *Context) NotFound(message string) { 36 ctx.ResponseWriter.WriteHeader(404)
37 ctx.ResponseWriter.WriteHeader(404) 37 ctx.ResponseWriter.Write([]byte(message))
38 ctx.ResponseWriter.Write([]byte(message)) 38 }
39 } 39
40 40 //Sets the content type by extension, as defined in the mime package.
41 //Sets the content type by extension, as defined in the mime package. 41 //For example, ctx.ContentType("json") sets the content-type to "application/json"
42 //For example, ctx.ContentType("json") sets the content-type to "application/json" 42 func (ctx *Context) ContentType(ext string) {
43 func (ctx *Context) ContentType(ext string) { 43 if !strings.HasPrefix(ext, ".") {
44 if !strings.HasPrefix(ext, ".") { 44 ext = "." + ext
45 ext = "." + ext 45 }
46 } 46 ctype := mime.TypeByExtension(ext)
47 ctype := mime.TypeByExtension(ext) 47 if ctype != "" {
48 if ctype != "" { 48 ctx.ResponseWriter.Header().Set("Content-Type", ctype)
49 ctx.ResponseWriter.Header().Set("Content-Type", ctype) 49 }
50 } 50 }
51 } 51
52 52 func (ctx *Context) SetHeader(hdr string, val string, unique bool) {
53 func (ctx *Context) SetHeader(hdr string, val string, unique bool) { 53 if unique {
54 if unique { 54 ctx.ResponseWriter.Header().Set(hdr, val)
55 ctx.ResponseWriter.Header().Set(hdr, val) 55 } else {
56 } else { 56 ctx.ResponseWriter.Header().Add(hdr, val)
57 ctx.ResponseWriter.Header().Add(hdr, val) 57 }
58 } 58 }
59 } 59
60 60 //Sets a cookie -- duration is the amount of time in seconds. 0 = forever
61 //Sets a cookie -- duration is the amount of time in seconds. 0 = forever 61 func (ctx *Context) SetCookie(name string, value string, age int64) {
62 func (ctx *Context) SetCookie(name string, value string, age int64) { 62 var utctime time.Time
63 var utctime time.Time 63 if age == 0 {
64 if age == 0 { 64 // 2^31 - 1 seconds (roughly 2038)
65 // 2^31 - 1 seconds (roughly 2038) 65 utctime = time.Unix(2147483647, 0)
66 utctime = time.Unix(2147483647, 0) 66 } else {
67 } else { 67 utctime = time.Unix(time.Now().Unix()+age, 0)
68 utctime = time.Unix(time.Now().Unix()+age, 0) 68 }
69 } 69 cookie := fmt.Sprintf("%s=%s; expires=%s", name, value, webTime(utctime))
70 cookie := fmt.Sprintf("%s=%s; expires=%s", name, value, webTime(utctime)) 70 ctx.SetHeader("Set-Cookie", cookie, false)
71 ctx.SetHeader("Set-Cookie", cookie, false) 71 }
72 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!