diff --git a/doc/configuration.md b/doc/configuration.md index e73fde7..895831c 100644 --- a/doc/configuration.md +++ b/doc/configuration.md @@ -165,6 +165,36 @@ RUM SDK injection applies only to responses whose `Content-Type` media type is ` The check is case-insensitive and accepts parameters, such as `text/html; charset=utf-8`. Responses with a missing or different media type are left unchanged. +### Compressed HTML + +The module does not inject into a response that already has a `Content-Encoding` header, because +it cannot read compressed HTML. The response is sent unchanged. This often happens with +`mod_proxy`: Apache forwards the client `Accept-Encoding: gzip` header, and the backend returns +gzip HTML. + +To inject and still send compressed HTML to the client, let `mod_deflate` decompress the HTML +before the module and compress it again after: + +``` +LoadModule deflate_module modules/mod_deflate.so +LoadModule filter_module modules/mod_filter.so + + + ProxyPass "http://backend:8080/" + AddOutputFilterByType INFLATE;DEFLATE text/html + +``` + +`INFLATE` runs before the module and `DEFLATE` runs after it. Other content types are not changed. +Clients that do not accept gzip get uncompressed HTML. Each HTML response is decompressed and +compressed again, which uses CPU. + +`INFLATE` decodes only gzip. HTML compressed another way (for example `br`) is still not injected. + +> [!NOTE] +> `RequestHeader unset Accept-Encoding` also makes the backend send uncompressed HTML. But +> `mod_deflate` reads the same header, so the response to the client is not compressed either. + ## `DatadogRumSettings` section - **Description**: Container for Datadog RUM settings - **Syntax:**: diff --git a/test/rum/integration-test/test_rum_streaming.py b/test/rum/integration-test/test_rum_streaming.py index 7507e35..1c2367e 100644 --- a/test/rum/integration-test/test_rum_streaming.py +++ b/test/rum/integration-test/test_rum_streaming.py @@ -85,7 +85,13 @@ def streaming_proxy(server: Server, log_dir: str, module_path: str, mode: str) - with ExitStack() as cleanup: docroot = cleanup.enter_context(tempfile.TemporaryDirectory(prefix="rum-streaming-")) proxy = cleanup.enter_context(upstream_proxy(StreamingUpstream)) - if mode != "no-deflate": + if mode == "inflate-deflate": + proxy += ( + "\nLoadModule deflate_module modules/mod_deflate.so" + "\nLoadModule filter_module modules/mod_filter.so" + "\nAddOutputFilterByType INFLATE;DEFLATE text/html\n" + ) + elif mode != "no-deflate": proxy += "\nLoadModule deflate_module modules/mod_deflate.so\nSetOutputFilter DEFLATE\n" cleanup.callback(server.stop, conf_path) configuration = { @@ -153,3 +159,13 @@ def test_rum_streaming_keeps_precompressed_body(streaming_proxy: Server) -> None assert response.headers.get("Content-Encoding") == "gzip" assert response.headers.get("x-datadog-rum-injected") != "1" assert response.raw.read() == PRECOMPRESSED_BODY + + +@pytest.mark.requires_rum +@pytest.mark.parametrize("mode", ["inflate-deflate"], indirect=True) +def test_rum_streaming_injects_after_inflate(streaming_proxy: Server) -> None: + with open_stream(streaming_proxy, "/upstream/precompressed", "gzip") as response: + assert response.status_code == 200 + assert response.headers.get("Content-Encoding") == "gzip" + assert response.headers.get("x-datadog-rum-injected") == "1" + assert b"DD_RUM" in response.content