| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library http_parser; | 5 library http_parser; |
| 6 | 6 |
| 7 import 'package:string_scanner/string_scanner.dart'; | 7 import 'package:string_scanner/string_scanner.dart'; |
| 8 | 8 |
| 9 export 'src/media_type.dart'; | |
| 10 export 'src/web_socket.dart'; | |
| 11 | |
| 12 const _WEEKDAYS = const ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; | 9 const _WEEKDAYS = const ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; |
| 13 const _MONTHS = const ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", | 10 const _MONTHS = const ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", |
| 14 "Sep", "Oct", "Nov", "Dec"]; | 11 "Sep", "Oct", "Nov", "Dec"]; |
| 15 | 12 |
| 16 final _shortWeekdayRegExp = new RegExp(r"Mon|Tue|Wed|Thu|Fri|Sat|Sun"); | 13 final _shortWeekdayRegExp = new RegExp(r"Mon|Tue|Wed|Thu|Fri|Sat|Sun"); |
| 17 final _longWeekdayRegExp = | 14 final _longWeekdayRegExp = |
| 18 new RegExp(r"Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday"); | 15 new RegExp(r"Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday"); |
| 19 final _monthRegExp = | 16 final _monthRegExp = |
| 20 new RegExp(r"Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec"); | 17 new RegExp(r"Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec"); |
| 21 final _digitRegExp = new RegExp(r"\d+"); | 18 final _digitRegExp = new RegExp(r"\d+"); |
| 22 | 19 |
| 23 // TODO(nweiz): Move this into an http_parser package. | |
| 24 /// Return a HTTP-formatted string representation of [date]. | 20 /// Return a HTTP-formatted string representation of [date]. |
| 25 /// | 21 /// |
| 26 /// This follows [RFC 822](http://tools.ietf.org/html/rfc822) as updated by [RFC | 22 /// This follows [RFC 822](http://tools.ietf.org/html/rfc822) as updated by [RFC |
| 27 /// 1123](http://tools.ietf.org/html/rfc1123). | 23 /// 1123](http://tools.ietf.org/html/rfc1123). |
| 28 String formatHttpDate(DateTime date) { | 24 String formatHttpDate(DateTime date) { |
| 29 date = date.toUtc(); | 25 date = date.toUtc(); |
| 30 var buffer = new StringBuffer() | 26 var buffer = new StringBuffer() |
| 31 ..write(_WEEKDAYS[date.weekday - 1]) | 27 ..write(_WEEKDAYS[date.weekday - 1]) |
| 32 ..write(", ") | 28 ..write(", ") |
| 33 ..write(date.day.toString()) | 29 ..write(date.day.toString()) |
| 34 ..write(" ") | 30 ..write(" ") |
| 35 ..write(_MONTHS[date.month - 1]) | 31 ..write(_MONTHS[date.month - 1]) |
| 36 ..write(" ") | 32 ..write(" ") |
| 37 ..write(date.year.toString()) | 33 ..write(date.year.toString()) |
| 38 ..write(date.hour < 9 ? " 0" : " ") | 34 ..write(date.hour < 9 ? " 0" : " ") |
| 39 ..write(date.hour.toString()) | 35 ..write(date.hour.toString()) |
| 40 ..write(date.minute < 9 ? ":0" : ":") | 36 ..write(date.minute < 9 ? ":0" : ":") |
| 41 ..write(date.minute.toString()) | 37 ..write(date.minute.toString()) |
| 42 ..write(date.second < 9 ? ":0" : ":") | 38 ..write(date.second < 9 ? ":0" : ":") |
| 43 ..write(date.second.toString()) | 39 ..write(date.second.toString()) |
| 44 ..write(" GMT"); | 40 ..write(" GMT"); |
| 45 return buffer.toString(); | 41 return buffer.toString(); |
| 46 } | 42 } |
| 47 | 43 |
| 48 // TODO(nweiz): Move this into an http_parser package. | |
| 49 /// Parses an HTTP-formatted date into a UTC [DateTime]. | 44 /// Parses an HTTP-formatted date into a UTC [DateTime]. |
| 50 /// | 45 /// |
| 51 /// This follows [RFC | 46 /// This follows [RFC |
| 52 /// 2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3). It will | 47 /// 2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3). It will |
| 53 /// throw a [FormatException] if [date] is invalid. | 48 /// throw a [FormatException] if [date] is invalid. |
| 54 DateTime parseHttpDate(String date) { | 49 DateTime parseHttpDate(String date) { |
| 55 try { | 50 try { |
| 56 var scanner = new StringScanner(date); | 51 var scanner = new StringScanner(date); |
| 57 | 52 |
| 58 if (scanner.scan(_longWeekdayRegExp)) { | 53 if (scanner.scan(_longWeekdayRegExp)) { |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 DateTime _makeDateTime(int year, int month, int day, DateTime time) { | 142 DateTime _makeDateTime(int year, int month, int day, DateTime time) { |
| 148 var dateTime = new DateTime.utc( | 143 var dateTime = new DateTime.utc( |
| 149 year, month, day, time.hour, time.minute, time.second); | 144 year, month, day, time.hour, time.minute, time.second); |
| 150 | 145 |
| 151 // If [day] was too large, it will cause [month] to overflow. | 146 // If [day] was too large, it will cause [month] to overflow. |
| 152 if (dateTime.month != month) { | 147 if (dateTime.month != month) { |
| 153 throw new FormatException("invalid day '$day' for month '$month'."); | 148 throw new FormatException("invalid day '$day' for month '$month'."); |
| 154 } | 149 } |
| 155 return dateTime; | 150 return dateTime; |
| 156 } | 151 } |
| OLD | NEW |