diff --git a/context/context.go b/context/context.go index 89b5ffe4..b60650a8 100644 --- a/context/context.go +++ b/context/context.go @@ -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 } diff --git a/context/input.go b/context/input.go index d9aa2634..f535e6a2 100644 --- a/context/input.go +++ b/context/input.go @@ -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. diff --git a/httplib/httplib.go b/httplib/httplib.go index 37ba3b33..7ff2f1d2 100644 --- a/httplib/httplib.go +++ b/httplib/httplib.go @@ -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. diff --git a/logs/conn.go b/logs/conn.go index 612634fa..2240eece 100644 --- a/logs/conn.go +++ b/logs/conn.go @@ -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 { diff --git a/logs/console.go b/logs/console.go index 461291c2..ce7ecd54 100644 --- a/logs/console.go +++ b/logs/console.go @@ -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 } diff --git a/logs/file.go b/logs/file.go index e237170b..2d3449ce 100644 --- a/logs/file.go +++ b/logs/file.go @@ -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 } diff --git a/logs/log.go b/logs/log.go index 6abfb005..32e0187c 100644 --- a/logs/log.go +++ b/logs/log.go @@ -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() diff --git a/logs/smtp.go b/logs/smtp.go index 19a0f510..95123ebf 100644 --- a/logs/smtp.go +++ b/logs/smtp.go @@ -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. diff --git a/middleware/error.go b/middleware/error.go index 052e977c..00cf9de5 100644 --- a/middleware/error.go +++ b/middleware/error.go @@ -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) } diff --git a/middleware/i18n.go b/middleware/i18n.go index e4dab693..f54b4bb5 100644 --- a/middleware/i18n.go +++ b/middleware/i18n.go @@ -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{