前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Netty 4.1.55版本文件上传参数接受时死循环 cpu100%的bug记录

Netty 4.1.55版本文件上传参数接受时死循环 cpu100%的bug记录

作者头像
heasy3
发布2021-12-06 13:36:16
6050
发布2021-12-06 13:36:16
举报

由于使用了2.4.1版本的springbootstarter,声明的netty版本为4.1.55 在使用netty进行http服务时,接受参数get方法一切正常 但post方法中的form-data方式一直无法接受参数,程序会卡在

代码语言:javascript
复制
new HttpPostRequestDecoder(request)

这一句,同时cpu观察到100%,猜测为死循环。跟踪代码发现在

代码语言:javascript
复制
private static int findDelimiter(ByteBuf undecodedChunk, String delimiter, int offset) {
        ....
        while (delimiterNotFound && newOffset + delimeterLength <= toRead) {

处一直循环无法退出,搜索到相同问题:https://github.com/netty/netty/issues/10851

只有form-data方式存在这个问题,x-www-form-urlencoded可以正常使用。

最终升级了netty到当前最新的4.1.65 问题解决。

另附接受参数代码:

代码语言:javascript
复制
private Map<String, String> getParamMap(ChannelHandlerContext ctx, FullHttpRequest request) throws IOException, TipException {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("url", StrUtil.subBefore(request.uri(), "?", false));
        if (HttpMethod.GET == request.method()) {
            try {
                QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
                decoder.parameters().forEach((k, v) -> paramMap.put(k, v.get(0)));
            } catch (Exception e) {
                log.error(request.uri() + " 解析参数出错");
                throw new ParamErrorException();
            }
        } else if (HttpMethod.POST == request.method()) {
            final String contentType = request.headers().get(HttpHeaderNames.CONTENT_TYPE);
            if (StrUtil.equals(HttpHeaderValues.APPLICATION_JSON, contentType)) {
                String json = request.content().toString(Charsets.toCharset(CharEncoding.UTF_8));
                try {
                    Map<String, String> map = JsonUtil.fromJson(json, JsonUtil.getMapType(String.class, String.class));
                    if (map != null) {
                        paramMap.putAll(map);
                    }
                } catch (Exception e) {
                    log.error(request.uri() + " 解析参数出错:" + json);
                    throw new ParamErrorException();
                }
            } else {
                HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(request);
                List<InterfaceHttpData> httpPostData = decoder.getBodyHttpDatas();
                for (InterfaceHttpData data : httpPostData) {
                    if (data.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) {
                        Attribute attribute = (Attribute) data;
                        paramMap.put(attribute.getName(), attribute.getValue());
                    } else if (data.getHttpDataType() == InterfaceHttpData.HttpDataType.FileUpload) {
                        final FileUpload fileUpload = (FileUpload) data;
                        //这里处理文件参数逻辑
                    }
                }
            }
        } else {
            throw new TipException("不支持的请求方法");
        }
        return paramMap;
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-07-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档