Files
ladybird/Services/RequestServer/ResourceSubstitutionMap.h
Andreas Kling 18aee32084 RequestServer: Add --resource-map option for URL-to-file substitution
This adds support for intercepting network requests and serving local
file content instead. When a URL matches an entry in the substitution
map, the local file is served while preserving the original URL's
origin for cross-origin checks.

Usage:
    Ladybird --resource-map=/path/to/map.json

The JSON file format is:
    {
      "substitutions": [
        {
          "url": "https://example.com/script.js",
          "file": "/path/to/local/script.js",
          "content_type": "application/javascript",
          "status_code": 200
        }
      ]
    }

Fields:
  - url (required): Exact URL to intercept (query string and fragment
    are stripped before matching)
  - file (required): Absolute path to local file to serve
  - content_type (optional): Override Content-Type header (defaults to
    guessing from filename)
  - status_code (optional): HTTP status code (defaults to 200)

This is incredibly useful for debugging production websites: you can
intercept any script, stylesheet, or other resource and replace it with
a local copy containing your own debug instrumentation, console.log
statements, or experimental fixes - all without modifying the actual
site or setting up a local dev server.
2026-01-19 10:23:26 +01:00

37 lines
754 B
C++

/*
* Copyright (c) 2026, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/ByteString.h>
#include <AK/HashMap.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <LibURL/URL.h>
namespace RequestServer {
struct ResourceSubstitution {
ByteString file_path;
Optional<String> content_type;
u32 status_code { 200 };
};
class ResourceSubstitutionMap {
public:
static ErrorOr<NonnullOwnPtr<ResourceSubstitutionMap>> load_from_file(StringView path);
Optional<ResourceSubstitution const&> lookup(URL::URL const&) const;
private:
ResourceSubstitutionMap() = default;
HashMap<String, ResourceSubstitution> m_substitutions;
};
}