-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path177.java
More file actions
205 lines (176 loc) · 6.76 KB
/
Copy path177.java
File metadata and controls
205 lines (176 loc) · 6.76 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright 2015 Danilo Reinert
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.reinert.requestor.uri;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gwt.core.client.GWT;
/**
* Parses a URI.
*/
public class UriParser {
private Uri uri;
private UrlCodec urlCodec;
private String scheme;
private String user;
private String password;
private String host;
private int port = -1;
private String[] segments;
private Map<String, Buckets> matrixParams;
private Buckets queryParams;
private String fragment;
private String uriString;
private UriParser() {
this.urlCodec = UrlCodec.getInstance();
}
public Uri getUri() {
if (uri == null)
uri = new Uri(scheme, user, password, host, port, segments, matrixParams, queryParams, fragment, uriString);
return uri;
}
public static UriParser newInstance() {
return new UriParser();
}
public UriParser parse(String uri) {
if (uri == null || uri.isEmpty())
throw new UriParseException("The uri argument cannot be null or empty");
resetParser();
uriString = uri;
String parsedUri = uri;
String query;
int pos;
// Extract fragment
pos = parsedUri.indexOf('#');
if (pos > -1) {
// Check last char
fragment = parsedUri.length() - pos == 1 ? null : urlCodec.decode(parsedUri.substring(pos + 1));
// Remove parsed part from parsing uri
parsedUri = parsedUri.substring(0, pos);
}
// Extract query
pos = parsedUri.indexOf('?');
if (pos > -1) {
// Check last char
query = parsedUri.length() - pos == 1 ? null : parsedUri.substring(pos + 1);
parseQuery(query);
// Remove parsed part from parsing uri
parsedUri = parsedUri.substring(0, pos);
}
// Extract protocol
if (parsedUri.length() > 2 && parsedUri.substring(0, 2).equals("//")) {
// Relative-Scheme
scheme = null;
parsedUri = parsedUri.substring(2);
// Extract "user:pass@host:port"
parsedUri = parseAuthority(parsedUri);
} else {
pos = parsedUri.indexOf("://");
if (pos > -1) {
scheme = pos > 0 ? parsedUri.substring(0, pos) : null;
parsedUri = parsedUri.substring(pos + 3);
// Extract "user:pass@host:port"
parsedUri = parseAuthority(parsedUri);
}
}
// The left part must be the path
final List<String> parsedSegments = new ArrayList<String>();
final String[] rawSegments = parsedUri.split("/");
for (String segment : rawSegments) {
if (!segment.isEmpty()) {
String[] matrixParts = segment.split(";");
final String parsedSegment = urlCodec.decode(matrixParts[0]);
parsedSegments.add(parsedSegment);
if (matrixParts.length > 1) {
if (matrixParams == null) {
matrixParams = new HashMap<String, Buckets>();
}
final Buckets buckets = GWT.create(Buckets.class);
matrixParams.put(parsedSegment, buckets);
for (int i = 1; i < matrixParts.length; i++) {
String[] matrixElements = matrixParts[i].split("=");
if (matrixElements.length == 1) {
buckets.add(urlCodec.decode(matrixElements[0]), null);
} else {
buckets.add(urlCodec.decode(matrixElements[0]), urlCodec.decode(matrixElements[1]));
}
}
}
}
}
this.segments = parsedSegments.toArray(new String[parsedSegments.size()]);
return this;
}
private String parseAuthority(String uri) {
uri = parseUserInfo(uri);
return parseHost(uri);
}
private String parseUserInfo(String uri) {
// Extract username:password
int pathDivider = uri.indexOf('/');
int pos = uri.lastIndexOf('@', pathDivider > -1 ? pathDivider : uri.length() - 1);
// authority@ must come before /path
if (pos > -1 && (pathDivider == -1 || pos < pathDivider)) {
String[] t = uri.substring(0, pos).split(":");
user = !t[0].isEmpty() ? urlCodec.decode(t[0]) : null;
password = t.length > 1 && !t[1].isEmpty() ? urlCodec.decode(t[1]) : null;
uri = uri.substring(pos + 1);
} else {
user = null;
password = null;
}
return uri;
}
private String parseHost(String uri) {
// Extract host:port
int pos = uri.indexOf('/');
if (pos == -1) pos = uri.length();
String[] authority = uri.substring(0, pos).split(":");
host = !authority[0].isEmpty() ? authority[0] : null;
port = authority.length > 1 && !authority[1].isEmpty() ? Integer.parseInt(authority[1]) : -1;
return pos == uri.length() ? "/" : uri.substring(pos);
}
private void parseQuery(String query) {
// throw out the funky business - "?"[name"="value"&"]+
query = query.replaceAll("/&+/g", "&").replaceAll("/^\\?*&*|&+$/g", "");
if (query.isEmpty())
return;
queryParams = GWT.create(Buckets.class);
String[] p, pairs = query.split("&");
String name, value;
for (final String pair : pairs) {
p = pair.split("=");
name = urlCodec.decodeQueryString(p[0]);
// no "=" is null according to http://dvcs.w3.org/hg/url/raw-file/tip/Overview.html#collect-url-parameters
value = p.length > 1 && !p[1].isEmpty() ? urlCodec.decodeQueryString(p[1]) : null;
queryParams.add(name, value);
}
}
private void resetParser() {
uri = null;
scheme = null;
user = null;
password = null;
host = null;
port = -1;
segments = null;
matrixParams = null;
queryParams = null;
fragment = null;
uriString = null;
}
}