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

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

Issue 10082003: Start refactoring of the HTTP header handling (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments Created 8 years, 8 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 #library("chat_server"); 5 #library("chat_server");
6 #import("dart:io"); 6 #import("dart:io");
7 #import("dart:isolate"); 7 #import("dart:isolate");
8 #import("dart:json"); 8 #import("dart:json");
9 9
10 typedef void RequestHandler(HttpRequest request, HttpResponse response); 10 typedef void RequestHandler(HttpRequest request, HttpResponse response);
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 </html>"""; 323 </html>""";
324 static final String notFoundPageHtml = """ 324 static final String notFoundPageHtml = """
325 <html><head> 325 <html><head>
326 <title>404 Not Found</title> 326 <title>404 Not Found</title>
327 </head><body> 327 </head><body>
328 <h1>Not Found</h1> 328 <h1>Not Found</h1>
329 <p>The requested URL was not found on this server.</p> 329 <p>The requested URL was not found on this server.</p>
330 </body></html>"""; 330 </body></html>""";
331 331
332 void _sendJSONResponse(HttpResponse response, Map responseData) { 332 void _sendJSONResponse(HttpResponse response, Map responseData) {
333 response.setHeader("Content-Type", "application/json; charset=UTF-8"); 333 response.headers.set("Content-Type", "application/json; charset=UTF-8");
334 response.outputStream.writeString(JSON.stringify(responseData)); 334 response.outputStream.writeString(JSON.stringify(responseData));
335 response.outputStream.close(); 335 response.outputStream.close();
336 } 336 }
337 337
338 IsolatedServer() : super() { 338 IsolatedServer() : super() {
339 _requestHandlers = new Map(); 339 _requestHandlers = new Map();
340 } 340 }
341 341
342 void redirectPageHandler(HttpRequest request, 342 void redirectPageHandler(HttpRequest request,
343 HttpResponse response, 343 HttpResponse response,
344 String redirectPath) { 344 String redirectPath) {
345 if (_redirectPage == null) { 345 if (_redirectPage == null) {
346 _redirectPage = redirectPageHtml.charCodes(); 346 _redirectPage = redirectPageHtml.charCodes();
347 } 347 }
348 response.statusCode = HttpStatus.FOUND; 348 response.statusCode = HttpStatus.FOUND;
349 response.setHeader( 349 response.headers.set(
350 "Location", "http://$_host:$_port/${redirectPath}"); 350 "Location", "http://$_host:$_port/${redirectPath}");
351 response.contentLength = _redirectPage.length; 351 response.contentLength = _redirectPage.length;
352 response.outputStream.write(_redirectPage); 352 response.outputStream.write(_redirectPage);
353 response.outputStream.close(); 353 response.outputStream.close();
354 } 354 }
355 355
356 // Serve the content of a file. 356 // Serve the content of a file.
357 void fileHandler( 357 void fileHandler(
358 HttpRequest request, HttpResponse response, [String fileName = null]) { 358 HttpRequest request, HttpResponse response, [String fileName = null]) {
359 final int BUFFER_SIZE = 4096; 359 final int BUFFER_SIZE = 4096;
360 if (fileName == null) { 360 if (fileName == null) {
361 fileName = request.path.substring(1); 361 fileName = request.path.substring(1);
362 } 362 }
363 File file = new File(fileName); 363 File file = new File(fileName);
364 if (file.existsSync()) { 364 if (file.existsSync()) {
365 String mimeType = "text/html; charset=UTF-8"; 365 String mimeType = "text/html; charset=UTF-8";
366 int lastDot = fileName.lastIndexOf(".", fileName.length); 366 int lastDot = fileName.lastIndexOf(".", fileName.length);
367 if (lastDot != -1) { 367 if (lastDot != -1) {
368 String extension = fileName.substring(lastDot); 368 String extension = fileName.substring(lastDot);
369 if (extension == ".css") { mimeType = "text/css"; } 369 if (extension == ".css") { mimeType = "text/css"; }
370 if (extension == ".js") { mimeType = "application/javascript"; } 370 if (extension == ".js") { mimeType = "application/javascript"; }
371 if (extension == ".ico") { mimeType = "image/vnd.microsoft.icon"; } 371 if (extension == ".ico") { mimeType = "image/vnd.microsoft.icon"; }
372 if (extension == ".png") { mimeType = "image/png"; } 372 if (extension == ".png") { mimeType = "image/png"; }
373 } 373 }
374 response.setHeader("Content-Type", mimeType); 374 response.headers.set("Content-Type", mimeType);
375 // Get the length of the file for setting the Content-Length header. 375 // Get the length of the file for setting the Content-Length header.
376 RandomAccessFile openedFile = file.openSync(); 376 RandomAccessFile openedFile = file.openSync();
377 response.contentLength = openedFile.lengthSync(); 377 response.contentLength = openedFile.lengthSync();
378 openedFile.closeSync(); 378 openedFile.closeSync();
379 // Pipe the file content into the response. 379 // Pipe the file content into the response.
380 file.openInputStream().pipe(response.outputStream); 380 file.openInputStream().pipe(response.outputStream);
381 } else { 381 } else {
382 print("File not found: $fileName"); 382 print("File not found: $fileName");
383 _notFoundHandler(request, response); 383 _notFoundHandler(request, response);
384 } 384 }
385 } 385 }
386 386
387 // Serve the not found page. 387 // Serve the not found page.
388 void _notFoundHandler(HttpRequest request, HttpResponse response) { 388 void _notFoundHandler(HttpRequest request, HttpResponse response) {
389 if (_notFoundPage == null) { 389 if (_notFoundPage == null) {
390 _notFoundPage = notFoundPageHtml.charCodes(); 390 _notFoundPage = notFoundPageHtml.charCodes();
391 } 391 }
392 response.statusCode = HttpStatus.NOT_FOUND; 392 response.statusCode = HttpStatus.NOT_FOUND;
393 response.setHeader("Content-Type", "text/html; charset=UTF-8"); 393 response.headers.set("Content-Type", "text/html; charset=UTF-8");
394 response.contentLength = _notFoundPage.length; 394 response.contentLength = _notFoundPage.length;
395 response.outputStream.write(_notFoundPage); 395 response.outputStream.write(_notFoundPage);
396 response.outputStream.close(); 396 response.outputStream.close();
397 } 397 }
398 398
399 // Unexpected protocol data. 399 // Unexpected protocol data.
400 void _protocolError(HttpRequest request, HttpResponse response) { 400 void _protocolError(HttpRequest request, HttpResponse response) {
401 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR; 401 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
402 response.contentLength = 0; 402 response.contentLength = 0;
403 response.outputStream.close(); 403 response.outputStream.close();
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
709 } 709 }
710 } 710 }
711 711
712 int _timeRange; 712 int _timeRange;
713 List<int> _buckets; 713 List<int> _buckets;
714 int _currentBucket; 714 int _currentBucket;
715 int _currentBucketTime; 715 int _currentBucketTime;
716 num _bucketTimeRange; 716 num _bucketTimeRange;
717 int _sum; 717 int _sum;
718 } 718 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698