diff --git a/config.go b/config.go index ae334cd7..6f87fed1 100644 --- a/config.go +++ b/config.go @@ -141,7 +141,7 @@ var ( ) type beegoAppConfig struct { - innerConfig config.ConfigContainer + innerConfig config.Configer } func newAppConfig(AppConfigProvider, AppConfigPath string) (*beegoAppConfig, error) { diff --git a/context/context.go b/context/context.go index f6aa85d6..fb9c6d96 100644 --- a/context/context.go +++ b/context/context.go @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +// Package context provide the context utils // Usage: // // import "github.com/astaxie/beego/context" @@ -34,14 +35,14 @@ import ( "github.com/astaxie/beego/utils" ) -// Http request context struct including BeegoInput, BeegoOutput, http.Request and http.ResponseWriter. +// Context Http request context struct including BeegoInput, BeegoOutput, http.Request and http.ResponseWriter. // BeegoInput and BeegoOutput provides some api to operate request and response more easily. type Context struct { Input *BeegoInput Output *BeegoOutput Request *http.Request ResponseWriter http.ResponseWriter - _xsrf_token string + _xsrfToken string } // Redirect does redirection to localurl with http header status code. @@ -58,25 +59,25 @@ func (ctx *Context) Abort(status int, body string) { panic(body) } -// Write string to response body. +// WriteString Write string to response body. // it sends response body. func (ctx *Context) WriteString(content string) { ctx.ResponseWriter.Write([]byte(content)) } -// Get cookie from request by a given key. +// GetCookie Get cookie from request by a given key. // It's alias of BeegoInput.Cookie. func (ctx *Context) GetCookie(key string) string { return ctx.Input.Cookie(key) } -// Set cookie for response. +// SetCookie Set cookie for response. // It's alias of BeegoOutput.Cookie. func (ctx *Context) SetCookie(name string, value string, others ...interface{}) { ctx.Output.Cookie(name, value, others...) } -// Get secure cookie from request by a given key. +// GetSecureCookie Get secure cookie from request by a given key. func (ctx *Context) GetSecureCookie(Secret, key string) (string, bool) { val := ctx.Input.Cookie(key) if val == "" { @@ -103,7 +104,7 @@ func (ctx *Context) GetSecureCookie(Secret, key string) (string, bool) { return string(res), true } -// Set Secure cookie for response. +// SetSecureCookie Set Secure cookie for response. func (ctx *Context) SetSecureCookie(Secret, name, value string, others ...interface{}) { vs := base64.URLEncoding.EncodeToString([]byte(value)) timestamp := strconv.FormatInt(time.Now().UnixNano(), 10) @@ -114,23 +115,23 @@ func (ctx *Context) SetSecureCookie(Secret, name, value string, others ...interf ctx.Output.Cookie(name, cookie, others...) } -// XsrfToken creates a xsrf token string and returns. -func (ctx *Context) XsrfToken(key string, expire int64) string { - if ctx._xsrf_token == "" { +// XSRFToken creates a xsrf token string and returns. +func (ctx *Context) XSRFToken(key string, expire int64) string { + if ctx._xsrfToken == "" { token, ok := ctx.GetSecureCookie(key, "_xsrf") if !ok { token = string(utils.RandomCreateBytes(32)) ctx.SetSecureCookie(key, "_xsrf", token, expire) } - ctx._xsrf_token = token + ctx._xsrfToken = token } - return ctx._xsrf_token + return ctx._xsrfToken } -// CheckXsrfCookie checks xsrf token in this request is valid or not. +// 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". -func (ctx *Context) CheckXsrfCookie() bool { +func (ctx *Context) CheckXSRFCookie() bool { token := ctx.Input.Query("_xsrf") if token == "" { token = ctx.Request.Header.Get("X-Xsrftoken") @@ -142,7 +143,7 @@ func (ctx *Context) CheckXsrfCookie() bool { ctx.Abort(403, "'_xsrf' argument missing from POST") return false } - if ctx._xsrf_token != token { + if ctx._xsrfToken != token { ctx.Abort(403, "XSRF cookie does not match POST argument") return false } diff --git a/context/input.go b/context/input.go index c8a8aaa9..2cf3235f 100644 --- a/context/input.go +++ b/context/input.go @@ -31,9 +31,9 @@ import ( // Regexes for checking the accept headers // TODO make sure these are correct var ( - acceptsHtmlRegex = regexp.MustCompile(`(text/html|application/xhtml\+xml)(?:,|$)`) - acceptsXmlRegex = regexp.MustCompile(`(application/xml|text/xml)(?:,|$)`) - acceptsJsonRegex = regexp.MustCompile(`(application/json)(?:,|$)`) + acceptsHTMLRegex = regexp.MustCompile(`(text/html|application/xhtml\+xml)(?:,|$)`) + acceptsXMLRegex = regexp.MustCompile(`(application/xml|text/xml)(?:,|$)`) + acceptsJSONRegex = regexp.MustCompile(`(application/json)(?:,|$)`) ) // BeegoInput operates the http request header, data, cookie and body. @@ -62,13 +62,13 @@ func (input *BeegoInput) Protocol() string { return input.Request.Proto } -// Uri returns full request url with query string, fragment. -func (input *BeegoInput) Uri() string { +// URI returns full request url with query string, fragment. +func (input *BeegoInput) URI() string { return input.Request.RequestURI } -// Url returns request url path (without query string, fragment). -func (input *BeegoInput) Url() string { +// URL returns request url path (without query string, fragment). +func (input *BeegoInput) URL() string { return input.Request.URL.Path } @@ -117,37 +117,37 @@ func (input *BeegoInput) Is(method string) bool { return input.Method() == method } -// Is this a GET method request? +// IsGet Is this a GET method request? func (input *BeegoInput) IsGet() bool { return input.Is("GET") } -// Is this a POST method request? +// IsPost Is this a POST method request? func (input *BeegoInput) IsPost() bool { return input.Is("POST") } -// Is this a Head method request? +// IsHead Is this a Head method request? func (input *BeegoInput) IsHead() bool { return input.Is("HEAD") } -// Is this a OPTIONS method request? +// IsOptions Is this a OPTIONS method request? func (input *BeegoInput) IsOptions() bool { return input.Is("OPTIONS") } -// Is this a PUT method request? +// IsPut Is this a PUT method request? func (input *BeegoInput) IsPut() bool { return input.Is("PUT") } -// Is this a DELETE method request? +// IsDelete Is this a DELETE method request? func (input *BeegoInput) IsDelete() bool { return input.Is("DELETE") } -// Is this a PATCH method request? +// IsPatch Is this a PATCH method request? func (input *BeegoInput) IsPatch() bool { return input.Is("PATCH") } @@ -172,19 +172,19 @@ func (input *BeegoInput) IsUpload() bool { return strings.Contains(input.Header("Content-Type"), "multipart/form-data") } -// Checks if request accepts html response -func (input *BeegoInput) AcceptsHtml() bool { - return acceptsHtmlRegex.MatchString(input.Header("Accept")) +// AcceptsHTML Checks if request accepts html response +func (input *BeegoInput) AcceptsHTML() bool { + return acceptsHTMLRegex.MatchString(input.Header("Accept")) } -// Checks if request accepts xml response -func (input *BeegoInput) AcceptsXml() bool { - return acceptsXmlRegex.MatchString(input.Header("Accept")) +// AcceptsXML Checks if request accepts xml response +func (input *BeegoInput) AcceptsXML() bool { + return acceptsXMLRegex.MatchString(input.Header("Accept")) } -// Checks if request accepts json response -func (input *BeegoInput) AcceptsJson() bool { - return acceptsJsonRegex.MatchString(input.Header("Accept")) +// AcceptsJSON Checks if request accepts json response +func (input *BeegoInput) AcceptsJSON() bool { + return acceptsJSONRegex.MatchString(input.Header("Accept")) } // IP returns request client ip. @@ -314,7 +314,7 @@ func (input *BeegoInput) SetData(key, val interface{}) { input.Data[key] = val } -// parseForm or parseMultiForm based on Content-type +// ParseFormOrMulitForm parseForm or parseMultiForm based on Content-type func (input *BeegoInput) ParseFormOrMulitForm(maxMemory int64) error { // Parse the body depending on the content type. if strings.Contains(input.Header("Content-Type"), "multipart/form-data") { diff --git a/context/output.go b/context/output.go index 7edde552..743beb96 100644 --- a/context/output.go +++ b/context/output.go @@ -54,7 +54,7 @@ func (output *BeegoOutput) Header(key, val string) { // if EnableGzip, compress content string. // it sends out response body directly. func (output *BeegoOutput) Body(content []byte) { - output_writer := output.Context.ResponseWriter.(io.Writer) + outputWriter := output.Context.ResponseWriter.(io.Writer) if output.EnableGzip == true && output.Context.Input.Header("Accept-Encoding") != "" { splitted := strings.SplitN(output.Context.Input.Header("Accept-Encoding"), ",", -1) encodings := make([]string, len(splitted)) @@ -65,12 +65,12 @@ func (output *BeegoOutput) Body(content []byte) { for _, val := range encodings { if val == "gzip" { output.Header("Content-Encoding", "gzip") - output_writer, _ = gzip.NewWriterLevel(output.Context.ResponseWriter, gzip.BestSpeed) + outputWriter, _ = gzip.NewWriterLevel(output.Context.ResponseWriter, gzip.BestSpeed) break } else if val == "deflate" { output.Header("Content-Encoding", "deflate") - output_writer, _ = flate.NewWriter(output.Context.ResponseWriter, flate.BestSpeed) + outputWriter, _ = flate.NewWriter(output.Context.ResponseWriter, flate.BestSpeed) break } } @@ -85,12 +85,12 @@ func (output *BeegoOutput) Body(content []byte) { output.Status = 0 } - output_writer.Write(content) - switch output_writer.(type) { + outputWriter.Write(content) + switch outputWriter.(type) { case *gzip.Writer: - output_writer.(*gzip.Writer).Close() + outputWriter.(*gzip.Writer).Close() case *flate.Writer: - output_writer.(*flate.Writer).Close() + outputWriter.(*flate.Writer).Close() } } @@ -100,29 +100,29 @@ func (output *BeegoOutput) Cookie(name string, value string, others ...interface var b bytes.Buffer fmt.Fprintf(&b, "%s=%s", sanitizeName(name), sanitizeValue(value)) - //fix cookie not work in IE - if len(others) > 0 { - switch v := others[0].(type) { - case int: - if v > 0 { - fmt.Fprintf(&b, "; Expires=%s; Max-Age=%d", time.Now().Add(time.Duration(v) * time.Second).UTC().Format(time.RFC1123), v) - } else if v < 0 { - fmt.Fprintf(&b, "; Max-Age=0") - } - case int64: - if v > 0 { - fmt.Fprintf(&b, "; Expires=%s; Max-Age=%d", time.Now().Add(time.Duration(v) * time.Second).UTC().Format(time.RFC1123), v) - } else if v < 0 { - fmt.Fprintf(&b, "; Max-Age=0") - } - case int32: - if v > 0 { - fmt.Fprintf(&b, "; Expires=%s; Max-Age=%d", time.Now().Add(time.Duration(v) * time.Second).UTC().Format(time.RFC1123), v) - } else if v < 0 { - fmt.Fprintf(&b, "; Max-Age=0") - } - } - } + //fix cookie not work in IE + if len(others) > 0 { + switch v := others[0].(type) { + case int: + if v > 0 { + fmt.Fprintf(&b, "; Expires=%s; Max-Age=%d", time.Now().Add(time.Duration(v)*time.Second).UTC().Format(time.RFC1123), v) + } else if v < 0 { + fmt.Fprintf(&b, "; Max-Age=0") + } + case int64: + if v > 0 { + fmt.Fprintf(&b, "; Expires=%s; Max-Age=%d", time.Now().Add(time.Duration(v)*time.Second).UTC().Format(time.RFC1123), v) + } else if v < 0 { + fmt.Fprintf(&b, "; Max-Age=0") + } + case int32: + if v > 0 { + fmt.Fprintf(&b, "; Expires=%s; Max-Age=%d", time.Now().Add(time.Duration(v)*time.Second).UTC().Format(time.RFC1123), v) + } else if v < 0 { + fmt.Fprintf(&b, "; Max-Age=0") + } + } + } // the settings below // Path, Domain, Secure, HttpOnly @@ -188,9 +188,9 @@ func sanitizeValue(v string) string { return cookieValueSanitizer.Replace(v) } -// Json writes json to response body. +// JSON writes json to response body. // if coding is true, it converts utf-8 to \u0000 type. -func (output *BeegoOutput) Json(data interface{}, hasIndent bool, coding bool) error { +func (output *BeegoOutput) JSON(data interface{}, hasIndent bool, coding bool) error { output.Header("Content-Type", "application/json; charset=utf-8") var content []byte var err error @@ -204,14 +204,14 @@ func (output *BeegoOutput) Json(data interface{}, hasIndent bool, coding bool) e return err } if coding { - content = []byte(stringsToJson(string(content))) + content = []byte(stringsToJSON(string(content))) } output.Body(content) return nil } -// Jsonp writes jsonp to response body. -func (output *BeegoOutput) Jsonp(data interface{}, hasIndent bool) error { +// JSONP writes jsonp to response body. +func (output *BeegoOutput) JSONP(data interface{}, hasIndent bool) error { output.Header("Content-Type", "application/javascript; charset=utf-8") var content []byte var err error @@ -228,16 +228,16 @@ func (output *BeegoOutput) Jsonp(data interface{}, hasIndent bool) error { if callback == "" { return errors.New(`"callback" parameter required`) } - callback_content := bytes.NewBufferString(" " + template.JSEscapeString(callback)) - callback_content.WriteString("(") - callback_content.Write(content) - callback_content.WriteString(");\r\n") - output.Body(callback_content.Bytes()) + callbackContent := bytes.NewBufferString(" " + template.JSEscapeString(callback)) + callbackContent.WriteString("(") + callbackContent.Write(content) + callbackContent.WriteString(");\r\n") + output.Body(callbackContent.Bytes()) return nil } -// Xml writes xml string to response body. -func (output *BeegoOutput) Xml(data interface{}, hasIndent bool) error { +// XML writes xml string to response body. +func (output *BeegoOutput) XML(data interface{}, hasIndent bool) error { output.Header("Content-Type", "application/xml; charset=utf-8") var content []byte var err error @@ -331,7 +331,7 @@ func (output *BeegoOutput) IsNotFound(status int) bool { return output.Status == 404 } -// IsClient returns boolean of this request client sends error data. +// IsClientError returns boolean of this request client sends error data. // HTTP 4xx means forbidden. func (output *BeegoOutput) IsClientError(status int) bool { return output.Status >= 400 && output.Status < 500 @@ -343,7 +343,7 @@ func (output *BeegoOutput) IsServerError(status int) bool { return output.Status >= 500 && output.Status < 600 } -func stringsToJson(str string) string { +func stringsToJSON(str string) string { rs := []rune(str) jsons := "" for _, r := range rs { @@ -357,7 +357,7 @@ func stringsToJson(str string) string { return jsons } -// Sessions sets session item value with given key. +// Session sets session item value with given key. func (output *BeegoOutput) Session(name interface{}, value interface{}) { output.Context.Input.CruSession.Set(name, value) } diff --git a/controller.go b/controller.go index b225df03..39a7c91b 100644 --- a/controller.go +++ b/controller.go @@ -327,7 +327,7 @@ func (c *Controller) ServeJSON(encoding ...bool) { if len(encoding) > 0 && encoding[0] == true { hasencoding = true } - c.Ctx.Output.Json(c.Data["json"], hasIndent, hasencoding) + c.Ctx.Output.JSON(c.Data["json"], hasIndent, hasencoding) } // ServeJSONP sends a jsonp response. @@ -338,7 +338,7 @@ func (c *Controller) ServeJSONP() { } else { hasIndent = true } - c.Ctx.Output.Jsonp(c.Data["jsonp"], hasIndent) + c.Ctx.Output.JSONP(c.Data["jsonp"], hasIndent) } // ServeXML sends xml response. @@ -349,7 +349,7 @@ func (c *Controller) ServeXML() { } else { hasIndent = true } - c.Ctx.Output.Xml(c.Data["xml"], hasIndent) + c.Ctx.Output.XML(c.Data["xml"], hasIndent) } // ServeFormatted serve Xml OR Json, depending on the value of the Accept header @@ -630,7 +630,7 @@ func (c *Controller) XSRFToken() string { } else { expire = int64(XSRFExpire) } - c._xsrfToken = c.Ctx.XsrfToken(XSRFKEY, expire) + c._xsrfToken = c.Ctx.XSRFToken(XSRFKEY, expire) } return c._xsrfToken } @@ -642,7 +642,7 @@ func (c *Controller) CheckXSRFCookie() bool { if !c.EnableXSRF { return true } - return c.Ctx.CheckXsrfCookie() + return c.Ctx.CheckXSRFCookie() } // XSRFFormHTML writes an input field contains xsrf token value. diff --git a/error.go b/error.go index b4708b30..e1d6dfae 100644 --- a/error.go +++ b/error.go @@ -87,7 +87,7 @@ func showErr(err interface{}, ctx *context.Context, Stack string) { data := make(map[string]string) data["AppError"] = AppName + ":" + fmt.Sprint(err) data["RequestMethod"] = ctx.Input.Method() - data["RequestURL"] = ctx.Input.Uri() + data["RequestURL"] = ctx.Input.URI() data["RemoteAddr"] = ctx.Input.IP() data["Stack"] = Stack data["BeegoVersion"] = VERSION diff --git a/router.go b/router.go index 12b5f6ae..efb1a1f3 100644 --- a/router.go +++ b/router.go @@ -875,7 +875,7 @@ func (p *ControllerRegister) recoverPanic(context *beecontext.Context) { } } var stack string - Critical("the request url is ", context.Input.Url()) + Critical("the request url is ", context.Input.URL()) Critical("Handler crashed with error", err) for i := 1; ; i++ { _, file, line, ok := runtime.Caller(i)