From d6da47a9106e2a441cf22b21a419827548252871 Mon Sep 17 00:00:00 2001 From: imago Date: Sun, 18 Aug 2024 20:25:08 +0300 Subject: [PATCH] Fixed issue where html files without header didn't get mirrored --- internal/wsinject/wsinject.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/internal/wsinject/wsinject.go b/internal/wsinject/wsinject.go index b714d7b..d89f2e2 100644 --- a/internal/wsinject/wsinject.go +++ b/internal/wsinject/wsinject.go @@ -31,6 +31,8 @@ type Fileserver struct { wsDispatcherStartedMu *sync.Mutex } +var ErrNoHeaderTagFound = errors.New("no header tag found") + const deltaStreamer = ` ` @@ -59,7 +61,6 @@ func (fs *Fileserver) mirrorFile(origPath string) error { } injected, injectedBytes, err := injectWebsocketScript(fileB) if err != nil { - return fmt.Errorf("failed to ineject websocket script: %v", err) } if injected { ancli.PrintfNotice("injected delta-streamer script loading tag in: '%v'", origPath) @@ -167,7 +168,7 @@ func injectScript(html []byte, scriptTag string) ([]byte, error) { // Find the location of the closing `` tag idx := strings.Index(htmlStr, "") if idx == -1 { - return nil, fmt.Errorf("no tag found in the HTML") + return html, ErrNoHeaderTagFound } var buf bytes.Buffer @@ -193,14 +194,20 @@ func injectScript(html []byte, scriptTag string) ([]byte, error) { func injectWebsocketScript(b []byte) (bool, []byte, error) { contentType := http.DetectContentType(b) + injected := false // Only act on html files if !strings.Contains(contentType, "text/html") { - return false, b, nil + return injected, b, nil } b, err := injectScript(b, deltaStreamer) + injected = true if err != nil { - return false, nil, fmt.Errorf("failed to inject script tag: %w", err) + if !errors.Is(err, ErrNoHeaderTagFound) { + return injected, nil, fmt.Errorf("failed to inject script tag: %w", err) + } else { + injected = false + } } - return true, b, nil + return injected, b, nil }