-
Notifications
You must be signed in to change notification settings - Fork 6
4. How to manipulate requests
By implementing the Request Interceptor, via its process method we will have access to the actual request. The request is available via the org.rockhill.mitm.proxy.http.MitmJavaProxyHttpRequest class. Let's assume that the request will be available via the request variable:
public void process(final MitmJavaProxyHttpRequest request) {
//here you may work with the "request"
}
Main manipulation methods are the followings:
Header access
Header[] allHeaders = request.getMethod().getAllHeaders(); //to get all headers
Header header = request.getMethod().getFirstHeader(String headerKey); //get first header with the specified key
Header header = request.getMethod().getLastHeader(String headerKey); //get last header with the specified key
request.getMethod.addHeader(String headerKey, String headerValue); //adds a new or updates an existing header
request.getMethod.removeHeader(Header header); //removes the specified header
Body access
The message body will be accessible as an InputStream, this way:
InputStream clonedInputStream = request.getPlayGround();
Since this InputStream is used by the Proxy, if you read it, you must restore its initial state in order to let the Proxy read it again. Like:
String body = IOUtils.toString(clonedInputStream);
clonedInputStream.reset();
Of course, if the request is encoded (like gzip), you need to decode it too.
Body manipulation
The request body can be manipulated too. You must (well) prepare the response as byte[], then use the following approach to update the request:
byte[] newBody = //here prepare your new body
if (newBody != null) {
if (request.getMethod() instanceof HttpEntityEnclosingRequestBase) {
HttpEntityEnclosingRequestBase enclosingRequest = (HttpEntityEnclosingRequestBase) request.getMethod();
enclosingRequest.setEntity(new ByteArrayEntity(newBody));
}
}
Overwrite the target URI
The actual URI can be accessed this way:
URI uri = request.getMethod().getURI();
Here you may manipulate the URI as you want, you may even generate and use a brand new URI:
request.getMethod().setURI(URI newURI);
BUT! Beware that if you change the port or the target server name/IP, that will really ask the Proxy to use the new/updated URI, so the request will be targeted to another server, not to the original one.
Specifying the response volatility
Response volatility (if the responses can be altered or not) shall be declared in advance. The general approach is determined via Proxy settings, but can be overwritten latest during the process of the request. So to set whether the response will be volatile or not, can be set by this command:
request.setResponseVolatile(boolean isVolatile);
Get Message ID
Every request and response has a unique ID generated by the Proxy. With this information the requests can be connected to right responses easily.
To get the Request ID, call this method: String messageId = request.getMessageId()
Next Step
After accessing and manipulating the request, you are ready to access and manipulate the responses too.