Browse Source

Move encoding up a level

Kevin Moore 10 years ago
parent
commit
e83c353b39
1 changed files with 13 additions and 10 deletions
  1. 13 10
      frameworks/Dart/dart/server.dart

+ 13 - 10
frameworks/Dart/dart/server.dart

@@ -22,8 +22,9 @@ void main(List<String> args) {
   var arguments = parser.parse(args);
   var arguments = parser.parse(args);
   var isolates = int.parse(arguments['isolates']);
   var isolates = int.parse(arguments['isolates']);
   var dbConnections = int.parse(arguments['dbconnections']) ~/ isolates;
   var dbConnections = int.parse(arguments['dbconnections']) ~/ isolates;
-  ServerSocket.bind(arguments['address'], int.parse(arguments['port'])).then(
-      (server) {
+  ServerSocket
+      .bind(arguments['address'], int.parse(arguments['port']))
+      .then((server) {
     var ref = server.reference;
     var ref = server.reference;
     for (int i = 1; i < isolates; i++) {
     for (int i = 1; i < isolates; i++) {
       Isolate.spawn(startInIsolate, [ref, dbConnections]);
       Isolate.spawn(startInIsolate, [ref, dbConnections]);
@@ -128,16 +129,16 @@ int _parseInt(String text) =>
 
 
 /// Completes the given [request] by writing the [response] with the given
 /// Completes the given [request] by writing the [response] with the given
 /// [statusCode] and [type].
 /// [statusCode] and [type].
-void _sendResponse(HttpRequest request, int statusCode, [type, response]) {
+void _sendResponse(HttpRequest request, int statusCode,
+    {ContentType type, List<int> response}) {
   request.response.statusCode = statusCode;
   request.response.statusCode = statusCode;
   request.response.headers.date = new DateTime.now();
   request.response.headers.date = new DateTime.now();
   if (type != null) {
   if (type != null) {
     request.response.headers.contentType = type;
     request.response.headers.contentType = type;
   }
   }
   if (response != null) {
   if (response != null) {
-    var data = UTF8.encode(response);
-    request.response.contentLength = data.length;
-    request.response.add(data);
+    request.response.contentLength = response.length;
+    request.response.add(response);
   } else {
   } else {
     request.response.contentLength = 0;
     request.response.contentLength = 0;
   }
   }
@@ -146,18 +147,20 @@ void _sendResponse(HttpRequest request, int statusCode, [type, response]) {
 
 
 /// Completes the given [request] by writing the [response] as HTML.
 /// Completes the given [request] by writing the [response] as HTML.
 void _sendHtml(HttpRequest request, String response) {
 void _sendHtml(HttpRequest request, String response) {
-  _sendResponse(request, HttpStatus.OK, ContentType.HTML, response);
+  _sendResponse(request, HttpStatus.OK,
+      type: ContentType.HTML, response: UTF8.encode(response));
 }
 }
 
 
 /// Completes the given [request] by writing the [response] as JSON.
 /// Completes the given [request] by writing the [response] as JSON.
 void _sendJson(HttpRequest request, Object response) {
 void _sendJson(HttpRequest request, Object response) {
-  _sendResponse(
-      request, HttpStatus.OK, ContentType.JSON, JSON.encode(response));
+  _sendResponse(request, HttpStatus.OK,
+      type: ContentType.JSON, response: UTF8.encode(JSON.encode(response)));
 }
 }
 
 
 /// Completes the given [request] by writing the [response] as plain text.
 /// Completes the given [request] by writing the [response] as plain text.
 void _sendText(HttpRequest request, String response) {
 void _sendText(HttpRequest request, String response) {
-  _sendResponse(request, HttpStatus.OK, ContentType.TEXT, response);
+  _sendResponse(request, HttpStatus.OK,
+      type: ContentType.TEXT, response: UTF8.encode(response));
 }
 }
 
 
 /// Responds with the JSON test to the [request].
 /// Responds with the JSON test to the [request].