-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HandleDir AssestValidator: http/2.0 push seems not working #1562
Comments
@xiaozhuai If you are going to |
Also, that could be feature request too, we can add an option to handle |
assetNames := GzipAssetNames()
pushTargets := make([]string, 0)
for _, name := range assetNames {
// ignore *.map file
if !strings.HasSuffix(name, ".map") {
pos := strings.Index(name, "public/")
target := name[pos+6:]
// skip /index.html
if target != "/index.html" {
pushTargets = append(pushTargets, target)
}
}
}
app.Use(func(ctx iris.Context) {
if ctx.Request().RequestURI == "/" {
for _, target := range pushTargets {
app.Logger().Infof("!!!! Push %s", target)
err := ctx.ResponseWriter().Push(target, nil)
if err != nil {
break
}
}
}
ctx.Next()
})
app.HandleDir("/", "public", iris.DirOptions{
Asset: GzipAsset,
AssetInfo: GzipAssetInfo,
AssetNames: GzipAssetNames,
AssetValidator: func(ctx iris.Context, name string) bool {
ctx.Header("Vary", "Accept-Encoding")
ctx.Header("Content-Encoding", "gzip")
return true
},
}) assetNames := GzipAssetNames()
pushTargets := make([]string, 0)
for _, name := range assetNames {
// ignore *.map file
if !strings.HasSuffix(name, ".map") {
pos := strings.Index(name, "public/")
target := name[pos+6:]
// skip /index.html
if target != "/index.html" {
pushTargets = append(pushTargets, target)
}
}
}
app.HandleDir("/", "public", iris.DirOptions{
Asset: GzipAsset,
AssetInfo: GzipAssetInfo,
AssetNames: GzipAssetNames,
AssetValidator: func(ctx iris.Context, name string) bool {
ctx.Header("Vary", "Accept-Encoding")
ctx.Header("Content-Encoding", "gzip")
if strings.HasSuffix(name, "public/index.html") {
for _, target := range pushTargets {
app.Logger().Infof("!!!! Push %s", target)
err := ctx.ResponseWriter().Push(target, nil)
if err != nil {
break
}
}
data, _ := GzipAsset(name)
ctx.Write(data)
return false
}
return true
},
}) Thanks @kataras ! And I came up with these two, but both not work. Why? |
Give me some time @xiaozhuai, I will push an example with a new feature for that one ^ EDIT: you are running the Iris server under |
@kataras Yes,I run iris with TLS |
@xiaozhuai Here you are: iris/_examples/file-server/http2push-embedded-gzipped/main.go Lines 17 to 44 in 4e3836e
|
:( Still not work, I have no idea why.
And one more thing if configuration.HttpsSupport && configuration.RedirectToHttps {
app.Use(AutoRedirectToHttpsMiddleware(configuration.HttpsPort, configuration.RedirectToHttpsCode))
}
............
go app.Run(
iris.Addr(fmt.Sprintf("%s:%d", configuration.ListenAddress, configuration.HttpPort)),
iris.WithoutServerError(iris.ErrServerClosed),
)
app.Run(
iris.TLS(
fmt.Sprintf("%s:%d", configuration.ListenAddress, configuration.HttpsPort),
configuration.CertFile,
configuration.KeyFile,
func(su *host.Supervisor) {
su.NoRedirect()
},
),
iris.WithoutServerError(iris.ErrServerClosed),
) I can't specify the redirect http server's listen address and port. So I need to |
The example runs and works as expected here, let me clone your repository and see what's goin on |
@xiaozhuai remove the func AssetsDirOptions() iris.DirOptions {
return iris.DirOptions{
IndexName: "/index.html",
PushTargets: map[string][]string{
"/": GetPushTargets(),
},
Asset: GzipAsset,
AssetInfo: GzipAssetInfo,
AssetNames: GzipAssetNames,
AssetValidator: func(ctx iris.Context, name string) bool {
ctx.Header("Content-Encoding", "gzip")
return true
},
}
}
func GetPushTargets() (pushTargets []string) {
assetNames := GzipAssetNames()
for _, name := range assetNames {
// ignore *.map file
if !strings.HasSuffix(name, ".map") {
target := strings.TrimPrefix(name, "public")
// skip /index.html
if target != "/index.html" {
pushTargets = append(pushTargets, target)
}
}
}
return
}
var runner iris.Runner
if configuration.HttpsSupport {
addr := fmt.Sprintf("%s:%d", configuration.ListenAddress, configuration.HttpsPort)
runner = iris.TLS(addr, configuration.CertFile, configuration.KeyFile)
} else {
addr := fmt.Sprintf("%s:%d", configuration.ListenAddress, configuration.HttpPort)
runner = iris.Addr(addr)
}
app.Run(runner, iris.WithoutServerError(iris.ErrServerClosed)) // all options go here.
|
Awesome! Thank you very much @kataras ! It works now! You are a very kind man! Thanks for spending time to help me and make the great iris! For |
By the way, I found this not work on go1.13 and works on go1.14. Lines 184 to 190 in 4e3836e
Because there is no |
Yes, the |
@xiaozhuai You are very welcomed, I thank YOU. Example using var opts = iris.DirOptions{
IndexName: "/index.html",
PushTargetsRegexp: map[string]*regexp.Regexp{
"/": iris.MatchCommonAssets,
/* OR a lighter version:
"/": regexp.MustCompile("((.*).js|(.*).css|(.*).ico)$"), */
},
Compress: true,
} |
Former-commit-id: 5a3f626ba0fbcf10be979b5a800eba51e88602cd
Former-commit-id: 940f07fdb1184266dc315441fb91afa5754092fa
Former-commit-id: 60d9b0d77693895fdfbebe83712e9cc1ee3f8f26
rel to: #1562 (comment) Former-commit-id: 778cf9146b730d424040ea9e259ce6500f53b563
…system rel to: #1562 (comment) Former-commit-id: 9e3fb4d71e14dda025a3af86cf210ff72127b716
rel to: #1562 (comment) Former-commit-id: da04ae0543e9b743cd4989ded5983ae15316a879
Describe the bug
Here is my code snippets
The log shows
But the Chrome
Initiator
do not hintPush
The text was updated successfully, but these errors were encountered: