From 5612f61a9384e61279cf289faee2fceb667ccb5d Mon Sep 17 00:00:00 2001 From: Artem Nistratov Date: Wed, 8 Jul 2015 17:42:00 +0300 Subject: [PATCH 01/13] fix #671 --- orm/models_boot.go | 5 ++++- orm/models_info_f.go | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/orm/models_boot.go b/orm/models_boot.go index cb44bc05..5232a48d 100644 --- a/orm/models_boot.go +++ b/orm/models_boot.go @@ -269,7 +269,10 @@ func bootStrap() { if found == false { mForC: for _, ffi := range fi.relModelInfo.fields.fieldsByType[RelManyToMany] { - if ffi.relModelInfo == mi { + conditions := fi.relThrough != "" && fi.relThrough == ffi.relThrough || + fi.relTable != "" && fi.relTable == ffi.relTable || + fi.relThrough == "" && fi.relTable == "" + if ffi.relModelInfo == mi && conditions { found = true fi.reverseField = ffi.reverseFieldInfoTwo.name diff --git a/orm/models_info_f.go b/orm/models_info_f.go index 84a0c024..01863aaa 100644 --- a/orm/models_info_f.go +++ b/orm/models_info_f.go @@ -223,6 +223,11 @@ checkType: break checkType case "many": fieldType = RelReverseMany + if tv := tags["rel_table"]; tv != "" { + fi.relTable = tv + } else if tv := tags["rel_through"]; tv != "" { + fi.relThrough = tv + } break checkType default: err = fmt.Errorf("error") From cfcce4f5dcdcd7167eaa93a88f113370c90519f1 Mon Sep 17 00:00:00 2001 From: Pelle Johnsen Date: Tue, 22 Sep 2015 12:23:51 +0200 Subject: [PATCH 02/13] Fix handling of rel(fk) to model with string pk --- orm/cmd_utils.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/orm/cmd_utils.go b/orm/cmd_utils.go index 5b40fd07..bddada7c 100644 --- a/orm/cmd_utils.go +++ b/orm/cmd_utils.go @@ -45,13 +45,14 @@ func getDbDropSQL(al *alias) (sqls []string) { func getColumnTyp(al *alias, fi *fieldInfo) (col string) { T := al.DbBaser.DbTypes() fieldType := fi.fieldType + fieldSize := fi.size checkColumn: switch fieldType { case TypeBooleanField: col = T["bool"] case TypeCharField: - col = fmt.Sprintf(T["string"], fi.size) + col = fmt.Sprintf(T["string"], fieldSize) case TypeTextField: col = T["string-text"] case TypeDateField: @@ -89,6 +90,7 @@ checkColumn: } case RelForeignKey, RelOneToOne: fieldType = fi.relModelInfo.fields.pk.fieldType + fieldSize = fi.relModelInfo.fields.pk.size goto checkColumn } From 174e758d19cad49094aac236494c2b5d5aa5cb18 Mon Sep 17 00:00:00 2001 From: Pelle Johnsen Date: Mon, 28 Sep 2015 14:07:35 +0200 Subject: [PATCH 03/13] Fix dbBase.Update not returning error --- orm/db.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/orm/db.go b/orm/db.go index 2f2c52da..b62c165b 100644 --- a/orm/db.go +++ b/orm/db.go @@ -488,7 +488,8 @@ func (d *dbBase) Update(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time. d.ins.ReplaceMarks(&query) - if res, err := q.Exec(query, setValues...); err == nil { + res, err := q.Exec(query, setValues...) + if err == nil { return res.RowsAffected() } return 0, err From 4ba50e5df52c2b850a463d859542ec81808190db Mon Sep 17 00:00:00 2001 From: astaxie Date: Mon, 12 Oct 2015 20:50:58 +0800 Subject: [PATCH 04/13] fix #1385 --- validation/validators.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/validation/validators.go b/validation/validators.go index ec1545fb..2662b701 100644 --- a/validation/validators.go +++ b/validation/validators.go @@ -74,6 +74,33 @@ func (r Required) IsSatisfied(obj interface{}) bool { if i, ok := obj.(int); ok { return i != 0 } + if i, ok := obj.(uint); ok { + return i != 0 + } + if i, ok := obj.(int8); ok { + return i != 0 + } + if i, ok := obj.(uint8); ok { + return i != 0 + } + if i, ok := obj.(int16); ok { + return i != 0 + } + if i, ok := obj.(uint16); ok { + return i != 0 + } + if i, ok := obj.(uint32); ok { + return i != 0 + } + if i, ok := obj.(int32); ok { + return i != 0 + } + if i, ok := obj.(int64); ok { + return i != 0 + } + if i, ok := obj.(uint64); ok { + return i != 0 + } if t, ok := obj.(time.Time); ok { return !t.IsZero() } From 912abe3272b513ff195e762f8297e751c9b33b73 Mon Sep 17 00:00:00 2001 From: astaxie Date: Mon, 12 Oct 2015 21:26:18 +0800 Subject: [PATCH 05/13] fix #1388 --- router.go | 2 +- router_test.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/router.go b/router.go index c0a50988..bb6bb786 100644 --- a/router.go +++ b/router.go @@ -353,7 +353,7 @@ func (p *ControllerRegister) Handler(pattern string, h http.Handler, options ... route.handler = h if len(options) > 0 { if _, ok := options[0].(bool); ok { - pattern = path.Join(pattern, "?:all") + pattern = path.Join(pattern, "?:all(.*)") } } for _, m := range HTTPMETHOD { diff --git a/router_test.go b/router_test.go index c6ec9c92..9598f99c 100644 --- a/router_test.go +++ b/router_test.go @@ -333,6 +333,18 @@ func TestRouterHandler(t *testing.T) { } } +func TestRouterHandlerAll(t *testing.T) { + r, _ := http.NewRequest("POST", "/sayhi/a/b/c", nil) + w := httptest.NewRecorder() + + handler := NewControllerRegister() + handler.Handler("/sayhi", http.HandlerFunc(sayhello), true) + handler.ServeHTTP(w, r) + if w.Body.String() != "sayhello" { + t.Errorf("TestRouterHandler can't run") + } +} + // // Benchmarks NewApp: // From 5d18a7466c0b08aa91930e4076d799769cb5471c Mon Sep 17 00:00:00 2001 From: Mikhail Devyatov Date: Tue, 27 Oct 2015 20:16:47 +0300 Subject: [PATCH 06/13] XSRFFormHtml() should also generate XSRF token. --- controller.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/controller.go b/controller.go index 7fbf6dc5..0750daf4 100644 --- a/controller.go +++ b/controller.go @@ -647,8 +647,8 @@ func (c *Controller) CheckXSRFCookie() bool { // XSRFFormHTML writes an input field contains xsrf token value. func (c *Controller) XSRFFormHTML() string { - return "" + return `` } // GetControllerAndAction gets the executing controller name and action name. From f81929c28ca023d349f3f52a7d1b0b3cd810d89c Mon Sep 17 00:00:00 2001 From: shaoguang Date: Tue, 3 Nov 2015 14:53:26 +0800 Subject: [PATCH 07/13] bugfix: graceful failed when both enable http and https --- app.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/app.go b/app.go index d10d436c..772fb172 100644 --- a/app.go +++ b/app.go @@ -89,6 +89,7 @@ func (app *App) Run() { } } else { if Graceful { + httpsAddr := addr app.Server.Addr = addr app.Server.Handler = app.Handlers app.Server.ReadTimeout = time.Duration(HTTPServerTimeOut) * time.Second @@ -97,11 +98,12 @@ func (app *App) Run() { go func() { time.Sleep(20 * time.Microsecond) if HTTPSPort != 0 { - addr = fmt.Sprintf("%s:%d", HTTPAddr, HTTPSPort) - app.Server.Addr = addr + httpsAddr = fmt.Sprintf("%s:%d", HTTPAddr, HTTPSPort) + app.Server.Addr = httpsAddr } - server := grace.NewServer(addr, app.Handlers) - server.Server = app.Server + server := grace.NewServer(httpsAddr, app.Handlers) + server.Server.ReadTimeout = app.Server.ReadTimeout + server.Server.WriteTimeout = app.Server.WriteTimeout err := server.ListenAndServeTLS(HTTPCertFile, HTTPKeyFile) if err != nil { BeeLogger.Critical("ListenAndServeTLS: ", err, fmt.Sprintf("%d", os.Getpid())) @@ -113,7 +115,8 @@ func (app *App) Run() { if EnableHTTPListen { go func() { server := grace.NewServer(addr, app.Handlers) - server.Server = app.Server + server.Server.ReadTimeout = app.Server.ReadTimeout + server.Server.WriteTimeout = app.Server.WriteTimeout if ListenTCP4 && HTTPAddr == "" { server.Network = "tcp4" } From 205de8418d43973faf54ef9d41ead6a5c9ba3513 Mon Sep 17 00:00:00 2001 From: John Deng Date: Tue, 3 Nov 2015 23:43:34 +0800 Subject: [PATCH 08/13] Fixed typos --- admin.go | 2 +- app.go | 2 +- config.go | 10 +++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/admin.go b/admin.go index fa4b1db0..ddc936ef 100644 --- a/admin.go +++ b/admin.go @@ -107,7 +107,7 @@ func listConf(rw http.ResponseWriter, r *http.Request) { m["SessionGCMaxLifetime"] = SessionGCMaxLifetime m["SessionProviderConfig"] = SessionProviderConfig m["SessionCookieLifeTime"] = SessionCookieLifeTime - m["EnabelFcgi"] = EnabelFcgi + m["EnableFcgi"] = EnableFcgi m["MaxMemory"] = MaxMemory m["EnableGzip"] = EnableGzip m["DirectoryIndex"] = DirectoryIndex diff --git a/app.go b/app.go index d10d436c..019d6ff4 100644 --- a/app.go +++ b/app.go @@ -64,7 +64,7 @@ func (app *App) Run() { ) endRunning := make(chan bool, 1) - if EnabelFcgi { + if EnableFcgi { if EnableStdIo { err = fcgi.Serve(nil, app.Handlers) // standard I/O if err == nil { diff --git a/config.go b/config.go index 6f87fed1..d4a0f0e2 100644 --- a/config.go +++ b/config.go @@ -60,15 +60,15 @@ var ( EnableDocs bool // EnableErrorsShow wheather show errors in page. if true, show error and trace info in page rendered with error template. EnableErrorsShow bool - // EnabelFcgi turn on the fcgi Listen, default is false - EnabelFcgi bool + // EnableFcgi turn on the fcgi Listen, default is false + EnableFcgi bool // EnableGzip means gzip the response EnableGzip bool // EnableHTTPListen represent whether turn on the HTTP, default is true EnableHTTPListen bool // EnableHTTPTLS represent whether turn on the HTTPS, default is true EnableHTTPTLS bool - // EnableStdIo works with EnabelFcgi Use FCGI via standard I/O + // EnableStdIo works with EnableFcgi Use FCGI via standard I/O EnableStdIo bool // EnableXSRF whether turn on xsrf. default is false EnableXSRF bool @@ -435,8 +435,8 @@ func ParseConfig() (err error) { SessionCookieLifeTime = sesscookielifetime } - if enabelFcgi, err := AppConfig.Bool("EnabelFcgi"); err == nil { - EnabelFcgi = enabelFcgi + if enableFcgi, err := AppConfig.Bool("EnableFcgi"); err == nil { + EnableFcgi = enableFcgi } if enablegzip, err := AppConfig.Bool("EnableGzip"); err == nil { From fd4630c6dd1d3c90bee500e06795febf3745a8aa Mon Sep 17 00:00:00 2001 From: astaxie Date: Wed, 4 Nov 2015 23:52:42 +0800 Subject: [PATCH 09/13] impove the ResponseWriter. fix #1410 --- router.go | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/router.go b/router.go index bb6bb786..1e8144bf 100644 --- a/router.go +++ b/router.go @@ -15,10 +15,7 @@ package beego import ( - "bufio" - "errors" "fmt" - "net" "net/http" "os" "path" @@ -581,7 +578,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) var runMethod string var routerInfo *controllerInfo - w := &responseWriter{writer: rw} + w := &responseWriter{rw, false, 0} if RunMode == "dev" { w.Header().Set("Server", BeegoServerName) @@ -856,7 +853,7 @@ Admin: // Call WriteHeader if status code has been set changed if context.Output.Status != 0 { - w.writer.WriteHeader(context.Output.Status) + w.WriteHeader(context.Output.Status) } } @@ -895,14 +892,14 @@ func (p *ControllerRegister) recoverPanic(context *beecontext.Context) { //responseWriter is a wrapper for the http.ResponseWriter //started set to true if response was written to then don't execute other handler type responseWriter struct { - writer http.ResponseWriter + http.ResponseWriter started bool status int } // Header returns the header map that will be sent by WriteHeader. func (w *responseWriter) Header() http.Header { - return w.writer.Header() + return w.ResponseWriter.Header() } // Write writes the data to the connection as part of an HTTP reply, @@ -910,7 +907,7 @@ func (w *responseWriter) Header() http.Header { // started means the response has sent out. func (w *responseWriter) Write(p []byte) (int, error) { w.started = true - return w.writer.Write(p) + return w.ResponseWriter.Write(p) } // WriteHeader sends an HTTP response header with status code, @@ -918,23 +915,7 @@ func (w *responseWriter) Write(p []byte) (int, error) { func (w *responseWriter) WriteHeader(code int) { w.status = code w.started = true - w.writer.WriteHeader(code) -} - -// hijacker for http -func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) { - hj, ok := w.writer.(http.Hijacker) - if !ok { - return nil, nil, errors.New("webserver doesn't support hijacking") - } - return hj.Hijack() -} - -func (w *responseWriter) Flush() { - f, ok := w.writer.(http.Flusher) - if ok { - f.Flush() - } + w.ResponseWriter.WriteHeader(code) } func tourl(params map[string]string) string { From a26dee556d3ae4c583f5495d6273c472f1ad8cbb Mon Sep 17 00:00:00 2001 From: astaxie Date: Thu, 5 Nov 2015 00:19:09 +0800 Subject: [PATCH 10/13] fix #1335 --- app.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app.go b/app.go index 4978e5b1..ed57b978 100644 --- a/app.go +++ b/app.go @@ -117,7 +117,7 @@ func (app *App) Run() { server := grace.NewServer(addr, app.Handlers) server.Server.ReadTimeout = app.Server.ReadTimeout server.Server.WriteTimeout = app.Server.WriteTimeout - if ListenTCP4 && HTTPAddr == "" { + if ListenTCP4 { server.Network = "tcp4" } err := server.ListenAndServe() @@ -154,7 +154,7 @@ func (app *App) Run() { go func() { app.Server.Addr = addr BeeLogger.Info("http server Running on %s", app.Server.Addr) - if ListenTCP4 && HTTPAddr == "" { + if ListenTCP4 { ln, err := net.Listen("tcp4", app.Server.Addr) if err != nil { BeeLogger.Critical("ListenAndServe: ", err) From b8fc42d38dcaac48284bca16a5137881e6b85ff4 Mon Sep 17 00:00:00 2001 From: Yongzheng Lai Date: Thu, 5 Nov 2015 21:20:57 +0800 Subject: [PATCH 11/13] Update context.go all this status was setting in error.go, this line will cause multi-resp --- context/context.go | 1 - 1 file changed, 1 deletion(-) diff --git a/context/context.go b/context/context.go index f6aa85d6..30a47c02 100644 --- a/context/context.go +++ b/context/context.go @@ -54,7 +54,6 @@ func (ctx *Context) Redirect(status int, localurl string) { // Abort stops this request. // if beego.ErrorMaps exists, panic body. func (ctx *Context) Abort(status int, body string) { - ctx.ResponseWriter.WriteHeader(status) panic(body) } From 860568cb6c457a203157804569fcbb031313c75b Mon Sep 17 00:00:00 2001 From: JessonChan Date: Fri, 6 Nov 2015 18:51:53 +0800 Subject: [PATCH 12/13] modified as astaxie reviews https://github.com/astaxie/beego/pull/1376 --- config.go | 1 - memzipfile.go | 6 +++--- staticfile.go | 3 +-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/config.go b/config.go index c2f23f67..e8370f4f 100644 --- a/config.go +++ b/config.go @@ -529,7 +529,6 @@ func ParseConfig() (err error) { if ext == "" { continue } - ext = strings.ToLower(ext) if !strings.HasPrefix(ext, ".") { ext = "." + ext } diff --git a/memzipfile.go b/memzipfile.go index 594f1060..b61e87f2 100644 --- a/memzipfile.go +++ b/memzipfile.go @@ -206,9 +206,9 @@ func getAcceptEncodingZip(r *http.Request) string { ss = strings.ToLower(ss) if strings.Contains(ss, "gzip") { return "gzip" - } - if strings.Contains(ss, "deflate") { + } else if strings.Contains(ss, "deflate") { return "deflate" + } else { + return "" } - return "" } diff --git a/staticfile.go b/staticfile.go index e1fc4f73..de530b0c 100644 --- a/staticfile.go +++ b/staticfile.go @@ -96,9 +96,8 @@ func serverStaticRouter(ctx *context.Context) { } isStaticFileToCompress := false - lowerFileName := strings.ToLower(filePath) for _, statExtension := range StaticExtensionsToGzip { - if strings.HasSuffix(lowerFileName, statExtension) { + if strings.HasSuffix(strings.ToLower(filePath), strings.ToLower(statExtension)) { isStaticFileToCompress = true break } From 821b2f832ead32422b92a675d089f9710028be0d Mon Sep 17 00:00:00 2001 From: astaxie Date: Mon, 9 Nov 2015 11:03:57 +0800 Subject: [PATCH 13/13] fix the type assert --- context/output.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/context/output.go b/context/output.go index a4bb3d09..d132e392 100644 --- a/context/output.go +++ b/context/output.go @@ -103,7 +103,19 @@ func (output *BeegoOutput) Cookie(name string, value string, others ...interface //fix cookie not work in IE if len(others) > 0 { switch v := others[0].(type) { - case int, int32, int64: + 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 {