Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

让WhaleFilter支持form的hidden项 #1616

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ Nutz遵循Apache协议,完全开源,文档齐全,永远免费(商用也是)
### Maven 资源

```xml
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutz</artifactId>
<version>1.r.69.v20220703</version>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutz</artifactId>
<version>1.r.72</version>
</dependency>
```

详情: [https://nutzam.com/core/basic/maven.html](https://nutzam.com/core/basic/maven.html)
Expand Down
14 changes: 12 additions & 2 deletions doc/manual/mvc/whale_filter.man
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@ http方法隐式声明
...
</form>
}}}


或者直接把这个字段放到 form 的 hidden 里面

{{{<html>
<form action="/admin/topic/23" method="post">
<input type="hidden" name="_method" value="delete" />
</form>
}}}

对应这样的入口方法

{{{<JAVA>
Expand All @@ -86,8 +94,10 @@ http方法隐式声明
http.hidden_method_param=_method
}}}

对应的value仅支持「PUT」、「DELETE」、「PATCH」(不区分大小写),除此之外将会无效.

顺带说一下, 还有一种方式, "X-HTTP-Method-Override", 可通过下面的配置开启.

{{{
http.method_override=true
}}}
}}}
32 changes: 16 additions & 16 deletions src/org/nutz/mvc/WhaleFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
Expand All @@ -30,7 +30,6 @@
import org.nutz.lang.Lang;
import org.nutz.lang.Mirror;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.log.LogAdapter;
import org.nutz.log.Logs;
import org.nutz.mvc.upload.FastUploading;
Expand All @@ -49,6 +48,7 @@ public class WhaleFilter implements Filter {
private static WhaleFilter _me;
protected ServletContext sc;
protected Object uc;
private static final List<String> ALLOWED_METHODS = Arrays.asList("DELETE", "PUT", "PATCH");

public static WhaleFilter me() {
return _me;
Expand Down Expand Up @@ -134,29 +134,29 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
// 设置resp的编码
if (outputEnc != null)
resp.setCharacterEncoding(outputEnc);

// 如果是POST请求,有很多可以hack的东西
if ("POST".equals(req.getMethod())) {
String methodValue = null;
// 处理隐藏HTTP METHOD, _method参数模式
if (methodParam != null) {
String qs = req.getQueryString();
if (qs != null && qs.contains("_method=")) {
final NutMap map = Mvcs.toParamMap(new StringReader(qs), inputEnc == null ? Charset.defaultCharset().name() : inputEnc);
methodValue = req.getParameter(methodParam);
}
// 处理 X-HTTP-Method-Override
else if (allowHTTPMethodOverride) {
methodValue = req.getHeader("X-HTTP-Method-Override");
}

// 取得到method的值的时候进行处理
if (!Strings.isEmpty(methodValue)) {
String method = methodValue.toUpperCase(Locale.ENGLISH);
if (ALLOWED_METHODS.contains(method)) {
request = new HttpServletRequestWrapper(req) {
public String getMethod() {
return map.getString(methodParam);
return method;
}
};
}
}
// 处理 X-HTTP-Method-Override
else if (allowHTTPMethodOverride && req.getHeader("X-HTTP-Method-Override") != null) {
request = new HttpServletRequestWrapper(req) {
public String getMethod() {
return req.getHeader("X-HTTP-Method-Override");
}
};
}

// 处理文件上传
String contentType = req.getContentType();
Expand Down