Repository navigation
Expand file tree
/
Copy pathRequestImplBase.java
More file actions
126 lines (112 loc) · 3.18 KB
/
Copy pathRequestImplBase.java
File metadata and controls
126 lines (112 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package act;
import act.conf.AppConfig;
import org.osgl.$;
import org.osgl.http.H;
import org.osgl.util.E;
import org.osgl.util.FastStr;
import org.osgl.util.S;
public abstract class RequestImplBase<T extends H.Request> extends H.Request<T> {
private AppConfig cfg;
private H.Method method;
private String path;
private String query;
private Boolean secure;
protected RequestImplBase(AppConfig config) {
E.NPE(config);
cfg = config;
}
protected abstract String _uri();
protected abstract H.Method _method();
@Override
public String contextPath() {
return cfg.urlContext();
}
protected final boolean hasContextPath() {
String ctxPath = contextPath();
return S.notBlank(ctxPath) && !"/".equals(ctxPath);
}
@Override
public T method(H.Method method) {
this.method = $.NPE(method);
return me();
}
@Override
public H.Method method() {
if (null == method) {
method = _method();
if (method == H.Method.POST) {
// check the method overload
String s = header(H.Header.Names.X_HTTP_METHOD_OVERRIDE);
if (S.blank(s)) {
s = paramVal("_method"); // Spring use this
}
if (S.notBlank(s)) {
method = H.Method.valueOfIgnoreCase(s);
}
}
}
return method;
}
@Override
public String path() {
if (null == path) {
parseUri();
}
return path;
}
@Override
public String query() {
if (null == query) {
parseUri();
}
return query;
}
@Override
public boolean secure() {
if (null == secure) {
if ("https".equals(cfg.xForwardedProtocol())) {
secure = true;
} else {
secure = parseSecureXHeaders();
}
}
return secure;
}
protected void parseUri() {
FastStr fs = FastStr.unsafeOf(_uri());
if (fs.startsWith("http")) {
// the uri include the scheme, domain and port
fs = fs.afterFirst("://"); // strip the scheme
fs = fs.afterFirst('/'); // strip the domain className, port
fs = fs.prepend('/'); // attach the '/' to the path
}
if (hasContextPath()) {
fs = fs.after(contextPath());
}
if (fs.contains('?')) {
path = fs.beforeFirst('?').toString();
query = fs.afterFirst('?').toString();
} else {
path = fs.toString();
}
}
private boolean parseSecureXHeaders() {
String s = header(H.Header.Names.X_FORWARDED_PROTO);
if ("https".equals(s)) {
return true;
}
s = header(H.Header.Names.X_FORWARDED_SSL);
if ("on".equals(s)) {
return true;
}
s = header(H.Header.Names.FRONT_END_HTTPS);
if ("on".equals(s)) {
return true;
}
s = header(H.Header.Names.X_URL_SCHEME);
if ("https".equals(s)) {
return true;
}
return false;
}
}