From 50132df809e73105b1f4a96971627f9ea94e63ef Mon Sep 17 00:00:00 2001 From: nkbai Date: Fri, 20 Nov 2015 21:34:01 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E8=BF=99=E7=A7=8Dif=E5=9E=8B=E7=9A=84?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E6=98=AF=E6=9C=89=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/docs.go b/docs.go index 1dbed6cc..a660ddeb 100644 --- a/docs.go +++ b/docs.go @@ -21,12 +21,10 @@ import ( ) // GlobalDocAPI store the swagger api documents -var GlobalDocAPI map[string]interface{} +var GlobalDocAPI map[string]interface{}=make(map[string]interface{}) func init() { - if EnableDocs { - GlobalDocAPI = make(map[string]interface{}) - } + } func serverDocs(ctx *context.Context) { From f0474214fec6bc6643f3a4bd83333c59e5feb430 Mon Sep 17 00:00:00 2001 From: nkbai Date: Wed, 25 Nov 2015 23:39:26 +0800 Subject: [PATCH 2/8] =?UTF-8?q?GetInt=E7=AD=89=E5=87=BD=E6=95=B0=E7=A8=8D?= =?UTF-8?q?=E5=BE=AE=E7=AE=80=E5=8C=96=E4=B8=80=E4=B8=8B,=E8=BF=99?= =?UTF-8?q?=E6=A0=B7=E7=9C=8B=E4=B8=8A=E5=8E=BB=E6=9B=B4=E5=90=88=E7=90=86?= =?UTF-8?q?=E4=B8=80=E4=BA=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller.go | 71 +++++++++++++++++---------------------------------- 1 file changed, 24 insertions(+), 47 deletions(-) diff --git a/controller.go b/controller.go index 0750daf4..79f3349b 100644 --- a/controller.go +++ b/controller.go @@ -413,91 +413,68 @@ func (c *Controller) GetStrings(key string, def ...[]string) []string { // GetInt returns input as an int or the default value while it's present and input is blank func (c *Controller) GetInt(key string, def ...int) (int, error) { - if strv := c.Ctx.Input.Query(key); strv != "" { - return strconv.Atoi(strv) - } else if len(def) > 0 { + strv := c.Ctx.Input.Query(key) + if len(strv) == 0 && len(def) > 0 { return def[0], nil - } else { - return strconv.Atoi(strv) } + return strconv.Atoi(strv) } // GetInt8 return input as an int8 or the default value while it's present and input is blank 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 - } else if len(def) > 0 { + strv := c.Ctx.Input.Query(key) + if len(strv) == 0 && len(def) > 0 { return def[0], nil - } else { - i64, err := strconv.ParseInt(strv, 10, 8) - i8 := int8(i64) - return i8, err } + i64, err := strconv.ParseInt(strv, 10, 8) + return int8(i64), err } // GetInt16 returns input as an int16 or the default value while it's present and input is blank 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 - } else if len(def) > 0 { + strv := c.Ctx.Input.Query(key) + if len(strv) == 0 && len(def) > 0 { return def[0], nil - } else { - i64, err := strconv.ParseInt(strv, 10, 16) - i16 := int16(i64) - return i16, err } + i64, err := strconv.ParseInt(strv, 10, 16) + return int16(i64), err } // GetInt32 returns input as an int32 or the default value while it's present and input is blank 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 - } else if len(def) > 0 { + strv := c.Ctx.Input.Query(key) + if len(strv) == 0 && len(def) > 0 { return def[0], nil - } else { - i64, err := strconv.ParseInt(c.Ctx.Input.Query(key), 10, 32) - i32 := int32(i64) - return i32, err } + i64, err := strconv.ParseInt(strv, 10, 32) + return int32(i64), err } // GetInt64 returns input value as int64 or the default value while it's present and input is blank. func (c *Controller) GetInt64(key string, def ...int64) (int64, error) { - if strv := c.Ctx.Input.Query(key); strv != "" { - return strconv.ParseInt(strv, 10, 64) - } else if len(def) > 0 { + strv := c.Ctx.Input.Query(key) + if len(strv) == 0 && len(def) > 0 { return def[0], nil - } else { - return strconv.ParseInt(strv, 10, 64) } + return strconv.ParseInt(strv, 10, 64) } // GetBool returns input value as bool or the default value while it's present and input is blank. func (c *Controller) GetBool(key string, def ...bool) (bool, error) { - if strv := c.Ctx.Input.Query(key); strv != "" { - return strconv.ParseBool(strv) - } else if len(def) > 0 { + strv := c.Ctx.Input.Query(key) + if len(strv) == 0 && len(def) > 0 { return def[0], nil - } else { - return strconv.ParseBool(strv) } + return strconv.ParseBool(strv) } // GetFloat returns input value as float64 or the default value while it's present and input is blank. func (c *Controller) GetFloat(key string, def ...float64) (float64, error) { - if strv := c.Ctx.Input.Query(key); strv != "" { - return strconv.ParseFloat(strv, 64) - } else if len(def) > 0 { + strv := c.Ctx.Input.Query(key) + if len(strv) == 0 && len(def) > 0 { return def[0], nil - } else { - return strconv.ParseFloat(strv, 64) } + return strconv.ParseFloat(strv, 64) } // GetFile returns the file data in file upload field named as key. From 6aaca2eca8f04e14a78c0ca324ce6a8d066f3d02 Mon Sep 17 00:00:00 2001 From: nkbai Date: Thu, 26 Nov 2015 14:56:39 +0800 Subject: [PATCH 3/8] =?UTF-8?q?router.go=20Header=20=E6=AF=AB=E6=97=A0?= =?UTF-8?q?=E7=94=A8=E5=A4=84=20context/output.go=20=E7=AE=80=E5=8C=96?= =?UTF-8?q?=E4=B8=80=E4=B8=8B=E4=BB=A3=E7=A0=81,=E6=9B=B4=E6=B8=85?= =?UTF-8?q?=E6=99=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- context/output.go | 8 ++------ router.go | 4 ---- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/context/output.go b/context/output.go index d132e392..0a194753 100644 --- a/context/output.go +++ b/context/output.go @@ -66,7 +66,6 @@ func (output *BeegoOutput) Body(content []byte) { if val == "gzip" { output.Header("Content-Encoding", "gzip") outputWriter, _ = gzip.NewWriterLevel(output.Context.ResponseWriter, gzip.BestSpeed) - break } else if val == "deflate" { output.Header("Content-Encoding", "deflate") @@ -86,11 +85,8 @@ func (output *BeegoOutput) Body(content []byte) { } outputWriter.Write(content) - switch outputWriter.(type) { - case *gzip.Writer: - outputWriter.(*gzip.Writer).Close() - case *flate.Writer: - outputWriter.(*flate.Writer).Close() + if c, ok := outputWriter.(io.Closer); ok { + c.Close() } } diff --git a/router.go b/router.go index 1e8144bf..5c6b7bf9 100644 --- a/router.go +++ b/router.go @@ -897,10 +897,6 @@ type responseWriter struct { status int } -// Header returns the header map that will be sent by WriteHeader. -func (w *responseWriter) Header() http.Header { - return w.ResponseWriter.Header() -} // Write writes the data to the connection as part of an HTTP reply, // and sets `started` to true. From d693ecf046be6cae8af4b879a0293694c05e130d Mon Sep 17 00:00:00 2001 From: nkbai Date: Thu, 26 Nov 2015 20:37:36 +0800 Subject: [PATCH 4/8] =?UTF-8?q?Revert=20"=E8=BF=99=E7=A7=8Dif=E5=9E=8B?= =?UTF-8?q?=E7=9A=84=E5=88=9D=E5=A7=8B=E5=8C=96=E6=98=AF=E6=9C=89=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E7=9A=84"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 50132df809e73105b1f4a96971627f9ea94e63ef. --- docs.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs.go b/docs.go index a660ddeb..1dbed6cc 100644 --- a/docs.go +++ b/docs.go @@ -21,10 +21,12 @@ import ( ) // GlobalDocAPI store the swagger api documents -var GlobalDocAPI map[string]interface{}=make(map[string]interface{}) +var GlobalDocAPI map[string]interface{} func init() { - + if EnableDocs { + GlobalDocAPI = make(map[string]interface{}) + } } func serverDocs(ctx *context.Context) { From 2c8cb5693ee650b4352926cf178d5d24786698ff Mon Sep 17 00:00:00 2001 From: nkbai Date: Fri, 27 Nov 2015 12:10:39 +0800 Subject: [PATCH 5/8] =?UTF-8?q?windows=E4=B8=8B=E9=9D=99=E6=80=81=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=98=A0=E5=B0=84=E6=89=BE=E4=B8=8D=E5=88=B0=E9=97=AE?= =?UTF-8?q?=E9=A2=98,=20path.Clean=E5=92=8Cfilepath.Clean=E6=98=AF?= =?UTF-8?q?=E6=9C=89=E5=8C=BA=E5=88=AB=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- staticfile.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/staticfile.go b/staticfile.go index de530b0c..56c5fc08 100644 --- a/staticfile.go +++ b/staticfile.go @@ -18,20 +18,18 @@ import ( "net/http" "os" "path" - "path/filepath" "strconv" "strings" - "github.com/astaxie/beego/context" "github.com/astaxie/beego/utils" ) func serverStaticRouter(ctx *context.Context) { + if ctx.Input.Method() != "GET" && ctx.Input.Method() != "HEAD" { return } - requestPath := filepath.Clean(ctx.Input.Request.URL.Path) - + requestPath := path.Clean(ctx.Input.Request.URL.Path) // special processing : favicon.ico/robots.txt can be in any static dir if requestPath == "/favicon.ico" || requestPath == "/robots.txt" { file := path.Join(".", requestPath) From 74ebcd28b2d62e0c7c4bd892742a5f751174bdec Mon Sep 17 00:00:00 2001 From: nkbai Date: Mon, 30 Nov 2015 16:15:35 +0800 Subject: [PATCH 6/8] =?UTF-8?q?grace=20init=E4=B8=AD=E7=9A=84=E5=B7=A5?= =?UTF-8?q?=E4=BD=9C=E5=8F=AF=E4=BB=A5=E5=BB=B6=E5=90=8E=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=20=E5=AE=9E=E9=99=85=E4=B8=8A=E5=A6=82=E6=9E=9C=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E4=BD=BF=E7=94=A8graceful=E5=90=AF=E5=8A=A8,=E8=BF=99?= =?UTF-8?q?=E4=BA=9Binit=E7=9A=84=E5=B7=A5=E4=BD=9C=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E6=B2=A1=E7=94=A8=20=E5=BD=93=E7=84=B6=E5=85=B6=E4=BB=96?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E4=B9=9F=E5=BA=94=E8=AF=A5=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E8=BF=99=E6=A0=B7=E7=9A=84=E5=B7=A5=E4=BD=9C,=E6=AF=94?= =?UTF-8?q?=E5=A6=82session=E4=B8=AD=E7=9A=84sess=5Futils.go=E4=B8=AD?= =?UTF-8?q?=E7=9A=84init=E5=B7=A5=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- grace/grace.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/grace/grace.go b/grace/grace.go index b066a639..40eef144 100644 --- a/grace/grace.go +++ b/grace/grace.go @@ -86,23 +86,25 @@ var ( isChild bool socketOrder string + once sync.Once=sync.Once{} ) func init() { + DefaultMaxHeaderBytes = 0 + DefaultTimeout = 60 * time.Second +} +func onceInit(){ regLock = &sync.Mutex{} flag.BoolVar(&isChild, "graceful", false, "listen on open fd (after forking)") flag.StringVar(&socketOrder, "socketorder", "", "previous initialization order - used when more than one listener was started") runningServers = make(map[string]*Server) runningServersOrder = []string{} socketPtrOffsetMap = make(map[string]uint) - - DefaultMaxHeaderBytes = 0 - - DefaultTimeout = 60 * time.Second } // NewServer returns a new graceServer. func NewServer(addr string, handler http.Handler) (srv *Server) { + once.Do(onceInit) regLock.Lock() defer regLock.Unlock() if !flag.Parsed() { From 82e0105d22b9defd73c1d8815f9ff9f3952b2692 Mon Sep 17 00:00:00 2001 From: nkbai Date: Fri, 4 Dec 2015 23:15:06 +0800 Subject: [PATCH 7/8] =?UTF-8?q?Revert=20"windows=E4=B8=8B=E9=9D=99?= =?UTF-8?q?=E6=80=81=E6=96=87=E4=BB=B6=E6=98=A0=E5=B0=84=E6=89=BE=E4=B8=8D?= =?UTF-8?q?=E5=88=B0=E9=97=AE=E9=A2=98,"=20windows=E4=B8=8B=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E9=9C=80=E8=A6=81filepath.ToSlash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 2c8cb5693ee650b4352926cf178d5d24786698ff. --- staticfile.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/staticfile.go b/staticfile.go index 90d0be0d..f2eb0654 100644 --- a/staticfile.go +++ b/staticfile.go @@ -19,6 +19,7 @@ import ( "net/http" "os" "path" + "path/filepath" "strconv" "strings" "sync" @@ -143,7 +144,7 @@ func isStaticCompress(filePath string) bool { // searchFile search the file by url path // if none the static file prefix matches ,return notStaticRequestErr func searchFile(ctx *context.Context) (string, os.FileInfo, error) { - requestPath := path.Clean(ctx.Input.Request.URL.Path) + requestPath := filepath.ToSlash(filepath.Clean(ctx.Input.Request.URL.Path)) // special processing : favicon.ico/robots.txt can be in any static dir if requestPath == "/favicon.ico" || requestPath == "/robots.txt" { file := path.Join(".", requestPath) @@ -188,7 +189,7 @@ func lookupFile(ctx *context.Context) (bool, string, os.FileInfo, error) { if !fi.IsDir() { return false, fp, fi, err } - ifp := path.Join(fp, "index.html") + ifp := filepath.Join(fp, "index.html") if ifi, _ := os.Stat(ifp); ifi != nil && ifi.Mode().IsRegular() { return false, ifp, ifi, err } From e8fe859a58dee7172a29236c74c01e290b9a47ad Mon Sep 17 00:00:00 2001 From: nkbai Date: Wed, 9 Dec 2015 09:03:10 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- grace/grace.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/grace/grace.go b/grace/grace.go index 40eef144..355e5d1a 100644 --- a/grace/grace.go +++ b/grace/grace.go @@ -53,18 +53,18 @@ import ( ) const ( - // PreSignal is the position to add filter before signal +// PreSignal is the position to add filter before signal PreSignal = iota - // PostSignal is the position to add filter after signal +// PostSignal is the position to add filter after signal PostSignal - // StateInit represent the application inited +// StateInit represent the application inited StateInit - // StateRunning represent the application is running +// StateRunning represent the application is running StateRunning - // StateShuttingDown represent the application is shutting down +// StateShuttingDown represent the application is shutting down StateShuttingDown - // StateTerminate represent the application is killed +// StateTerminate represent the application is killed StateTerminate ) @@ -75,25 +75,25 @@ var ( socketPtrOffsetMap map[string]uint runningServersForked bool - // DefaultReadTimeOut is the HTTP read timeout +// DefaultReadTimeOut is the HTTP read timeout DefaultReadTimeOut time.Duration - // DefaultWriteTimeOut is the HTTP Write timeout +// DefaultWriteTimeOut is the HTTP Write timeout DefaultWriteTimeOut time.Duration - // DefaultMaxHeaderBytes is the Max HTTP Herder size, default is 0, no limit +// DefaultMaxHeaderBytes is the Max HTTP Herder size, default is 0, no limit DefaultMaxHeaderBytes int - // DefaultTimeout is the shutdown server's timeout. default is 60s +// DefaultTimeout is the shutdown server's timeout. default is 60s DefaultTimeout time.Duration - isChild bool + isChild bool socketOrder string - once sync.Once=sync.Once{} + once sync.Once ) func init() { DefaultMaxHeaderBytes = 0 DefaultTimeout = 60 * time.Second } -func onceInit(){ +func onceInit() { regLock = &sync.Mutex{} flag.BoolVar(&isChild, "graceful", false, "listen on open fd (after forking)") flag.StringVar(&socketOrder, "socketorder", "", "previous initialization order - used when more than one listener was started")