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

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

Powered by Google App Engine
This is Rietveld 408576698