Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(710)

Side by Side Diff: sdk/lib/io/http_utils.dart

Issue 16812004: Make some HTTP utility functions public (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Minor fixes Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 part of dart.io; 5 part of dart.io;
6 6
7 class _HttpUtils { 7 /**
8 static String decodeUrlEncodedString( 8 * Utility functions for working with dates in HTTP.
Anders Johnsen 2013/06/12 11:08:45 ... with HTTP specific date formats.
Søren Gjesse 2013/06/12 12:16:00 Done.
9 String urlEncoded, 9 */
10 {Encoding encoding: Encoding.UTF_8}) { 10 class HttpDate {
11 // First check the string for any encoding. 11 // From RFC-2616 section "3.3.1 Full Date",
12 int index = 0; 12 // http://tools.ietf.org/html/rfc2616#section-3.3.1
13 bool encoded = false; 13 //
14 while (!encoded && index < urlEncoded.length) {
15 encoded = urlEncoded[index] == "+" || urlEncoded[index] == "%";
16 index++;
17 }
18 if (!encoded) return urlEncoded;
19 index--;
20
21 // Start decoding from the first encoded character.
22 List<int> bytes = new List<int>();
23 for (int i = 0; i < index; i++) bytes.add(urlEncoded.codeUnitAt(i));
24 for (int i = index; i < urlEncoded.length; i++) {
25 if (urlEncoded[i] == "+") {
26 bytes.add(32);
27 } else if (urlEncoded[i] == "%") {
28 if (urlEncoded.length - i < 2) {
29 throw new HttpException("Invalid URL encoding");
30 }
31 int byte = 0;
32 for (int j = 0; j < 2; j++) {
33 var charCode = urlEncoded.codeUnitAt(i + j + 1);
34 if (0x30 <= charCode && charCode <= 0x39) {
35 byte = byte * 16 + charCode - 0x30;
36 } else {
37 // Check ranges A-F (0x41-0x46) and a-f (0x61-0x66).
38 charCode |= 0x20;
39 if (0x61 <= charCode && charCode <= 0x66) {
40 byte = byte * 16 + charCode - 0x57;
41 } else {
42 throw new ArgumentError("Invalid URL encoding");
43 }
44 }
45 }
46 bytes.add(byte);
47 i += 2;
48 } else {
49 bytes.add(urlEncoded.codeUnitAt(i));
50 }
51 }
52 return _decodeString(bytes, encoding);
53 }
54
55 static Map<String, String> splitQueryString(
56 String queryString,
57 {Encoding encoding: Encoding.UTF_8}) {
58 Map<String, String> result = new Map<String, String>();
59 int currentPosition = 0;
60 int length = queryString.length;
61
62 while (currentPosition < length) {
63
64 // Find the first equals character between current position and
65 // the provided end.
66 int indexOfEquals(int end) {
67 int index = currentPosition;
68 while (index < end) {
69 if (queryString.codeUnitAt(index) == _CharCode.EQUAL) return index;
70 index++;
71 }
72 return -1;
73 }
74
75 // Find the next separator (either & or ;), see
76 // http://www.w3.org/TR/REC-html40/appendix/notes.html#ampersands-in-uris
77 // relating the ; separator. If no separator is found returns
78 // the length of the query string.
79 int indexOfSeparator() {
80 int end = length;
81 int index = currentPosition;
82 while (index < end) {
83 int codeUnit = queryString.codeUnitAt(index);
84 if (codeUnit == _CharCode.AMPERSAND ||
85 codeUnit == _CharCode.SEMI_COLON) {
86 return index;
87 }
88 index++;
89 }
90 return end;
91 }
92
93 int seppos = indexOfSeparator();
94 int equalspos = indexOfEquals(seppos);
95 String name;
96 String value;
97 if (equalspos == -1) {
98 name = queryString.substring(currentPosition, seppos);
99 value = '';
100 } else {
101 name = queryString.substring(currentPosition, equalspos);
102 value = queryString.substring(equalspos + 1, seppos);
103 }
104 currentPosition = seppos + 1; // This also works when seppos == length.
105 if (name == '') continue;
106 result[_HttpUtils.decodeUrlEncodedString(name, encoding: encoding)] =
107 _HttpUtils.decodeUrlEncodedString(value, encoding: encoding);
108 }
109 return result;
110 }
111
112 // From RFC 2616 section "3.3.1 Full Date"
113 // HTTP-date = rfc1123-date | rfc850-date | asctime-date 14 // HTTP-date = rfc1123-date | rfc850-date | asctime-date
114 // rfc1123-date = wkday "," SP date1 SP time SP "GMT" 15 // rfc1123-date = wkday "," SP date1 SP time SP "GMT"
115 // rfc850-date = weekday "," SP date2 SP time SP "GMT" 16 // rfc850-date = weekday "," SP date2 SP time SP "GMT"
116 // asctime-date = wkday SP date3 SP time SP 4DIGIT 17 // asctime-date = wkday SP date3 SP time SP 4DIGIT
117 // date1 = 2DIGIT SP month SP 4DIGIT 18 // date1 = 2DIGIT SP month SP 4DIGIT
118 // ; day month year (e.g., 02 Jun 1982) 19 // ; day month year (e.g., 02 Jun 1982)
119 // date2 = 2DIGIT "-" month "-" 2DIGIT 20 // date2 = 2DIGIT "-" month "-" 2DIGIT
120 // ; day-month-year (e.g., 02-Jun-82) 21 // ; day-month-year (e.g., 02-Jun-82)
121 // date3 = month SP ( 2DIGIT | ( SP 1DIGIT )) 22 // date3 = month SP ( 2DIGIT | ( SP 1DIGIT ))
122 // ; month day (e.g., Jun 2) 23 // ; month day (e.g., Jun 2)
123 // time = 2DIGIT ":" 2DIGIT ":" 2DIGIT 24 // time = 2DIGIT ":" 2DIGIT ":" 2DIGIT
124 // ; 00:00:00 - 23:59:59 25 // ; 00:00:00 - 23:59:59
125 // wkday = "Mon" | "Tue" | "Wed" 26 // wkday = "Mon" | "Tue" | "Wed"
126 // | "Thu" | "Fri" | "Sat" | "Sun" 27 // | "Thu" | "Fri" | "Sat" | "Sun"
127 // weekday = "Monday" | "Tuesday" | "Wednesday" 28 // weekday = "Monday" | "Tuesday" | "Wednesday"
128 // | "Thursday" | "Friday" | "Saturday" | "Sunday" 29 // | "Thursday" | "Friday" | "Saturday" | "Sunday"
129 // month = "Jan" | "Feb" | "Mar" | "Apr" 30 // month = "Jan" | "Feb" | "Mar" | "Apr"
130 // | "May" | "Jun" | "Jul" | "Aug" 31 // | "May" | "Jun" | "Jul" | "Aug"
131 // | "Sep" | "Oct" | "Nov" | "Dec" 32 // | "Sep" | "Oct" | "Nov" | "Dec"
132 33
133 // Format as RFC 1123 date. 34 /**
134 static String formatDate(DateTime date) { 35 * Format a date according to
36 * [RFC-1123](http://tools.ietf.org/html/rfc1123 "RFC-1123"),
37 * e.g. `Thu, 1 Jan 1970 00:00:00 GMT`.
38 */
39 static String format(DateTime date) {
135 const List wkday = const ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; 40 const List wkday = const ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
136 const List month = const ["Jan", "Feb", "Mar", "Apr", "May", "Jun", 41 const List month = const ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
137 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; 42 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
138 43
139 DateTime d = date.toUtc(); 44 DateTime d = date.toUtc();
140 StringBuffer sb = new StringBuffer(); 45 StringBuffer sb = new StringBuffer();
141 sb.write(wkday[d.weekday - 1]); 46 sb.write(wkday[d.weekday - 1]);
142 sb.write(", "); 47 sb.write(", ");
143 sb.write(d.day.toString()); 48 sb.write(d.day.toString());
144 sb.write(" "); 49 sb.write(" ");
145 sb.write(month[d.month - 1]); 50 sb.write(month[d.month - 1]);
146 sb.write(" "); 51 sb.write(" ");
147 sb.write(d.year.toString()); 52 sb.write(d.year.toString());
148 sb.write(d.hour < 9 ? " 0" : " "); 53 sb.write(d.hour < 9 ? " 0" : " ");
149 sb.write(d.hour.toString()); 54 sb.write(d.hour.toString());
150 sb.write(d.minute < 9 ? ":0" : ":"); 55 sb.write(d.minute < 9 ? ":0" : ":");
151 sb.write(d.minute.toString()); 56 sb.write(d.minute.toString());
152 sb.write(d.second < 9 ? ":0" : ":"); 57 sb.write(d.second < 9 ? ":0" : ":");
153 sb.write(d.second.toString()); 58 sb.write(d.second.toString());
154 sb.write(" GMT"); 59 sb.write(" GMT");
155 return sb.toString(); 60 return sb.toString();
156 } 61 }
157 62
158 static DateTime parseDate(String date) { 63 /**
64 * Parse a date string in either of the formats
65 * [RFC-1123](http://tools.ietf.org/html/rfc1123 "RFC-1123"),
66 * [RFC-850](http://tools.ietf.org/html/rfc850 "RFC-850") or
67 * ANSI C's asctime() format. These formats are listed here.
68 *
69 * Thu, 1 Jan 1970 00:00:00 GMT
70 * Thursday, 1-Jan-1970 00:00:00 GMT
71 * Thu Jan 1 00:00:00 1970
72 *
73 * For more information see [RFC-2616 section 3.1.1]
74 * (http://tools.ietf.org/html/rfc2616#section-3.3.1
75 * "RFC-2616 section 3.1.1").
76 */
77 static DateTime parse(String date) {
159 final int SP = 32; 78 final int SP = 32;
160 const List wkdays = const ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; 79 const List wkdays = const ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
161 const List weekdays = const ["Monday", "Tuesday", "Wednesday", "Thursday", 80 const List weekdays = const ["Monday", "Tuesday", "Wednesday", "Thursday",
162 "Friday", "Saturday", "Sunday"]; 81 "Friday", "Saturday", "Sunday"];
163 const List months = const ["Jan", "Feb", "Mar", "Apr", "May", "Jun", 82 const List months = const ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
164 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; 83 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
165 const List wkdaysLowerCase = 84 const List wkdaysLowerCase =
166 const ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]; 85 const ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
167 const List weekdaysLowerCase = const ["monday", "tuesday", "wednesday", 86 const List weekdaysLowerCase = const ["monday", "tuesday", "wednesday",
168 "thursday", "friday", "saturday", 87 "thursday", "friday", "saturday",
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 month = expectMonth(format == formatRfc1123 ? " " : "-"); 194 month = expectMonth(format == formatRfc1123 ? " " : "-");
276 year = expectNum(" "); 195 year = expectNum(" ");
277 hours = expectNum(":"); 196 hours = expectNum(":");
278 minutes = expectNum(":"); 197 minutes = expectNum(":");
279 seconds = expectNum(" "); 198 seconds = expectNum(" ");
280 expect("GMT"); 199 expect("GMT");
281 } 200 }
282 expectEnd(); 201 expectEnd();
283 return new DateTime.utc(year, month + 1, day, hours, minutes, seconds, 0); 202 return new DateTime.utc(year, month + 1, day, hours, minutes, seconds, 0);
284 } 203 }
204 }
205
206 class _HttpUtils {
207 static String decodeUrlEncodedString(
208 String urlEncoded,
209 {Encoding encoding: Encoding.UTF_8}) {
210 // First check the string for any encoding.
211 int index = 0;
212 bool encoded = false;
213 while (!encoded && index < urlEncoded.length) {
214 encoded = urlEncoded[index] == "+" || urlEncoded[index] == "%";
215 index++;
216 }
217 if (!encoded) return urlEncoded;
218 index--;
219
220 // Start decoding from the first encoded character.
221 List<int> bytes = new List<int>();
222 for (int i = 0; i < index; i++) bytes.add(urlEncoded.codeUnitAt(i));
223 for (int i = index; i < urlEncoded.length; i++) {
224 if (urlEncoded[i] == "+") {
225 bytes.add(32);
226 } else if (urlEncoded[i] == "%") {
227 if (urlEncoded.length - i < 2) {
228 throw new HttpException("Invalid URL encoding");
229 }
230 int byte = 0;
231 for (int j = 0; j < 2; j++) {
232 var charCode = urlEncoded.codeUnitAt(i + j + 1);
233 if (0x30 <= charCode && charCode <= 0x39) {
234 byte = byte * 16 + charCode - 0x30;
235 } else {
236 // Check ranges A-F (0x41-0x46) and a-f (0x61-0x66).
237 charCode |= 0x20;
238 if (0x61 <= charCode && charCode <= 0x66) {
239 byte = byte * 16 + charCode - 0x57;
240 } else {
241 throw new ArgumentError("Invalid URL encoding");
242 }
243 }
244 }
245 bytes.add(byte);
246 i += 2;
247 } else {
248 bytes.add(urlEncoded.codeUnitAt(i));
249 }
250 }
251 return _decodeString(bytes, encoding);
252 }
253
254 static Map<String, String> splitQueryString(
255 String queryString,
256 {Encoding encoding: Encoding.UTF_8}) {
257 Map<String, String> result = new Map<String, String>();
258 int currentPosition = 0;
259 int length = queryString.length;
260
261 while (currentPosition < length) {
262
263 // Find the first equals character between current position and
264 // the provided end.
265 int indexOfEquals(int end) {
266 int index = currentPosition;
267 while (index < end) {
268 if (queryString.codeUnitAt(index) == _CharCode.EQUAL) return index;
269 index++;
270 }
271 return -1;
272 }
273
274 // Find the next separator (either & or ;), see
275 // http://www.w3.org/TR/REC-html40/appendix/notes.html#ampersands-in-uris
276 // relating the ; separator. If no separator is found returns
277 // the length of the query string.
278 int indexOfSeparator() {
279 int end = length;
280 int index = currentPosition;
281 while (index < end) {
282 int codeUnit = queryString.codeUnitAt(index);
283 if (codeUnit == _CharCode.AMPERSAND ||
284 codeUnit == _CharCode.SEMI_COLON) {
285 return index;
286 }
287 index++;
288 }
289 return end;
290 }
291
292 int seppos = indexOfSeparator();
293 int equalspos = indexOfEquals(seppos);
294 String name;
295 String value;
296 if (equalspos == -1) {
297 name = queryString.substring(currentPosition, seppos);
298 value = '';
299 } else {
300 name = queryString.substring(currentPosition, equalspos);
301 value = queryString.substring(equalspos + 1, seppos);
302 }
303 currentPosition = seppos + 1; // This also works when seppos == length.
304 if (name == '') continue;
305 result[_HttpUtils.decodeUrlEncodedString(name, encoding: encoding)] =
306 _HttpUtils.decodeUrlEncodedString(value, encoding: encoding);
307 }
308 return result;
309 }
285 310
286 static DateTime parseCookieDate(String date) { 311 static DateTime parseCookieDate(String date) {
287 const List monthsLowerCase = const ["jan", "feb", "mar", "apr", "may", 312 const List monthsLowerCase = const ["jan", "feb", "mar", "apr", "may",
288 "jun", "jul", "aug", "sep", "oct", 313 "jun", "jul", "aug", "sep", "oct",
289 "nov", "dec"]; 314 "nov", "dec"];
290 315
291 int position = 0; 316 int position = 0;
292 317
293 void error() { 318 void error() {
294 throw new HttpException("Invalid cookie date $date"); 319 throw new HttpException("Invalid cookie date $date");
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 while ((amp = input.indexOf('&', offset)) >= 0) { 448 while ((amp = input.indexOf('&', offset)) >= 0) {
424 buffer.write(input.substring(offset, amp)); 449 buffer.write(input.substring(offset, amp));
425 int end = input.indexOf(';', amp); 450 int end = input.indexOf(';', amp);
426 parse(amp, end); 451 parse(amp, end);
427 offset = end + 1; 452 offset = end + 1;
428 } 453 }
429 buffer.write(input.substring(offset)); 454 buffer.write(input.substring(offset));
430 return buffer.toString(); 455 return buffer.toString();
431 } 456 }
432 } 457 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698