| 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 static String decodeUrlEncodedString(String urlEncoded) { | 6 static String decodeUrlEncodedString(String urlEncoded) { |
| 7 // First check the string for any encoding. | 7 // First check the string for any encoding. |
| 8 int index = 0; | 8 int index = 0; |
| 9 bool encoded = false; | 9 bool encoded = false; |
| 10 while (!encoded && index < urlEncoded.length) { | 10 while (!encoded && index < urlEncoded.length) { |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 d.hours < 9 ? sb.add(" 0") : sb.add(" "); | 110 d.hours < 9 ? sb.add(" 0") : sb.add(" "); |
| 111 sb.add(d.hours.toString()); | 111 sb.add(d.hours.toString()); |
| 112 d.minutes < 9 ? sb.add(":0") : sb.add(":"); | 112 d.minutes < 9 ? sb.add(":0") : sb.add(":"); |
| 113 sb.add(d.minutes.toString()); | 113 sb.add(d.minutes.toString()); |
| 114 d.seconds < 9 ? sb.add(":0") : sb.add(":"); | 114 d.seconds < 9 ? sb.add(":0") : sb.add(":"); |
| 115 sb.add(d.seconds.toString()); | 115 sb.add(d.seconds.toString()); |
| 116 sb.add(" GMT"); | 116 sb.add(" GMT"); |
| 117 return sb.toString(); | 117 return sb.toString(); |
| 118 } | 118 } |
| 119 | 119 |
| 120 static Date parseDate(String date) { | 120 static Date parseDate(String date, [bool strict = true]) { |
| 121 final int SP = 32; | 121 final int SP = 32; |
| 122 List wkdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; | 122 List wkdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; |
| 123 List weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", | 123 List weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", |
| 124 "Friday", "Saturday", "Sunday"]; | 124 "Friday", "Saturday", "Sunday"]; |
| 125 List months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", | 125 List months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 126 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; | 126 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; |
| 127 List wkdaysLowerCase = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]; |
| 128 List weekdaysLowerCase = ["monday", "tuesday", "wednesday", "thursday", |
| 129 "friday", "saturday", "sunday"]; |
| 130 List monthsLowerCase = ["jan", "feb", "mar", "apr", "may", "jun", |
| 131 "jul", "aug", "sep", "oct", "nov", "dec"]; |
| 127 | 132 |
| 128 final int formatRfc1123 = 0; | 133 final int formatRfc1123 = 0; |
| 129 final int formatRfc850 = 1; | 134 final int formatRfc850 = 1; |
| 130 final int formatAsctime = 2; | 135 final int formatAsctime = 2; |
| 131 | 136 |
| 132 int index = 0; | 137 int index = 0; |
| 133 String tmp; | 138 String tmp; |
| 134 int format; | 139 int format; |
| 135 | 140 |
| 136 void expect(String s) { | 141 void expect(String s) { |
| 137 if (date.length - index < s.length) { | 142 if (date.length - index < s.length) { |
| 138 throw new HttpException("Invalid HTTP date $date"); | 143 throw new HttpException("Invalid HTTP date $date"); |
| 139 } | 144 } |
| 140 String tmp = date.substring(index, index + s.length); | 145 String tmp = date.substring(index, index + s.length); |
| 141 if (tmp != s) { | 146 if (strict) { |
| 142 throw new HttpException("Invalid HTTP date $date"); | 147 if (tmp != s) { |
| 148 throw new HttpException("Invalid HTTP date $date"); |
| 149 } |
| 150 } else { |
| 151 if (tmp.toLowerCase() != s.toLowerCase()) { |
| 152 throw new HttpException("Invalid HTTP date $date"); |
| 153 } |
| 143 } | 154 } |
| 144 index += s.length; | 155 index += s.length; |
| 145 } | 156 } |
| 146 | 157 |
| 147 int expectWeekday() { | 158 int expectWeekday() { |
| 148 int weekday; | 159 int weekday; |
| 149 // The formatting of the weekday signals the format of the date string. | 160 // The formatting of the weekday signals the format of the date string. |
| 150 int pos = date.indexOf(",", index); | 161 int pos = date.indexOf(",", index); |
| 151 if (pos == -1) { | 162 if (pos == -1) { |
| 152 int pos = date.indexOf(" ", index); | 163 int pos = date.indexOf(" ", index); |
| 153 if (pos == -1) throw new HttpException("Invalid HTTP date $date"); | 164 if (pos == -1) throw new HttpException("Invalid HTTP date $date"); |
| 154 tmp = date.substring(index, pos); | 165 tmp = date.substring(index, pos); |
| 155 index = pos + 1; | 166 index = pos + 1; |
| 156 weekday = wkdays.indexOf(tmp); | 167 weekday = strict ? wkdays.indexOf(tmp) |
| 168 : wkdaysLowerCase.indexOf(tmp.toLowerCase()); |
| 157 if (weekday != -1) { | 169 if (weekday != -1) { |
| 158 format = formatAsctime; | 170 format = formatAsctime; |
| 159 return weekday; | 171 return weekday; |
| 160 } | 172 } |
| 161 } else { | 173 } else { |
| 162 tmp = date.substring(index, pos); | 174 tmp = date.substring(index, pos); |
| 163 index = pos + 1; | 175 index = pos + 1; |
| 164 weekday = wkdays.indexOf(tmp); | 176 weekday = strict ? wkdays.indexOf(tmp) |
| 177 : wkdaysLowerCase.indexOf(tmp.toLowerCase()); |
| 165 if (weekday != -1) { | 178 if (weekday != -1) { |
| 166 format = formatRfc1123; | 179 format = formatRfc1123; |
| 167 return weekday; | 180 return weekday; |
| 168 } | 181 } |
| 169 weekday = weekdays.indexOf(tmp); | 182 weekday = strict ? weekdays.indexOf(tmp) |
| 183 : weekdaysLowerCase.indexOf(tmp.toLowerCase()); |
| 170 if (weekday != -1) { | 184 if (weekday != -1) { |
| 171 format = formatRfc850; | 185 format = formatRfc850; |
| 172 return weekday; | 186 return weekday; |
| 173 } | 187 } |
| 174 } | 188 } |
| 175 throw new HttpException("Invalid HTTP date $date"); | 189 throw new HttpException("Invalid HTTP date $date"); |
| 176 } | 190 } |
| 177 | 191 |
| 178 int expectMonth(String separator) { | 192 int expectMonth(String separator) { |
| 179 int pos = date.indexOf(separator, index); | 193 int pos = date.indexOf(separator, index); |
| 180 if (pos - index != 3) throw new HttpException("Invalid HTTP date $date"); | 194 if (pos - index != 3) throw new HttpException("Invalid HTTP date $date"); |
| 181 tmp = date.substring(index, pos); | 195 tmp = date.substring(index, pos); |
| 182 index = pos + 1; | 196 index = pos + 1; |
| 183 int month = months.indexOf(tmp); | 197 int month = strict ? months.indexOf(tmp) |
| 198 : monthsLowerCase.indexOf(tmp.toLowerCase()); |
| 184 if (month != -1) return month; | 199 if (month != -1) return month; |
| 185 throw new HttpException("Invalid HTTP date $date"); | 200 throw new HttpException("Invalid HTTP date $date"); |
| 186 } | 201 } |
| 187 | 202 |
| 188 int expectNum(String separator) { | 203 int expectNum(String separator) { |
| 189 int pos; | 204 int pos; |
| 190 if (separator.length > 0) { | 205 if (separator.length > 0) { |
| 191 pos = date.indexOf(separator, index); | 206 pos = date.indexOf(separator, index); |
| 192 } else { | 207 } else { |
| 193 pos = date.length; | 208 pos = date.length; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 minutes = expectNum(":"); | 247 minutes = expectNum(":"); |
| 233 seconds = expectNum(" "); | 248 seconds = expectNum(" "); |
| 234 expect("GMT"); | 249 expect("GMT"); |
| 235 } | 250 } |
| 236 expectEnd(); | 251 expectEnd(); |
| 237 TimeZone utc = new TimeZone.utc(); | 252 TimeZone utc = new TimeZone.utc(); |
| 238 return new Date.withTimeZone( | 253 return new Date.withTimeZone( |
| 239 year, month + 1, day, hours, minutes, seconds, 0, utc); | 254 year, month + 1, day, hours, minutes, seconds, 0, utc); |
| 240 } | 255 } |
| 241 } | 256 } |
| OLD | NEW |