Skip to content

Commit

Permalink
#### Version 1.7.3
Browse files Browse the repository at this point in the history
* New Feature: Request.PostBody增加Post内容大小限制,默认为32mb
* About MaxBodySize:
    - 通过app.HttpServer.SetMaxBodySize设置
    - 默认为 32 << 20 (32 mb)
    - -1 : unlimted
    - 0 : use default value
    - other: use other value
* 感谢 @wziww 提供 PR
* 2019-10-29 12:00 at ShangHai
  • Loading branch information
devfeel committed Oct 29, 2019
1 parent d17c05a commit 64ba924
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 18 deletions.
10 changes: 5 additions & 5 deletions config/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ type (
EnabledTLS bool `xml:"enabledtls,attr"` // enable TLS
TLSCertFile string `xml:"tlscertfile,attr"` // certifications file for TLS
TLSKeyFile string `xml:"tlskeyfile,attr"` // keys file for TLS
IndexPage string `xml:"indexpage,attr"` // default index page
EnabledDetailRequestData bool `xml:"enableddetailrequestdata,attr"` // enable detailed statics for requests, default is false. Please use with care, it will have performance issues if the site have lots of URLs
VirtualPath string // virtual path when deploy on no root path
IndexPage string `xml:"IndexPage,attr"` // default index page
EnabledDetailRequestData bool `xml:"EnabledDetailRequestData,attr"` // enable detailed statics for requests, default is false. Please use with care, it will have performance issues if the site have lots of URLs
VirtualPath string `xml:"VirtualPath,attr"` // virtual path when deploy on no root path
// To limit the request's body size to be read
// which can avoid unexpected or malicious request to cause the service's OOM
// default is 32 << 20 (32 mb)
// default is 32 << 20 (32 mb), MaxBodySize use go runtime default zero value
// -1 : unlimted
// 0 : use default value
MaxBodySize int64
MaxBodySize int64 `xml:"MaxBodySize,attr"` // To limit the request's body size to be read
}

// SessionNode dotweb app's session config
Expand Down
9 changes: 7 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ func main() {
}()

//初始化DotServer
app := dotweb.Classic("D:/gotmp")
app := dotweb.Classic("c:/gotmp")

//设置dotserver日志目录
//如果不设置,默认不启用,且默认为当前目录
app.SetEnabledLog(true)

app.HttpServer.SetEnabledRequestID(true)
// 设置解析请求体大小为 10MB
app.HttpServer.SetMaxBodySize(10 << 20)
app.HttpServer.SetMaxBodySize(-1)

//开启development模式
app.SetDevelopmentMode()
Expand Down Expand Up @@ -122,6 +122,10 @@ func IndexReg(ctx dotweb.Context) error {
return ctx.WriteString("welcome to dotweb")
}

func ReadPost(ctx dotweb.Context) error {
return ctx.WriteString(ctx.Request().PostBody())
}

func IndexParam(ctx dotweb.Context) error {
ctx.Response().Header().Set("Content-Type", "text/html; charset=utf-8")
return ctx.WriteString("IndexParam", ctx.GetRouterName("id"))
Expand Down Expand Up @@ -167,5 +171,6 @@ func InitRoute(server *dotweb.HttpServer) {
server.GET("/error", DefaultError)
server.GET("/returnerr", ReturnError)
server.GET("/redirect", Redirect)
server.POST("/readpost", ReadPost)
//server.Router().RegisterRoute(dotweb.RouteMethod_GET, "/index", IndexReg)
}
13 changes: 11 additions & 2 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ func (req *Request) release() {
req.realUrl = ""
}

func (req *Request) httpServer() *HttpServer {
return req.httpCtx.HttpServer()
}

func (req *Request) httpApp() *DotWeb {
return req.httpCtx.HttpServer().DotApp
}

// RequestID get unique ID with current request
// must HttpServer.SetEnabledRequestID(true)
// default is empty string
Expand Down Expand Up @@ -144,13 +152,14 @@ func (req *Request) PostBody() []byte {
req.Body = http.MaxBytesReader(req.httpCtx.response.Writer(), req.Body, maxBodySize)
break
default:
req.Body = http.MaxBytesReader(req.httpCtx.response.Writer(), req.Body, req.httpCtx.httpServer.DotApp.Config.Server.MaxBodySize)
req.Body = http.MaxBytesReader(req.httpCtx.response.Writer(), req.Body, req.httpApp().Config.Server.MaxBodySize)
break
}
}
bts, err := ioutil.ReadAll(req.Body)
if err != nil {
return []byte{}
//if err, panic it
panic(err)
} else {
req.isReadBody = true
req.postBody = bts
Expand Down
11 changes: 2 additions & 9 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ type (
binder Binder
render Renderer
offline bool
virtualPath string // virtual path when deploy on no root path
}

pool struct {
Expand Down Expand Up @@ -81,12 +80,6 @@ func NewHttpServer() *HttpServer {

// initConfig init config from app config
func (server *HttpServer) initConfig() {
server.SetEnabledGzip(server.ServerConfig().EnabledGzip)
server.SetMaxBodySize(server.ServerConfig().MaxBodySize)
// VirtualPath config
if server.virtualPath == "" {
server.virtualPath = server.ServerConfig().VirtualPath
}
}

// ServerConfig a shortcut for App.Config.ServerConfig
Expand Down Expand Up @@ -184,14 +177,14 @@ func (server *HttpServer) IsOffline() bool {

// SetVirtualPath set current server's VirtualPath
func (server *HttpServer) SetVirtualPath(path string) {
server.virtualPath = path
server.ServerConfig().VirtualPath = path
server.DotApp.Logger().Debug("DotWeb:HttpServer SetVirtualPath ["+path+"]", LogTarget_HttpServer)

}

// VirtualPath return current server's VirtualPath
func (server *HttpServer) VirtualPath() string {
return server.virtualPath
return server.ServerConfig().VirtualPath
}

// SetOffline set server offline config
Expand Down
11 changes: 11 additions & 0 deletions version.MD
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
## dotweb版本记录:


#### Version 1.7.3
* New Feature: Request.PostBody增加Post内容大小限制,默认为32mb
* About MaxBodySize:
- 通过app.HttpServer.SetMaxBodySize设置
- 默认为 32 << 20 (32 mb)
- -1 : unlimted
- 0 : use default value
- other: use other value
* 感谢 @wziww 提供 PR
* 2019-10-29 12:00 at ShangHai

#### Version 1.7.2
* Bug Fixed: Request.Release()增加对realUrl的处理
* 2019-10-23 12:00 at ShangHai
Expand Down

0 comments on commit 64ba924

Please sign in to comment.