Revert 24099

Review URL: https://codereview.chromium.org//17284004

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@24100 260f80e4-7a28-3924-810f-c04153c831b5
diff --git a/runtime/bin/builtin.dart b/runtime/bin/builtin.dart
index 039a212..716ec71 100644
--- a/runtime/bin/builtin.dart
+++ b/runtime/bin/builtin.dart
@@ -3,20 +3,61 @@
 // BSD-style license that can be found in the LICENSE file.
 
 library builtin;
+import 'dart:io';
+
+int _httpRequestResponseCode = 0;
+String _httpRequestStatusString;
+var _httpRequestResponse;
+
+void _requestCompleted(HttpClientResponseBody body) {
+  _httpRequestResponseCode = body.statusCode;
+  _httpRequestStatusString = '${body.statusCode} ${body.reasonPhrase}';
+  _httpRequestResponse = null;
+  if (body.statusCode != 200 || body.type == "json") {
+    return;
+  }
+  _httpRequestResponse = body.body;
+}
+
+void _requestFailed(error) {
+  _httpRequestResponseCode = 0;
+  _httpRequestStatusString = error.toString();
+  _httpRequestResponse = null;
+}
+
+HttpClient _client = new HttpClient();
+void _makeHttpRequest(String uri) {
+  _httpRequestResponseCode = 0;
+  _httpRequestStatusString = null;
+  _httpRequestResponse = null;
+  Uri requestUri = Uri.parse(uri);
+  _client.getUrl(requestUri)
+      .then((HttpClientRequest request) => request.close())
+      .then(HttpBodyHandler.processResponse)
+      .then((HttpClientResponseBody body) {
+        _requestCompleted(body);
+      }).catchError((error) {
+        _requestFailed(error);
+      });
+}
 
 // Corelib 'print' implementation.
 void _print(arg) {
   _Logger._printString(arg.toString());
 }
 
-
 class _Logger {
   static void _printString(String s) native "Logger_PrintString";
 }
 
-
 _getPrintClosure() => _print;
 
+// The URI that the entrypoint script was loaded from. Remembered so that
+// package imports can be resolved relative to it.
+var _entrypoint;
+
+// The directory to look in to resolve "package:" scheme URIs.
+var _packageRoot;
 
 void _logResolution(String msg) {
   final enabled = false;
@@ -25,25 +66,22 @@
   }
 }
 
