mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add HttpHeaderUtil
This commit is contained in:
parent
85c685b46a
commit
44da9c3488
@ -18,18 +18,21 @@ package org.dromara.hutool.http.meta;
|
|||||||
|
|
||||||
import org.dromara.hutool.core.collection.CollUtil;
|
import org.dromara.hutool.core.collection.CollUtil;
|
||||||
import org.dromara.hutool.core.map.CaseInsensitiveMap;
|
import org.dromara.hutool.core.map.CaseInsensitiveMap;
|
||||||
|
import org.dromara.hutool.core.net.url.UrlDecoder;
|
||||||
import org.dromara.hutool.core.regex.ReUtil;
|
import org.dromara.hutool.core.regex.ReUtil;
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
import org.dromara.hutool.core.text.split.SplitUtil;
|
import org.dromara.hutool.core.text.split.SplitUtil;
|
||||||
|
import org.dromara.hutool.core.util.CharsetUtil;
|
||||||
import org.dromara.hutool.core.util.ObjUtil;
|
import org.dromara.hutool.core.util.ObjUtil;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HTTP头相关方法<br>
|
* HTTP头相关方法<br>
|
||||||
* 相关规范见:https://www.rfc-editor.org/rfc/rfc5987
|
* 相关规范见:https://www.rfc-editor.org/rfc/rfc5987
|
||||||
* TODO Rfc5987 extended value需要单独解析
|
|
||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
* @since 6.0.0
|
* @since 6.0.0
|
||||||
@ -114,12 +117,83 @@ public class HttpHeaderUtil {
|
|||||||
* @return 结果值
|
* @return 结果值
|
||||||
*/
|
*/
|
||||||
private static String getRfc5987Value(final String value) {
|
private static String getRfc5987Value(final String value) {
|
||||||
final List<String> split = SplitUtil.split(value, "'");
|
return ExtendedValue.of(value).getDecodeValue();
|
||||||
if(3 == split.size()){
|
|
||||||
return split.get(2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 普通值
|
/**
|
||||||
return StrUtil.unWrap(value, '"');
|
* 根据rfc5987的扩展值,格式为:
|
||||||
|
* <pre>{@code
|
||||||
|
* 编码'语言'值
|
||||||
|
* }</pre>
|
||||||
|
*/
|
||||||
|
public static class ExtendedValue implements Serializable {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建扩展值
|
||||||
|
*
|
||||||
|
* @param value 扩展值或普通值
|
||||||
|
* @return 扩展值
|
||||||
|
*/
|
||||||
|
public static ExtendedValue of(final String value) {
|
||||||
|
return new ExtendedValue(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Charset charset;
|
||||||
|
private String language;
|
||||||
|
private final String value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造
|
||||||
|
*
|
||||||
|
* @param value 扩展值或普通值
|
||||||
|
*/
|
||||||
|
public ExtendedValue(final String value) {
|
||||||
|
final List<String> split = SplitUtil.split(value, "'");
|
||||||
|
if (3 == split.size()) {
|
||||||
|
this.charset = CharsetUtil.charset(split.get(0));
|
||||||
|
this.language = split.get(1);
|
||||||
|
this.value = split.get(2);
|
||||||
|
} else {
|
||||||
|
this.value = StrUtil.unWrap(value, '"');
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取编码,无定义为{@code null}
|
||||||
|
*
|
||||||
|
* @return 编码,无定义为{@code null}
|
||||||
|
*/
|
||||||
|
public Charset getCharset() {
|
||||||
|
return charset;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取语言
|
||||||
|
*
|
||||||
|
* @return 语言
|
||||||
|
*/
|
||||||
|
public String getLanguage() {
|
||||||
|
return language;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取原始值
|
||||||
|
*
|
||||||
|
* @return 原始值
|
||||||
|
*/
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取解码后的值
|
||||||
|
*
|
||||||
|
* @return 解码后的值
|
||||||
|
*/
|
||||||
|
public String getDecodeValue() {
|
||||||
|
return UrlDecoder.decode(value, charset);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ public class HttpHeaderUtilTest {
|
|||||||
headers.put(HeaderName.CONTENT_DISPOSITION.getValue(),
|
headers.put(HeaderName.CONTENT_DISPOSITION.getValue(),
|
||||||
ListUtil.of("attachment; filename*=utf-8''%E6%B5%8B%E8%AF%95.xlsx; filename=\"æµ\u008Bè¯\u0095.xlsx\""));
|
ListUtil.of("attachment; filename*=utf-8''%E6%B5%8B%E8%AF%95.xlsx; filename=\"æµ\u008Bè¯\u0095.xlsx\""));
|
||||||
final String fileNameFromDisposition = HttpHeaderUtil.getFileNameFromDisposition(headers, null);
|
final String fileNameFromDisposition = HttpHeaderUtil.getFileNameFromDisposition(headers, null);
|
||||||
Assertions.assertEquals("%E6%B5%8B%E8%AF%95.xlsx", fileNameFromDisposition);
|
Assertions.assertEquals("测试.xlsx", fileNameFromDisposition);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -24,7 +24,7 @@ public class HttpHeaderUtilTest {
|
|||||||
headers.put(HeaderName.CONTENT_DISPOSITION.getValue(),
|
headers.put(HeaderName.CONTENT_DISPOSITION.getValue(),
|
||||||
ListUtil.of("attachment; filename*=utf-8''%E6%B5%8B%E8%AF%95.xlsx"));
|
ListUtil.of("attachment; filename*=utf-8''%E6%B5%8B%E8%AF%95.xlsx"));
|
||||||
final String fileNameFromDisposition = HttpHeaderUtil.getFileNameFromDisposition(headers, null);
|
final String fileNameFromDisposition = HttpHeaderUtil.getFileNameFromDisposition(headers, null);
|
||||||
Assertions.assertEquals("%E6%B5%8B%E8%AF%95.xlsx", fileNameFromDisposition);
|
Assertions.assertEquals("测试.xlsx", fileNameFromDisposition);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user