Netty POST 表单输入类型
Netty POST form input types
我在从 netty 中收到的 POST 获取输入类型时遇到了一些麻烦。我有一个 class 从 post 请求中读取所有接收到的属性,我想从表单中过滤掉与提交类型的输入对应的属性。也就是说,如果我有这种形式:
<form action="https://127.0.0.1:10005/firmarMultiplesDocumentos" name="" method="POST" enctype="multipart/form-data">
File: <input type="file" name="File1" /><br>
NHC: <input type="text" name="NHC" value="555555" /><br>
<input type="submit" name="Send" />
</form>
我只想获取 File1 和 NHC 属性并放弃 Send 属性。
如果有帮助,这是我的频道定义:
secureBossGroup = new NioEventLoopGroup();
secureWorkerGroup = new NioEventLoopGroup();
secureServerBootstrap = new ServerBootstrap();
secureServerBootstrap.group(secureBossGroup, secureWorkerGroup)
.channel(NioServerSocketChannel.class) // (3)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addFirst(getSSLContext().newHandler(ch.alloc()));
ch.pipeline().addLast(new HttpServerCodec());
ch.pipeline().addLast("aggregator", new HttpObjectAggregator(100 * 1024 * 1024));
ch.pipeline().addLast(new CustomChannelHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
这是我的 CustomChannelHandler 的一部分:
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
if (msg instanceof FullHttpRequest) {
FullHttpRequest request = fullHttpRequest = (FullHttpRequest) msg;
try {
decoder = new HttpPostRequestDecoder(dataFactory, request);
decoder.setDiscardThreshold(0);
} catch (Exception e) {
// Error Handler
}
}
if (decoder != null) {
if (msg instanceof HttpContent) {
HttpContent chunk = (HttpContent) msg;
try {
decoder.offer(chunk);
} catch (ErrorDataDecoderException e) {
//Error handler
}
// Read data as it becomes available, chunk by chunk.
readChunkByChunk(ctx);
if (chunk instanceof LastHttpContent) {
readChunkByChunk(ctx);
try {
prepareResponse(ctx);
} catch (Exception e){
// Error handler
}
resetPostRequestDecoder();
}
}
} else {
// Error handler
}
}
private void readChunkByChunk(ChannelHandlerContext ctx) {
try {
while (decoder.hasNext()) {
InterfaceHttpData data = decoder.next();
if (data != null) {
try {
processChunk(ctx, data);
} catch (IOException e) {
// Error handler
} finally {
data.release();
}
}
}
} catch (EndOfDataDecoderException e) {
// No more data to decode, that's fine
}
}
private void processChunk(ChannelHandlerContext ctx, InterfaceHttpData data) throws IOException {
LOGGER.debug("HTTP Data Name: {}, Type: {}" + data.getName() + data.getHttpDataType());
switch (data.getHttpDataType()) {
case Attribute:
Attribute attrib = (Attribute) data;
try {
int bytes = attrib.getByteBuf().readableBytes();
String name = attrib.getName();
readData = attrib.getByteBuf().toString(CharsetUtil.UTF_8);
// Attribute Handling
} catch (IOException e) {
// Error handler
}
break;
case FileUpload:
// FileUpload stuff
现在,表单的所有字段都通过 processChunk 的 "Attribute" 案例,所以我猜测是否有可能获得它们的类型以从表单中过滤提交字段。
谢谢,
克里斯.
我相信客户端在基于表单发送请求时不会将类型传递给服务器。所以没有办法知道类型=文本,提交或文件...除了文件因为有特殊处理...
我在从 netty 中收到的 POST 获取输入类型时遇到了一些麻烦。我有一个 class 从 post 请求中读取所有接收到的属性,我想从表单中过滤掉与提交类型的输入对应的属性。也就是说,如果我有这种形式:
<form action="https://127.0.0.1:10005/firmarMultiplesDocumentos" name="" method="POST" enctype="multipart/form-data">
File: <input type="file" name="File1" /><br>
NHC: <input type="text" name="NHC" value="555555" /><br>
<input type="submit" name="Send" />
</form>
我只想获取 File1 和 NHC 属性并放弃 Send 属性。
如果有帮助,这是我的频道定义:
secureBossGroup = new NioEventLoopGroup();
secureWorkerGroup = new NioEventLoopGroup();
secureServerBootstrap = new ServerBootstrap();
secureServerBootstrap.group(secureBossGroup, secureWorkerGroup)
.channel(NioServerSocketChannel.class) // (3)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addFirst(getSSLContext().newHandler(ch.alloc()));
ch.pipeline().addLast(new HttpServerCodec());
ch.pipeline().addLast("aggregator", new HttpObjectAggregator(100 * 1024 * 1024));
ch.pipeline().addLast(new CustomChannelHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
这是我的 CustomChannelHandler 的一部分:
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
if (msg instanceof FullHttpRequest) {
FullHttpRequest request = fullHttpRequest = (FullHttpRequest) msg;
try {
decoder = new HttpPostRequestDecoder(dataFactory, request);
decoder.setDiscardThreshold(0);
} catch (Exception e) {
// Error Handler
}
}
if (decoder != null) {
if (msg instanceof HttpContent) {
HttpContent chunk = (HttpContent) msg;
try {
decoder.offer(chunk);
} catch (ErrorDataDecoderException e) {
//Error handler
}
// Read data as it becomes available, chunk by chunk.
readChunkByChunk(ctx);
if (chunk instanceof LastHttpContent) {
readChunkByChunk(ctx);
try {
prepareResponse(ctx);
} catch (Exception e){
// Error handler
}
resetPostRequestDecoder();
}
}
} else {
// Error handler
}
}
private void readChunkByChunk(ChannelHandlerContext ctx) {
try {
while (decoder.hasNext()) {
InterfaceHttpData data = decoder.next();
if (data != null) {
try {
processChunk(ctx, data);
} catch (IOException e) {
// Error handler
} finally {
data.release();
}
}
}
} catch (EndOfDataDecoderException e) {
// No more data to decode, that's fine
}
}
private void processChunk(ChannelHandlerContext ctx, InterfaceHttpData data) throws IOException {
LOGGER.debug("HTTP Data Name: {}, Type: {}" + data.getName() + data.getHttpDataType());
switch (data.getHttpDataType()) {
case Attribute:
Attribute attrib = (Attribute) data;
try {
int bytes = attrib.getByteBuf().readableBytes();
String name = attrib.getName();
readData = attrib.getByteBuf().toString(CharsetUtil.UTF_8);
// Attribute Handling
} catch (IOException e) {
// Error handler
}
break;
case FileUpload:
// FileUpload stuff
现在,表单的所有字段都通过 processChunk 的 "Attribute" 案例,所以我猜测是否有可能获得它们的类型以从表单中过滤提交字段。
谢谢, 克里斯.
我相信客户端在基于表单发送请求时不会将类型传递给服务器。所以没有办法知道类型=文本,提交或文件...除了文件因为有特殊处理...