Merge pull request #415 from fuxiaohei/master
add comments for context package.
Showing
3 changed files
with
91 additions
and
2 deletions
| ... | @@ -6,6 +6,8 @@ import ( | ... | @@ -6,6 +6,8 @@ import ( |
| 6 | "github.com/astaxie/beego/middleware" | 6 | "github.com/astaxie/beego/middleware" |
| 7 | ) | 7 | ) |
| 8 | 8 | ||
| 9 | // Http request context struct including BeegoInput, BeegoOutput, http.Request and http.ResponseWriter. | ||
| 10 | // BeegoInput and BeegoOutput provides some api to operate request and response more easily. | ||
| 9 | type Context struct { | 11 | type Context struct { |
| 10 | Input *BeegoInput | 12 | Input *BeegoInput |
| 11 | Output *BeegoOutput | 13 | Output *BeegoOutput |
| ... | @@ -13,11 +15,16 @@ type Context struct { | ... | @@ -13,11 +15,16 @@ type Context struct { |
| 13 | ResponseWriter http.ResponseWriter | 15 | ResponseWriter http.ResponseWriter |
| 14 | } | 16 | } |
| 15 | 17 | ||
| 18 | // Redirect does redirection to localurl with http header status code. | ||
| 19 | // It sends http response header directly. | ||
| 16 | func (ctx *Context) Redirect(status int, localurl string) { | 20 | func (ctx *Context) Redirect(status int, localurl string) { |
| 17 | ctx.Output.Header("Location", localurl) | 21 | ctx.Output.Header("Location", localurl) |
| 18 | ctx.Output.SetStatus(status) | 22 | ctx.Output.SetStatus(status) |
| 19 | } | 23 | } |
| 20 | 24 | ||
| 25 | // Abort stops this request. | ||
| 26 | // if middleware.ErrorMaps exists, panic body. | ||
| 27 | // if middleware.HTTPExceptionMaps exists, panic HTTPException struct with status and body string. | ||
| 21 | func (ctx *Context) Abort(status int, body string) { | 28 | func (ctx *Context) Abort(status int, body string) { |
| 22 | ctx.Output.SetStatus(status) | 29 | ctx.Output.SetStatus(status) |
| 23 | // first panic from ErrorMaps, is is user defined error functions. | 30 | // first panic from ErrorMaps, is is user defined error functions. |
| ... | @@ -35,14 +42,20 @@ func (ctx *Context) Abort(status int, body string) { | ... | @@ -35,14 +42,20 @@ func (ctx *Context) Abort(status int, body string) { |
| 35 | panic(body) | 42 | panic(body) |
| 36 | } | 43 | } |
| 37 | 44 | ||
| 45 | // Write string to response body. | ||
| 46 | // it sends response body. | ||
| 38 | func (ctx *Context) WriteString(content string) { | 47 | func (ctx *Context) WriteString(content string) { |
| 39 | ctx.Output.Body([]byte(content)) | 48 | ctx.Output.Body([]byte(content)) |
| 40 | } | 49 | } |
| 41 | 50 | ||
| 51 | // Get cookie from request by a given key. | ||
| 52 | // It's alias of BeegoInput.Cookie. | ||
| 42 | func (ctx *Context) GetCookie(key string) string { | 53 | func (ctx *Context) GetCookie(key string) string { |
| 43 | return ctx.Input.Cookie(key) | 54 | return ctx.Input.Cookie(key) |
| 44 | } | 55 | } |
| 45 | 56 | ||
| 57 | // Set cookie for response. | ||
| 58 | // It's alias of BeegoOutput.Cookie. | ||
| 46 | func (ctx *Context) SetCookie(name string, value string, others ...interface{}) { | 59 | func (ctx *Context) SetCookie(name string, value string, others ...interface{}) { |
| 47 | ctx.Output.Cookie(name, value, others...) | 60 | ctx.Output.Cookie(name, value, others...) |
| 48 | } | 61 | } | ... | ... |
| ... | @@ -10,14 +10,17 @@ import ( | ... | @@ -10,14 +10,17 @@ import ( |
| 10 | "github.com/astaxie/beego/session" | 10 | "github.com/astaxie/beego/session" |
| 11 | ) | 11 | ) |
| 12 | 12 | ||
| 13 | // BeegoInput operates the http request header ,data ,cookie and body. | ||
| 14 | // it also contains router params and current session. | ||
| 13 | type BeegoInput struct { | 15 | type BeegoInput struct { |
| 14 | CruSession session.SessionStore | 16 | CruSession session.SessionStore |
| 15 | Params map[string]string | 17 | Params map[string]string |
| 16 | Data map[interface{}]interface{} | 18 | Data map[interface{}]interface{} // store some values in this context when calling context in filter or controller. |
| 17 | Request *http.Request | 19 | Request *http.Request |
| 18 | RequestBody []byte | 20 | RequestBody []byte |
| 19 | } | 21 | } |
| 20 | 22 | ||
| 23 | // NewInput return BeegoInput generated by http.Request. | ||
| 21 | func NewInput(req *http.Request) *BeegoInput { | 24 | func NewInput(req *http.Request) *BeegoInput { |
| 22 | return &BeegoInput{ | 25 | return &BeegoInput{ |
| 23 | Params: make(map[string]string), | 26 | Params: make(map[string]string), |
| ... | @@ -26,22 +29,27 @@ func NewInput(req *http.Request) *BeegoInput { | ... | @@ -26,22 +29,27 @@ func NewInput(req *http.Request) *BeegoInput { |
| 26 | } | 29 | } |
| 27 | } | 30 | } |
| 28 | 31 | ||
| 32 | // Protocol returns request protocol name, such as HTTP/1.1 . | ||
| 29 | func (input *BeegoInput) Protocol() string { | 33 | func (input *BeegoInput) Protocol() string { |
| 30 | return input.Request.Proto | 34 | return input.Request.Proto |
| 31 | } | 35 | } |
| 32 | 36 | ||
| 37 | // Uri returns full request url with query string, fragment. | ||
| 33 | func (input *BeegoInput) Uri() string { | 38 | func (input *BeegoInput) Uri() string { |
| 34 | return input.Request.RequestURI | 39 | return input.Request.RequestURI |
| 35 | } | 40 | } |
| 36 | 41 | ||
| 42 | // Url returns request url path (without query string, fragment). | ||
| 37 | func (input *BeegoInput) Url() string { | 43 | func (input *BeegoInput) Url() string { |
| 38 | return input.Request.URL.String() | 44 | return input.Request.URL.String() |
| 39 | } | 45 | } |
| 40 | 46 | ||
| 47 | // Site returns base site url as scheme://domain type. | ||
| 41 | func (input *BeegoInput) Site() string { | 48 | func (input *BeegoInput) Site() string { |
| 42 | return input.Scheme() + "://" + input.Domain() | 49 | return input.Scheme() + "://" + input.Domain() |
| 43 | } | 50 | } |
| 44 | 51 | ||
| 52 | // Scheme returns request scheme as "http" or "https". | ||
| 45 | func (input *BeegoInput) Scheme() string { | 53 | func (input *BeegoInput) Scheme() string { |
| 46 | if input.Request.URL.Scheme != "" { | 54 | if input.Request.URL.Scheme != "" { |
| 47 | return input.Request.URL.Scheme | 55 | return input.Request.URL.Scheme |
| ... | @@ -52,10 +60,14 @@ func (input *BeegoInput) Scheme() string { | ... | @@ -52,10 +60,14 @@ func (input *BeegoInput) Scheme() string { |
| 52 | } | 60 | } |
| 53 | } | 61 | } |
| 54 | 62 | ||
| 63 | // Domain returns host name. | ||
| 64 | // Alias of Host method. | ||
| 55 | func (input *BeegoInput) Domain() string { | 65 | func (input *BeegoInput) Domain() string { |
| 56 | return input.Host() | 66 | return input.Host() |
| 57 | } | 67 | } |
| 58 | 68 | ||
| 69 | // Host returns host name. | ||
| 70 | // if no host info in request, return localhost. | ||
| 59 | func (input *BeegoInput) Host() string { | 71 | func (input *BeegoInput) Host() string { |
| 60 | if input.Request.Host != "" { | 72 | if input.Request.Host != "" { |
| 61 | hostParts := strings.Split(input.Request.Host, ":") | 73 | hostParts := strings.Split(input.Request.Host, ":") |
| ... | @@ -67,30 +79,39 @@ func (input *BeegoInput) Host() string { | ... | @@ -67,30 +79,39 @@ func (input *BeegoInput) Host() string { |
| 67 | return "localhost" | 79 | return "localhost" |
| 68 | } | 80 | } |
| 69 | 81 | ||
| 82 | // Method returns http request method. | ||
| 70 | func (input *BeegoInput) Method() string { | 83 | func (input *BeegoInput) Method() string { |
| 71 | return input.Request.Method | 84 | return input.Request.Method |
| 72 | } | 85 | } |
| 73 | 86 | ||
| 87 | // Is returns boolean of this request is on given method, such as Is("POST"). | ||
| 74 | func (input *BeegoInput) Is(method string) bool { | 88 | func (input *BeegoInput) Is(method string) bool { |
| 75 | return input.Method() == method | 89 | return input.Method() == method |
| 76 | } | 90 | } |
| 77 | 91 | ||
| 92 | // IsAjax returns boolean of this request is generated by ajax. | ||
| 78 | func (input *BeegoInput) IsAjax() bool { | 93 | func (input *BeegoInput) IsAjax() bool { |
| 79 | return input.Header("X-Requested-With") == "XMLHttpRequest" | 94 | return input.Header("X-Requested-With") == "XMLHttpRequest" |
| 80 | } | 95 | } |
| 81 | 96 | ||
| 97 | // IsSecure returns boolean of this request is in https. | ||
| 82 | func (input *BeegoInput) IsSecure() bool { | 98 | func (input *BeegoInput) IsSecure() bool { |
| 83 | return input.Scheme() == "https" | 99 | return input.Scheme() == "https" |
| 84 | } | 100 | } |
| 85 | 101 | ||
| 102 | // IsSecure returns boolean of this request is in webSocket. | ||
| 86 | func (input *BeegoInput) IsWebsocket() bool { | 103 | func (input *BeegoInput) IsWebsocket() bool { |
| 87 | return input.Header("Upgrade") == "websocket" | 104 | return input.Header("Upgrade") == "websocket" |
| 88 | } | 105 | } |
| 89 | 106 | ||
| 107 | // IsSecure returns boolean of whether file uploads in this request or not.. | ||
| 90 | func (input *BeegoInput) IsUpload() bool { | 108 | func (input *BeegoInput) IsUpload() bool { |
| 91 | return input.Request.MultipartForm != nil | 109 | return input.Request.MultipartForm != nil |
| 92 | } | 110 | } |
| 93 | 111 | ||
| 112 | // IP returns request client ip. | ||
| 113 | // if in proxy, return first proxy id. | ||
| 114 | // if error, return 127.0.0.1. | ||
| 94 | func (input *BeegoInput) IP() string { | 115 | func (input *BeegoInput) IP() string { |
| 95 | ips := input.Proxy() | 116 | ips := input.Proxy() |
| 96 | if len(ips) > 0 && ips[0] != "" { | 117 | if len(ips) > 0 && ips[0] != "" { |
| ... | @@ -98,13 +119,14 @@ func (input *BeegoInput) IP() string { | ... | @@ -98,13 +119,14 @@ func (input *BeegoInput) IP() string { |
| 98 | } | 119 | } |
| 99 | ip := strings.Split(input.Request.RemoteAddr, ":") | 120 | ip := strings.Split(input.Request.RemoteAddr, ":") |
| 100 | if len(ip) > 0 { | 121 | if len(ip) > 0 { |
| 101 | if ip[0] != "["{ | 122 | if ip[0] != "[" { |
| 102 | return ip[0] | 123 | return ip[0] |
| 103 | } | 124 | } |
| 104 | } | 125 | } |
| 105 | return "127.0.0.1" | 126 | return "127.0.0.1" |
| 106 | } | 127 | } |
| 107 | 128 | ||
| 129 | // Proxy returns proxy client ips slice. | ||
| 108 | func (input *BeegoInput) Proxy() []string { | 130 | func (input *BeegoInput) Proxy() []string { |
| 109 | if ips := input.Header("X-Forwarded-For"); ips != "" { | 131 | if ips := input.Header("X-Forwarded-For"); ips != "" { |
| 110 | return strings.Split(ips, ",") | 132 | return strings.Split(ips, ",") |
| ... | @@ -112,15 +134,20 @@ func (input *BeegoInput) Proxy() []string { | ... | @@ -112,15 +134,20 @@ func (input *BeegoInput) Proxy() []string { |
| 112 | return []string{} | 134 | return []string{} |
| 113 | } | 135 | } |
| 114 | 136 | ||
| 137 | // Refer returns http referer header. | ||
| 115 | func (input *BeegoInput) Refer() string { | 138 | func (input *BeegoInput) Refer() string { |
| 116 | return input.Header("Referer") | 139 | return input.Header("Referer") |
| 117 | } | 140 | } |
| 118 | 141 | ||
| 142 | // SubDomains returns sub domain string. | ||
| 143 | // if aa.bb.domain.com, returns aa.bb . | ||
| 119 | func (input *BeegoInput) SubDomains() string { | 144 | func (input *BeegoInput) SubDomains() string { |
| 120 | parts := strings.Split(input.Host(), ".") | 145 | parts := strings.Split(input.Host(), ".") |
| 121 | return strings.Join(parts[len(parts)-2:], ".") | 146 | return strings.Join(parts[len(parts)-2:], ".") |
| 122 | } | 147 | } |
| 123 | 148 | ||
| 149 | // Port returns request client port. | ||
| 150 | // when error or empty, return 80. | ||
| 124 | func (input *BeegoInput) Port() int { | 151 | func (input *BeegoInput) Port() int { |
| 125 | parts := strings.Split(input.Request.Host, ":") | 152 | parts := strings.Split(input.Request.Host, ":") |
| 126 | if len(parts) == 2 { | 153 | if len(parts) == 2 { |
| ... | @@ -130,10 +157,12 @@ func (input *BeegoInput) Port() int { | ... | @@ -130,10 +157,12 @@ func (input *BeegoInput) Port() int { |
| 130 | return 80 | 157 | return 80 |
| 131 | } | 158 | } |
| 132 | 159 | ||
| 160 | // UserAgent returns request client user agent string. | ||
| 133 | func (input *BeegoInput) UserAgent() string { | 161 | func (input *BeegoInput) UserAgent() string { |
| 134 | return input.Header("User-Agent") | 162 | return input.Header("User-Agent") |
| 135 | } | 163 | } |
| 136 | 164 | ||
| 165 | // Param returns router param by a given key. | ||
| 137 | func (input *BeegoInput) Param(key string) string { | 166 | func (input *BeegoInput) Param(key string) string { |
| 138 | if v, ok := input.Params[key]; ok { | 167 | if v, ok := input.Params[key]; ok { |
| 139 | return v | 168 | return v |
| ... | @@ -141,15 +170,19 @@ func (input *BeegoInput) Param(key string) string { | ... | @@ -141,15 +170,19 @@ func (input *BeegoInput) Param(key string) string { |
| 141 | return "" | 170 | return "" |
| 142 | } | 171 | } |
| 143 | 172 | ||
| 173 | // Query returns input data item string by a given string. | ||
| 144 | func (input *BeegoInput) Query(key string) string { | 174 | func (input *BeegoInput) Query(key string) string { |
| 145 | input.Request.ParseForm() | 175 | input.Request.ParseForm() |
| 146 | return input.Request.Form.Get(key) | 176 | return input.Request.Form.Get(key) |
| 147 | } | 177 | } |
| 148 | 178 | ||
| 179 | // Header returns request header item string by a given string. | ||
| 149 | func (input *BeegoInput) Header(key string) string { | 180 | func (input *BeegoInput) Header(key string) string { |
| 150 | return input.Request.Header.Get(key) | 181 | return input.Request.Header.Get(key) |
| 151 | } | 182 | } |
| 152 | 183 | ||
| 184 | // Cookie returns request cookie item string by a given key. | ||
| 185 | // if non-existed, return empty string. | ||
| 153 | func (input *BeegoInput) Cookie(key string) string { | 186 | func (input *BeegoInput) Cookie(key string) string { |
| 154 | ck, err := input.Request.Cookie(key) | 187 | ck, err := input.Request.Cookie(key) |
| 155 | if err != nil { | 188 | if err != nil { |
| ... | @@ -158,10 +191,12 @@ func (input *BeegoInput) Cookie(key string) string { | ... | @@ -158,10 +191,12 @@ func (input *BeegoInput) Cookie(key string) string { |
| 158 | return ck.Value | 191 | return ck.Value |
| 159 | } | 192 | } |
| 160 | 193 | ||
| 194 | // Session returns current session item value by a given key. | ||
| 161 | func (input *BeegoInput) Session(key interface{}) interface{} { | 195 | func (input *BeegoInput) Session(key interface{}) interface{} { |
| 162 | return input.CruSession.Get(key) | 196 | return input.CruSession.Get(key) |
| 163 | } | 197 | } |
| 164 | 198 | ||
| 199 | // Body returns the raw request body data as bytes. | ||
| 165 | func (input *BeegoInput) Body() []byte { | 200 | func (input *BeegoInput) Body() []byte { |
| 166 | requestbody, _ := ioutil.ReadAll(input.Request.Body) | 201 | requestbody, _ := ioutil.ReadAll(input.Request.Body) |
| 167 | input.Request.Body.Close() | 202 | input.Request.Body.Close() |
| ... | @@ -171,6 +206,7 @@ func (input *BeegoInput) Body() []byte { | ... | @@ -171,6 +206,7 @@ func (input *BeegoInput) Body() []byte { |
| 171 | return requestbody | 206 | return requestbody |
| 172 | } | 207 | } |
| 173 | 208 | ||
| 209 | // GetData returns the stored data in this context. | ||
| 174 | func (input *BeegoInput) GetData(key interface{}) interface{} { | 210 | func (input *BeegoInput) GetData(key interface{}) interface{} { |
| 175 | if v, ok := input.Data[key]; ok { | 211 | if v, ok := input.Data[key]; ok { |
| 176 | return v | 212 | return v |
| ... | @@ -178,6 +214,8 @@ func (input *BeegoInput) GetData(key interface{}) interface{} { | ... | @@ -178,6 +214,8 @@ func (input *BeegoInput) GetData(key interface{}) interface{} { |
| 178 | return nil | 214 | return nil |
| 179 | } | 215 | } |
| 180 | 216 | ||
| 217 | // SetData stores data with given key in this context. | ||
| 218 | // This data are only available in this context. | ||
| 181 | func (input *BeegoInput) SetData(key, val interface{}) { | 219 | func (input *BeegoInput) SetData(key, val interface{}) { |
| 182 | input.Data[key] = val | 220 | input.Data[key] = val |
| 183 | } | 221 | } | ... | ... |
| ... | @@ -17,20 +17,27 @@ import ( | ... | @@ -17,20 +17,27 @@ import ( |
| 17 | "strings" | 17 | "strings" |
| 18 | ) | 18 | ) |
| 19 | 19 | ||
| 20 | // BeegoOutput does work for sending response header. | ||
| 20 | type BeegoOutput struct { | 21 | type BeegoOutput struct { |
| 21 | Context *Context | 22 | Context *Context |
| 22 | Status int | 23 | Status int |
| 23 | EnableGzip bool | 24 | EnableGzip bool |
| 24 | } | 25 | } |
| 25 | 26 | ||
| 27 | // NewOutput returns new BeegoOutput. | ||
| 28 | // it contains nothing now. | ||
| 26 | func NewOutput() *BeegoOutput { | 29 | func NewOutput() *BeegoOutput { |
| 27 | return &BeegoOutput{} | 30 | return &BeegoOutput{} |
| 28 | } | 31 | } |
| 29 | 32 | ||
| 33 | // Header sets response header item string via given key. | ||
| 30 | func (output *BeegoOutput) Header(key, val string) { | 34 | func (output *BeegoOutput) Header(key, val string) { |
| 31 | output.Context.ResponseWriter.Header().Set(key, val) | 35 | output.Context.ResponseWriter.Header().Set(key, val) |
| 32 | } | 36 | } |
| 33 | 37 | ||
| 38 | // Body sets response body content. | ||
| 39 | // if EnableGzip, compress content string. | ||
| 40 | // it sends out response body directly. | ||
| 34 | func (output *BeegoOutput) Body(content []byte) { | 41 | func (output *BeegoOutput) Body(content []byte) { |
| 35 | output_writer := output.Context.ResponseWriter.(io.Writer) | 42 | output_writer := output.Context.ResponseWriter.(io.Writer) |
| 36 | if output.EnableGzip == true && output.Context.Input.Header("Accept-Encoding") != "" { | 43 | if output.EnableGzip == true && output.Context.Input.Header("Accept-Encoding") != "" { |
| ... | @@ -64,6 +71,8 @@ func (output *BeegoOutput) Body(content []byte) { | ... | @@ -64,6 +71,8 @@ func (output *BeegoOutput) Body(content []byte) { |
| 64 | } | 71 | } |
| 65 | } | 72 | } |
| 66 | 73 | ||
| 74 | // Cookie sets cookie value via given key. | ||
| 75 | // others are ordered as cookie's max age time, path,domain, secure and httponly. | ||
| 67 | func (output *BeegoOutput) Cookie(name string, value string, others ...interface{}) { | 76 | func (output *BeegoOutput) Cookie(name string, value string, others ...interface{}) { |
| 68 | var b bytes.Buffer | 77 | var b bytes.Buffer |
| 69 | fmt.Fprintf(&b, "%s=%s", sanitizeName(name), sanitizeValue(value)) | 78 | fmt.Fprintf(&b, "%s=%s", sanitizeName(name), sanitizeValue(value)) |
| ... | @@ -116,6 +125,8 @@ func sanitizeValue(v string) string { | ... | @@ -116,6 +125,8 @@ func sanitizeValue(v string) string { |
| 116 | return cookieValueSanitizer.Replace(v) | 125 | return cookieValueSanitizer.Replace(v) |
| 117 | } | 126 | } |
| 118 | 127 | ||
| 128 | // Json writes json to response body. | ||
| 129 | // if coding is true, it converts utf-8 to \u0000 type. | ||
| 119 | func (output *BeegoOutput) Json(data interface{}, hasIndent bool, coding bool) error { | 130 | func (output *BeegoOutput) Json(data interface{}, hasIndent bool, coding bool) error { |
| 120 | output.Header("Content-Type", "application/json;charset=UTF-8") | 131 | output.Header("Content-Type", "application/json;charset=UTF-8") |
| 121 | var content []byte | 132 | var content []byte |
| ... | @@ -136,6 +147,7 @@ func (output *BeegoOutput) Json(data interface{}, hasIndent bool, coding bool) e | ... | @@ -136,6 +147,7 @@ func (output *BeegoOutput) Json(data interface{}, hasIndent bool, coding bool) e |
| 136 | return nil | 147 | return nil |
| 137 | } | 148 | } |
| 138 | 149 | ||
| 150 | // Jsonp writes jsonp to response body. | ||
| 139 | func (output *BeegoOutput) Jsonp(data interface{}, hasIndent bool) error { | 151 | func (output *BeegoOutput) Jsonp(data interface{}, hasIndent bool) error { |
| 140 | output.Header("Content-Type", "application/javascript;charset=UTF-8") | 152 | output.Header("Content-Type", "application/javascript;charset=UTF-8") |
| 141 | var content []byte | 153 | var content []byte |
| ... | @@ -161,6 +173,7 @@ func (output *BeegoOutput) Jsonp(data interface{}, hasIndent bool) error { | ... | @@ -161,6 +173,7 @@ func (output *BeegoOutput) Jsonp(data interface{}, hasIndent bool) error { |
| 161 | return nil | 173 | return nil |
| 162 | } | 174 | } |
| 163 | 175 | ||
| 176 | // Xml writes xml string to response body. | ||
| 164 | func (output *BeegoOutput) Xml(data interface{}, hasIndent bool) error { | 177 | func (output *BeegoOutput) Xml(data interface{}, hasIndent bool) error { |
| 165 | output.Header("Content-Type", "application/xml;charset=UTF-8") | 178 | output.Header("Content-Type", "application/xml;charset=UTF-8") |
| 166 | var content []byte | 179 | var content []byte |
| ... | @@ -178,6 +191,8 @@ func (output *BeegoOutput) Xml(data interface{}, hasIndent bool) error { | ... | @@ -178,6 +191,8 @@ func (output *BeegoOutput) Xml(data interface{}, hasIndent bool) error { |
| 178 | return nil | 191 | return nil |
| 179 | } | 192 | } |
| 180 | 193 | ||
| 194 | // Download forces response for download file. | ||
| 195 | // it prepares the download response header automatically. | ||
| 181 | func (output *BeegoOutput) Download(file string) { | 196 | func (output *BeegoOutput) Download(file string) { |
| 182 | output.Header("Content-Description", "File Transfer") | 197 | output.Header("Content-Description", "File Transfer") |
| 183 | output.Header("Content-Type", "application/octet-stream") | 198 | output.Header("Content-Type", "application/octet-stream") |
| ... | @@ -189,6 +204,8 @@ func (output *BeegoOutput) Download(file string) { | ... | @@ -189,6 +204,8 @@ func (output *BeegoOutput) Download(file string) { |
| 189 | http.ServeFile(output.Context.ResponseWriter, output.Context.Request, file) | 204 | http.ServeFile(output.Context.ResponseWriter, output.Context.Request, file) |
| 190 | } | 205 | } |
| 191 | 206 | ||
| 207 | // ContentType sets the content type from ext string. | ||
| 208 | // MIME type is given in mime package. | ||
| 192 | func (output *BeegoOutput) ContentType(ext string) { | 209 | func (output *BeegoOutput) ContentType(ext string) { |
| 193 | if !strings.HasPrefix(ext, ".") { | 210 | if !strings.HasPrefix(ext, ".") { |
| 194 | ext = "." + ext | 211 | ext = "." + ext |
| ... | @@ -199,43 +216,63 @@ func (output *BeegoOutput) ContentType(ext string) { | ... | @@ -199,43 +216,63 @@ func (output *BeegoOutput) ContentType(ext string) { |
| 199 | } | 216 | } |
| 200 | } | 217 | } |
| 201 | 218 | ||
| 219 | // SetStatus sets response status code. | ||
| 220 | // It writes response header directly. | ||
| 202 | func (output *BeegoOutput) SetStatus(status int) { | 221 | func (output *BeegoOutput) SetStatus(status int) { |
| 203 | output.Context.ResponseWriter.WriteHeader(status) | 222 | output.Context.ResponseWriter.WriteHeader(status) |
| 204 | output.Status = status | 223 | output.Status = status |
| 205 | } | 224 | } |
| 206 | 225 | ||
| 226 | // IsCachable returns boolean of this request is cached. | ||
| 227 | // HTTP 304 means cached. | ||
| 207 | func (output *BeegoOutput) IsCachable(status int) bool { | 228 | func (output *BeegoOutput) IsCachable(status int) bool { |
| 208 | return output.Status >= 200 && output.Status < 300 || output.Status == 304 | 229 | return output.Status >= 200 && output.Status < 300 || output.Status == 304 |
| 209 | } | 230 | } |
| 210 | 231 | ||
| 232 | // IsEmpty returns boolean of this request is empty. | ||
| 233 | // HTTP 201,204 and 304 means empty. | ||
| 211 | func (output *BeegoOutput) IsEmpty(status int) bool { | 234 | func (output *BeegoOutput) IsEmpty(status int) bool { |
| 212 | return output.Status == 201 || output.Status == 204 || output.Status == 304 | 235 | return output.Status == 201 || output.Status == 204 || output.Status == 304 |
| 213 | } | 236 | } |
| 214 | 237 | ||
| 238 | // IsOk returns boolean of this request runs well. | ||
| 239 | // HTTP 200 means ok. | ||
| 215 | func (output *BeegoOutput) IsOk(status int) bool { | 240 | func (output *BeegoOutput) IsOk(status int) bool { |
| 216 | return output.Status == 200 | 241 | return output.Status == 200 |
| 217 | } | 242 | } |
| 218 | 243 | ||
| 244 | // IsSuccessful returns boolean of this request runs successfully. | ||
| 245 | // HTTP 2xx means ok. | ||
| 219 | func (output *BeegoOutput) IsSuccessful(status int) bool { | 246 | func (output *BeegoOutput) IsSuccessful(status int) bool { |
| 220 | return output.Status >= 200 && output.Status < 300 | 247 | return output.Status >= 200 && output.Status < 300 |
| 221 | } | 248 | } |
| 222 | 249 | ||
| 250 | // IsRedirect returns boolean of this request is redirection header. | ||
| 251 | // HTTP 301,302,307 means redirection. | ||
| 223 | func (output *BeegoOutput) IsRedirect(status int) bool { | 252 | func (output *BeegoOutput) IsRedirect(status int) bool { |
| 224 | return output.Status == 301 || output.Status == 302 || output.Status == 303 || output.Status == 307 | 253 | return output.Status == 301 || output.Status == 302 || output.Status == 303 || output.Status == 307 |
| 225 | } | 254 | } |
| 226 | 255 | ||
| 256 | // IsForbidden returns boolean of this request is forbidden. | ||
| 257 | // HTTP 403 means forbidden. | ||
| 227 | func (output *BeegoOutput) IsForbidden(status int) bool { | 258 | func (output *BeegoOutput) IsForbidden(status int) bool { |
| 228 | return output.Status == 403 | 259 | return output.Status == 403 |
| 229 | } | 260 | } |
| 230 | 261 | ||
| 262 | // IsNotFound returns boolean of this request is not found. | ||
| 263 | // HTTP 404 means forbidden. | ||
| 231 | func (output *BeegoOutput) IsNotFound(status int) bool { | 264 | func (output *BeegoOutput) IsNotFound(status int) bool { |
| 232 | return output.Status == 404 | 265 | return output.Status == 404 |
| 233 | } | 266 | } |
| 234 | 267 | ||
| 268 | // IsClient returns boolean of this request client sends error data. | ||
| 269 | // HTTP 4xx means forbidden. | ||
| 235 | func (output *BeegoOutput) IsClientError(status int) bool { | 270 | func (output *BeegoOutput) IsClientError(status int) bool { |
| 236 | return output.Status >= 400 && output.Status < 500 | 271 | return output.Status >= 400 && output.Status < 500 |
| 237 | } | 272 | } |
| 238 | 273 | ||
| 274 | // IsServerError returns boolean of this server handler errors. | ||
| 275 | // HTTP 5xx means server internal error. | ||
| 239 | func (output *BeegoOutput) IsServerError(status int) bool { | 276 | func (output *BeegoOutput) IsServerError(status int) bool { |
| 240 | return output.Status >= 500 && output.Status < 600 | 277 | return output.Status >= 500 && output.Status < 600 |
| 241 | } | 278 | } |
| ... | @@ -254,6 +291,7 @@ func stringsToJson(str string) string { | ... | @@ -254,6 +291,7 @@ func stringsToJson(str string) string { |
| 254 | return jsons | 291 | return jsons |
| 255 | } | 292 | } |
| 256 | 293 | ||
| 294 | // Sessions sets session item value with given key. | ||
| 257 | func (output *BeegoOutput) Session(name interface{}, value interface{}) { | 295 | func (output *BeegoOutput) Session(name interface{}, value interface{}) { |
| 258 | output.Context.Input.CruSession.Set(name, value) | 296 | output.Context.Input.CruSession.Set(name, value) |
| 259 | } | 297 | } | ... | ... |
-
Please register or sign in to post a comment