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

Side by Side Diff: samples/chat/chat_server_lib.dart

Issue 8399033: First round of changes to the file interface. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years, 1 month 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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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("chat_server"); 5 #library("chat_server");
6 #import("http.dart"); 6 #import("http.dart");
7 #import("../../client/json/dart_json.dart"); 7 #import("../../client/json/dart_json.dart");
8 8
9 typedef void RequestHandler(HTTPRequest request, HTTPResponse response); 9 typedef void RequestHandler(HTTPRequest request, HTTPResponse response);
10 10
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 response.writeDone(); 336 response.writeDone();
337 } 337 }
338 338
339 // Serve the content of a file. 339 // Serve the content of a file.
340 void fileHandler( 340 void fileHandler(
341 HTTPRequest request, HTTPResponse response, [String fileName = null]) { 341 HTTPRequest request, HTTPResponse response, [String fileName = null]) {
342 final int BUFFER_SIZE = 4096; 342 final int BUFFER_SIZE = 4096;
343 if (fileName == null) { 343 if (fileName == null) {
344 fileName = request.path.substring(1); 344 fileName = request.path.substring(1);
345 } 345 }
346 File file = new File(fileName, false); 346 File file = new File(fileName);
347 if (file != null) { 347 if (file.existsSync()) {
348 file.openSync();
348 int totalRead = 0; 349 int totalRead = 0;
349 List<int> buffer = new List<int>(BUFFER_SIZE); 350 List<int> buffer = new List<int>(BUFFER_SIZE);
350 351
351 String mimeType = "text/html; charset=UTF-8"; 352 String mimeType = "text/html; charset=UTF-8";
352 int lastDot = fileName.lastIndexOf(".", fileName.length); 353 int lastDot = fileName.lastIndexOf(".", fileName.length);
353 if (lastDot != -1) { 354 if (lastDot != -1) {
354 String extension = fileName.substring(lastDot); 355 String extension = fileName.substring(lastDot);
355 if (extension == ".css") { mimeType = "text/css"; } 356 if (extension == ".css") { mimeType = "text/css"; }
356 if (extension == ".js") { mimeType = "application/javascript"; } 357 if (extension == ".js") { mimeType = "application/javascript"; }
357 if (extension == ".ico") { mimeType = "image/vnd.microsoft.icon"; } 358 if (extension == ".ico") { mimeType = "image/vnd.microsoft.icon"; }
358 if (extension == ".png") { mimeType = "image/png"; } 359 if (extension == ".png") { mimeType = "image/png"; }
359 } 360 }
360 response.setHeader("Content-Type", mimeType); 361 response.setHeader("Content-Type", mimeType);
361 response.contentLength = file.length; 362 response.contentLength = file.lengthSync();
362 363
363 void writeFileData() { 364 void writeFileData() {
364 while (totalRead < file.length) { 365 while (totalRead < file.lengthSync()) {
365 var read = file.readList(buffer, 0, BUFFER_SIZE); 366 var read = file.readListSync(buffer, 0, BUFFER_SIZE);
366 totalRead += read; 367 totalRead += read;
367 368
368 // Write this buffer and get a callback when it makes sense 369 // Write this buffer and get a callback when it makes sense
369 // to write more. 370 // to write more.
370 bool allWritten = response.writeList(buffer, 0, read, writeFileData); 371 bool allWritten = response.writeList(buffer, 0, read, writeFileData);
371 if (!allWritten) break; 372 if (!allWritten) break;
372 } 373 }
373 374
374 if (totalRead == file.length) { 375 if (totalRead == file.lengthSync()) {
375 response.writeDone(); 376 response.writeDone();
376 } 377 }
377 } 378 }
378 379
379 writeFileData(); 380 writeFileData();
380 } else { 381 } else {
381 print("File not found: $fileName"); 382 print("File not found: $fileName");
382 _notFoundHandler(request, response); 383 _notFoundHandler(request, response);
383 } 384 }
384 } 385 }
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after
702 } 703 }
703 } 704 }
704 705
705 int _timeRange; 706 int _timeRange;
706 List<int> _buckets; 707 List<int> _buckets;
707 int _currentBucket; 708 int _currentBucket;
708 int _currentBucketTime; 709 int _currentBucketTime;
709 num _bucketTimeRange; 710 num _bucketTimeRange;
710 int _sum; 711 int _sum;
711 } 712 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698