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 | |
package com.mattunderscore.http.headers.useragent.parser; |
27 | |
|
28 | |
import static com.mattunderscore.http.headers.useragent.parser.ParsingUtils.nextElement; |
29 | |
import static com.mattunderscore.http.headers.useragent.parser.ParsingUtils.nextElementStart; |
30 | |
|
31 | |
import com.mattunderscore.http.headers.useragent.details.application.EncryptionStrength; |
32 | |
import com.mattunderscore.http.headers.useragent.details.hardware.IPad; |
33 | |
import com.mattunderscore.http.headers.useragent.details.hardware.IPhone; |
34 | |
import com.mattunderscore.http.headers.useragent.details.hardware.IPod; |
35 | |
import com.mattunderscore.http.headers.useragent.details.platform.AppleMobileFirmware; |
36 | |
import com.mattunderscore.http.headers.useragent.details.platform.IOS; |
37 | |
import com.mattunderscore.http.headers.useragent.details.platform.IPhoneSimulator; |
38 | |
import com.mattunderscore.http.headers.useragent.details.platform.MacOSX; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
class IOSPlatformParser implements TokensParser |
46 | |
{ |
47 | 2 | private static final IOSBrowserParser iosParser = new IOSBrowserParser(); |
48 | |
|
49 | |
IOSPlatformParser() |
50 | 2 | { |
51 | 2 | } |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
@Override |
58 | |
public void parseTokens(ParsingState state) |
59 | |
{ |
60 | 14 | String header = state.getRemaining(); |
61 | 14 | header = header.trim(); |
62 | 134 | while (!"".equals(header)) |
63 | |
{ |
64 | 120 | String oldHeader = header; |
65 | 120 | if (header.startsWith("CPU OS")) |
66 | |
{ |
67 | 4 | String xVersion = nextElement(header.substring(7)); |
68 | 4 | String version = iOSVersion(xVersion); |
69 | 4 | header = header.substring(7 + xVersion.length()); |
70 | 4 | state.addDetail(new IOS(version)); |
71 | 4 | } |
72 | 116 | else if (header.startsWith("CPU iPhone OS")) |
73 | |
{ |
74 | 10 | String xVersion = nextElement(header.substring(14)); |
75 | 10 | String version = iOSVersion(xVersion); |
76 | 10 | header = header.substring(14 + xVersion.length()); |
77 | 10 | state.addDetail(new IOS(version)); |
78 | 10 | } |
79 | 106 | else if (header.startsWith("iPad")) |
80 | |
{ |
81 | 6 | state.setIPad(true); |
82 | 6 | state.addDetail(new IPad()); |
83 | 6 | header = header.substring(4); |
84 | |
} |
85 | 100 | else if (header.startsWith("iPod")) |
86 | |
{ |
87 | 2 | state.setIPod(true); |
88 | 2 | state.addDetail(new IPod()); |
89 | 2 | header = header.substring(4); |
90 | |
} |
91 | 98 | else if (header.startsWith("iPhone Simulator")) |
92 | |
{ |
93 | 0 | state.setIPhoneSimulator(true); |
94 | 0 | state.addDetail(new IPhoneSimulator()); |
95 | 0 | header = header.substring(16); |
96 | |
} |
97 | 98 | else if (header.startsWith("iPhone")) |
98 | |
{ |
99 | 6 | state.setIPhone(true); |
100 | 6 | state.addDetail(new IPhone()); |
101 | 6 | header = header.substring(6); |
102 | |
} |
103 | 92 | else if (header.startsWith("like Mac OS X")) |
104 | |
{ |
105 | 14 | state.addDetail(new MacOSX("like")); |
106 | 14 | header = header.substring(13); |
107 | |
} |
108 | 78 | else if (header.startsWith("Mobile")) |
109 | |
{ |
110 | 0 | if (state.isIPad() || state.isIPhone() || |
111 | 0 | state.isIPod() || state.isIPhoneSimulator()) |
112 | |
{ |
113 | 0 | String version = nextElement(header.substring(7)); |
114 | 0 | state.addDetail(new AppleMobileFirmware(version)); |
115 | 0 | header = header.substring(7 + version.length()); |
116 | 0 | } |
117 | |
else |
118 | |
{ |
119 | 0 | header = header.substring(6); |
120 | |
} |
121 | |
} |
122 | 78 | else if (header.startsWith("U")) |
123 | |
{ |
124 | 10 | state.addDetail(new EncryptionStrength("USA")); |
125 | 10 | header = header.substring(1); |
126 | |
} |
127 | 68 | else if (header.equals("I")) |
128 | |
{ |
129 | 0 | state.addDetail(new EncryptionStrength("International")); |
130 | 0 | header = header.substring(1); |
131 | |
} |
132 | 68 | else if (header.startsWith("N")) |
133 | |
{ |
134 | 0 | state.addDetail(new EncryptionStrength("None")); |
135 | 0 | header = header.substring(1); |
136 | |
} |
137 | |
|
138 | |
|
139 | 120 | if (header.startsWith(")")) |
140 | |
{ |
141 | 14 | state.setRemaining(header.substring(1)); |
142 | 14 | iosParser.parseTokens(state); |
143 | 14 | header = state.getRemaining(); |
144 | |
} |
145 | 106 | else if (oldHeader.equals(header)) |
146 | |
{ |
147 | 58 | int nextElement = nextElementStart(header); |
148 | 58 | if (nextElement == 0) |
149 | |
{ |
150 | 48 | header = header.substring(1).trim(); |
151 | |
} |
152 | 10 | else if (nextElement != -1) |
153 | |
{ |
154 | 10 | header = header.substring(nextElement).trim(); |
155 | |
} |
156 | |
else |
157 | |
{ |
158 | 0 | return; |
159 | |
} |
160 | |
} |
161 | 120 | } |
162 | 14 | state.setRemaining(header); |
163 | 14 | return; |
164 | |
} |
165 | |
|
166 | |
private static String iOSVersion(final String version) |
167 | |
{ |
168 | 14 | String theVersion = version; |
169 | 14 | String rVersion = ""; |
170 | |
while (true) |
171 | |
{ |
172 | 32 | if ("".equals(theVersion)) |
173 | |
{ |
174 | 0 | return rVersion; |
175 | |
} |
176 | 32 | int next = theVersion.indexOf("_"); |
177 | 32 | if (next != -1) |
178 | |
{ |
179 | 18 | rVersion += theVersion.substring(0,next) + "."; |
180 | 18 | theVersion = theVersion.substring(next+1); |
181 | |
} |
182 | |
else |
183 | |
{ |
184 | 14 | rVersion += theVersion; |
185 | 14 | return rVersion; |
186 | |
} |
187 | 18 | } |
188 | |
} |
189 | |
} |