fix issue in cors

This commit is contained in:
John
2019-09-26 15:54:13 +08:00
parent cb7c3a9fa4
commit ef4e128af7
6 changed files with 10 additions and 7 deletions

View File

@ -6,7 +6,7 @@ import "github.com/gogf/gf/frame/g"
func main() {
s := g.Server()
s.SetIndexFolder(true)
s.SetServerRoot("/Users/john/Temp")
s.SetServerRoot("/Users/john/Downloads")
s.AddSearchPath("/Users/john/Documents")
s.SetPort(8199)
s.Run()

2
go.mod
View File

@ -1,5 +1,7 @@
module github.com/gogf/gf
go 1.10
require (
github.com/BurntSushi/toml v0.3.1
github.com/clbanning/mxj v1.8.4

View File

@ -83,8 +83,8 @@ func (r *Request) WebSocket() (*WebSocket, error) {
}
}
// 获得指定名称的参数字符串(Router/GET/POST),同 GetRequestString
// 这是常用方法的简化别名
// Get是GetRequest方法的别名用于获得指定名称的参数值注意键值可能是字符串、数组、Map类型。
// 大多数场景下,你可能需要的是 GetString/GetVar。
func (r *Request) Get(key string, def ...interface{}) interface{} {
return r.GetRequest(key, def...)
}

View File

@ -23,7 +23,9 @@ func (r *Request) initRaw() {
}
}
// 获得router、post或者get提交的参数如果有同名参数那么按照router->get->post优先级进行覆盖
// 获得router、post或者get提交的参数,如果有同名参数,
// 那么按照 router->get->post->OtherHttpMethod 优先级进行覆盖。
// 注意获得参数值可能是字符串、数组、Map三种类型。
func (r *Request) GetRequest(key string, def ...interface{}) interface{} {
v := r.GetRouterValue(key)
if v == nil {

View File

@ -32,10 +32,10 @@ func (r *Response) DefaultCORSOptions() CORSOptions {
AllowOrigin: "*",
AllowMethods: HTTP_METHODS,
AllowCredentials: "true",
AllowHeaders: "Origin, X-Requested-With, Content-Type, Accept, Key",
AllowHeaders: "Origin,Content-Type,Accept,User-Agent,Cookie,Authorization,X-Auth-Token,X-Requested-With",
MaxAge: 3628800,
}
if origin := r.Header().Get("Origin"); origin != "" {
if origin := r.Request.Header.Get("Origin"); origin != "" {
options.AllowOrigin = origin
} else if referer := r.Request.Referer(); referer != "" {
if p := gstr.PosR(referer, "/", 6); p != -1 {

View File

@ -261,7 +261,6 @@ func (s *Server) serveFile(r *Request, f *staticServeFile, allowIndex ...bool) {
r.Response.WriteStatus(http.StatusForbidden)
}
} else {
// 读取文件内容返回, no buffer
http.ServeContent(r.Response.Writer, r.Request, info.Name(), info.ModTime(), file)
}
}