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 void invalidEscape() { | 7 void invalidEscape() { |
8 // TODO(sgjesse): Handle the error. | 8 // TODO(sgjesse): Handle the error. |
9 } | 9 } |
10 | 10 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 currentPosition = queryString.length; | 52 currentPosition = queryString.length; |
53 } else { | 53 } else { |
54 value = queryString.substring(currentPosition, position); | 54 value = queryString.substring(currentPosition, position); |
55 currentPosition = position + 1; | 55 currentPosition = position + 1; |
56 } | 56 } |
57 result[_HttpUtils.decodeUrlEncodedString(name)] = | 57 result[_HttpUtils.decodeUrlEncodedString(name)] = |
58 _HttpUtils.decodeUrlEncodedString(value); | 58 _HttpUtils.decodeUrlEncodedString(value); |
59 } | 59 } |
60 return result; | 60 return result; |
61 } | 61 } |
| 62 |
| 63 // From RFC 2616 section "3.3.1 Full Date" |
| 64 // HTTP-date = rfc1123-date | rfc850-date | asctime-date |
| 65 // rfc1123-date = wkday "," SP date1 SP time SP "GMT" |
| 66 // rfc850-date = weekday "," SP date2 SP time SP "GMT" |
| 67 // asctime-date = wkday SP date3 SP time SP 4DIGIT |
| 68 // date1 = 2DIGIT SP month SP 4DIGIT |
| 69 // ; day month year (e.g., 02 Jun 1982) |
| 70 // date2 = 2DIGIT "-" month "-" 2DIGIT |
| 71 // ; day-month-year (e.g., 02-Jun-82) |
| 72 // date3 = month SP ( 2DIGIT | ( SP 1DIGIT )) |
| 73 // ; month day (e.g., Jun 2) |
| 74 // time = 2DIGIT ":" 2DIGIT ":" 2DIGIT |
| 75 // ; 00:00:00 - 23:59:59 |
| 76 // wkday = "Mon" | "Tue" | "Wed" |
| 77 // | "Thu" | "Fri" | "Sat" | "Sun" |
| 78 // weekday = "Monday" | "Tuesday" | "Wednesday" |
| 79 // | "Thursday" | "Friday" | "Saturday" | "Sunday" |
| 80 // month = "Jan" | "Feb" | "Mar" | "Apr" |
| 81 // | "May" | "Jun" | "Jul" | "Aug" |
| 82 // | "Sep" | "Oct" | "Nov" | "Dec" |
| 83 |
| 84 // Format as RFC 1123 date. |
| 85 static String formatDate(Date date) { |
| 86 List wkday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; |
| 87 List month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 88 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; |
| 89 |
| 90 Date d = date.changeTimeZone(new TimeZone.utc()); |
| 91 StringBuffer sb = new StringBuffer(); |
| 92 sb.add(wkday[d.weekday]); |
| 93 sb.add(", "); |
| 94 sb.add(d.day.toString()); |
| 95 sb.add(" "); |
| 96 sb.add(month[d.month - 1]); |
| 97 sb.add(" "); |
| 98 sb.add(d.year.toString()); |
| 99 d.hours < 9 ? sb.add(" 0") : sb.add(" "); |
| 100 sb.add(d.hours.toString()); |
| 101 d.minutes < 9 ? sb.add(":0") : sb.add(":"); |
| 102 sb.add(d.minutes.toString()); |
| 103 d.seconds < 9 ? sb.add(":0") : sb.add(":"); |
| 104 sb.add(d.seconds.toString()); |
| 105 sb.add(" GMT"); |
| 106 return sb.toString(); |
| 107 } |
| 108 |
| 109 static Date parseDate(String date) { |
| 110 final int SP = 32; |
| 111 List wkdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; |
| 112 List weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", |
| 113 "Friday", "Saturday", "Sunday"]; |
| 114 List months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 115 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; |
| 116 |
| 117 final int formatRfc1123 = 0; |
| 118 final int formatRfc850 = 1; |
| 119 final int formatAsctime = 2; |
| 120 |
| 121 int index = 0; |
| 122 String tmp; |
| 123 int format; |
| 124 |
| 125 void expect(String s) { |
| 126 if (date.length - index < s.length) { |
| 127 throw new HttpException("Invalid HTTP date $date"); |
| 128 } |
| 129 String tmp = date.substring(index, index + s.length); |
| 130 if (tmp != s) { |
| 131 throw new HttpException("Invalid HTTP date $date"); |
| 132 } |
| 133 index += s.length; |
| 134 } |
| 135 |
| 136 int expectWeekday() { |
| 137 int weekday; |
| 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 weekday = wkdays.indexOf(tmp); |
| 146 if (weekday != -1) { |
| 147 format = formatAsctime; |
| 148 return weekday; |
| 149 } |
| 150 } else { |
| 151 tmp = date.substring(index, pos); |
| 152 index = pos + 1; |
| 153 weekday = wkdays.indexOf(tmp); |
| 154 if (weekday != -1) { |
| 155 format = formatRfc1123; |
| 156 return weekday; |
| 157 } |
| 158 weekday = weekdays.indexOf(tmp); |
| 159 if (weekday != -1) { |
| 160 format = formatRfc850; |
| 161 return weekday; |
| 162 } |
| 163 } |
| 164 throw new HttpException("Invalid HTTP date $date"); |
| 165 } |
| 166 |
| 167 int expectMonth(String separator) { |
| 168 int pos = date.indexOf(separator, index); |
| 169 if (pos - index != 3) throw new HttpException("Invalid HTTP date $date"); |
| 170 tmp = date.substring(index, pos); |
| 171 index = pos + 1; |
| 172 int month = months.indexOf(tmp); |
| 173 if (month != -1) return month; |
| 174 throw new HttpException("Invalid HTTP date $date"); |
| 175 } |
| 176 |
| 177 int expectNum(String separator) { |
| 178 int pos; |
| 179 if (separator.length > 0) { |
| 180 pos = date.indexOf(separator, index); |
| 181 } else { |
| 182 pos = date.length; |
| 183 } |
| 184 String tmp = date.substring(index, pos); |
| 185 index = pos + separator.length; |
| 186 try { |
| 187 int value = Math.parseInt(tmp); |
| 188 return value; |
| 189 } catch (BadNumberFormatException e) { |
| 190 throw new HttpException("Invalid HTTP date $date"); |
| 191 } |
| 192 } |
| 193 |
| 194 void expectEnd() { |
| 195 if (index != date.length) { |
| 196 throw new HttpException("Invalid HTTP date $date"); |
| 197 } |
| 198 } |
| 199 |
| 200 int weekday = expectWeekday(); |
| 201 int day; |
| 202 int month; |
| 203 int year; |
| 204 int hours; |
| 205 int minutes; |
| 206 int seconds; |
| 207 if (format == formatAsctime) { |
| 208 month = expectMonth(" "); |
| 209 if (date.charCodeAt(index) == SP) index++; |
| 210 day = expectNum(" "); |
| 211 hours = expectNum(":"); |
| 212 minutes = expectNum(":"); |
| 213 seconds = expectNum(" "); |
| 214 year = expectNum(""); |
| 215 } else { |
| 216 expect(" "); |
| 217 day = expectNum(format == formatRfc1123 ? " " : "-"); |
| 218 month = expectMonth(format == formatRfc1123 ? " " : "-"); |
| 219 year = expectNum(" "); |
| 220 hours = expectNum(":"); |
| 221 minutes = expectNum(":"); |
| 222 seconds = expectNum(" "); |
| 223 expect("GMT"); |
| 224 } |
| 225 expectEnd(); |
| 226 TimeZone utc = new TimeZone.utc(); |
| 227 return new Date.withTimeZone( |
| 228 year, month + 1, day, hours, minutes, seconds, 0, utc); |
| 229 } |
62 } | 230 } |
OLD | NEW |