| OLD | NEW |
| 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 #include "bin/dartutils.h" | 5 #include "bin/dartutils.h" |
| 6 | 6 |
| 7 #include "include/dart_api.h" | 7 #include "include/dart_api.h" |
| 8 #include "include/dart_native_api.h" | 8 #include "include/dart_native_api.h" |
| 9 | 9 |
| 10 #include "platform/assert.h" | 10 #include "platform/assert.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 | 37 |
| 38 static bool IsWindowsHost() { | 38 static bool IsWindowsHost() { |
| 39 #if defined(TARGET_OS_WINDOWS) | 39 #if defined(TARGET_OS_WINDOWS) |
| 40 return true; | 40 return true; |
| 41 #else // defined(TARGET_OS_WINDOWS) | 41 #else // defined(TARGET_OS_WINDOWS) |
| 42 return false; | 42 return false; |
| 43 #endif // defined(TARGET_OS_WINDOWS) | 43 #endif // defined(TARGET_OS_WINDOWS) |
| 44 } | 44 } |
| 45 | 45 |
| 46 | 46 |
| 47 static Dart_Handle SingleArgDart_Invoke(Dart_Handle arg, Dart_Handle lib, |
| 48 const char* method) { |
| 49 const int kNumArgs = 1; |
| 50 Dart_Handle dart_args[kNumArgs]; |
| 51 dart_args[0] = arg; |
| 52 return Dart_Invoke(lib, DartUtils::NewString(method), kNumArgs, dart_args); |
| 53 } |
| 54 |
| 55 |
| 47 const char* DartUtils::MapLibraryUrl(CommandLineOptions* url_mapping, | 56 const char* DartUtils::MapLibraryUrl(CommandLineOptions* url_mapping, |
| 48 const char* url_string) { | 57 const char* url_string) { |
| 49 ASSERT(url_mapping != NULL); | 58 ASSERT(url_mapping != NULL); |
| 50 // We need to check if the passed in url is found in the url_mapping array, | 59 // We need to check if the passed in url is found in the url_mapping array, |
| 51 // in that case use the mapped entry. | 60 // in that case use the mapped entry. |
| 52 int len = strlen(url_string); | 61 int len = strlen(url_string); |
| 53 for (int idx = 0; idx < url_mapping->count(); idx++) { | 62 for (int idx = 0; idx < url_mapping->count(); idx++) { |
| 54 const char* url_name = url_mapping->GetArgument(idx); | 63 const char* url_name = url_mapping->GetArgument(idx); |
| 55 if (!strncmp(url_string, url_name, len) && (url_name[len] == ',')) { | 64 if (!strncmp(url_string, url_name, len) && (url_name[len] == ',')) { |
| 56 const char* url_mapped_name = url_name + len + 1; | 65 const char* url_mapped_name = url_name + len + 1; |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 bool bytes_written = file_stream->WriteFully(buffer, num_bytes); | 245 bool bytes_written = file_stream->WriteFully(buffer, num_bytes); |
| 237 ASSERT(bytes_written); | 246 ASSERT(bytes_written); |
| 238 } | 247 } |
| 239 | 248 |
| 240 | 249 |
| 241 void DartUtils::CloseFile(void* stream) { | 250 void DartUtils::CloseFile(void* stream) { |
| 242 delete reinterpret_cast<File*>(stream); | 251 delete reinterpret_cast<File*>(stream); |
| 243 } | 252 } |
| 244 | 253 |
| 245 | 254 |
| 246 static Dart_Handle SingleArgDart_Invoke(Dart_Handle arg, Dart_Handle lib, | 255 // Writes string into socket. |
| 247 const char* method) { | 256 // Return < 0 indicates an error. |
| 248 const int kNumArgs = 1; | 257 // Return >= 0 number of bytes written. |
| 249 Dart_Handle dart_args[kNumArgs]; | 258 static intptr_t SocketWriteString(intptr_t socket, const char* str, |
| 250 dart_args[0] = arg; | 259 intptr_t len) { |
| 251 return Dart_Invoke(lib, DartUtils::NewString(method), kNumArgs, dart_args); | 260 int r; |
| 261 intptr_t cursor = 0; |
| 262 do { |
| 263 r = Socket::Write(socket, &str[cursor], len); |
| 264 if (r < 0) { |
| 265 return r; |
| 266 } |
| 267 cursor += r; |
| 268 len -= r; |
| 269 } while (len > 0); |
| 270 ASSERT(len == 0); |
| 271 return cursor; |
| 252 } | 272 } |
| 253 | 273 |
| 254 | 274 |
| 275 static uint8_t* SocketReadUntilEOF(intptr_t socket, intptr_t* response_len) { |
| 276 const intptr_t kInitialBufferSize = 16 * KB; |
| 277 intptr_t buffer_size = kInitialBufferSize; |
| 278 uint8_t* buffer = reinterpret_cast<uint8_t*>(malloc(buffer_size)); |
| 279 ASSERT(buffer != NULL); |
| 280 intptr_t buffer_cursor = 0; |
| 281 do { |
| 282 int bytes_read = Socket::Read(socket, &buffer[buffer_cursor], |
| 283 buffer_size - buffer_cursor - 1); |
| 284 if (bytes_read < 0) { |
| 285 free(buffer); |
| 286 return NULL; |
| 287 } |
| 288 |
| 289 buffer_cursor += bytes_read; |
| 290 |
| 291 if (bytes_read == 0) { |
| 292 *response_len = buffer_cursor; |
| 293 buffer[buffer_cursor] = '\0'; |
| 294 break; |
| 295 } |
| 296 |
| 297 // There is still more data to be read, check that we have room in the |
| 298 // buffer for more data. |
| 299 if (buffer_cursor == buffer_size - 1) { |
| 300 // Buffer is full. Increase buffer size. |
| 301 buffer_size *= 2; |
| 302 buffer = reinterpret_cast<uint8_t*>(realloc(buffer, buffer_size)); |
| 303 ASSERT(buffer != NULL); |
| 304 } |
| 305 } while (true); |
| 306 return buffer; |
| 307 } |
| 308 |
| 309 |
| 310 static bool HttpGetRequestOkay(const char* response) { |
| 311 static const char* kOkayReply = "HTTP/1.0 200 OK"; |
| 312 static const intptr_t kOkayReplyLen = strlen(kOkayReply); |
| 313 return (strncmp(response, kOkayReply, kOkayReplyLen) == 0); |
| 314 } |
| 315 |
| 316 |
| 317 static const uint8_t* HttpRequestGetPayload(const char* response) { |
| 318 const char* split = strstr(response, "\r\n\r\n"); |
| 319 if (split != NULL) { |
| 320 return reinterpret_cast<const uint8_t*>(split+4); |
| 321 } |
| 322 return NULL; |
| 323 } |
| 324 |
| 325 |
| 255 // TODO(iposva): Allocate from the zone instead of leaking error string | 326 // TODO(iposva): Allocate from the zone instead of leaking error string |
| 256 // here. On the other hand the binary is about the exit anyway. | 327 // here. On the other hand the binary is about the exit anyway. |
| 257 #define SET_ERROR_MSG(error_msg, format, ...) \ | 328 #define SET_ERROR_MSG(error_msg, format, ...) \ |
| 258 intptr_t len = snprintf(NULL, 0, format, __VA_ARGS__); \ | 329 intptr_t len = snprintf(NULL, 0, format, __VA_ARGS__); \ |
| 259 char *msg = reinterpret_cast<char*>(malloc(len + 1)); \ | 330 char *msg = reinterpret_cast<char*>(malloc(len + 1)); \ |
| 260 snprintf(msg, len + 1, format, __VA_ARGS__); \ | 331 snprintf(msg, len + 1, format, __VA_ARGS__); \ |
| 261 *error_msg = msg | 332 *error_msg = msg |
| 262 | 333 |
| 263 | 334 |
| 264 Dart_Handle MakeHttpRequest(Dart_Handle uri, Dart_Handle builtin_lib, | 335 static const uint8_t* HttpGetRequest(const char* host, const char* path, |
| 265 uint8_t** buffer, intptr_t* buffer_len) { | 336 int port, intptr_t* response_len, |
| 266 const intptr_t HttpResponseCodeOK = 200; | 337 const char** error_msg) { |
| 267 ASSERT(buffer != NULL); | 338 OSError* error = NULL; |
| 268 ASSERT(buffer_len != NULL); | 339 SocketAddresses* addresses = Socket::LookupAddress(host, -1, &error); |
| 269 ASSERT(!Dart_HasLivePorts()); | 340 if (addresses == NULL || addresses->count() == 0) { |
| 270 SingleArgDart_Invoke(uri, builtin_lib, "_makeHttpRequest"); | 341 SET_ERROR_MSG(error_msg, "Unable to resolve %s", host); |
| 271 // Run until all ports to isolate are closed. | 342 return NULL; |
| 272 Dart_Handle result = Dart_RunLoop(); | 343 } |
| 344 |
| 345 int preferred_address = 0; |
| 346 for (int i = 0; i < addresses->count(); i++) { |
| 347 SocketAddress* address = addresses->GetAt(i); |
| 348 if (address->GetType() == SocketAddress::ADDRESS_LOOPBACK_IP_V4) { |
| 349 // Prefer the IP_V4 loop back. |
| 350 preferred_address = i; |
| 351 break; |
| 352 } |
| 353 } |
| 354 |
| 355 RawAddr addr = addresses->GetAt(preferred_address)->addr(); |
| 356 intptr_t tcp_client = Socket::Create(addr); |
| 357 if (tcp_client < 0) { |
| 358 SET_ERROR_MSG(error_msg, "Unable to create socket to %s:%d", host, port); |
| 359 return NULL; |
| 360 } |
| 361 Socket::Connect(tcp_client, addr, port); |
| 362 if (tcp_client < 0) { |
| 363 SET_ERROR_MSG(error_msg, "Unable to connect to %s:%d", host, port); |
| 364 return NULL; |
| 365 } |
| 366 // Send get request. |
| 367 { |
| 368 const char* format = |
| 369 "GET %s HTTP/1.0\r\nUser-Agent: Dart VM\r\nHost: %s\r\n\r\n"; |
| 370 intptr_t len = snprintf(NULL, 0, format, path, host); |
| 371 char* get_request = reinterpret_cast<char*>(malloc(len + 1)); |
| 372 snprintf(get_request, len + 1, format, path, host); |
| 373 intptr_t r = SocketWriteString(tcp_client, get_request, len); |
| 374 free(get_request); |
| 375 if (r < len) { |
| 376 SET_ERROR_MSG(error_msg, "Unable to write to %s:%d - %d", host, port, |
| 377 static_cast<int>(r)); |
| 378 Socket::Close(tcp_client); |
| 379 return NULL; |
| 380 } |
| 381 ASSERT(r == len); |
| 382 } |
| 383 // Consume response. |
| 384 uint8_t* response = SocketReadUntilEOF(tcp_client, response_len); |
| 385 // Close socket. |
| 386 Socket::Close(tcp_client); |
| 387 if (response == NULL) { |
| 388 SET_ERROR_MSG(error_msg, "Unable to read from %s:%d", host, port); |
| 389 return NULL; |
| 390 } |
| 391 if (HttpGetRequestOkay(reinterpret_cast<const char*>(response)) == false) { |
| 392 SET_ERROR_MSG(error_msg, "Invalid HTTP response from %s:%d", host, port); |
| 393 free(response); |
| 394 return NULL; |
| 395 } |
| 396 return response; |
| 397 } |
| 398 |
| 399 |
| 400 static Dart_Handle ParseHttpUri(Dart_Handle uri, const char** host_str, |
| 401 int64_t* port_int, const char** path_str) { |
| 402 ASSERT(host_str != NULL); |
| 403 ASSERT(port_int != NULL); |
| 404 ASSERT(path_str != NULL); |
| 405 Dart_Handle result; |
| 406 Dart_Handle builtin_lib = |
| 407 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
| 408 Dart_Handle path = DartUtils::PathFromUri(uri, builtin_lib); |
| 409 if (Dart_IsError(path)) { |
| 410 return path; |
| 411 } |
| 412 Dart_Handle host = DartUtils::HostFromUri(uri, builtin_lib); |
| 413 if (Dart_IsError(host)) { |
| 414 return host; |
| 415 } |
| 416 Dart_Handle port = DartUtils::PortFromUri(uri, builtin_lib); |
| 417 if (Dart_IsError(port)) { |
| 418 return port; |
| 419 } |
| 420 result = Dart_StringToCString(path, path_str); |
| 273 if (Dart_IsError(result)) { | 421 if (Dart_IsError(result)) { |
| 274 return result; | 422 return result; |
| 275 } | 423 } |
| 276 intptr_t responseCode = | 424 result = Dart_StringToCString(host, host_str); |
| 277 DartUtils::GetIntegerField(builtin_lib, "_httpRequestResponseCode"); | 425 if (Dart_IsError(result)) { |
| 278 if (responseCode != HttpResponseCodeOK) { | 426 return result; |
| 279 // Return error. | |
| 280 Dart_Handle responseStatus = | |
| 281 Dart_GetField(builtin_lib, | |
| 282 DartUtils::NewString("_httpRequestStatusString")); | |
| 283 if (Dart_IsError(responseStatus)) { | |
| 284 return responseStatus; | |
| 285 } | |
| 286 if (Dart_IsNull(responseStatus)) { | |
| 287 return Dart_Error("HTTP error."); | |
| 288 } | |
| 289 return Dart_Error(DartUtils::GetStringValue(responseStatus)); | |
| 290 } | 427 } |
| 291 Dart_Handle response = | 428 if (DartUtils::GetInt64Value(port, port_int) == false) { |
| 292 Dart_GetField(builtin_lib, DartUtils::NewString("_httpRequestResponse")); | 429 return Dart_Error("Invalid port"); |
| 293 if (Dart_IsError(response)) { | |
| 294 return response; | |
| 295 } | |
| 296 if (Dart_IsString(response)) { | |
| 297 // Received response as string. | |
| 298 uint8_t* responseString = NULL; | |
| 299 intptr_t responseStringLength; | |
| 300 Dart_Handle r = Dart_StringToUTF8(response, &responseString, | |
| 301 &responseStringLength); | |
| 302 if (Dart_IsError(r)) { | |
| 303 *buffer = NULL; | |
| 304 *buffer_len = 0; | |
| 305 return r; | |
| 306 } | |
| 307 // Get payload as bytes. | |
| 308 *buffer_len = responseStringLength; | |
| 309 *buffer = reinterpret_cast<uint8_t*>(malloc(responseStringLength)); | |
| 310 memmove(*buffer, responseString, responseStringLength); | |
| 311 } else { | |
| 312 // Received response as list of bytes. | |
| 313 ASSERT(Dart_IsList(response)); | |
| 314 // Query list length. | |
| 315 result = Dart_ListLength(response, buffer_len); | |
| 316 if (Dart_IsError(result)) { | |
| 317 *buffer_len = 0; | |
| 318 *buffer = NULL; | |
| 319 return result; | |
| 320 } | |
| 321 // Get payload as bytes. | |
| 322 *buffer = reinterpret_cast<uint8_t*>(malloc(*buffer_len)); | |
| 323 result = Dart_ListGetAsBytes(response, 0, *buffer, *buffer_len); | |
| 324 if (Dart_IsError(result)) { | |
| 325 free(*buffer); | |
| 326 *buffer_len = 0; | |
| 327 *buffer = NULL; | |
| 328 return result; | |
| 329 } | |
| 330 } | 430 } |
| 331 return result; | 431 return result; |
| 332 } | 432 } |
| 333 | 433 |
| 334 | 434 |
| 335 Dart_Handle DartUtils::ReadStringFromHttp(const char* script_uri) { | 435 Dart_Handle DartUtils::ReadStringFromHttp(const char* filename) { |
| 336 Dart_Handle uri = NewString(script_uri); | 436 Dart_Handle uri = NewString(filename); |
| 337 if (Dart_IsError(uri)) { | 437 if (Dart_IsError(uri)) { |
| 338 return uri; | 438 return uri; |
| 339 } | 439 } |
| 340 Dart_Handle builtin_lib = | 440 const char* host_str = NULL; |
| 341 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | 441 int64_t port_int = 0; |
| 342 uint8_t* buffer; | 442 const char* path_str = NULL; |
| 343 intptr_t bufferLen; | 443 Dart_Handle result = ParseHttpUri(uri, &host_str, &port_int, |
| 344 Dart_Handle result = MakeHttpRequest(uri, builtin_lib, &buffer, &bufferLen); | 444 &path_str); |
| 345 if (Dart_IsError(result)) { | 445 if (Dart_IsError(result)) { |
| 346 return result; | 446 return result; |
| 347 } | 447 } |
| 348 Dart_Handle str = Dart_NewStringFromUTF8(buffer, | 448 const char* error_msg = NULL; |
| 349 bufferLen); | 449 intptr_t len; |
| 350 free(buffer); | 450 const uint8_t* text_buffer = HttpGetRequest(host_str, path_str, port_int, |
| 451 &len, &error_msg); |
| 452 if (text_buffer == NULL) { |
| 453 return Dart_Error(error_msg); |
| 454 } |
| 455 const uint8_t* payload = HttpRequestGetPayload( |
| 456 reinterpret_cast<const char*>(text_buffer)); |
| 457 if (payload == NULL) { |
| 458 return Dart_Error("Invalid HTTP response."); |
| 459 } |
| 460 // Subtract HTTP response from length. |
| 461 len -= (payload-text_buffer); |
| 462 ASSERT(len >= 0); |
| 463 Dart_Handle str = Dart_NewStringFromUTF8(payload, len); |
| 351 return str; | 464 return str; |
| 352 } | 465 } |
| 353 | 466 |
| 354 | 467 |
| 355 static const uint8_t* ReadFileFully(const char* filename, | 468 static const uint8_t* ReadFileFully(const char* filename, |
| 356 intptr_t* file_len, | 469 intptr_t* file_len, |
| 357 const char** error_msg) { | 470 const char** error_msg) { |
| 358 void* stream = DartUtils::OpenFile(filename, false); | 471 void* stream = DartUtils::OpenFile(filename, false); |
| 359 if (stream == NULL) { | 472 if (stream == NULL) { |
| 360 SET_ERROR_MSG(error_msg, "Unable to open file: %s", filename); | 473 SET_ERROR_MSG(error_msg, "Unable to open file: %s", filename); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 377 intptr_t len; | 490 intptr_t len; |
| 378 const uint8_t* text_buffer = ReadFileFully(filename, &len, &error_msg); | 491 const uint8_t* text_buffer = ReadFileFully(filename, &len, &error_msg); |
| 379 if (text_buffer == NULL) { | 492 if (text_buffer == NULL) { |
| 380 return Dart_Error(error_msg); | 493 return Dart_Error(error_msg); |
| 381 } | 494 } |
| 382 Dart_Handle str = Dart_NewStringFromUTF8(text_buffer, len); | 495 Dart_Handle str = Dart_NewStringFromUTF8(text_buffer, len); |
| 383 return str; | 496 return str; |
| 384 } | 497 } |
| 385 | 498 |
| 386 | 499 |
| 500 Dart_Handle DartUtils::SetWorkingDirectory(Dart_Handle builtin_lib) { |
| 501 Dart_Handle directory = NewString(original_working_directory); |
| 502 return SingleArgDart_Invoke(directory, builtin_lib, "_setWorkingDirectory"); |
| 503 } |
| 504 |
| 505 |
| 387 Dart_Handle DartUtils::ResolveScriptUri(Dart_Handle script_uri, | 506 Dart_Handle DartUtils::ResolveScriptUri(Dart_Handle script_uri, |
| 388 Dart_Handle builtin_lib) { | 507 Dart_Handle builtin_lib) { |
| 389 const int kNumArgs = 3; | 508 return SingleArgDart_Invoke(script_uri, builtin_lib, "_resolveScriptUri"); |
| 390 Dart_Handle dart_args[kNumArgs]; | |
| 391 dart_args[0] = NewString(original_working_directory); | |
| 392 dart_args[1] = script_uri; | |
| 393 dart_args[2] = (IsWindowsHost() ? Dart_True() : Dart_False()); | |
| 394 return Dart_Invoke(builtin_lib, | |
| 395 NewString("_resolveScriptUri"), | |
| 396 kNumArgs, | |
| 397 dart_args); | |
| 398 } | 509 } |
| 399 | 510 |
| 400 | 511 |
| 401 Dart_Handle DartUtils::FilePathFromUri(Dart_Handle script_uri, | 512 Dart_Handle DartUtils::FilePathFromUri(Dart_Handle script_uri, |
| 402 Dart_Handle builtin_lib) { | 513 Dart_Handle builtin_lib) { |
| 403 const int kNumArgs = 2; | 514 return SingleArgDart_Invoke(script_uri, builtin_lib, "_filePathFromUri"); |
| 404 Dart_Handle dart_args[kNumArgs]; | |
| 405 dart_args[0] = script_uri; | |
| 406 dart_args[1] = (IsWindowsHost() ? Dart_True() : Dart_False()); | |
| 407 return Dart_Invoke(builtin_lib, | |
| 408 NewString("_filePathFromUri"), | |
| 409 kNumArgs, | |
| 410 dart_args); | |
| 411 } | 515 } |
| 412 | 516 |
| 413 | 517 |
| 518 Dart_Handle DartUtils::PathFromUri(Dart_Handle script_uri, |
| 519 Dart_Handle builtin_lib) { |
| 520 return SingleArgDart_Invoke(script_uri, builtin_lib, "_pathFromHttpUri"); |
| 521 } |
| 522 |
| 523 |
| 524 Dart_Handle DartUtils::HostFromUri(Dart_Handle script_uri, |
| 525 Dart_Handle builtin_lib) { |
| 526 return SingleArgDart_Invoke(script_uri, builtin_lib, "_hostFromHttpUri"); |
| 527 } |
| 528 |
| 529 |
| 530 Dart_Handle DartUtils::PortFromUri(Dart_Handle script_uri, |
| 531 Dart_Handle builtin_lib) { |
| 532 return SingleArgDart_Invoke(script_uri, builtin_lib, "_portFromHttpUri"); |
| 533 } |
| 534 |
| 535 |
| 414 Dart_Handle DartUtils::ResolveUri(Dart_Handle library_url, | 536 Dart_Handle DartUtils::ResolveUri(Dart_Handle library_url, |
| 415 Dart_Handle url, | 537 Dart_Handle url, |
| 416 Dart_Handle builtin_lib) { | 538 Dart_Handle builtin_lib) { |
| 417 const int kNumArgs = 2; | 539 const int kNumArgs = 2; |
| 418 Dart_Handle dart_args[kNumArgs]; | 540 Dart_Handle dart_args[kNumArgs]; |
| 419 dart_args[0] = library_url; | 541 dart_args[0] = library_url; |
| 420 dart_args[1] = url; | 542 dart_args[1] = url; |
| 421 return Dart_Invoke( | 543 return Dart_Invoke( |
| 422 builtin_lib, NewString("_resolveUri"), kNumArgs, dart_args); | 544 builtin_lib, NewString("_resolveUri"), kNumArgs, dart_args); |
| 423 } | 545 } |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 533 | 655 |
| 534 void DartUtils::WriteMagicNumber(File* file) { | 656 void DartUtils::WriteMagicNumber(File* file) { |
| 535 // Write a magic number and version information into the snapshot file. | 657 // Write a magic number and version information into the snapshot file. |
| 536 bool bytes_written = file->WriteFully(magic_number, sizeof(magic_number)); | 658 bool bytes_written = file->WriteFully(magic_number, sizeof(magic_number)); |
| 537 ASSERT(bytes_written); | 659 ASSERT(bytes_written); |
| 538 } | 660 } |
| 539 | 661 |
| 540 | 662 |
| 541 Dart_Handle DartUtils::LoadScriptHttp(Dart_Handle uri, | 663 Dart_Handle DartUtils::LoadScriptHttp(Dart_Handle uri, |
| 542 Dart_Handle builtin_lib) { | 664 Dart_Handle builtin_lib) { |
| 543 intptr_t len = 0; | 665 const char* host_str = NULL; |
| 544 uint8_t* buffer = NULL; | 666 int64_t port_int = 0; |
| 545 Dart_Handle result = MakeHttpRequest(uri, builtin_lib, &buffer, &len); | 667 const char* path_str = NULL; |
| 668 Dart_Handle result = ParseHttpUri(uri, &host_str, &port_int, |
| 669 &path_str); |
| 546 if (Dart_IsError(result)) { | 670 if (Dart_IsError(result)) { |
| 547 return result; | 671 return result; |
| 548 } | 672 } |
| 549 const uint8_t* payload = buffer; | 673 const char* error_msg = NULL; |
| 674 intptr_t len; |
| 675 const uint8_t* text_buffer; |
| 676 text_buffer = HttpGetRequest(host_str, path_str, port_int, &len, |
| 677 &error_msg); |
| 678 if (text_buffer == NULL) { |
| 679 return Dart_Error(error_msg); |
| 680 } |
| 681 const uint8_t* payload = HttpRequestGetPayload( |
| 682 reinterpret_cast<const char*>(text_buffer)); |
| 683 if (payload == NULL) { |
| 684 return Dart_Error("Invalid HTTP response."); |
| 685 } |
| 686 // Subtract HTTP response from length. |
| 687 len -= (payload-text_buffer); |
| 688 ASSERT(len >= 0); |
| 689 // At this point we have received a valid HTTP 200 reply and |
| 690 // payload points at the beginning of the script or snapshot. |
| 550 bool is_snapshot = false; | 691 bool is_snapshot = false; |
| 551 payload = SniffForMagicNumber(payload, &len, &is_snapshot); | 692 payload = SniffForMagicNumber(payload, &len, &is_snapshot); |
| 552 if (is_snapshot) { | 693 if (is_snapshot) { |
| 553 return Dart_LoadScriptFromSnapshot(payload, len); | 694 return Dart_LoadScriptFromSnapshot(payload, len); |
| 554 } else { | 695 } else { |
| 555 Dart_Handle source = Dart_NewStringFromUTF8(payload, len); | 696 Dart_Handle source = Dart_NewStringFromUTF8(payload, len); |
| 556 free(buffer); | |
| 557 if (Dart_IsError(source)) { | 697 if (Dart_IsError(source)) { |
| 558 return source; | 698 return source; |
| 559 } | 699 } |
| 560 return Dart_LoadScript(uri, source, 0, 0); | 700 return Dart_LoadScript(uri, source, 0, 0); |
| 561 } | 701 } |
| 562 } | 702 } |
| 563 | 703 |
| 564 | 704 |
| 565 Dart_Handle DartUtils::LoadScript(const char* script_uri, | 705 Dart_Handle DartUtils::LoadScript(const char* script_uri, |
| 566 Dart_Handle builtin_lib) { | 706 Dart_Handle builtin_lib) { |
| 567 // Always call ResolveScriptUri because as a side effect it sets | |
| 568 // the script entry path which is used when automatically resolving | |
| 569 // package root. | |
| 570 Dart_Handle resolved_script_uri = | 707 Dart_Handle resolved_script_uri = |
| 571 ResolveScriptUri(NewString(script_uri), builtin_lib); | 708 ResolveScriptUri(NewString(script_uri), builtin_lib); |
| 572 if (Dart_IsError(resolved_script_uri)) { | 709 if (Dart_IsError(resolved_script_uri)) { |
| 573 return resolved_script_uri; | 710 return resolved_script_uri; |
| 574 } | 711 } |
| 575 // Handle http: requests separately. | 712 // Handle http: requests separately. |
| 576 if (DartUtils::IsHttpSchemeURL(script_uri)) { | 713 if (DartUtils::IsHttpSchemeURL(script_uri)) { |
| 577 return LoadScriptHttp(resolved_script_uri, builtin_lib); | 714 return LoadScriptHttp(resolved_script_uri, builtin_lib); |
| 578 } | 715 } |
| 579 Dart_Handle script_path = DartUtils::FilePathFromUri(resolved_script_uri, | 716 Dart_Handle script_path = DartUtils::FilePathFromUri(resolved_script_uri, |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 659 Dart_Handle async_lib = Dart_LookupLibrary(url); | 796 Dart_Handle async_lib = Dart_LookupLibrary(url); |
| 660 DART_CHECK_VALID(async_lib); | 797 DART_CHECK_VALID(async_lib); |
| 661 Dart_Handle io_lib = Builtin::LoadAndCheckLibrary(Builtin::kIOLibrary); | 798 Dart_Handle io_lib = Builtin::LoadAndCheckLibrary(Builtin::kIOLibrary); |
| 662 Dart_Handle timer_closure = | 799 Dart_Handle timer_closure = |
| 663 Dart_Invoke(io_lib, NewString("_getTimerFactoryClosure"), 0, NULL); | 800 Dart_Invoke(io_lib, NewString("_getTimerFactoryClosure"), 0, NULL); |
| 664 Dart_Handle args[1]; | 801 Dart_Handle args[1]; |
| 665 args[0] = timer_closure; | 802 args[0] = timer_closure; |
| 666 DART_CHECK_VALID(Dart_Invoke( | 803 DART_CHECK_VALID(Dart_Invoke( |
| 667 async_lib, NewString("_setTimerFactoryClosure"), 1, args)); | 804 async_lib, NewString("_setTimerFactoryClosure"), 1, args)); |
| 668 | 805 |
| 806 |
| 807 if (IsWindowsHost()) { |
| 808 result = Dart_Invoke(builtin_lib, NewString("_setWindows"), 0, 0); |
| 809 if (Dart_IsError(result)) { |
| 810 return result; |
| 811 } |
| 812 } |
| 813 |
| 814 if (Dart_IsError(result)) { |
| 815 return result; |
| 816 } |
| 817 // Set current working directory. |
| 818 result = SetWorkingDirectory(builtin_lib); |
| 819 if (Dart_IsError(result)) { |
| 820 return result; |
| 821 } |
| 822 |
| 669 // Set up package root if specified. | 823 // Set up package root if specified. |
| 670 if (package_root != NULL) { | 824 if (package_root != NULL) { |
| 671 result = NewString(package_root); | 825 result = NewString(package_root); |
| 672 if (!Dart_IsError(result)) { | 826 if (!Dart_IsError(result)) { |
| 673 const int kNumArgs = 1; | 827 const int kNumArgs = 1; |
| 674 Dart_Handle dart_args[kNumArgs]; | 828 Dart_Handle dart_args[kNumArgs]; |
| 675 dart_args[0] = result; | 829 dart_args[0] = result; |
| 676 return Dart_Invoke(builtin_lib, | 830 return Dart_Invoke(builtin_lib, |
| 677 NewString("_setPackageRoot"), | 831 NewString("_setPackageRoot"), |
| 678 kNumArgs, | 832 kNumArgs, |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 958 new CObjectString(CObject::NewString(os_error->message())); | 1112 new CObjectString(CObject::NewString(os_error->message())); |
| 959 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 1113 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
| 960 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 1114 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
| 961 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 1115 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
| 962 result->SetAt(2, error_message); | 1116 result->SetAt(2, error_message); |
| 963 return result; | 1117 return result; |
| 964 } | 1118 } |
| 965 | 1119 |
| 966 } // namespace bin | 1120 } // namespace bin |
| 967 } // namespace dart | 1121 } // namespace dart |
| OLD | NEW |