mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-11-03 12:20:27 +08:00
优化“页面优化”功能相关代码
This commit is contained in:
@@ -2,17 +2,6 @@
|
||||
|
||||
package serverconfigs
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"github.com/tdewolff/minify/v2"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var httpPageOptimizationLimiter = make(chan bool, 64)
|
||||
|
||||
type HTTPPageOptimizationMimeType = string
|
||||
|
||||
const (
|
||||
@@ -21,6 +10,10 @@ const (
|
||||
HTTPPageOptimizationMimeTypeCSS HTTPPageOptimizationMimeType = "text/css"
|
||||
)
|
||||
|
||||
type HTTPPageMinifier interface {
|
||||
Bytes(mediaType string, data []byte) ([]byte, error)
|
||||
}
|
||||
|
||||
type HTTPPageOptimizationConfig struct {
|
||||
IsPrior bool `yaml:"isPrior" json:"isPrior"`
|
||||
|
||||
@@ -28,8 +21,9 @@ type HTTPPageOptimizationConfig struct {
|
||||
Javascript *HTTPJavascriptOptimizationConfig `yaml:"javascript" json:"javascript"`
|
||||
CSS *HTTPCSSOptimizationConfig `yaml:"css" json:"css"`
|
||||
|
||||
isOn bool
|
||||
minifyInstance *minify.M
|
||||
isOn bool
|
||||
|
||||
minifyInstance HTTPPageMinifier
|
||||
}
|
||||
|
||||
func NewHTTPPageOptimizationConfig() *HTTPPageOptimizationConfig {
|
||||
@@ -44,11 +38,6 @@ func NewHTTPPageOptimizationConfig() *HTTPPageOptimizationConfig {
|
||||
func (this *HTTPPageOptimizationConfig) Init() error {
|
||||
this.isOn = this.CheckIsOn()
|
||||
|
||||
if this.isOn {
|
||||
// MUST NOT create instance for every config
|
||||
this.minifyInstance = minify.New()
|
||||
}
|
||||
|
||||
if this.HTML != nil {
|
||||
err := this.HTML.Init()
|
||||
if err != nil {
|
||||
@@ -56,8 +45,6 @@ func (this *HTTPPageOptimizationConfig) Init() error {
|
||||
}
|
||||
if this.HTML.IsOn {
|
||||
this.isOn = true
|
||||
this.minifyInstance.Add(HTTPPageOptimizationMimeTypeHTML, this.HTML.AsMinifier())
|
||||
|
||||
}
|
||||
}
|
||||
if this.Javascript != nil {
|
||||
@@ -67,7 +54,6 @@ func (this *HTTPPageOptimizationConfig) Init() error {
|
||||
}
|
||||
if this.Javascript.IsOn {
|
||||
this.isOn = true
|
||||
this.minifyInstance.Add(HTTPPageOptimizationMimeTypeJavascript, this.Javascript.AsMinifier())
|
||||
}
|
||||
}
|
||||
if this.CSS != nil {
|
||||
@@ -77,7 +63,6 @@ func (this *HTTPPageOptimizationConfig) Init() error {
|
||||
}
|
||||
if this.CSS.IsOn {
|
||||
this.isOn = true
|
||||
this.minifyInstance.Add(HTTPPageOptimizationMimeTypeCSS, this.CSS.AsMinifier())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,78 +79,10 @@ func (this *HTTPPageOptimizationConfig) CheckIsOn() bool {
|
||||
(this.CSS != nil && this.CSS.IsOn)
|
||||
}
|
||||
|
||||
func (this *HTTPPageOptimizationConfig) FilterResponse(url string, resp *http.Response) error {
|
||||
if !this.isOn || this.minifyInstance == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var contentType = resp.Header.Get("Content-Type")
|
||||
if len(contentType) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// validate content length
|
||||
if resp.ContentLength <= 0 || resp.ContentLength > (1<<20) {
|
||||
return nil
|
||||
}
|
||||
|
||||
contentType, _, _ = strings.Cut(contentType, ";")
|
||||
var mimeType = ""
|
||||
switch contentType {
|
||||
case "text/html":
|
||||
if this.HTML != nil && this.HTML.IsOn && this.HTML.MatchURL(url) {
|
||||
mimeType = HTTPPageOptimizationMimeTypeHTML
|
||||
}
|
||||
case "text/javascript", "application/javascript":
|
||||
if this.Javascript != nil && this.Javascript.IsOn && this.Javascript.MatchURL(url) {
|
||||
mimeType = HTTPPageOptimizationMimeTypeJavascript
|
||||
}
|
||||
case "text/css":
|
||||
if this.CSS != nil && this.CSS.IsOn && this.CSS.MatchURL(url) {
|
||||
mimeType = HTTPPageOptimizationMimeTypeCSS
|
||||
}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(mimeType) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// concurrent limiter, to prevent memory overflow
|
||||
select {
|
||||
case httpPageOptimizationLimiter <- true:
|
||||
defer func() {
|
||||
<-httpPageOptimizationLimiter
|
||||
}()
|
||||
|
||||
var contentLength int64
|
||||
var err error
|
||||
resp.Body, contentLength, err = this.minify(mimeType, resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// fix resp.ContentLength and Content-Length header
|
||||
resp.ContentLength = contentLength
|
||||
resp.Header.Set("Content-Length", types.String(contentLength))
|
||||
default:
|
||||
}
|
||||
return nil
|
||||
func (this *HTTPPageOptimizationConfig) InternalInstance() HTTPPageMinifier {
|
||||
return this.minifyInstance
|
||||
}
|
||||
|
||||
func (this *HTTPPageOptimizationConfig) minify(mimeType HTTPPageOptimizationMimeType, rawReader io.ReadCloser) (newReader io.ReadCloser, newContentLength int64, err error) {
|
||||
|
||||
var rawData []byte
|
||||
rawData, err = io.ReadAll(rawReader)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
resultData, err := this.minifyInstance.Bytes(mimeType, rawData)
|
||||
if err != nil {
|
||||
return io.NopCloser(bytes.NewReader(rawData)), int64(len(rawData)), nil // return rawData, and ignore error
|
||||
}
|
||||
|
||||
return io.NopCloser(bytes.NewReader(resultData)), int64(len(resultData)), nil
|
||||
func (this *HTTPPageOptimizationConfig) SetInternalInstance(instance HTTPPageMinifier) {
|
||||
this.minifyInstance = instance
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
package serverconfigs
|
||||
|
||||
import "github.com/tdewolff/minify/v2/css"
|
||||
|
||||
type HTTPCSSOptimizationConfig struct {
|
||||
HTTPBaseOptimizationConfig
|
||||
|
||||
@@ -26,10 +24,3 @@ func (this *HTTPCSSOptimizationConfig) Init() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *HTTPCSSOptimizationConfig) AsMinifier() *css.Minifier {
|
||||
return &css.Minifier{
|
||||
KeepCSS2: this.KeepCSS2,
|
||||
Precision: this.Precision,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,6 @@
|
||||
|
||||
package serverconfigs
|
||||
|
||||
import (
|
||||
"github.com/tdewolff/minify/v2/html"
|
||||
)
|
||||
|
||||
type HTTPHTMLOptimizationConfig struct {
|
||||
HTTPBaseOptimizationConfig
|
||||
|
||||
@@ -36,15 +32,3 @@ func (this *HTTPHTMLOptimizationConfig) Init() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *HTTPHTMLOptimizationConfig) AsMinifier() *html.Minifier {
|
||||
return &html.Minifier{
|
||||
KeepComments: this.KeepComments,
|
||||
KeepConditionalComments: this.KeepConditionalComments,
|
||||
KeepDefaultAttrVals: this.KeepDefaultAttrVals,
|
||||
KeepDocumentTags: this.KeepDocumentTags,
|
||||
KeepEndTags: this.KeepEndTags,
|
||||
KeepQuotes: this.KeepQuotes,
|
||||
KeepWhitespace: this.KeepWhitespace,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
package serverconfigs
|
||||
|
||||
import "github.com/tdewolff/minify/v2/js"
|
||||
|
||||
type HTTPJavascriptOptimizationConfig struct {
|
||||
HTTPBaseOptimizationConfig
|
||||
|
||||
@@ -25,11 +23,3 @@ func (this *HTTPJavascriptOptimizationConfig) Init() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *HTTPJavascriptOptimizationConfig) AsMinifier() *js.Minifier {
|
||||
return &js.Minifier{
|
||||
Precision: this.Precision,
|
||||
KeepVarNames: this.KeepVarNames,
|
||||
Version: this.Version,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user