ghttp gzip特性开发中

This commit is contained in:
john
2018-08-22 19:56:01 +08:00
parent 7ce4f02533
commit 13d6fc00a2
8 changed files with 71 additions and 34 deletions

View File

@ -153,6 +153,11 @@ func (r *Request) GetHost() string {
return host
}
// 判断是否为AJAX请求
func (r *Request) IsAjax() bool {
return strings.EqualFold(r.Header.Get("X-Requested-With"), "XMLHttpRequest")
}
// 获取请求的客户端IP地址
func (r *Request) GetClientIp() string {
ip := r.clientIp.Val()

View File

@ -207,5 +207,6 @@ func (r *Response) ClearBuffer() {
// 输出缓冲区数据到客户端
func (r *Response) OutputBuffer() {
r.Header().Set("Server", r.Server.config.ServerAgent)
//r.handleGzip()
r.Writer.OutputBuffer()
}

View File

@ -7,6 +7,14 @@
package ghttp
import (
"mime"
"gitee.com/johng/gf/g/os/gfile"
"gitee.com/johng/gf/g/encoding/gcompress"
"strings"
"gitee.com/johng/gf/g/util/gconv"
)
// 默认的gzip压缩文件类型
var defaultGzipContentTypes = []string{
"application/atom+xml",
@ -42,4 +50,29 @@ var defaultGzipContentTypes = []string{
"text/x-component",
"text/x-cross-domain-policy",
"text/xml",
}
// 返回内容gzip检查处理
func (r *Response) handleGzip() {
// 如果客户端支持gzip压缩并且服务端设置开启gzip压缩特性那么执行压缩
encoding := r.request.Header.Get("Accept-Encoding")
if encoding != "" && strings.Contains(encoding, "gzip") {
mimeType := ""
ext := gfile.Ext(r.request.URL.Path)
if ext != "" {
mimeType = strings.Split(mime.TypeByExtension(ext), ";")[0]
}
if mimeType == "" {
contentType := r.Header().Get("Content-Type")
if contentType != "" {
mimeType = strings.Split(contentType, ";")[0]
}
}
if _, ok := r.Server.gzipMimesMap[mimeType]; ok {
r.SetBuffer(gcompress.Gzip(r.buffer))
r.Header().Set("Content-Length", gconv.String(len(r.buffer)))
r.Header().Set("Content-Encoding", "gzip")
}
}
}

View File

@ -62,7 +62,7 @@ type Server struct {
paths *gspath.SPath // 静态文件检索对象
config ServerConfig // 配置对象
servers []*gracefulServer // 底层http.Server列表
methodsMap map[string]bool // 所有支持的HTTP Method(初始化时自动填充)
methodsMap map[string]struct{} // 所有支持的HTTP Method(初始化时自动填充)
servedCount *gtype.Int // 已经服务的请求数(4-8字节不考虑溢出情况)同时作为请求ID
closeQueue *gqueue.Queue // 请求结束的关闭队列(存放的是需要异步关闭处理的*Request对象)
// 服务注册相关
@ -90,6 +90,7 @@ type Server struct {
errorLogger *glog.Logger // error log日志对象
// 其他属性
nameToUriType *gtype.Int // 服务注册时对象和方法名称转换为URI时的规则
gzipMimesMap map[string]struct{} // 支持gzip压缩的类型
}
// 路由对象
@ -181,7 +182,7 @@ func GetServer(name...interface{}) (*Server) {
name : sname,
paths : gspath.New(),
servers : make([]*gracefulServer, 0),
methodsMap : make(map[string]bool),
methodsMap : make(map[string]struct{}),
statusHandlerMap : make(map[string]HandlerFunc),
serveTree : make(map[string]interface{}),
hooksTree : make(map[string]interface{}),
@ -203,6 +204,7 @@ func GetServer(name...interface{}) (*Server) {
errorLogEnabled : gtype.NewBool(),
logHandler : gtype.NewInterface(),
nameToUriType : gtype.NewInt(),
gzipMimesMap : make(map[string]struct{}),
}
s.errorLogger.SetBacktraceSkip(4)
s.accessLogger.SetBacktraceSkip(4)
@ -210,7 +212,7 @@ func GetServer(name...interface{}) (*Server) {
s.serveCache.SetCap(gSERVE_CACHE_LRU_SIZE)
s.hooksCache.SetCap(gHOOKS_CACHE_LRU_SIZE)
for _, v := range strings.Split(gHTTP_METHODS, ",") {
s.methodsMap[v] = true
s.methodsMap[v] = struct{}{}
}
// 初始化时使用默认配置
s.SetConfig(defaultServerConfig)
@ -254,6 +256,12 @@ func (s *Server) Start() error {
})
}
}
// gzip压缩文件类型
if s.config.GzipContentTypes != nil {
for _, v := range s.config.GzipContentTypes {
s.gzipMimesMap[v] = struct{}{}
}
}
// 启动http server
reloaded := false

View File

@ -112,7 +112,10 @@ func (s *Server)SetConfig(c ServerConfig) {
if s.config.ServerAgent == "" {
s.SetServerAgent(defaultServerConfig.ServerAgent)
}
// 其他可动态设置的配置
// **********************
// 可动态设置的配置处理
// **********************
s.SetLogPath(c.LogPath)
s.SetLogHandler(c.LogHandler)
s.SetErrorLogEnabled(c.ErrorLogEnabled)
@ -128,7 +131,6 @@ func (s *Server)SetConfig(c ServerConfig) {
s.SetSessionIdName(c.SessionIdName)
}
s.SetNameToUriType(c.NameToUriType)
}
// 设置http server参数 - Addr
@ -287,6 +289,13 @@ func (s *Server) SetDenyRoutes(routes []string) {
s.config.DenyRoutes = routes
}
func (s *Server) SetGzipContentTypes(types []string) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error("cannot be changed while running")
}
s.config.GzipContentTypes = types
}
// 设置http server参数 - CookieMaxAge
func (s *Server)SetCookieMaxAge(maxage int) {
s.cookieMaxAge.Set(maxage)

View File

@ -97,12 +97,6 @@ func (s *Server)handleRequest(w http.ResponseWriter, r *http.Request) {
request.Response.OutputBuffer()
// 事件 - AfterOutput
s.callHookHandler(HOOK_AFTER_OUTPUT, request)
// gzip压缩处理
encoding := request.Header.Get("Accept-Encoding")
if encoding != "" {
}
}
// 初始化控制器

View File

@ -7,7 +7,7 @@ import (
func main() {
s := ghttp.GetServer()
s.SetIndexFolder(true)
s.SetServerRoot("/home/john/Workspace/view")
s.SetServerRoot("/home/john/Workspace/Go/gf-home/static/plugin/editor.md/css")
s.SetPort(8199)
s.Run()
}

View File

@ -1,28 +1,15 @@
package main
import "fmt"
import (
"gitee.com/johng/gf/g"
"fmt"
)
type A struct {
S string
}
type B struct {
A
}
func (a *A) editA () {
a.S += "a"
}
func (b *B) editB () {
b.S += "b"
}
func main() {
b := new(B)
b.editA()
b.editB()
b.A.editA()
fmt.Println(b.S)
v := g.View()
v.AddPath("/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/other")
b, e := v.Parse("index.html")
fmt.Println(e)
fmt.Println(string(b))
}