1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 21:01:31 +00:00

Merge pull request #1044 from fuxiaohei/develop

code style simplify
This commit is contained in:
astaxie 2015-02-23 22:25:34 +08:00
commit 71b9854f48
10 changed files with 76 additions and 80 deletions

View File

@ -155,8 +155,11 @@ func (ctx *Context) CheckXsrfCookie() bool {
}
if token == "" {
ctx.Abort(403, "'_xsrf' argument missing from POST")
} else if ctx._xsrf_token != token {
return false
}
if ctx._xsrf_token != token {
ctx.Abort(403, "XSRF cookie does not match POST argument")
return false
}
return true
}

View File

@ -72,11 +72,11 @@ func (input *BeegoInput) Site() string {
func (input *BeegoInput) Scheme() string {
if input.Request.URL.Scheme != "" {
return input.Request.URL.Scheme
} else if input.Request.TLS == nil {
return "http"
} else {
return "https"
}
if input.Request.TLS == nil {
return "http"
}
return "https"
}
// Domain returns host name.

View File

@ -253,30 +253,20 @@ func (b *BeegoHttpRequest) Body(data interface{}) *BeegoHttpRequest {
return b
}
func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
if b.resp.StatusCode != 0 {
return b.resp, nil
}
var paramBody string
if len(b.params) > 0 {
var buf bytes.Buffer
for k, v := range b.params {
buf.WriteString(url.QueryEscape(k))
buf.WriteByte('=')
buf.WriteString(url.QueryEscape(v))
buf.WriteByte('&')
}
paramBody = buf.String()
paramBody = paramBody[0 : len(paramBody)-1]
}
func (b *BeegoHttpRequest) buildUrl(paramBody string) {
// build GET url with query string
if b.req.Method == "GET" && len(paramBody) > 0 {
if strings.Index(b.url, "?") != -1 {
b.url += "&" + paramBody
} else {
b.url = b.url + "?" + paramBody
}
} else if b.req.Method == "POST" && b.req.Body == nil {
return
}
// build POST url and body
if b.req.Method == "POST" && b.req.Body == nil {
// with files
if len(b.files) > 0 {
pr, pw := io.Pipe()
bodyWriter := multipart.NewWriter(pw)
@ -305,12 +295,35 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
}()
b.Header("Content-Type", bodyWriter.FormDataContentType())
b.req.Body = ioutil.NopCloser(pr)
} else if len(paramBody) > 0 {
return
}
// with params
if len(paramBody) > 0 {
b.Header("Content-Type", "application/x-www-form-urlencoded")
b.Body(paramBody)
}
}
}
func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
if b.resp.StatusCode != 0 {
return b.resp, nil
}
var paramBody string
if len(b.params) > 0 {
var buf bytes.Buffer
for k, v := range b.params {
buf.WriteString(url.QueryEscape(k))
buf.WriteByte('=')
buf.WriteString(url.QueryEscape(v))
buf.WriteByte('&')
}
paramBody = buf.String()
paramBody = paramBody[0 : len(paramBody)-1]
}
b.buildUrl(paramBody)
url, err := url.Parse(b.url)
if err != nil {
return nil, err
@ -342,14 +355,12 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
}
}
var jar http.CookieJar
var jar http.CookieJar = nil
if b.setting.EnableCookie {
if defaultCookieJar == nil {
createDefaultCookie()
}
jar = defaultCookieJar
} else {
jar = nil
}
client := &http.Client{
@ -402,12 +413,11 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) {
return nil, nil
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
b.body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
b.body = data
return data, nil
return b.body, nil
}
// ToFile saves the body data in response to one file.
@ -438,8 +448,7 @@ func (b *BeegoHttpRequest) ToJson(v interface{}) error {
if err != nil {
return err
}
err = json.Unmarshal(data, v)
return err
return json.Unmarshal(data, v)
}
// ToXml returns the map that marshals from the body bytes as xml in response .
@ -449,8 +458,7 @@ func (b *BeegoHttpRequest) ToXml(v interface{}) error {
if err != nil {
return err
}
err = xml.Unmarshal(data, v)
return err
return xml.Unmarshal(data, v)
}
// Response executes request client gets response mannually.

View File

