OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 class _HttpUtils { | 5 class _HttpUtils { |
6 | |
Anders Johnsen
2012/03/06 10:41:01
Whitespace
Søren Gjesse
2012/03/06 12:01:16
Done.
| |
6 static String decodeUrlEncodedString(String urlEncoded) { | 7 static String decodeUrlEncodedString(String urlEncoded) { |
7 void invalidEscape() { | 8 void invalidEscape() { |
8 // TODO(sgjesse): Handle the error. | 9 // TODO(sgjesse): Handle the error. |
9 } | 10 } |
10 | 11 |
11 StringBuffer result = new StringBuffer(); | 12 StringBuffer result = new StringBuffer(); |
12 for (int ii = 0; urlEncoded.length > ii; ++ii) { | 13 for (int ii = 0; urlEncoded.length > ii; ++ii) { |
13 if ('+' == urlEncoded[ii]) { | 14 if ('+' == urlEncoded[ii]) { |
14 result.add(' '); | 15 result.add(' '); |
15 } else if ('%' == urlEncoded[ii] && | 16 } else if ('%' == urlEncoded[ii] && |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
52 currentPosition = queryString.length; | 53 currentPosition = queryString.length; |
53 } else { | 54 } else { |
54 value = queryString.substring(currentPosition, position); | 55 value = queryString.substring(currentPosition, position); |
55 currentPosition = position + 1; | 56 currentPosition = position + 1; |
56 } | 57 } |
57 result[_HttpUtils.decodeUrlEncodedString(name)] = | 58 result[_HttpUtils.decodeUrlEncodedString(name)] = |
58 _HttpUtils.decodeUrlEncodedString(value); | 59 _HttpUtils.decodeUrlEncodedString(value); |
59 } | 60 } |
60 return result; | 61 return result; |
61 } | 62 } |
63 | |
64 // From RFC 2616 section "3.3.1 Full Date" | |
65 // HTTP-date = rfc1123-date | rfc850-date | asctime-date | |
66 // rfc1123-date = wkday "," SP date1 SP time SP "GMT" | |
67 // rfc850-date = weekday "," SP date2 SP time SP "GMT" | |
68 // asctime-date = wkday SP date3 SP time SP 4DIGIT | |
69 // date1 = 2DIGIT SP month SP 4DIGIT | |
70 // ; day month year (e.g., 02 Jun 1982) | |
71 // date2 = 2DIGIT "-" month "-" 2DIGIT | |
72 // ; day-month-year (e.g., 02-Jun-82) | |
73 // date3 = month SP ( 2DIGIT | ( SP 1DIGIT )) | |
74 // ; month day (e.g., Jun 2) | |
75 // time = 2DIGIT ":" 2DIGIT ":" 2DIGIT | |
76 // ; 00:00:00 - 23:59:59 | |
77 // wkday = "Mon" | "Tue" | "Wed" | |
78 // | "Thu" | "Fri" | "Sat" | "Sun" | |
79 // weekday = "Monday" | "Tuesday" | "Wednesday" | |
80 // | "Thursday" | "Friday" | "Saturday" | "Sunday" | |
81 // month = "Jan" | "Feb" | "Mar" | "Apr" | |
82 // | "May" | "Jun" | "Jul" | "Aug" | |
83 // | "Sep" | "Oct" | "Nov" | "Dec" | |
84 | |
85 // Format as RFC 1123 date. | |
86 static String formatDate(Date date) { | |
87 List wkday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; | |
88 List month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
89 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; | |
90 | |
91 Date d = date.changeTimeZone(new TimeZone.utc()); | |
92 StringBuffer sb = new StringBuffer(); | |
93 sb.add(wkday[d.weekday]); | |
94 sb.add(", "); | |
95 sb.add(d.day.toString()); | |
96 sb.add(" "); | |
97 sb.add(month[d.month - 1]); | |
98 sb.add(" "); | |
99 sb.add(d.year.toString()); | |
100 d.hours < 9 ? sb.add(" 0") : sb.add(" "); | |
101 sb.add(d.hours.toString()); | |
102 d.minutes < 9 ? sb.add(":0") : sb.add(":"); | |
103 sb.add(d.minutes.toString()); | |
104 d.seconds < 9 ? sb.add(":0") : sb.add(":"); | |
105 sb.add(d.seconds.toString()); | |
106 sb.add(" GMT"); | |
107 return sb.toString(); | |
108 } | |
109 | |
110 static Date parseDate(String date) { | |
111 final int SP = 32; | |
112 List wkdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; | |
113 List weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", | |
114 "Friday", "Saturday", "Sunday"]; | |
115 List months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
116 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; | |
117 | |
118 final int formatRfc1123 = 0; | |
119 final int formatRfc850 = 1; | |
120 final int formatAsctime = 2; | |
121 | |
122 int index = 0; | |
123 String tmp; | |
124 int format; | |
125 | |
126 void expect(String s) { | |
127 if (date.length - index < s.length) { | |
128 throw new HttpException("Invalid HTTP date $date"); | |
129 } | |
130 String tmp = date.substring(index, index + s.length); | |
131 if (tmp != s) { | |
132 throw new HttpException("Invalid HTTP date $date"); | |
133 } | |
134 index += s.length; | |
135 } | |
136 | |
137 int expectWeekday() { | |
138 // The formatting of the weekday signals the format of the date string. | |
139 int pos = date.indexOf(",", index); | |
140 if (pos == -1) { | |
141 int pos = date.indexOf(" ", index); | |
142 if (pos == -1) throw new HttpException("Invalid HTTP date $date"); | |
143 tmp = date.substring(index, pos); | |
144 index = pos + 1; | |
145 for (int i = 0; i < wkdays.length; i++) { | |
Anders Johnsen
2012/03/06 10:41:01
wkdays.indexOf(tmp)?
Søren Gjesse
2012/03/06 12:01:16
I forgot about that API, thanks.
| |
146 if (wkdays[i] == tmp) { | |
147 format = formatAsctime; | |
148 return i; | |
149 } | |
150 } | |
Anders Johnsen
2012/03/06 10:41:01
Is it an error if we end here? Maybe move the exce
Søren Gjesse
2012/03/06 12:01:16
Done.
| |
151 } else { | |
152 tmp = date.substring(index, pos); | |
153 index = pos + 1; | |
154 for (int i = 0; i < wkdays.length; i++) { | |
Anders Johnsen
2012/03/06 10:41:01
wkdays.indexOf(tmp)?
Søren Gjesse
2012/03/06 12:01:16
Done.
| |
155 if (wkdays[i] == tmp) { | |
156 format = formatRfc1123; | |
157 return i; | |
158 } | |
159 } | |
160 for (int i = 0; i < weekdays.length; i++) { | |
Anders Johnsen
2012/03/06 10:41:01
weekdays.indexOf(tmp)?
Søren Gjesse
2012/03/06 12:01:16
Done.
| |
161 if (weekdays[i] == tmp) { | |
162 format = formatRfc850; | |
163 return i; | |
164 } | |
165 } | |
166 throw new HttpException("Invalid HTTP date $date"); | |
167 } | |
168 } | |
169 | |
170 int expectMonth(String separator) { | |
171 int pos = date.indexOf(separator, index); | |
172 if (pos - index != 3) throw new HttpException("Invalid HTTP date $date"); | |
173 tmp = date.substring(index, pos); | |
174 index = pos + 1; | |
175 for (int i = 0; i < months.length; i++) { | |
Anders Johnsen
2012/03/06 10:41:01
months.indexOf(tmp)?
Søren Gjesse
2012/03/06 12:01:16
Done.
| |
176 if (months[i] == tmp) return i; | |
177 } | |
178 throw new HttpException("Invalid HTTP date $date"); | |
179 } | |
180 | |
181 int expectNum(String separator) { | |
182 int pos; | |
183 if (separator.length > 0) { | |
184 pos = date.indexOf(separator, index); | |
185 } else { | |
186 pos = date.length; | |
187 } | |
188 String tmp = date.substring(index, pos); | |
189 index = pos + separator.length; | |
190 try { | |
191 int value = Math.parseInt(tmp); | |
192 return value; | |
193 } catch (BadNumberFormatException e) { | |
194 throw new HttpException("Invalid HTTP date $date"); | |
195 } | |
196 } | |
197 | |
198 void expectEnd() { | |
199 if (index != date.length) { | |
200 throw new HttpException("Invalid HTTP date $date"); | |
201 } | |
202 } | |
203 | |
204 int weekday = expectWeekday(); | |
205 int day; | |
206 int month; | |
207 int year; | |
208 int hours; | |
209 int minutes; | |
210 int seconds; | |
211 if (format == formatAsctime) { | |
212 month = expectMonth(" "); | |
213 if (date.charCodeAt(index) == SP) index++; | |
214 day = expectNum(" "); | |
215 hours = expectNum(":"); | |
216 minutes = expectNum(":"); | |
217 seconds = expectNum(" "); | |
218 year = expectNum(""); | |
219 } else { | |
220 expect(" "); | |
221 day = expectNum(format == formatRfc1123 ? " " : "-"); | |
222 month = expectMonth(format == formatRfc1123 ? " " : "-"); | |
223 year = expectNum(" "); | |
224 hours = expectNum(":"); | |
225 minutes = expectNum(":"); | |
226 seconds = expectNum(" "); | |
227 expect("GMT"); | |
228 } | |
229 expectEnd(); | |
230 TimeZone utc = new TimeZone.utc(); | |
231 return new Date.withTimeZone( | |
232 year, month + 1, day, hours, minutes, seconds, 0, utc); | |
233 } | |
62 } | 234 } |
OLD | NEW |