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