2014-08-18 08:41:43 +00:00
|
|
|
// Copyright 2014 beego Author. All Rights Reserved.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2012-12-19 08:20:55 +00:00
|
|
|
package beego
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2013-04-21 03:28:20 +00:00
|
|
|
"errors"
|
2012-12-19 08:20:55 +00:00
|
|
|
"html/template"
|
2013-04-16 10:20:55 +00:00
|
|
|
"io"
|
2012-12-19 08:20:55 +00:00
|
|
|
"io/ioutil"
|
2013-04-09 15:33:48 +00:00
|
|
|
"mime/multipart"
|
2012-12-19 08:20:55 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2013-04-16 10:20:55 +00:00
|
|
|
"os"
|
2013-11-10 15:05:07 +00:00
|
|
|
"reflect"
|
2012-12-19 08:20:55 +00:00
|
|
|
"strconv"
|
2013-04-09 16:24:28 +00:00
|
|
|
"strings"
|
2013-12-03 13:37:39 +00:00
|
|
|
|
2014-09-04 00:47:09 +00:00
|
|
|
"github.com/astaxie/beego/context"
|
|
|
|
"github.com/astaxie/beego/session"
|
2012-12-19 08:20:55 +00:00
|
|
|
)
|
|
|
|
|
2014-05-16 17:26:59 +00:00
|
|
|
//commonly used mime-types
|
|
|
|
const (
|
|
|
|
applicationJson = "application/json"
|
2014-06-06 16:56:34 +00:00
|
|
|
applicationXml = "application/xml"
|
2014-05-16 17:26:59 +00:00
|
|
|
textXml = "text/xml"
|
|
|
|
)
|
|
|
|
|
2013-12-17 00:53:15 +00:00
|
|
|
var (
|
2013-12-20 13:16:26 +00:00
|
|
|
// custom error when user stop request handler manually.
|
2014-06-09 09:33:04 +00:00
|
|
|
USERSTOPRUN = errors.New("User stop run")
|
|
|
|
GlobalControllerRouter map[string][]ControllerComments = make(map[string][]ControllerComments) //pkgpath+controller:comments
|
2013-12-17 00:53:15 +00:00
|
|
|
)
|
|
|
|
|
2014-06-09 02:11:37 +00:00
|
|
|
// store the comment for the controller method
|
|
|
|
type ControllerComments struct {
|
2014-06-09 09:46:13 +00:00
|
|
|
Method string
|
|
|
|
Router string
|
|
|
|
AllowHTTPMethods []string
|
|
|
|
Params []map[string]string
|
2014-06-09 02:11:37 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// Controller defines some basic http request handler operations, such as
|
|
|
|
// http context, template and view, session and xsrf.
|
2012-12-19 08:20:55 +00:00
|
|
|
type Controller struct {
|
2013-12-17 03:19:18 +00:00
|
|
|
Ctx *context.Context
|
|
|
|
Data map[interface{}]interface{}
|
|
|
|
controllerName string
|
|
|
|
actionName string
|
|
|
|
TplNames string
|
|
|
|
Layout string
|
2013-12-23 16:13:24 +00:00
|
|
|
LayoutSections map[string]string // the key is the section name and the value is the template name
|
2013-12-17 03:19:18 +00:00
|
|
|
TplExt string
|
|
|
|
_xsrf_token string
|
|
|
|
gotofunc string
|
|
|
|
CruSession session.SessionStore
|
|
|
|
XSRFExpire int
|
|
|
|
AppController interface{}
|
2014-05-08 02:51:29 +00:00
|
|
|
EnableRender bool
|
2014-05-16 16:12:25 +00:00
|
|
|
EnableXSRF bool
|
2014-06-09 02:11:37 +00:00
|
|
|
methodMapping map[string]func() //method:routertree
|
2012-12-19 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// ControllerInterface is an interface to uniform all controller handler.
|
2012-12-19 08:20:55 +00:00
|
|
|
type ControllerInterface interface {
|
2013-12-17 03:19:18 +00:00
|
|
|
Init(ct *context.Context, controllerName, actionName string, app interface{})
|
2012-12-19 08:20:55 +00:00
|
|
|
Prepare()
|
|
|
|
Get()
|
|
|
|
Post()
|
|
|
|
Delete()
|
|
|
|
Put()
|
|
|
|
Head()
|
|
|
|
Patch()
|
|
|
|
Options()
|
|
|
|
Finish()
|
|
|
|
Render() error
|
2013-12-18 13:32:25 +00:00
|
|
|
XsrfToken() string
|
|
|
|
CheckXsrfCookie() bool
|
2014-06-10 03:02:41 +00:00
|
|
|
HandlerFunc(fn string) bool
|
2014-06-08 12:24:01 +00:00
|
|
|
URLMapping()
|
2012-12-19 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// Init generates default values of controller operations.
|
2013-12-17 03:19:18 +00:00
|
|
|
func (c *Controller) Init(ctx *context.Context, controllerName, actionName string, app interface{}) {
|
2012-12-19 08:20:55 +00:00
|
|
|
c.Layout = ""
|
|
|
|
c.TplNames = ""
|
2013-12-17 03:19:18 +00:00
|
|
|
c.controllerName = controllerName
|
|
|
|
c.actionName = actionName
|
2012-12-19 08:20:55 +00:00
|
|
|
c.Ctx = ctx
|
|
|
|
c.TplExt = "tpl"
|
2013-09-28 15:30:36 +00:00
|
|
|
c.AppController = app
|
2014-05-08 02:51:29 +00:00
|
|
|
c.EnableRender = true
|
2014-05-16 16:12:25 +00:00
|
|
|
c.EnableXSRF = true
|
2014-02-08 02:42:34 +00:00
|
|
|
c.Data = ctx.Input.Data
|
2014-06-09 02:11:37 +00:00
|
|
|
c.methodMapping = make(map[string]func())
|
2012-12-19 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// Prepare runs after Init before request function execution.
|
2012-12-19 08:20:55 +00:00
|
|
|
func (c *Controller) Prepare() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// Finish runs after request function execution.
|
2012-12-19 08:20:55 +00:00
|
|
|
func (c *Controller) Finish() {
|
2013-05-08 05:56:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// Get adds a request function to handle GET request.
|
2012-12-19 08:20:55 +00:00
|
|
|
func (c *Controller) Get() {
|
|
|
|
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// Post adds a request function to handle POST request.
|
2012-12-19 08:20:55 +00:00
|
|
|
func (c *Controller) Post() {
|
|
|
|
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// Delete adds a request function to handle DELETE request.
|
2012-12-19 08:20:55 +00:00
|
|
|
func (c *Controller) Delete() {
|
|
|
|
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// Put adds a request function to handle PUT request.
|
2012-12-19 08:20:55 +00:00
|
|
|
func (c *Controller) Put() {
|
|
|
|
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// Head adds a request function to handle HEAD request.
|
2012-12-19 08:20:55 +00:00
|
|
|
func (c *Controller) Head() {
|
|
|
|
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// Patch adds a request function to handle PATCH request.
|
2012-12-19 08:20:55 +00:00
|
|
|
func (c *Controller) Patch() {
|
|
|
|
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// Options adds a request function to handle OPTIONS request.
|
2012-12-19 08:20:55 +00:00
|
|
|
func (c *Controller) Options() {
|
|
|
|
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
|
|
|
|
}
|
|
|
|
|
2014-06-08 12:24:01 +00:00
|
|
|
// call function fn
|
2014-06-10 03:02:41 +00:00
|
|
|
func (c *Controller) HandlerFunc(fnname string) bool {
|
2014-06-09 02:11:37 +00:00
|
|
|
if v, ok := c.methodMapping[fnname]; ok {
|
2014-06-08 12:24:01 +00:00
|
|
|
v()
|
2014-06-10 03:02:41 +00:00
|
|
|
return true
|
2014-06-09 02:11:37 +00:00
|
|
|
} else {
|
2014-06-10 03:02:41 +00:00
|
|
|
return false
|
2014-06-08 12:24:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// URLMapping register the internal Controller router.
|
|
|
|
func (c *Controller) URLMapping() {
|
|
|
|
}
|
|
|
|
|
2014-06-09 02:11:37 +00:00
|
|
|
func (c *Controller) Mapping(method string, fn func()) {
|
|
|
|
c.methodMapping[method] = fn
|
2014-06-08 12:24:01 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// Render sends the response with rendered template bytes as text/html type.
|
2012-12-19 08:20:55 +00:00
|
|
|
func (c *Controller) Render() error {
|
2014-05-08 02:51:29 +00:00
|
|
|
if !c.EnableRender {
|
2014-02-06 16:38:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
2013-03-14 13:17:37 +00:00
|
|
|
rb, err := c.RenderBytes()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
2013-09-09 16:00:11 +00:00
|
|
|
c.Ctx.Output.Header("Content-Type", "text/html; charset=utf-8")
|
|
|
|
c.Ctx.Output.Body(rb)
|
2013-03-14 13:17:37 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// RenderString returns the rendered template string. Do not send out response.
|
2013-04-03 15:37:59 +00:00
|
|
|
func (c *Controller) RenderString() (string, error) {
|
|
|
|
b, e := c.RenderBytes()
|
|
|
|
return string(b), e
|
|
|
|
}
|
|
|
|
|
2013-12-28 12:14:36 +00:00
|
|
|
// RenderBytes returns the bytes of rendered template string. Do not send out response.
|
2013-03-14 13:17:37 +00:00
|
|
|
func (c *Controller) RenderBytes() ([]byte, error) {
|
2012-12-19 08:20:55 +00:00
|
|
|
//if the controller has set layout, then first get the tplname's content set the content to the layout
|
|
|
|
if c.Layout != "" {
|
|
|
|
if c.TplNames == "" {
|
2013-12-20 14:35:16 +00:00
|
|
|
c.TplNames = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt
|
2012-12-19 08:20:55 +00:00
|
|
|
}
|
2013-04-02 15:57:15 +00:00
|
|
|
if RunMode == "dev" {
|
2013-04-03 15:37:59 +00:00
|
|
|
BuildTemplate(ViewsPath)
|
2013-04-02 15:57:15 +00:00
|
|
|
}
|
2012-12-19 08:20:55 +00:00
|
|
|
newbytes := bytes.NewBufferString("")
|
2013-09-12 09:20:32 +00:00
|
|
|
if _, ok := BeeTemplates[c.TplNames]; !ok {
|
2013-12-20 14:35:16 +00:00
|
|
|
panic("can't find templatefile in the path:" + c.TplNames)
|
2013-04-21 03:28:20 +00:00
|
|
|
}
|
2013-09-12 09:20:32 +00:00
|
|
|
err := BeeTemplates[c.TplNames].ExecuteTemplate(newbytes, c.TplNames, c.Data)
|
2013-09-12 05:44:12 +00:00
|
|
|
if err != nil {
|
2013-09-12 07:24:08 +00:00
|
|
|
Trace("template Execute err:", err)
|
2013-12-15 18:17:27 +00:00
|
|
|
return nil, err
|
2013-09-12 05:44:12 +00:00
|
|
|
}
|
2012-12-19 08:20:55 +00:00
|
|
|
tplcontent, _ := ioutil.ReadAll(newbytes)
|
|
|
|
c.Data["LayoutContent"] = template.HTML(string(tplcontent))
|
2013-12-23 16:13:24 +00:00
|
|
|
|
|
|
|
if c.LayoutSections != nil {
|
|
|
|
for sectionName, sectionTpl := range c.LayoutSections {
|
2014-01-11 09:01:33 +00:00
|
|
|
if sectionTpl == "" {
|
2013-12-23 16:13:24 +00:00
|
|
|
c.Data[sectionName] = ""
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
sectionBytes := bytes.NewBufferString("")
|
|
|
|
err = BeeTemplates[sectionTpl].ExecuteTemplate(sectionBytes, sectionTpl, c.Data)
|
|
|
|
if err != nil {
|
|
|
|
Trace("template Execute err:", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sectionContent, _ := ioutil.ReadAll(sectionBytes)
|
|
|
|
c.Data[sectionName] = template.HTML(string(sectionContent))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-14 13:17:37 +00:00
|
|
|
ibytes := bytes.NewBufferString("")
|
2013-09-12 09:20:32 +00:00
|
|
|
err = BeeTemplates[c.Layout].ExecuteTemplate(ibytes, c.Layout, c.Data)
|
2012-12-19 08:20:55 +00:00
|
|
|
if err != nil {
|
|
|
|
Trace("template Execute err:", err)
|
2013-12-15 18:17:27 +00:00
|
|
|
return nil, err
|
2012-12-19 08:20:55 +00:00
|
|
|
}
|
2013-03-14 13:17:37 +00:00
|
|
|
icontent, _ := ioutil.ReadAll(ibytes)
|
|
|
|
return icontent, nil
|
2012-12-19 08:20:55 +00:00
|
|
|
} else {
|
|
|
|
if c.TplNames == "" {
|
2013-12-20 14:35:16 +00:00
|
|
|
c.TplNames = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt
|
2012-12-19 08:20:55 +00:00
|
|
|
}
|
2013-04-02 15:57:15 +00:00
|
|
|
if RunMode == "dev" {
|
2013-04-03 15:37:59 +00:00
|
|
|
BuildTemplate(ViewsPath)
|
2013-04-02 15:57:15 +00:00
|
|
|
}
|
2013-03-14 13:17:37 +00:00
|
|
|
ibytes := bytes.NewBufferString("")
|
2013-09-12 09:20:32 +00:00
|
|
|
if _, ok := BeeTemplates[c.TplNames]; !ok {
|
2013-12-20 14:35:16 +00:00
|
|
|
panic("can't find templatefile in the path:" + c.TplNames)
|
2013-04-21 03:28:20 +00:00
|
|
|
}
|
2013-09-12 09:20:32 +00:00
|
|
|
err := BeeTemplates[c.TplNames].ExecuteTemplate(ibytes, c.TplNames, c.Data)
|
2012-12-19 08:20:55 +00:00
|
|
|
if err != nil {
|
2013-09-12 09:20:32 +00:00
|
|
|
Trace("template Execute err:", err)
|
2013-12-15 18:17:27 +00:00
|
|
|
return nil, err
|
2012-12-19 08:20:55 +00:00
|
|
|
}
|
2013-03-14 13:17:37 +00:00
|
|
|
icontent, _ := ioutil.ReadAll(ibytes)
|
|
|
|
return icontent, nil
|
2012-12-19 08:20:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// Redirect sends the redirection response to url with status code.
|
2012-12-19 08:20:55 +00:00
|
|
|
func (c *Controller) Redirect(url string, code int) {
|
|
|
|
c.Ctx.Redirect(code, url)
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// Aborts stops controller handler and show the error data if code is defined in ErrorMap or code string.
|
2013-05-06 16:17:25 +00:00
|
|
|
func (c *Controller) Abort(code string) {
|
2013-12-16 14:54:29 +00:00
|
|
|
status, err := strconv.Atoi(code)
|
2015-02-26 16:12:10 +00:00
|
|
|
if err != nil {
|
|
|
|
status = 200
|
2013-12-16 14:54:29 +00:00
|
|
|
}
|
2015-02-26 16:12:10 +00:00
|
|
|
c.CustomAbort(status, code)
|
2013-12-16 14:54:29 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 09:26:22 +00:00
|
|
|
// CustomAbort stops controller handler and show the error data, it's similar Aborts, but support status code and body.
|
|
|
|
func (c *Controller) CustomAbort(status int, body string) {
|
2015-02-26 16:12:10 +00:00
|
|
|
c.Ctx.ResponseWriter.WriteHeader(status)
|
|
|
|
// first panic from ErrorMaps, is is user defined error functions.
|
|
|
|
if _, ok := ErrorMaps[body]; ok {
|
|
|
|
panic(body)
|
|
|
|
}
|
|
|
|
// last panic user string
|
|
|
|
c.Ctx.ResponseWriter.Write([]byte(body))
|
|
|
|
panic(USERSTOPRUN)
|
2014-08-13 09:26:22 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// StopRun makes panic of USERSTOPRUN error and go to recover function if defined.
|
2013-12-16 14:54:29 +00:00
|
|
|
func (c *Controller) StopRun() {
|
2013-12-17 00:53:15 +00:00
|
|
|
panic(USERSTOPRUN)
|
2013-05-06 16:17:25 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// UrlFor does another controller handler in this request function.
|
|
|
|
// it goes to this controller method if endpoint is not clear.
|
2014-12-17 07:52:48 +00:00
|
|
|
func (c *Controller) UrlFor(endpoint string, values ...interface{}) string {
|
2013-11-10 15:05:07 +00:00
|
|
|
if len(endpoint) <= 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if endpoint[0] == '.' {
|
2013-12-20 14:35:16 +00:00
|
|
|
return UrlFor(reflect.Indirect(reflect.ValueOf(c.AppController)).Type().Name()+endpoint, values...)
|
2013-11-10 15:05:07 +00:00
|
|
|
} else {
|
|
|
|
return UrlFor(endpoint, values...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// ServeJson sends a json response with encoding charset.
|
2013-08-06 08:37:41 +00:00
|
|
|
func (c *Controller) ServeJson(encoding ...bool) {
|
2013-09-09 16:00:11 +00:00
|
|
|
var hasIndent bool
|
|
|
|
var hasencoding bool
|
2013-08-16 07:49:16 +00:00
|
|
|
if RunMode == "prod" {
|
2013-09-09 16:00:11 +00:00
|
|
|
hasIndent = false
|
2013-08-16 07:49:16 +00:00
|
|
|
} else {
|
2013-09-09 16:00:11 +00:00
|
|
|
hasIndent = true
|
2013-08-16 07:49:16 +00:00
|
|
|
}
|
2013-08-06 08:37:41 +00:00
|
|
|
if len(encoding) > 0 && encoding[0] == true {
|
2013-09-09 16:00:11 +00:00
|
|
|
hasencoding = true
|
2013-08-06 08:37:41 +00:00
|
|
|
}
|
2013-09-09 16:00:11 +00:00
|
|
|
c.Ctx.Output.Json(c.Data["json"], hasIndent, hasencoding)
|
2012-12-19 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2013-12-22 07:09:33 +00:00
|
|
|
// ServeJsonp sends a jsonp response.
|
2013-06-13 09:10:35 +00:00
|
|
|
func (c *Controller) ServeJsonp() {
|
2013-09-09 16:00:11 +00:00
|
|
|
var hasIndent bool
|
2013-08-16 07:49:16 +00:00
|
|
|
if RunMode == "prod" {
|
2013-09-09 16:00:11 +00:00
|
|
|
hasIndent = false
|
2013-08-16 07:49:16 +00:00
|
|
|
} else {
|
2013-09-09 16:00:11 +00:00
|
|
|
hasIndent = true
|
2013-06-13 09:10:35 +00:00
|
|
|
}
|
2013-09-09 16:00:11 +00:00
|
|
|
c.Ctx.Output.Jsonp(c.Data["jsonp"], hasIndent)
|
2013-06-13 09:10:35 +00:00
|
|
|
}
|
|
|
|
|
2013-12-22 07:09:33 +00:00
|
|
|
// ServeXml sends xml response.
|
2012-12-19 08:20:55 +00:00
|
|
|
func (c *Controller) ServeXml() {
|
2013-09-09 16:00:11 +00:00
|
|
|
var hasIndent bool
|
2013-08-16 07:49:16 +00:00
|
|
|
if RunMode == "prod" {
|
2013-09-09 16:00:11 +00:00
|
|
|
hasIndent = false
|
2013-08-16 07:49:16 +00:00
|
|
|
} else {
|
2013-09-09 16:00:11 +00:00
|
|
|
hasIndent = true
|
2013-08-16 07:49:16 +00:00
|
|
|
}
|
2013-09-09 16:00:11 +00:00
|
|
|
c.Ctx.Output.Xml(c.Data["xml"], hasIndent)
|
2012-12-19 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2014-05-16 17:26:59 +00:00
|
|
|
// ServeFormatted serve Xml OR Json, depending on the value of the Accept header
|
|
|
|
func (c *Controller) ServeFormatted() {
|
|
|
|
accept := c.Ctx.Input.Header("Accept")
|
|
|
|
switch accept {
|
|
|
|
case applicationJson:
|
|
|
|
c.ServeJson()
|
|
|
|
case applicationXml, textXml:
|
|
|
|
c.ServeXml()
|
|
|
|
default:
|
|
|
|
c.ServeJson()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// Input returns the input data map from POST or PUT request body and query string.
|
2012-12-19 08:20:55 +00:00
|
|
|
func (c *Controller) Input() url.Values {
|
2014-04-10 14:20:46 +00:00
|
|
|
if c.Ctx.Request.Form == nil {
|
2013-04-09 13:49:17 +00:00
|
|
|
c.Ctx.Request.ParseForm()
|
|
|
|
}
|
2012-12-19 08:20:55 +00:00
|
|
|
return c.Ctx.Request.Form
|
|
|
|
}
|
2013-01-01 15:11:15 +00:00
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// ParseForm maps input data map to obj struct.
|
2013-07-25 14:26:31 +00:00
|
|
|
func (c *Controller) ParseForm(obj interface{}) error {
|
|
|
|
return ParseForm(c.Input(), obj)
|
|
|
|
}
|
|
|
|
|
2014-12-19 07:28:18 +00:00
|
|
|
// GetString returns the input value by key string or the default value while it's present and input is blank
|
2014-12-17 07:23:11 +00:00
|
|
|
func (c *Controller) GetString(key string, def ...string) string {
|
|
|
|
var defv string
|
|
|
|
if len(def) > 0 {
|
|
|
|
defv = def[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := c.Ctx.Input.Query(key); v != "" {
|
|
|
|
return v
|
|
|
|
} else {
|
|
|
|
return defv
|
|
|
|
}
|
2013-04-09 13:49:17 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 07:28:18 +00:00
|
|
|
// GetStrings returns the input string slice by key string or the default value while it's present and input is blank
|
2013-12-20 13:16:26 +00:00
|
|
|
// it's designed for multi-value input field such as checkbox(input[type=checkbox]), multi-selection.
|
2014-12-17 07:23:11 +00:00
|
|
|
func (c *Controller) GetStrings(key string, def ...[]string) []string {
|
|
|
|
var defv []string
|
|
|
|
if len(def) > 0 {
|
|
|
|
defv = def[0]
|
|
|
|
}
|
|
|
|
|
2014-02-26 07:02:58 +00:00
|
|
|
f := c.Input()
|
|
|
|
if f == nil {
|
2014-12-17 07:23:11 +00:00
|
|
|
return defv
|
2013-06-17 07:12:35 +00:00
|
|
|
}
|
2014-12-17 07:23:11 +00:00
|
|
|
|
2014-02-26 07:02:58 +00:00
|
|
|
vs := f[key]
|
2013-07-02 01:45:12 +00:00
|
|
|
if len(vs) > 0 {
|
|
|
|
return vs
|
2014-12-17 07:23:11 +00:00
|
|
|
} else {
|
|
|
|
return defv
|
2013-07-02 01:45:12 +00:00
|
|
|
}
|
2013-06-17 07:12:35 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 07:28:18 +00:00
|
|
|
// GetInt returns input as an int or the default value while it's present and input is blank
|
2014-12-17 07:23:11 +00:00
|
|
|
func (c *Controller) GetInt(key string, def ...int) (int, error) {
|
|
|
|
if strv := c.Ctx.Input.Query(key); strv != "" {
|
|
|
|
return strconv.Atoi(strv)
|
2015-04-01 15:31:40 +00:00
|
|
|
} else if len(def) > 0 {
|
|
|
|
return def[0], nil
|
2014-12-17 07:23:11 +00:00
|
|
|
} else {
|
2015-04-01 15:31:40 +00:00
|
|
|
return strconv.Atoi(strv)
|
2014-12-17 07:23:11 +00:00
|
|
|
}
|
2014-08-23 21:33:17 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 07:28:18 +00:00
|
|
|
// GetInt8 return input as an int8 or the default value while it's present and input is blank
|
2014-12-17 07:23:11 +00:00
|
|
|
func (c *Controller) GetInt8(key string, def ...int8) (int8, error) {
|
|
|
|
if strv := c.Ctx.Input.Query(key); strv != "" {
|
|
|
|
i64, err := strconv.ParseInt(strv, 10, 8)
|
|
|
|
i8 := int8(i64)
|
|
|
|
return i8, err
|
2015-04-01 15:31:40 +00:00
|
|
|
} else if len(def) > 0 {
|
|
|
|
return def[0], nil
|
2014-12-17 07:23:11 +00:00
|
|
|
} else {
|
2015-04-01 15:31:40 +00:00
|
|
|
i64, err := strconv.ParseInt(strv, 10, 8)
|
|
|
|
i8 := int8(i64)
|
|
|
|
return i8, err
|
2014-12-17 07:23:11 +00:00
|
|
|
}
|
2014-08-23 21:33:17 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 07:28:18 +00:00
|
|
|
// GetInt16 returns input as an int16 or the default value while it's present and input is blank
|
2014-12-17 07:23:11 +00:00
|
|
|
func (c *Controller) GetInt16(key string, def ...int16) (int16, error) {
|
|
|
|
if strv := c.Ctx.Input.Query(key); strv != "" {
|
|
|
|
i64, err := strconv.ParseInt(strv, 10, 16)
|
|
|
|
i16 := int16(i64)
|
|
|
|
return i16, err
|
2015-04-01 15:31:40 +00:00
|
|
|
} else if len(def) > 0 {
|
|
|
|
return def[0], nil
|
2014-12-17 07:23:11 +00:00
|
|
|
} else {
|
2015-04-01 15:31:40 +00:00
|
|
|
i64, err := strconv.ParseInt(strv, 10, 16)
|
|
|
|
i16 := int16(i64)
|
|
|
|
return i16, err
|
2014-12-17 07:23:11 +00:00
|
|
|
}
|
2014-08-23 21:33:17 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 07:28:18 +00:00
|
|
|
// GetInt32 returns input as an int32 or the default value while it's present and input is blank
|
2014-12-17 07:23:11 +00:00
|
|
|
func (c *Controller) GetInt32(key string, def ...int32) (int32, error) {
|
|
|
|
if strv := c.Ctx.Input.Query(key); strv != "" {
|
|
|
|
i64, err := strconv.ParseInt(c.Ctx.Input.Query(key), 10, 32)
|
|
|
|
i32 := int32(i64)
|
|
|
|
return i32, err
|
2015-04-01 15:31:40 +00:00
|
|
|
} else if len(def) > 0 {
|
|
|
|
return def[0], nil
|
2014-12-17 07:23:11 +00:00
|
|
|
} else {
|
2015-04-01 15:31:40 +00:00
|
|
|
i64, err := strconv.ParseInt(c.Ctx.Input.Query(key), 10, 32)
|
|
|
|
i32 := int32(i64)
|
|
|
|
return i32, err
|
2014-12-17 07:23:11 +00:00
|
|
|
}
|
2014-08-23 21:33:17 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 07:28:18 +00:00
|
|
|
// GetInt64 returns input value as int64 or the default value while it's present and input is blank.
|
2014-12-17 07:23:11 +00:00
|
|
|
func (c *Controller) GetInt64(key string, def ...int64) (int64, error) {
|
|
|
|
if strv := c.Ctx.Input.Query(key); strv != "" {
|
|
|
|
return strconv.ParseInt(strv, 10, 64)
|
2015-04-01 15:31:40 +00:00
|
|
|
} else if len(def) > 0 {
|
|
|
|
return def[0], nil
|
2014-12-17 07:23:11 +00:00
|
|
|
} else {
|
2015-04-01 15:31:40 +00:00
|
|
|
return strconv.ParseInt(strv, 10, 64)
|
2014-12-17 07:23:11 +00:00
|
|
|
}
|
2013-04-09 13:49:17 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 07:28:18 +00:00
|
|
|
// GetBool returns input value as bool or the default value while it's present and input is blank.
|
2014-12-17 07:23:11 +00:00
|
|
|
func (c *Controller) GetBool(key string, def ...bool) (bool, error) {
|
|
|
|
if strv := c.Ctx.Input.Query(key); strv != "" {
|
|
|
|
return strconv.ParseBool(strv)
|
2015-04-01 15:31:40 +00:00
|
|
|
} else if len(def) > 0 {
|
|
|
|
return def[0], nil
|
2014-12-17 07:23:11 +00:00
|
|
|
} else {
|
2015-04-01 15:31:40 +00:00
|
|
|
return strconv.ParseBool(strv)
|
2014-12-17 07:23:11 +00:00
|
|
|
}
|
2013-04-09 13:49:17 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 07:28:18 +00:00
|
|
|
// GetFloat returns input value as float64 or the default value while it's present and input is blank.
|
2014-12-17 07:23:11 +00:00
|
|
|
func (c *Controller) GetFloat(key string, def ...float64) (float64, error) {
|
|
|
|
if strv := c.Ctx.Input.Query(key); strv != "" {
|
2015-04-01 15:31:40 +00:00
|
|
|
return strconv.ParseFloat(strv, 64)
|
|
|
|
} else if len(def) > 0 {
|
|
|
|
return def[0], nil
|
2014-12-17 07:23:11 +00:00
|
|
|
} else {
|
2015-04-01 15:31:40 +00:00
|
|
|
return strconv.ParseFloat(strv, 64)
|
2014-12-17 07:23:11 +00:00
|
|
|
}
|
2013-09-09 16:00:11 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// GetFile returns the file data in file upload field named as key.
|
|
|
|
// it returns the first one of multi-uploaded files.
|
2013-04-09 15:33:48 +00:00
|
|
|
func (c *Controller) GetFile(key string) (multipart.File, *multipart.FileHeader, error) {
|
|
|
|
return c.Ctx.Request.FormFile(key)
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// SaveToFile saves uploaded file to new path.
|
|
|
|
// it only operates the first one of mutil-upload form file field.
|
2013-04-16 10:20:55 +00:00
|
|
|
func (c *Controller) SaveToFile(fromfile, tofile string) error {
|
|
|
|
file, _, err := c.Ctx.Request.FormFile(fromfile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2013-12-20 14:35:16 +00:00
|
|
|
f, err := os.OpenFile(tofile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
2013-04-16 10:20:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
io.Copy(f, file)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// StartSession starts session and load old session data info this controller.
|
2013-05-07 07:36:43 +00:00
|
|
|
func (c *Controller) StartSession() session.SessionStore {
|
|
|
|
if c.CruSession == nil {
|
2013-09-22 03:43:22 +00:00
|
|
|
c.CruSession = c.Ctx.Input.CruSession
|
2013-05-07 07:36:43 +00:00
|
|
|
}
|
|
|
|
return c.CruSession
|
2013-01-01 15:11:15 +00:00
|
|
|
}
|
2013-04-05 15:50:53 +00:00
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// SetSession puts value into session.
|
2013-05-13 15:30:50 +00:00
|
|
|
func (c *Controller) SetSession(name interface{}, value interface{}) {
|
2013-05-07 07:36:43 +00:00
|
|
|
if c.CruSession == nil {
|
|
|
|
c.StartSession()
|
|
|
|
}
|
|
|
|
c.CruSession.Set(name, value)
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// GetSession gets value from session.
|
2013-05-13 15:30:50 +00:00
|
|
|
func (c *Controller) GetSession(name interface{}) interface{} {
|
2013-05-07 07:36:43 +00:00
|
|
|
if c.CruSession == nil {
|
|
|
|
c.StartSession()
|
|
|
|
}
|
|
|
|
return c.CruSession.Get(name)
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// SetSession removes value from session.
|
2013-05-13 15:30:50 +00:00
|
|
|
func (c *Controller) DelSession(name interface{}) {
|
2013-05-07 07:36:43 +00:00
|
|
|
if c.CruSession == nil {
|
|
|
|
c.StartSession()
|
|
|
|
}
|
|
|
|
c.CruSession.Delete(name)
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
2013-07-02 01:45:12 +00:00
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// SessionRegenerateID regenerates session id for this session.
|
|
|
|
// the session data have no changes.
|
2013-11-05 14:07:09 +00:00
|
|
|
func (c *Controller) SessionRegenerateID() {
|
2014-02-27 00:44:31 +00:00
|
|
|
if c.CruSession != nil {
|
|
|
|
c.CruSession.SessionRelease(c.Ctx.ResponseWriter)
|
|
|
|
}
|
2013-11-05 14:07:09 +00:00
|
|
|
c.CruSession = GlobalSessions.SessionRegenerateId(c.Ctx.ResponseWriter, c.Ctx.Request)
|
|
|
|
c.Ctx.Input.CruSession = c.CruSession
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// DestroySession cleans session data and session cookie.
|
2013-08-10 15:58:25 +00:00
|
|
|
func (c *Controller) DestroySession() {
|
2014-01-23 11:28:58 +00:00
|
|
|
c.Ctx.Input.CruSession.Flush()
|
2013-08-10 15:58:25 +00:00
|
|
|
GlobalSessions.SessionDestroy(c.Ctx.ResponseWriter, c.Ctx.Request)
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// IsAjax returns this request is ajax or not.
|
2013-07-02 01:45:12 +00:00
|
|
|
func (c *Controller) IsAjax() bool {
|
2013-09-09 16:00:11 +00:00
|
|
|
return c.Ctx.Input.IsAjax()
|
2013-07-02 01:45:12 +00:00
|
|
|
}
|
2013-07-08 08:17:08 +00:00
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// GetSecureCookie returns decoded cookie value from encoded browser cookie values.
|
2013-09-25 15:05:47 +00:00
|
|
|
func (c *Controller) GetSecureCookie(Secret, key string) (string, bool) {
|
2014-02-22 03:58:53 +00:00
|
|
|
return c.Ctx.GetSecureCookie(Secret, key)
|
2013-09-25 15:05:47 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// SetSecureCookie puts value into cookie after encoded the value.
|
2014-02-22 03:58:53 +00:00
|
|
|
func (c *Controller) SetSecureCookie(Secret, name, value string, others ...interface{}) {
|
|
|
|
c.Ctx.SetSecureCookie(Secret, name, value, others...)
|
2013-09-25 15:05:47 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// XsrfToken creates a xsrf token string and returns.
|
2013-07-08 08:17:08 +00:00
|
|
|
func (c *Controller) XsrfToken() string {
|
|
|
|
if c._xsrf_token == "" {
|
2014-07-24 15:12:21 +00:00
|
|
|
var expire int64
|
|
|
|
if c.XSRFExpire > 0 {
|
|
|
|
expire = int64(c.XSRFExpire)
|
|
|
|
} else {
|
|
|
|
expire = int64(XSRFExpire)
|
2013-07-08 08:17:08 +00:00
|
|
|
}
|
2014-07-24 15:12:21 +00:00
|
|
|
c._xsrf_token = c.Ctx.XsrfToken(XSRFKEY, expire)
|
2013-07-08 08:17:08 +00:00
|
|
|
}
|
|
|
|
return c._xsrf_token
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// CheckXsrfCookie checks xsrf token in this request is valid or not.
|
|
|
|
// the token can provided in request header "X-Xsrftoken" and "X-CsrfToken"
|
|
|
|
// or in form field value named as "_xsrf".
|
2013-07-08 08:17:08 +00:00
|
|
|
func (c *Controller) CheckXsrfCookie() bool {
|
2014-05-16 16:12:25 +00:00
|
|
|
if !c.EnableXSRF {
|
|
|
|
return true
|
|
|
|
}
|
2014-07-24 15:12:21 +00:00
|
|
|
return c.Ctx.CheckXsrfCookie()
|
2013-07-08 08:17:08 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// XsrfFormHtml writes an input field contains xsrf token value.
|
2013-07-08 08:17:08 +00:00
|
|
|
func (c *Controller) XsrfFormHtml() string {
|
|
|
|
return "<input type=\"hidden\" name=\"_xsrf\" value=\"" +
|
2013-12-20 14:35:16 +00:00
|
|
|
c._xsrf_token + "\"/>"
|
2013-07-08 08:17:08 +00:00
|
|
|
}
|
2013-07-08 09:35:09 +00:00
|
|
|
|
2013-12-20 13:16:26 +00:00
|
|
|
// GetControllerAndAction gets the executing controller name and action name.
|
2013-12-17 12:39:25 +00:00
|
|
|
func (c *Controller) GetControllerAndAction() (controllerName, actionName string) {
|
|
|
|
return c.controllerName, c.actionName
|
2013-07-08 09:35:09 +00:00
|
|
|
}
|