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 const _WEEKDAYS = const ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; | 9 const _WEEKDAYS = const ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; |
10 const _MONTHS = const ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", | 10 const _MONTHS = const ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", |
11 "Sep", "Oct", "Nov", "Dec"]; | 11 "Sep", "Oct", "Nov", "Dec"]; |
12 | 12 |
13 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"); |
14 final _longWeekdayRegExp = | 14 final _longWeekdayRegExp = |
15 new RegExp(r"Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday"); | 15 new RegExp(r"Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday"); |
16 final _monthRegExp = | 16 final _monthRegExp = |
17 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"); |
18 final _digitRegExp = new RegExp(r"\d+"); | 18 final _digitRegExp = new RegExp(r"\d+"); |
19 | 19 |
20 /// Return a HTTP-formatted string representation of [date]. | 20 /// Return a HTTP-formatted string representation of [date]. |
21 /// | 21 /// |
22 /// 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 |
23 /// 1123](http://tools.ietf.org/html/rfc1123). | 23 /// 1123](http://tools.ietf.org/html/rfc1123). |
24 String formatHttpDate(DateTime date) { | 24 String formatHttpDate(DateTime date) { |
25 date = date.toUtc(); | 25 date = date.toUtc(); |
26 var buffer = new StringBuffer() | 26 var buffer = new StringBuffer() |
27 ..write(_WEEKDAYS[date.weekday - 1]) | 27 ..write(_WEEKDAYS[date.weekday - 1]) |
28 ..write(", ") | 28 ..write(", ") |
29 ..write(date.day <= 9 ? "0" : "") | |
29 ..write(date.day.toString()) | 30 ..write(date.day.toString()) |
30 ..write(" ") | 31 ..write(" ") |
31 ..write(_MONTHS[date.month - 1]) | 32 ..write(_MONTHS[date.month - 1]) |
32 ..write(" ") | 33 ..write(" ") |
33 ..write(date.year.toString()) | 34 ..write(date.year.toString()) |
34 ..write(date.hour < 9 ? " 0" : " ") | 35 ..write(date.hour <= 9 ? " 0" : " ") |
35 ..write(date.hour.toString()) | 36 ..write(date.hour.toString()) |
36 ..write(date.minute < 9 ? ":0" : ":") | 37 ..write(date.minute <= 9 ? ":0" : ":") |
37 ..write(date.minute.toString()) | 38 ..write(date.minute.toString()) |
38 ..write(date.second < 9 ? ":0" : ":") | 39 ..write(date.second <= 9 ? ":0" : ":") |
39 ..write(date.second.toString()) | 40 ..write(date.second.toString()) |
40 ..write(" GMT"); | 41 ..write(" GMT"); |
41 return buffer.toString(); | 42 return buffer.toString(); |
nweiz
2014/05/09 20:24:31
This code came straight from dart:io, so it's prob
kevmoo
2014/05/09 20:33:02
It looks like it's okay here https://github.com/da
nweiz
2014/05/09 20:39:35
That's still missing a leading zero for the day, i
| |
42 } | 43 } |
43 | 44 |
44 /// Parses an HTTP-formatted date into a UTC [DateTime]. | 45 /// Parses an HTTP-formatted date into a UTC [DateTime]. |
45 /// | 46 /// |
46 /// This follows [RFC | 47 /// This follows [RFC |
47 /// 2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3). It will | 48 /// 2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3). It will |
48 /// throw a [FormatException] if [date] is invalid. | 49 /// throw a [FormatException] if [date] is invalid. |
49 DateTime parseHttpDate(String date) { | 50 DateTime parseHttpDate(String date) { |
50 try { | 51 try { |
51 var scanner = new StringScanner(date); | 52 var scanner = new StringScanner(date); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 DateTime _makeDateTime(int year, int month, int day, DateTime time) { | 143 DateTime _makeDateTime(int year, int month, int day, DateTime time) { |
143 var dateTime = new DateTime.utc( | 144 var dateTime = new DateTime.utc( |
144 year, month, day, time.hour, time.minute, time.second); | 145 year, month, day, time.hour, time.minute, time.second); |
145 | 146 |
146 // If [day] was too large, it will cause [month] to overflow. | 147 // If [day] was too large, it will cause [month] to overflow. |
147 if (dateTime.month != month) { | 148 if (dateTime.month != month) { |
148 throw new FormatException("invalid day '$day' for month '$month'."); | 149 throw new FormatException("invalid day '$day' for month '$month'."); |
149 } | 150 } |
150 return dateTime; | 151 return dateTime; |
151 } | 152 } |
OLD | NEW |