| 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 shelf.util; | 5 library shelf.util; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:stack_trace/stack_trace.dart'; | 9 import 'package:stack_trace/stack_trace.dart'; |
| 10 import 'package:string_scanner/string_scanner.dart'; | |
| 11 | 10 |
| 12 /// Like [Future.sync], but wraps the Future in [Chain.track] as well. | 11 /// Like [Future.sync], but wraps the Future in [Chain.track] as well. |
| 13 Future syncFuture(callback()) => Chain.track(new Future.sync(callback)); | 12 Future syncFuture(callback()) => Chain.track(new Future.sync(callback)); |
| 14 | |
| 15 const _WEEKDAYS = const ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; | |
| 16 const _MONTHS = const ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", | |
| 17 "Sep", "Oct", "Nov", "Dec"]; | |
| 18 | |
| 19 final _shortWeekdayRegExp = new RegExp(r"Mon|Tue|Wed|Thu|Fri|Sat|Sun"); | |
| 20 final _longWeekdayRegExp = | |
| 21 new RegExp(r"Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday"); | |
| 22 final _monthRegExp = | |
| 23 new RegExp(r"Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec"); | |
| 24 final _digitRegExp = new RegExp(r"\d+"); | |
| 25 | |
| 26 // TODO(nweiz): Move this into an http_parser package. | |
| 27 /// Return a HTTP-formatted string representation of [date]. | |
| 28 /// | |
| 29 /// This follows [RFC 822](http://tools.ietf.org/html/rfc822) as updated by [RFC | |
| 30 /// 1123](http://tools.ietf.org/html/rfc1123). | |
| 31 String formatHttpDate(DateTime date) { | |
| 32 date = date.toUtc(); | |
| 33 var buffer = new StringBuffer() | |
| 34 ..write(_WEEKDAYS[date.weekday - 1]) | |
| 35 ..write(", ") | |
| 36 ..write(date.day.toString()) | |
| 37 ..write(" ") | |
| 38 ..write(_MONTHS[date.month - 1]) | |
| 39 ..write(" ") | |
| 40 ..write(date.year.toString()) | |
| 41 ..write(date.hour < 9 ? " 0" : " ") | |
| 42 ..write(date.hour.toString()) | |
| 43 ..write(date.minute < 9 ? ":0" : ":") | |
| 44 ..write(date.minute.toString()) | |
| 45 ..write(date.second < 9 ? ":0" : ":") | |
| 46 ..write(date.second.toString()) | |
| 47 ..write(" GMT"); | |
| 48 return buffer.toString(); | |
| 49 } | |
| 50 | |
| 51 // TODO(nweiz): Move this into an http_parser package. | |
| 52 /// Parses an HTTP-formatted date into a UTC [DateTime]. | |
| 53 /// | |
| 54 /// This follows [RFC | |
| 55 /// 2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3). It will | |
| 56 /// throw a [FormatException] if [date] is invalid. | |
| 57 DateTime parseHttpDate(String date) { | |
| 58 try { | |
| 59 var scanner = new StringScanner(date); | |
| 60 | |
| 61 if (scanner.scan(_longWeekdayRegExp)) { | |
| 62 // RFC 850 starts with a long weekday. | |
| 63 scanner.expect(", "); | |
| 64 var day = _parseInt(scanner, 2); | |
| 65 scanner.expect("-"); | |
| 66 var month = _parseMonth(scanner); | |
| 67 scanner.expect("-"); | |
| 68 var year = 1900 + _parseInt(scanner, 2); | |
| 69 scanner.expect(" "); | |
| 70 var time = _parseTime(scanner); | |
| 71 scanner.expect(" GMT"); | |
| 72 scanner.expectDone(); | |
| 73 | |
| 74 return _makeDateTime(year, month, day, time); | |
| 75 } | |
| 76 | |
| 77 // RFC 1123 and asctime both start with a short weekday. | |
| 78 scanner.expect(_shortWeekdayRegExp); | |
| 79 if (scanner.scan(", ")) { | |
| 80 // RFC 1123 follows the weekday with a comma. | |
| 81 var day = _parseInt(scanner, 2); | |
| 82 scanner.expect(" "); | |
| 83 var month = _parseMonth(scanner); | |
| 84 scanner.expect(" "); | |
| 85 var year = _parseInt(scanner, 4); | |
| 86 scanner.expect(" "); | |
| 87 var time = _parseTime(scanner); | |
| 88 scanner.expect(" GMT"); | |
| 89 scanner.expectDone(); | |
| 90 | |
| 91 return _makeDateTime(year, month, day, time); | |
| 92 } | |
| 93 | |
| 94 // asctime follows the weekday with a space. | |
| 95 scanner.expect(" "); | |
| 96 var month = _parseMonth(scanner); | |
| 97 scanner.expect(" "); | |
| 98 var day = scanner.scan(" ") ? | |
| 99 _parseInt(scanner, 1) : | |
| 100 _parseInt(scanner, 2); | |
| 101 scanner.expect(" "); | |
| 102 var time = _parseTime(scanner); | |
| 103 scanner.expect(" "); | |
| 104 var year = _parseInt(scanner, 4); | |
| 105 scanner.expectDone(); | |
| 106 | |
| 107 return _makeDateTime(year, month, day, time); | |
| 108 } on FormatException catch (error) { | |
| 109 throw new FormatException('Invalid HTTP date "$date": ${error.message}'); | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 /// Parses a short-form month name to a form accepted by [DateTime]. | |
| 114 int _parseMonth(StringScanner scanner) { | |
| 115 scanner.expect(_monthRegExp); | |
| 116 // DateTime uses 1-indexed months. | |
| 117 return _MONTHS.indexOf(scanner.lastMatch[0]) + 1; | |
| 118 } | |
| 119 | |
| 120 /// Parses an int an enforces that it has exactly [digits] digits. | |
| 121 int _parseInt(StringScanner scanner, int digits) { | |
| 122 scanner.expect(_digitRegExp); | |
| 123 if (scanner.lastMatch[0].length != digits) { | |
| 124 scanner.error("expected a $digits-digit number."); | |
| 125 } else { | |
| 126 return int.parse(scanner.lastMatch[0]); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 /// Parses an timestamp of the form "HH:MM:SS" on a 24-hour clock. | |
| 131 DateTime _parseTime(StringScanner scanner) { | |
| 132 var hours = _parseInt(scanner, 2); | |
| 133 if (hours >= 24) scanner.error("hours may not be greater than 24."); | |
| 134 scanner.expect(':'); | |
| 135 | |
| 136 var minutes = _parseInt(scanner, 2); | |
| 137 if (minutes >= 60) scanner.error("minutes may not be greater than 60."); | |
| 138 scanner.expect(':'); | |
| 139 | |
| 140 var seconds = _parseInt(scanner, 2); | |
| 141 if (seconds >= 60) scanner.error("seconds may not be greater than 60."); | |
| 142 | |
| 143 return new DateTime(1, 1, 1, hours, minutes, seconds); | |
| 144 } | |
| 145 | |
| 146 /// Returns a UTC [DateTime] from the given components. | |
| 147 /// | |
| 148 /// Validates that [day] is a valid day for [month]. If it's not, throws a | |
| 149 /// [FormatException]. | |
| 150 DateTime _makeDateTime(int year, int month, int day, DateTime time) { | |
| 151 var dateTime = new DateTime.utc( | |
| 152 year, month, day, time.hour, time.minute, time.second); | |
| 153 | |
| 154 // If [day] was too large, it will cause [month] to overflow. | |
| 155 if (dateTime.month != month) { | |
| 156 throw new FormatException("invalid day '$day' for month '$month'."); | |
| 157 } | |
| 158 return dateTime; | |
| 159 } | |
| OLD | NEW |