Skip to content

A free, open-source alternative to Burp Suite: an intercepting proxy and web app security testing suite with Repeater, Intruder, Collaborator and a built-in OWASP ZAP scanner. Linux, AGPL-3.0.

2.1 KB
Raw Blame
package opensuite.script;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import opensuite.core.HttpMessage;

/**
 * What changed between two versions of a request, so an action's change is
 * never silent: headers added ({@code + Name}), changed ({@code ~ Name}) and
 * removed ({@code - Name}), then the body's size before and after.
 */
public final class RequestDiff {

    private RequestDiff() {}

    public static List<String> summarise(HttpMessage before, HttpMessage after) {
        List<String> out = new ArrayList<>();
        if (Arrays.equals(before.raw(), after.raw())) return out;
        if (!before.startLine().equals(after.startLine())) out.add("~ request line");

        Map<String, Header> was = headers(before), now = headers(after);
        for (Map.Entry<String, Header> e : now.entrySet()) {
            if (!was.containsKey(e.getKey())) out.add("+ " + e.getValue().name);
        }
        for (Map.Entry<String, Header> e : now.entrySet()) {
            Header old = was.get(e.getKey());
            if (old != null && !old.values.equals(e.getValue().values)) out.add("~ " + e.getValue().name);
        }
        for (Map.Entry<String, Header> e : was.entrySet()) {
            if (!now.containsKey(e.getKey())) out.add("- " + e.getValue().name);
        }
        byte[] b = before.body(), a = after.body();
        if (!Arrays.equals(b, a)) out.add("body " + b.length + " -> " + a.length + " bytes");
        return out;
    }

    /** A header as first spelled, with every value it has, in order. */
    private record Header(String name, List<String> values) {}

    private static Map<String, Header> headers(HttpMessage m) {
        Map<String, Header> map = new LinkedHashMap<>();
        for (String l : m.headerLines()) {
            int c = l.indexOf(':');
            if (c <= 0) continue;
            String name = l.substring(0, c).strip();
            map.computeIfAbsent(name.toLowerCase(Locale.ROOT), k -> new Header(name, new ArrayList<>()))
               .values.add(l.substring(c + 1).strip());
        }
        return map;
    }
}