Browse Source

Fix serving videos behind Cloudflare for Safari and Mobile Safari (redo of 35241bc) by adding a --disable-gzip flag

pull/459/head
James Mills 3 days ago
parent
commit
adb5552168
Signed by: prologic GPG Key ID: AC4C014F1440EBD6
  1. 6
      cmd/yarnd/main.go
  2. 1
      internal/config.go
  3. 12
      internal/options.go
  4. 16
      internal/server.go

6
cmd/yarnd/main.go

@ -66,6 +66,7 @@ var (
// Pod Settings
openProfiles bool
openRegistrations bool
disableGzip bool
// Pod Limits
twtsPerPage int
@ -135,6 +136,10 @@ func init() {
&openProfiles, "open-profiles", "O", internal.DefaultOpenProfiles,
"whether or not to have open user profiles",
)
flag.BoolVar(
&disableGzip, "disable-gzip", internal.DefaultDisableGzip,
"whether or not to disable Gzip compression",
)
// Pod Limits
flag.IntVarP(
@ -302,6 +307,7 @@ func main() {
// Pod Settings
internal.WithOpenProfiles(openProfiles),
internal.WithOpenRegistrations(openRegistrations),
internal.WithDisableGzip(disableGzip),
// Pod Limits
internal.WithTwtsPerPage(twtsPerPage),

1
internal/config.go

@ -94,6 +94,7 @@ type Config struct {
MsgsPerPage int
OpenProfiles bool
OpenRegistrations bool
DisableGzip bool
SessionExpiry time.Duration
SessionCacheTTL time.Duration
TranscoderTimeout time.Duration

12
internal/options.go

@ -54,6 +54,9 @@ const (
// DefaultOpenRegistrations is the default for open user registrations
DefaultOpenRegistrations = false
// DefaultDisableGzip is the default for disabling Gzip compression
DefaultDisableGzip = false
// DefaultRegisterMessage is the default message displayed when registrations are disabled
DefaultRegisterMessage = ""
@ -168,6 +171,7 @@ func NewConfig() *Config {
MsgsPerPage: DefaultMsgsPerPage,
OpenProfiles: DefaultOpenProfiles,
OpenRegistrations: DefaultOpenRegistrations,
DisableGzip: DefaultDisableGzip,
SessionExpiry: DefaultSessionExpiry,
MagicLinkSecret: DefaultMagicLinkSecret,
SMTPHost: DefaultSMTPHost,
@ -289,6 +293,14 @@ func WithOpenRegistrations(openRegistrations bool) Option {
}
}
// WithDisableGzip sets the disable Gzip flag
func WithDisableGzip(disableGzip bool) Option {
return func(cfg *Config) error {
cfg.DisableGzip = disableGzip
return nil
}
}
// WithCookieSecret sets the server's cookie secret
func WithCookieSecret(secret string) Option {
return func(cfg *Config) error {

16
internal/server.go

@ -783,9 +783,19 @@ func NewServer(bind string, options ...Option) (*Server, error) {
smtpService := NewSMTPService(config, db, pm, msgs, tasks)
var handler http.Handler
csrfHandler := nosurf.New(router)
csrfHandler.ExemptGlob("/api/v1/*")
// Useful for Safari / Mobile Safari when behind Cloudflare to streaming
// videos _actually_ works :O
if config.DisableGzip {
handler = sm.Handler(csrfHandler)
} else {
handler = gziphandler.GzipHandler(sm.Handler(csrfHandler))
}
server := &Server{
bind: bind,
config: config,
@ -797,11 +807,7 @@ func NewServer(bind string, options ...Option) (*Server, error) {
Handler: logger.New(logger.Options{
Prefix: "twtxt",
RemoteAddressHeaders: []string{"X-Forwarded-For"},
}).Handler(
gziphandler.GzipHandler(
sm.Handler(csrfHandler),
),
),
}).Handler(handler),
},
// API

Loading…
Cancel
Save