admin管理员组文章数量:1122846
I have the LittleProxy
sample, that is used for replacing the response's HTTP headers when it is intercepted and sent back:
package com.example.proxy;
in pom.xml we have the dependency:
<dependency>
<groupId>org.littleshoot</groupId>
<artifactId>littleproxy</artifactId>
<version>1.1.2</version>
</dependency>
and then the classes. The main class:
import org.littleshoot.proxy.HttpProxyServer;
import org.littleshoot.proxy.HttpProxyServerBootstrap;
import org.littleshoot.proxy.extras.SelfSignedMitmManager;
import org.littleshoot.proxy.impl.DefaultHttpProxyServer;
public class LittleProxyExample {
public static void main(String[] args) {
// Настраиваем и запускаем прокси-сервер
HttpProxyServer proxyServer = org.littleshoot.proxy.impl.DefaultHttpProxyServer.bootstrap()
.withPort(8081) // Указываем порт, на котором будет работать прокси
.withManInTheMiddle(new SelfSignedMitmManager()) // Добавляем обработчик запросов
.withFiltersSource(new RequestInterceptor())
.start();
System.out.println("Proxy server started on port 8081");
}
}
And the interceptor:
package com.example.proxy;
import ioty.buffer.CompositeByteBuf;
import ioty.buffer.Unpooled;
import ioty.handler.codec.DecoderResult;
import ioty.handler.codec.http.*;
import ioty.util.CharsetUtil;
import org.littleshoot.proxy.HttpFiltersAdapter;
import org.littleshoot.proxy.HttpFiltersSourceAdapter;
import java.nio.charset.StandardCharsets;
public class RequestInterceptor extends HttpFiltersSourceAdapter {
@Override
public HttpFiltersAdapter filterRequest(HttpRequest originalRequest) {
return new HttpFiltersAdapter(originalRequest) {
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
if (httpObject instanceof HttpRequest) {
HttpRequest request = (HttpRequest) httpObject;
// replace the header
request.headers().set("User-Agent", "LittleProxy/Example");
// logging the query
System.out.println("Modified Request: " + request.uri());
}
return null; // going ahead
}
@Override
public HttpObject serverToProxyResponse(HttpObject httpObject) {
// updating headers of the response here
HttpResponse response = (HttpResponse) httpObject;
response.headers().set("Test", "Test");
return super.serverToProxyResponse(response);
}
};
}
@Override
public int getMaximumRequestBufferSizeInBytes() {
return 10 * 1024 * 1024;
}
@Override
public int getMaximumResponseBufferSizeInBytes() {
return 10 * 1024 * 1024;
}
}
So I'd like to cut the second half of the response data off (to test some client in order will it crash on the response with the broken structure). Also I'd like to apply this cut only for the specific URI of the reuest from the client to the server. And HttpResponse
object doesn't have any API for that.
How can I get it?
I have the LittleProxy
sample, that is used for replacing the response's HTTP headers when it is intercepted and sent back:
package com.example.proxy;
in pom.xml we have the dependency:
<dependency>
<groupId>org.littleshoot</groupId>
<artifactId>littleproxy</artifactId>
<version>1.1.2</version>
</dependency>
and then the classes. The main class:
import org.littleshoot.proxy.HttpProxyServer;
import org.littleshoot.proxy.HttpProxyServerBootstrap;
import org.littleshoot.proxy.extras.SelfSignedMitmManager;
import org.littleshoot.proxy.impl.DefaultHttpProxyServer;
public class LittleProxyExample {
public static void main(String[] args) {
// Настраиваем и запускаем прокси-сервер
HttpProxyServer proxyServer = org.littleshoot.proxy.impl.DefaultHttpProxyServer.bootstrap()
.withPort(8081) // Указываем порт, на котором будет работать прокси
.withManInTheMiddle(new SelfSignedMitmManager()) // Добавляем обработчик запросов
.withFiltersSource(new RequestInterceptor())
.start();
System.out.println("Proxy server started on port 8081");
}
}
And the interceptor:
package com.example.proxy;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.DecoderResult;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;
import org.littleshoot.proxy.HttpFiltersAdapter;
import org.littleshoot.proxy.HttpFiltersSourceAdapter;
import java.nio.charset.StandardCharsets;
public class RequestInterceptor extends HttpFiltersSourceAdapter {
@Override
public HttpFiltersAdapter filterRequest(HttpRequest originalRequest) {
return new HttpFiltersAdapter(originalRequest) {
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
if (httpObject instanceof HttpRequest) {
HttpRequest request = (HttpRequest) httpObject;
// replace the header
request.headers().set("User-Agent", "LittleProxy/Example");
// logging the query
System.out.println("Modified Request: " + request.uri());
}
return null; // going ahead
}
@Override
public HttpObject serverToProxyResponse(HttpObject httpObject) {
// updating headers of the response here
HttpResponse response = (HttpResponse) httpObject;
response.headers().set("Test", "Test");
return super.serverToProxyResponse(response);
}
};
}
@Override
public int getMaximumRequestBufferSizeInBytes() {
return 10 * 1024 * 1024;
}
@Override
public int getMaximumResponseBufferSizeInBytes() {
return 10 * 1024 * 1024;
}
}
So I'd like to cut the second half of the response data off (to test some client in order will it crash on the response with the broken structure). Also I'd like to apply this cut only for the specific URI of the reuest from the client to the server. And HttpResponse
object doesn't have any API for that.
How can I get it?
Share Improve this question asked Nov 21, 2024 at 20:03 EljahEljah 5,1027 gold badges60 silver badges108 bronze badges1 Answer
Reset to default 0FullHttpResponse
is needed to manipulate the body of the http response.
@Override
public HttpObject serverToProxyResponse(HttpObject httpObject) {
HttpResponse response = (HttpResponse) httpObject;
response.headers().set("Test", "Test");
if (httpObject instanceof FullHttpResponse) {
System.out.println("FullHttpResponse ----------------------------------------");
FullHttpResponse fullHttpResponse = (FullHttpResponse) response;
//I'd like to do if for very specific request and tha't how I can filter for its URL
if (originalRequest.uri().toString().contains("search")) {
String responseBody = fullHttpResponse.content().toString(StandardCharsets.UTF_8);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
response.setStatus(HttpResponseStatus.BAD_GATEWAY);
int newLength = responseBody.length() / 2;
String truncatedBody = responseBody.substring(0, newLength);
//here we replace the response with the reduced body
fullHttpResponse.content().clear().writeBytes(Unpooled.copiedBuffer(truncatedBody, StandardCharsets.UTF_8));
}
}
return super.serverToProxyResponse(response);
}
};
}
本文标签: javaHow to make LittleProxy to cut off the half of the HTTP response39s bodyStack Overflow
版权声明:本文标题:java - How to make LittleProxy to cut off the half of the HTTP response's body? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736307506a1933333.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论