1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.rewrite.handler;
20
21 import java.io.IOException;
22
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.eclipse.jetty.util.URIUtil;
27 import org.eclipse.jetty.util.log.Log;
28 import org.eclipse.jetty.util.log.Logger;
29
30
31
32
33
34
35
36
37
38
39
40 public class ValidUrlRule extends Rule
41 {
42 private static final Logger LOG = Log.getLogger(ValidUrlRule.class);
43
44 String _code = "400";
45 String _reason = "Illegal Url";
46
47 public ValidUrlRule()
48 {
49 _handling = true;
50 _terminating = true;
51 }
52
53
54
55
56
57
58
59
60 public void setCode(String code)
61 {
62 _code = code;
63 }
64
65
66
67
68
69
70
71 public void setReason(String reason)
72 {
73 _reason = reason;
74 }
75
76 @Override
77 public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
78 {
79
80
81 String uri = URIUtil.decodePath(request.getRequestURI());
82
83 for (int i = 0; i < uri.length();)
84 {
85 int codepoint = uri.codePointAt(i);
86
87 if (!isValidChar(uri.codePointAt(i)))
88 {
89
90 int code = Integer.parseInt(_code);
91
92
93 if (code >= 400)
94 {
95 response.sendError(code,_reason);
96 }
97 else
98 {
99 response.setStatus(code);
100 }
101
102
103 return target;
104 }
105 i += Character.charCount(codepoint);
106 }
107
108
109 return null;
110 }
111
112 protected boolean isValidChar(int codepoint)
113 {
114 Character.UnicodeBlock block = Character.UnicodeBlock.of(codepoint);
115
116 LOG.debug("{} {} {} {}", Character.charCount(codepoint), codepoint, block, Character.isISOControl(codepoint));
117
118 return (!Character.isISOControl(codepoint)) && block != null && block != Character.UnicodeBlock.SPECIALS;
119 }
120
121 public String toString()
122 {
123 return super.toString() + "[" + _code + ":" + _reason + "]";
124 }
125 }