@ -43,11 +43,7 @@ func NewConn() LoggerInterface {
// init connection writer with json config.
// json config only need key "level".
func (c *ConnWriter) Init(jsonconfig string) error {
err := json.Unmarshal([]byte(jsonconfig), c)
if err != nil {
return err
}
return nil
return json.Unmarshal([]byte(jsonconfig), c)
}
// write message in connection.
@ -77,10 +73,9 @@ func (c *ConnWriter) Flush() {
// destroy connection writer and close tcp listener.
func (c *ConnWriter) Destroy() {
if c.innerWriter == nil {
return
if c.innerWriter != nil {
c.innerWriter.Close()
}
c.innerWriter.Close()
}
func (c *ConnWriter) connect() error {

View File

@ -50,9 +50,10 @@ type ConsoleWriter struct {
// create ConsoleWriter returning as LoggerInterface.
func NewConsole() LoggerInterface {
cw := new(ConsoleWriter)
cw.lg = log.New(os.Stdout, "", log.Ldate|log.Ltime)
cw.Level = LevelDebug
cw := &ConsoleWriter{
lg: log.New(os.Stdout, "", log.Ldate|log.Ltime),
Level: LevelDebug,
}
return cw
}
@ -62,11 +63,7 @@ func (c *ConsoleWriter) Init(jsonconfig string) error {
if len(jsonconfig) == 0 {
return nil
}
err := json.Unmarshal([]byte(jsonconfig), c)
if err != nil {
return err
}
return nil
return json.Unmarshal([]byte(jsonconfig), c)
}
// write message in console.
@ -76,9 +73,10 @@ func (c *ConsoleWriter) WriteMsg(msg string, level int) error {
}
if goos := runtime.GOOS; goos == "windows" {
c.lg.Println(msg)
} else {
c.lg.Println(colors[level](msg))
return nil
}
c.lg.Println(colors[level](msg))
return nil
}

View File

@ -123,11 +123,7 @@ func (w *FileLogWriter) startLogger() error {
return err
}
w.mw.SetFd(fd)
err = w.initFd()
if err != nil {
return err
}
return nil
return w.initFd()
}
func (w *FileLogWriter) docheck(size int) {
@ -170,14 +166,13 @@ func (w *FileLogWriter) initFd() error {
}
w.maxsize_cursize = int(finfo.Size())
w.daily_opendate = time.Now().Day()
w.maxlines_curlines = 0
if finfo.Size() > 0 {
count, err := w.lines()
if err != nil {
return err
}
w.maxlines_curlines = count
} else {
w.maxlines_curlines = 0
}
return nil
}

View File

@ -292,9 +292,9 @@ func (bl *BeeLogger) Close() {
fmt.Println("ERROR, unable to WriteMsg (while closing logger):", err)
}
}
} else {
break
continue
}
break
}
for _, l := range bl.outputs {
l.Flush()

View File

@ -25,7 +25,8 @@ import (
)
const (
subjectPhrase = "Diagnostic message from server"
// no usage
// subjectPhrase = "Diagnostic message from server"
)
// smtpWriter implements LoggerInterface and is used to send emails via given SMTP-server.
@ -146,9 +147,7 @@ func (s *SmtpWriter) WriteMsg(msg string, level int) error {
mailmsg := []byte("To: " + strings.Join(s.RecipientAddresses, ";") + "\r\nFrom: " + s.FromAddress + "<" + s.FromAddress +
">\r\nSubject: " + s.Subject + "\r\n" + content_type + "\r\n\r\n" + fmt.Sprintf(".%s", time.Now().Format("2006-01-02 15:04:05")) + msg)
err := s.sendMail(s.Host, auth, s.FromAddress, s.RecipientAddresses, mailmsg)
return err
return s.sendMail(s.Host, auth, s.FromAddress, s.RecipientAddresses, mailmsg)
}
// implementing method. empty.

View File

@ -323,17 +323,15 @@ func Exception(errcode string, w http.ResponseWriter, r *http.Request, msg strin
w.WriteHeader(isint)
h(w, r)
return
} else {
isint, err := strconv.Atoi(errcode)
if err != nil {
isint = 500
}
if isint == 404 {
msg = "404 page not found"
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(isint)
fmt.Fprintln(w, msg)
return
}
isint, err := strconv.Atoi(errcode)
if err != nil {
isint = 500
}
if isint == 404 {
msg = "404 page not found"
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(isint)
fmt.Fprintln(w, msg)
}

View File

@ -34,7 +34,6 @@ type Translation struct {
}
func NewLocale(filepath string, defaultlocal string) *Translation {
i18n := make(map[string]map[string]string)
file, err := os.Open(filepath)
if err != nil {
panic("open " + filepath + " err :" + err.Error())
@ -43,8 +42,9 @@ func NewLocale(filepath string, defaultlocal string) *Translation {
if err != nil {
panic("read " + filepath + " err :" + err.Error())
}
err = json.Unmarshal(data, &i18n)
if err != nil {
i18n := make(map[string]map[string]string)
if err = json.Unmarshal(data, &i18n); err != nil {
panic("json.Unmarshal " + filepath + " err :" + err.Error())
}
return &Translation{