| OLD | NEW |
| 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 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 | 359 |
| 360 // Serve the content of a file. | 360 // Serve the content of a file. |
| 361 void fileHandler( | 361 void fileHandler( |
| 362 HTTPRequest request, HTTPResponse response, [String fileName = null]) { | 362 HTTPRequest request, HTTPResponse response, [String fileName = null]) { |
| 363 final int BUFFER_SIZE = 4096; | 363 final int BUFFER_SIZE = 4096; |
| 364 if (fileName == null) { | 364 if (fileName == null) { |
| 365 fileName = request.path.substring(1); | 365 fileName = request.path.substring(1); |
| 366 } | 366 } |
| 367 File file = new File(fileName); | 367 File file = new File(fileName); |
| 368 if (file.existsSync()) { | 368 if (file.existsSync()) { |
| 369 file.openSync(); | 369 RandomAccessFile openedFile = file.openSync(); |
| 370 int totalRead = 0; | 370 int totalRead = 0; |
| 371 List<int> buffer = new List<int>(BUFFER_SIZE); | 371 List<int> buffer = new List<int>(BUFFER_SIZE); |
| 372 | 372 |
| 373 String mimeType = "text/html; charset=UTF-8"; | 373 String mimeType = "text/html; charset=UTF-8"; |
| 374 int lastDot = fileName.lastIndexOf(".", fileName.length); | 374 int lastDot = fileName.lastIndexOf(".", fileName.length); |
| 375 if (lastDot != -1) { | 375 if (lastDot != -1) { |
| 376 String extension = fileName.substring(lastDot); | 376 String extension = fileName.substring(lastDot); |
| 377 if (extension == ".css") { mimeType = "text/css"; } | 377 if (extension == ".css") { mimeType = "text/css"; } |
| 378 if (extension == ".js") { mimeType = "application/javascript"; } | 378 if (extension == ".js") { mimeType = "application/javascript"; } |
| 379 if (extension == ".ico") { mimeType = "image/vnd.microsoft.icon"; } | 379 if (extension == ".ico") { mimeType = "image/vnd.microsoft.icon"; } |
| 380 if (extension == ".png") { mimeType = "image/png"; } | 380 if (extension == ".png") { mimeType = "image/png"; } |
| 381 } | 381 } |
| 382 response.setHeader("Content-Type", mimeType); | 382 response.setHeader("Content-Type", mimeType); |
| 383 response.contentLength = file.lengthSync(); | 383 response.contentLength = openedFile.lengthSync(); |
| 384 |
| 385 bool checkDone() { |
| 386 if (totalRead == openedFile.lengthSync()) { |
| 387 openedFile.closeSync(); |
| 388 response.writeDone(); |
| 389 return true; |
| 390 } |
| 391 return false; |
| 392 } |
| 384 | 393 |
| 385 void writeFileData() { | 394 void writeFileData() { |
| 386 while (totalRead < file.lengthSync()) { | 395 if (checkDone()) return; |
| 387 var read = file.readListSync(buffer, 0, BUFFER_SIZE); | 396 while (totalRead < openedFile.lengthSync()) { |
| 397 var read = openedFile.readListSync(buffer, 0, BUFFER_SIZE); |
| 388 totalRead += read; | 398 totalRead += read; |
| 389 | 399 |
| 390 // Write this buffer and get a callback when it makes sense | 400 // Write this buffer and get a callback when it makes sense |
| 391 // to write more. | 401 // to write more. |
| 392 bool allWritten = response.writeList(buffer, 0, read, writeFileData); | 402 bool writeCompleted = |
| 393 if (!allWritten) break; | 403 response.writeList(buffer, 0, read, writeFileData); |
| 394 } | 404 if (writeCompleted) { |
| 395 | 405 if (checkDone()) return; |
| 396 if (totalRead == file.lengthSync()) { | 406 } else { |
| 397 response.writeDone(); | 407 break; |
| 408 } |
| 398 } | 409 } |
| 399 } | 410 } |
| 400 | 411 |
| 401 writeFileData(); | 412 writeFileData(); |
| 402 } else { | 413 } else { |
| 403 print("File not found: $fileName"); | 414 print("File not found: $fileName"); |
| 404 _notFoundHandler(request, response); | 415 _notFoundHandler(request, response); |
| 405 } | 416 } |
| 406 } | 417 } |
| 407 | 418 |
| (...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 727 } | 738 } |
| 728 } | 739 } |
| 729 | 740 |
| 730 int _timeRange; | 741 int _timeRange; |
| 731 List<int> _buckets; | 742 List<int> _buckets; |
| 732 int _currentBucket; | 743 int _currentBucket; |
| 733 int _currentBucketTime; | 744 int _currentBucketTime; |
| 734 num _bucketTimeRange; | 745 num _bucketTimeRange; |
| 735 int _sum; | 746 int _sum; |
| 736 } | 747 } |
| OLD | NEW |