-
-// Are we running on Windows?
-var _isWindows = false;
-// The current working directory
-var _workingDirectoryUri;
-// The URI that the entry point script was loaded from. Remembered so that
-// package imports can be resolved relative to it.
-var _entryPointScript;
-// The directory to look in to resolve "package:" scheme URIs.
-var _packageRoot;
-
-
-void _setWindows() {
-  _isWindows = true;
+_setPackageRoot(String packageRoot) {
+  // TODO(mattsh) - refactor windows drive and path handling code
+  // so it can be used here if needed.
+  _packageRoot = packageRoot;
 }
 
-
-void _setWorkingDirectory(cwd) {
-  if (_isWindows) {
+String _resolveScriptUri(String cwd, String scriptName, bool isWindows) {
+  var scriptUri = Uri.parse(scriptName);
+  if (scriptUri.scheme == 'http') {
+    _entrypoint = scriptUri;
+    _logResolution("# Resolved script to: $_entrypoint");
+    return _entrypoint.toString();
+  }
+  _logResolution("# Current working directory: $cwd");
+  _logResolution("# ScriptName: $scriptName");
+  if (isWindows) {
     // For Windows we need to massage the paths a bit according to
     // http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx
     //
@@ -52,49 +90,27 @@
     // to
     // /C:/one/two/three
     cwd = "/${cwd.replaceAll('\\', '/')}";
+    _logResolution("## cwd: $cwd");
+    if ((scriptName.length > 2) && (scriptName[1] == ":")) {
+      // This is an absolute path.
+      scriptName = "/${scriptName.replaceAll('\\', '/')}";
+    } else {
+      scriptName = scriptName.replaceAll('\\', '/');
+    }
+    _logResolution("## scriptName: $scriptName");
   }
+  var base =
+      new Uri(scheme: "file",
+              path: cwd.endsWith("/") ? cwd : "$cwd/");
+  _entrypoint = base.resolve(scriptName);
+  _logResolution("# Resolved script to: $_entrypoint");
 
-  // Ensure we have a trailing slash character.
-  if (cwd.endsWith('/')) {
-    cwd = cwd;
-  } else {
-    cwd = '$cwd/';
-  }
-  _workingDirectoryUri = new Uri(scheme: 'file', path: cwd);
-  _logResolution('# Working Directory: $cwd');
+  return _entrypoint.toString();
 }
 
-
-_setPackageRoot(String packageRoot) {
-  if (!packageRoot.endsWith('/')) {
-    // Ensure we have a trailing slash character.
-    packageRoot = '$packageRoot/';
-  }
-  _packageRoot = Uri.parse(packageRoot);
-}
-
-
-String _resolveScriptUri(String scriptName) {
-  if (_workingDirectoryUri == null) {
-    throw 'No current working directory set.';
-  }
-  var scriptUri = Uri.parse(scriptName);
-  if (scriptUri.scheme != '') {
-    // Script has a scheme, assume that it is fully formed.
-    _entryPointScript = scriptUri;
-  } else {
-    // Script does not have a scheme, assume that it is a path,
-    // resolve it against the working directory.
-    _entryPointScript = _workingDirectoryUri.resolve(scriptName);
-  }
-  _logResolution('# Resolved entry point to: $_entryPointScript');
-  return _entryPointScript.toString();
-}
-
-
 String _resolveUri(String base, String userString) {
   var baseUri = Uri.parse(base);
-  _logResolution('# Resolving: $userString from $base');
+  _logResolution("# Resolving: $userString from $base");
 
   var uri = Uri.parse(userString);
   var resolved;
@@ -109,18 +125,18 @@
       // package URI path part.
       path = _filePathFromPackageUri(resolved);
     }
-    resolved = new Uri(scheme: 'dart-ext', path: path);
+    resolved = new Uri(scheme: "dart-ext", path: path);
   } else {
     resolved = baseUri.resolve(userString);
   }
-  _logResolution('# Resolved to: $resolved');
+  _logResolution("# Resolved to: $resolved");
   return resolved.toString();
 }
 
 
-String _filePathFromUri(String userUri) {
+String _filePathFromUri(String userUri, bool isWindows) {
   var uri = Uri.parse(userUri);
-  _logResolution('# Getting file path from: $uri');
+  _logResolution("# Getting file path from: $uri");
 
   var path;
   switch (uri.scheme) {
@@ -138,43 +154,40 @@
       break;
     default:
       // Only handling file and package URIs in standalone binary.
-      _logResolution('# Unknown scheme (${uri.scheme}) in $uri.');
-      throw 'Not a known scheme: $uri';
+      _logResolution("# Unknown scheme (${uri.scheme}) in $uri.");
+      throw "Not a known scheme: $uri";
   }
 
-  if (_isWindows && path.startsWith('/')) {
+  if (isWindows && path.startsWith("/")) {
     // For Windows we need to massage the paths a bit according to
     // http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx
     //
     // Drop the leading / before the drive letter.
     path = path.substring(1);
-    _logResolution('# Path: Removed leading / -> $path');
+    _logResolution("# path: $path");
   }
 
   return path;
 }
 
-
 String _filePathFromFileUri(Uri uri) {
   if (!uri.host.isEmpty) {
     throw "URIs using the 'file:' scheme may not contain a host.";
   }
 
-  _logResolution('# Path: $uri -> ${uri.path}');
+  _logResolution("# Path: ${uri.path}");
   return uri.path;
 }
 
-
 String _filePathFromOtherUri(Uri uri) {
   if (!uri.host.isEmpty) {
-    throw 'URIs whose paths are used as file paths may not contain a host.';
+    throw "URIs whose paths are used as file paths may not contain a host.";
   }
 
-  _logResolution('# Path: $uri -> ${uri.path}');
+  _logResolution("# Path: ${uri.path}");
   return uri.path;
 }
 
-
 String _filePathFromPackageUri(Uri uri) {
   if (!uri.host.isEmpty) {
     var path = (uri.path != '') ? '${uri.host}${uri.path}' : uri.host;
@@ -185,45 +198,22 @@
           "'$right', not '$wrong'.";
   }
 
-  var packageUri;
   var path;
   if (_packageRoot != null) {
-    // Resolve against package root.
-    packageUri = _packageRoot.resolve(uri.path);
+    path = "${_packageRoot}${uri.path}";
   } else {
-    // Resolve against working directory.
-    packageUri = _entryPointScript.resolve('packages/${uri.path}');
+    if (_entrypoint.scheme == 'http') {
+      path = _entrypoint.resolve('packages/${uri.path}').toString();
+    } else {
+      path = _entrypoint.resolve('packages/${uri.path}').path;
+    }
   }
 
-  if (packageUri.scheme == 'file') {
-    path = packageUri.path;
-  } else {
-    path = packageUri.toString();
-  }
-  _logResolution('# Package: $uri -> $path');
+  _logResolution("# Package: $path");
   return path;
 }
 
-
 String _filePathFromHttpUri(Uri uri) {
-  _logResolution('# Path: $uri -> $uri');
+  _logResolution('# Path: $uri');
   return uri.toString();
 }
-
-
-String _pathFromHttpUri(String userUri) {
-  var uri = Uri.parse(userUri);
-  return uri.path;
-}
-
-
-String _hostFromHttpUri(String userUri) {
-  var uri = Uri.parse(userUri);
-  return uri.host;
-}
-
-
-int _portFromHttpUri(String userUri) {
-  var uri = Uri.parse(userUri);
-  return uri.port == 0 ? 80 : uri.port;
-}
diff --git a/runtime/bin/dartutils.cc b/runtime/bin/dartutils.cc
index facccd9..8134848 100644
--- a/runtime/bin/dartutils.cc
+++ b/runtime/bin/dartutils.cc
@@ -44,15 +44,6 @@
 }
 
 
-static Dart_Handle SingleArgDart_Invoke(Dart_Handle arg, Dart_Handle lib,
-                                        const char* method) {
-  const int kNumArgs = 1;
-  Dart_Handle dart_args[kNumArgs];
-  dart_args[0] = arg;
-  return Dart_Invoke(lib, DartUtils::NewString(method), kNumArgs, dart_args);
-}
-
-
 const char* DartUtils::MapLibraryUrl(CommandLineOptions* url_mapping,
                                      const char* url_string) {
   ASSERT(url_mapping != NULL);
@@ -252,74 +243,12 @@
 }
 
 
-// Writes string into socket.
-// Return < 0 indicates an error.
-// Return >= 0 number of bytes written.
-static intptr_t SocketWriteString(intptr_t socket, const char* str,
-                                  intptr_t len) {
-    int r;
-    intptr_t cursor = 0;
-    do {
-      r = Socket::Write(socket, &str[cursor], len);
-      if (r < 0) {
-        return r;
-      }
-      cursor += r;
-      len -= r;
-    } while (len > 0);
-    ASSERT(len == 0);
-    return cursor;
-}
-
-
-static uint8_t* SocketReadUntilEOF(intptr_t socket, intptr_t* response_len) {
-  const intptr_t kInitialBufferSize = 16 * KB;
-  intptr_t buffer_size = kInitialBufferSize;
-  uint8_t* buffer = reinterpret_cast<uint8_t*>(malloc(buffer_size));
-  ASSERT(buffer != NULL);
-  intptr_t buffer_cursor = 0;
-  do {
-    int bytes_read = Socket::Read(socket, &buffer[buffer_cursor],
-                                      buffer_size - buffer_cursor - 1);
-    if (bytes_read < 0) {
-      free(buffer);
-      return NULL;
-    }
-
-    buffer_cursor += bytes_read;
-
-    if (bytes_read == 0) {
-      *response_len = buffer_cursor;
-      buffer[buffer_cursor] = '\0';
-      break;
-    }
-
-    // There is still more data to be read, check that we have room in the
-    // buffer for more data.
-    if (buffer_cursor == buffer_size - 1) {
-      // Buffer is full. Increase buffer size.
-      buffer_size *= 2;
-      buffer = reinterpret_cast<uint8_t*>(realloc(buffer, buffer_size));
-      ASSERT(buffer != NULL);
-    }
-  } while (true);
-  return buffer;
-}
-
-
-static bool HttpGetRequestOkay(const char* response) {
-  static const char* kOkayReply = "HTTP/1.0 200 OK";
-  static const intptr_t kOkayReplyLen = strlen(kOkayReply);
-  return (strncmp(response, kOkayReply, kOkayReplyLen) == 0);
-}
-
-
-static const uint8_t* HttpRequestGetPayload(const char* response) {
-  const char* split = strstr(response, "\r\n\r\n");
-  if (split != NULL) {
-    return reinterpret_cast<const uint8_t*>(split+4);
-  }
-  return NULL;
+static Dart_Handle SingleArgDart_Invoke(Dart_Handle arg, Dart_Handle lib,
+                                        const char* method) {
+  const int kNumArgs = 1;
+  Dart_Handle dart_args[kNumArgs];
+  dart_args[0] = arg;
+  return Dart_Invoke(lib, DartUtils::NewString(method), kNumArgs, dart_args);
 }
 
 
@@ -332,135 +261,93 @@
   *error_msg = msg
 
 
-static const uint8_t* HttpGetRequest(const char* host, const char* path,
-                                     int port, intptr_t* response_len,
-                                     const char** error_msg) {
-  OSError* error = NULL;
-  SocketAddresses* addresses = Socket::LookupAddress(host, -1, &error);
-  if (addresses == NULL || addresses->count() == 0) {
-    SET_ERROR_MSG(error_msg, "Unable to resolve %s", host);
-    return NULL;
-  }
-
-  int preferred_address = 0;
-  for (int i = 0; i < addresses->count(); i++) {
-    SocketAddress* address = addresses->GetAt(i);
-    if (address->GetType() == SocketAddress::ADDRESS_LOOPBACK_IP_V4) {
-      // Prefer the IP_V4 loop back.
-      preferred_address = i;
-      break;
-    }
-  }
-
-  RawAddr addr = addresses->GetAt(preferred_address)->addr();
-  intptr_t tcp_client = Socket::Create(addr);
-  if (tcp_client < 0) {
-    SET_ERROR_MSG(error_msg, "Unable to create socket to %s:%d", host, port);
-    return NULL;
-  }
-  Socket::Connect(tcp_client, addr, port);
-  if (tcp_client < 0) {
-    SET_ERROR_MSG(error_msg, "Unable to connect to %s:%d", host, port);
-    return NULL;
-  }
-  // Send get request.
-  {
-    const char* format =
-        "GET %s HTTP/1.0\r\nUser-Agent: Dart VM\r\nHost: %s\r\n\r\n";
-    intptr_t len = snprintf(NULL, 0, format, path, host);
-    char* get_request = reinterpret_cast<char*>(malloc(len + 1));
-    snprintf(get_request, len + 1, format, path, host);
-    intptr_t r = SocketWriteString(tcp_client, get_request, len);
-    free(get_request);
-    if (r < len) {
-      SET_ERROR_MSG(error_msg, "Unable to write to %s:%d - %d", host, port,
-                    static_cast<int>(r));
-      Socket::Close(tcp_client);
-      return NULL;
-    }
-    ASSERT(r == len);
-  }
-  // Consume response.
-  uint8_t* response = SocketReadUntilEOF(tcp_client, response_len);
-  // Close socket.
-  Socket::Close(tcp_client);
-  if (response == NULL) {
-    SET_ERROR_MSG(error_msg, "Unable to read from %s:%d", host, port);
-    return NULL;
-  }
-  if (HttpGetRequestOkay(reinterpret_cast<const char*>(response)) == false) {
-    SET_ERROR_MSG(error_msg, "Invalid HTTP response from %s:%d", host, port);
-    free(response);
-    return NULL;
-  }
-  return response;
-}
-
-
-static Dart_Handle ParseHttpUri(Dart_Handle uri, const char** host_str,
-                                int64_t* port_int, const char** path_str) {
-  ASSERT(host_str != NULL);
-  ASSERT(port_int != NULL);
-  ASSERT(path_str != NULL);
-  Dart_Handle result;
-  Dart_Handle builtin_lib =
-      Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary);
-  Dart_Handle path = DartUtils::PathFromUri(uri, builtin_lib);
-  if (Dart_IsError(path)) {
-    return path;
-  }
-  Dart_Handle host = DartUtils::HostFromUri(uri, builtin_lib);
-  if (Dart_IsError(host)) {
-    return host;
-  }
-  Dart_Handle port = DartUtils::PortFromUri(uri, builtin_lib);
-  if (Dart_IsError(port)) {
-    return port;
-  }
-  result = Dart_StringToCString(path, path_str);
+Dart_Handle MakeHttpRequest(Dart_Handle uri, Dart_Handle builtin_lib,
+                            uint8_t** buffer, intptr_t* buffer_len) {
+  const intptr_t HttpResponseCodeOK = 200;
+  ASSERT(buffer != NULL);
+  ASSERT(buffer_len != NULL);
+  ASSERT(!Dart_HasLivePorts());
+  SingleArgDart_Invoke(uri, builtin_lib, "_makeHttpRequest");
+  // Run until all ports to isolate are closed.
+  Dart_Handle result = Dart_RunLoop();
   if (Dart_IsError(result)) {
     return result;
   }
-  result = Dart_StringToCString(host, host_str);
-  if (Dart_IsError(result)) {
-    return result;
+  intptr_t responseCode =
+    DartUtils::GetIntegerField(builtin_lib, "_httpRequestResponseCode");
+  if (responseCode != HttpResponseCodeOK) {
+    // Return error.
+    Dart_Handle responseStatus =
+        Dart_GetField(builtin_lib,
+                      DartUtils::NewString("_httpRequestStatusString"));
+    if (Dart_IsError(responseStatus)) {
+      return responseStatus;
+    }
+    if (Dart_IsNull(responseStatus)) {
+      return Dart_Error("HTTP error.");
+    }
+    return Dart_Error(DartUtils::GetStringValue(responseStatus));
   }
-  if (DartUtils::GetInt64Value(port, port_int) == false) {
-    return Dart_Error("Invalid port");
+  Dart_Handle response =
+    Dart_GetField(builtin_lib, DartUtils::NewString("_httpRequestResponse"));
+  if (Dart_IsError(response)) {
+    return response;
+  }
+  if (Dart_IsString(response)) {
+    // Received response as string.
+    uint8_t* responseString = NULL;
+    intptr_t responseStringLength;
+    Dart_Handle r = Dart_StringToUTF8(response, &responseString,
+                                      &responseStringLength);
+    if (Dart_IsError(r)) {
+      *buffer = NULL;
+      *buffer_len = 0;
+      return r;
+    }
+    // Get payload as bytes.
+    *buffer_len = responseStringLength;
+    *buffer = reinterpret_cast<uint8_t*>(malloc(responseStringLength));
+    memmove(*buffer, responseString, responseStringLength);
+  } else {
+    // Received response as list of bytes.
+    ASSERT(Dart_IsList(response));
+    // Query list length.
+    result = Dart_ListLength(response, buffer_len);
+    if (Dart_IsError(result)) {
+      *buffer_len = 0;
+      *buffer = NULL;
+      return result;
+    }
+    // Get payload as bytes.
+    *buffer = reinterpret_cast<uint8_t*>(malloc(*buffer_len));
+    result = Dart_ListGetAsBytes(response, 0, *buffer, *buffer_len);
+    if (Dart_IsError(result)) {
+      free(*buffer);
+      *buffer_len = 0;
+      *buffer = NULL;
+      return result;
+    }
   }
   return result;
 }
 
 
-Dart_Handle DartUtils::ReadStringFromHttp(const char* filename) {
-  Dart_Handle uri = NewString(filename);
+Dart_Handle DartUtils::ReadStringFromHttp(const char* script_uri) {
+  Dart_Handle uri = NewString(script_uri);
   if (Dart_IsError(uri)) {
     return uri;
   }
-  const char* host_str = NULL;
-  int64_t port_int = 0;
-  const char* path_str = NULL;
-  Dart_Handle result = ParseHttpUri(uri, &host_str, &port_int,
-                                    &path_str);
+  Dart_Handle builtin_lib =
+      Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary);
+  uint8_t* buffer;
+  intptr_t bufferLen;
+  Dart_Handle result = MakeHttpRequest(uri, builtin_lib, &buffer, &bufferLen);
   if (Dart_IsError(result)) {
     return result;
   }
-  const char* error_msg = NULL;
-  intptr_t len;
-  const uint8_t* text_buffer = HttpGetRequest(host_str, path_str, port_int,
-                                              &len, &error_msg);
-  if (text_buffer == NULL) {
-    return Dart_Error(error_msg);
-  }
-  const uint8_t* payload = HttpRequestGetPayload(
-      reinterpret_cast<const char*>(text_buffer));
-  if (payload == NULL) {
-    return Dart_Error("Invalid HTTP response.");
-  }
-  // Subtract HTTP response from length.
-  len -= (payload-text_buffer);
-  ASSERT(len >= 0);
-  Dart_Handle str = Dart_NewStringFromUTF8(payload, len);
+  Dart_Handle str = Dart_NewStringFromUTF8(buffer,
+                                           bufferLen);
+  free(buffer);
   return str;
 }
 
@@ -497,39 +384,30 @@
 }
 
 
-Dart_Handle DartUtils::SetWorkingDirectory(Dart_Handle builtin_lib) {
-  Dart_Handle directory = NewString(original_working_directory);
-  return SingleArgDart_Invoke(directory, builtin_lib, "_setWorkingDirectory");
-}
-
-
 Dart_Handle DartUtils::ResolveScriptUri(Dart_Handle script_uri,
                                         Dart_Handle builtin_lib) {
-  return SingleArgDart_Invoke(script_uri, builtin_lib, "_resolveScriptUri");
+  const int kNumArgs = 3;
+  Dart_Handle dart_args[kNumArgs];
+  dart_args[0] = NewString(original_working_directory);
+  dart_args[1] = script_uri;
+  dart_args[2] = (IsWindowsHost() ? Dart_True() : Dart_False());
+  return Dart_Invoke(builtin_lib,
+                     NewString("_resolveScriptUri"),
+                     kNumArgs,
+                     dart_args);
 }
 
 
 Dart_Handle DartUtils::FilePathFromUri(Dart_Handle script_uri,
                                        Dart_Handle builtin_lib) {
-  return SingleArgDart_Invoke(script_uri, builtin_lib, "_filePathFromUri");
-}
-
-
-Dart_Handle DartUtils::PathFromUri(Dart_Handle script_uri,
-                                   Dart_Handle builtin_lib) {
-  return SingleArgDart_Invoke(script_uri, builtin_lib, "_pathFromHttpUri");
-}
-
-
-Dart_Handle DartUtils::HostFromUri(Dart_Handle script_uri,
-                                     Dart_Handle builtin_lib) {
-  return SingleArgDart_Invoke(script_uri, builtin_lib, "_hostFromHttpUri");
-}
-
-
-Dart_Handle DartUtils::PortFromUri(Dart_Handle script_uri,
-                                   Dart_Handle builtin_lib) {
-  return SingleArgDart_Invoke(script_uri, builtin_lib, "_portFromHttpUri");
+  const int kNumArgs = 2;
+  Dart_Handle dart_args[kNumArgs];
+  dart_args[0] = script_uri;
+  dart_args[1] = (IsWindowsHost() ? Dart_True() : Dart_False());
+  return Dart_Invoke(builtin_lib,
+                     NewString("_filePathFromUri"),
+                     kNumArgs,
+                     dart_args);
 }
 
 
@@ -662,38 +540,20 @@
 
 Dart_Handle DartUtils::LoadScriptHttp(Dart_Handle uri,
                                       Dart_Handle builtin_lib) {
-  const char* host_str = NULL;
-  int64_t port_int = 0;
-  const char* path_str = NULL;
-  Dart_Handle result = ParseHttpUri(uri, &host_str, &port_int,
-                                    &path_str);
+  intptr_t len = 0;
+  uint8_t* buffer = NULL;
+  Dart_Handle result = MakeHttpRequest(uri, builtin_lib, &buffer, &len);
   if (Dart_IsError(result)) {
     return result;
   }
-  const char* error_msg = NULL;
-  intptr_t len;
-  const uint8_t* text_buffer;
-  text_buffer = HttpGetRequest(host_str, path_str, port_int, &len,
-                               &error_msg);
-  if (text_buffer == NULL) {
-    return Dart_Error(error_msg);
-  }
-  const uint8_t* payload = HttpRequestGetPayload(
-      reinterpret_cast<const char*>(text_buffer));
-  if (payload == NULL) {
-    return Dart_Error("Invalid HTTP response.");
-  }
-  // Subtract HTTP response from length.
-  len -= (payload-text_buffer);
-  ASSERT(len >= 0);
-  // At this point we have received a valid HTTP 200 reply and
-  // payload points at the beginning of the script or snapshot.
+  const uint8_t* payload = buffer;
   bool is_snapshot = false;
   payload = SniffForMagicNumber(payload, &len, &is_snapshot);
   if (is_snapshot) {
     return Dart_LoadScriptFromSnapshot(payload, len);
   } else {
     Dart_Handle source = Dart_NewStringFromUTF8(payload, len);
+    free(buffer);
     if (Dart_IsError(source)) {
       return source;
     }
@@ -704,6 +564,9 @@
 
 Dart_Handle DartUtils::LoadScript(const char* script_uri,
                                   Dart_Handle builtin_lib) {
+  // Always call ResolveScriptUri because as a side effect it sets
+  // the script entry path which is used when automatically resolving
+  // package root.
   Dart_Handle resolved_script_uri =
       ResolveScriptUri(NewString(script_uri), builtin_lib);
   if (Dart_IsError(resolved_script_uri)) {
@@ -803,23 +666,6 @@
   DART_CHECK_VALID(Dart_Invoke(
       async_lib, NewString("_setTimerFactoryClosure"), 1, args));
 
-
-  if (IsWindowsHost()) {
-    result = Dart_Invoke(builtin_lib, NewString("_setWindows"), 0, 0);
-    if (Dart_IsError(result)) {
-      return result;
-    }
-  }
-
-  if (Dart_IsError(result)) {
-    return result;
-  }
-  // Set current working directory.
-  result = SetWorkingDirectory(builtin_lib);
-  if (Dart_IsError(result)) {
-    return result;
-  }
-
   // Set up package root if specified.
   if (package_root != NULL) {
     result = NewString(package_root);
diff --git a/runtime/bin/dartutils.h b/runtime/bin/dartutils.h
index 4dc44e0..026b83a 100644
--- a/runtime/bin/dartutils.h
+++ b/runtime/bin/dartutils.h
@@ -122,7 +122,7 @@
                                        Dart_Handle url);
   static Dart_Handle LoadScript(const char* script_uri,
                                 Dart_Handle builtin_lib);
-  static Dart_Handle LoadScriptHttp(Dart_Handle uri,
+  static Dart_Handle LoadScriptHttp(Dart_Handle script_uri,
                                     Dart_Handle builtin_lib);
   static Dart_Handle LoadSource(CommandLineOptions* url_mapping,
                                 Dart_Handle library,
@@ -163,23 +163,12 @@
   static const char* MapLibraryUrl(CommandLineOptions* url_mapping,
                                    const char* url_string);
 
-  static Dart_Handle SetWorkingDirectory(Dart_Handle builtin_lib);
-
   static Dart_Handle ResolveScriptUri(Dart_Handle script_uri,
                                       Dart_Handle builtin_lib);
 
   static Dart_Handle FilePathFromUri(Dart_Handle script_uri,
                                      Dart_Handle builtin_lib);
 
-  static Dart_Handle PathFromUri(Dart_Handle script_uri,
-                                 Dart_Handle builtin_lib);
-
-  static Dart_Handle HostFromUri(Dart_Handle script_uri,
-                                 Dart_Handle builtin_lib);
-
-  static Dart_Handle PortFromUri(Dart_Handle script_uri,
-                                 Dart_Handle builtin_lib);
-
   static Dart_Handle ResolveUri(Dart_Handle library_url,
                                 Dart_Handle url,
                                 Dart_Handle builtin_lib);