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.

3.4 KB
Raw Blame
package opensuite.script.api;

import java.util.List;
import java.util.function.Consumer;

import opensuite.core.HttpMessage;
import opensuite.core.HttpService;
import opensuite.core.Param;

/**
 * A request as a script sees it. It never changes: each {@code with...} method
 * returns a new request, so a script that fails half way has changed nothing.
 * A request that is not changed keeps its exact bytes.
 */
public final class HttpRequest {

    private final HttpService service;
    private final HttpMessage message;

    private HttpRequest(HttpService service, HttpMessage message) {
        this.service = service;
        this.message = message;
    }

    /** A request over a copy of {@code m}. */
    public static HttpRequest of(HttpService service, HttpMessage m) {
        return new HttpRequest(service, m == null ? new HttpMessage(new byte[0]) : m.copy());
    }

    /** A copy of the request's bytes. */
    public HttpMessage message() { return message.copy(); }

    /** Where the request is sent; null when there is no target yet. */
    public HttpService service() { return service; }

    public String method() { return message.method(); }

    /** The path, without the query string. */
    public String path() { return message.path(); }

    /** The query string without its "?", or "". */
    public String query() { return message.query(); }

    public String url() {
        String target = message.requestTarget();
        if (target.startsWith("http://") || target.startsWith("https://")) return target;
        return (service == null ? "" : service.url()) + target;
    }

    /** The first header with this name (any case), or null. */
    public String header(String name) { return message.header(name); }

    /** The header lines, without the request line. */
    public List<String> headers() { return Messages.headers(message); }

    /** The body's exact bytes. */
    public byte[] body() { return message.body(); }

    /** The body as text, in the declared charset or else UTF-8. */
    public String bodyText() { return Messages.bodyText(message); }

    public List<Param> parameters() { return message.parameters(); }

    /** This request with the header set, replacing any of that name. */
    public HttpRequest withHeader(String name, String value) { return edit(m -> m.setHeader(name, value)); }

    public HttpRequest withRemovedHeader(String name) { return edit(m -> m.removeHeader(name)); }

    /** This request with a new body and a matching Content-Length. */
    public HttpRequest withBody(byte[] body) {
        return edit(m -> {
            m.setBody(body == null ? new byte[0] : body.clone());
            m.updateContentLength();
        });
    }

    /** This request with a new body as text, encoded as {@link #bodyText()} decodes. */
    public HttpRequest withBody(String body) {
        return withBody((body == null ? "" : body).getBytes(Messages.charset(message)));
    }

    public HttpRequest withMethod(String method) { return edit(m -> m.setMethod(method)); }

    /** This request with a new path; the query string is kept unless {@code path} has one. */
    public HttpRequest withPath(String path) {
        String q = message.query();
        return edit(m -> m.setRequestTarget(path.contains("?") || q.isEmpty() ? path : path + "?" + q));
    }

    private HttpRequest edit(Consumer<HttpMessage> change) {
        HttpMessage m = message.copy();
        change.accept(m);
        return new HttpRequest(service, m);
    }
}