在不使用POST请求的情况下阅读POST请求
在“底拖”中,我有两个操纵器,它们是链接的:在不使用POST请求的情况下阅读POST请求,post,request,undertow,Post,Request,Undertow,在“底拖”中,我有两个操纵器,它们是链接的: 第一个处理程序读取请求,然后通过next.handleRequest(exchange)调用第二个处理程序 第二个处理程序是代理处理程序,它将请求发送到处理请求的外部服务器 我的问题是读取请求的第一个处理程序。请求头没什么大不了的,但是获取POST请求的主体数据是一个问题 问题中所示的现有解决方案使用请求体su,从而使处理程序链接不再工作 如何读取请求正文数据而不使用它或以处理程序链随后无法工作的方式更改请求?我发现了问题,最后是缺少对ByteBuf
next.handleRequest(exchange)调用第二个处理程序代码>
如何读取请求正文数据而不使用它或以处理程序链随后无法工作的方式更改请求?我发现了问题,最后是缺少对
ByteBuffer.flip()的调用
如果有人需要此类POST数据读取器,可以使用以下简化的AbstractStreamSourceConductor
实现,该实现能够读取传入的POST数据,而无需使用它:
exchange.addRequestWrapper(new ConduitWrapper<StreamSourceConduit>() {
@Override
public StreamSourceConduit wrap(ConduitFactory<StreamSourceConduit> factory, HttpServerExchange exchange) {
StreamSourceConduit source = factory.create();
return new AbstractStreamSourceConduit<StreamSourceConduit>(source) {
ByteArrayOutputStream bout = new ByteArrayOutputStream(8192);
@Override
public int read(ByteBuffer dst) throws IOException {
int x = super.read(dst);
if (x >= 0) {
ByteBuffer dup = dst.duplicate();
dup.flip();
byte[] data = new byte[x];
dup.get(data);
bout.write(data);
} else {
// end of stream reached
byte[] data = bout.toByteArray();
// ... to something with data
}
return x;
}
};
}
});
exchange.addRequestWrapper(新的ConduitWrapper(){
@凌驾
公共StreamSource导管包装(导管工厂,HttpServerExchange){
streamsourceconductor source=factory.create();
返回新的AbstractStreamSourceConductor(源){
ByteArrayOutputStream bout=新的ByteArrayOutputStream(8192);
@凌驾
公共整数读取(ByteBuffer dst)引发IOException{
int x=超级读取(dst);
如果(x>=0){
ByteBuffer dup=dst.duplicate();
dup.flip();
字节[]数据=新字节[x];
重复获取(数据);
写(数据);
}否则{
//已到达终点
byte[]data=bout.toByteArray();
//…与数据有关的东西
}
返回x;
}
};
}
});