Replace response for deny download not work

1. The problem I’m having:

I want to deny download access, but not work.

2. Error messages and/or full log output:

myplugin code

type responseWriterWrapper struct {
	*caddyhttp.ResponseWriterWrapper
	wroteHeader bool
	match       bool
}

func (rww *responseWriterWrapper) WriteHeader(status int) {
	if rww.wroteHeader {
		return
	}
	rww.wroteHeader = true

	ct := rww.Header().Get("Content-Type")
	cd := rww.Header().Get("Content-Disposition")
	if strings.Contains(ct, "octet-stream") || cd != "" { // download header match
		rww.match = true
		rww.Header().Set("Content-Type", "text/html; charset=utf-8")
		rww.Header().Del("Content-Disposition")
		rww.Header().Del("Content-Length")
		rww.ResponseWriterWrapper.WriteHeader(http.StatusForbidden)
		return 
	}

	rww.ResponseWriterWrapper.WriteHeader(status)
}

func (rww *responseWriterWrapper) Write(d []byte) (int, error) {
	if !rww.wroteHeader {
		rww.WriteHeader(http.StatusOK)
	}
	if rww.match { // if download discard data
		return len(d), nil
	}
	return rww.ResponseWriterWrapper.Write(d)
}



func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {

		ww := &responseWriterWrapper{
			ResponseWriterWrapper: &caddyhttp.ResponseWriterWrapper{ResponseWriter: w},
		}

		err := next.ServeHTTP(ww, r)

		// It's not work
                w.Header().Set("abc", "abc")
		_, _ = w.Write([]byte("deny download"))

		return err
	}

I set response header (w.Header().Set("abc", "abc")) and rewrite response (w.Write([]byte("deny download"))). When I curl,didn’t get the desired result

3. Caddy version:

2.6.0

4. How I installed and ran Caddy:

a. System environment:

b. Command:

PASTE OVER THIS, BETWEEN THE ``` LINES.
Please use the preview pane to ensure it looks nice.

c. Service/unit/compose file:


d. My complete Caddy config:

{
    debug
    order myplugin after encode
    auto_https off
}

a.com:443 {
    myplugin
    tls a.crt  a.key

    reverse_proxy  https://11.3.4.1 {
        trusted_proxies private_ranges
        transport http {
            tls
            tls_insecure_skip_verify
        }

    }
}

5. Links to relevant resources:

Please completely fill out the help topic template, as per the forum rules.

Your question is way too vague. You can’t just say “it doesn’t work”, that gives us absolutely no information about what you think is going wrong. Put yourself in our shoes. Assume we know nothing.

Sorry, I didn’t express my question clearly. I re-edited my question.I sincerely hope to get your help.

That’s a pretty old version. Please use the latest, v2.7.5

Anyway, you can use GitHub - caddyserver/replace-response: Caddy module that performs replacements in response bodies as a basis for your own plugin, or just use that instead if it does what you need already.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.