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

Side by Side Diff: runtime/bin/dartutils.cc

Issue 15736023: Remove hack http client and use the one in dart:io (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 6 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
« runtime/bin/builtin.dart ('K') | « runtime/bin/builtin.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "bin/dartutils.h" 5 #include "bin/dartutils.h"
6 6
7 #include "include/dart_api.h" 7 #include "include/dart_api.h"
8 8
9 #include "platform/assert.h" 9 #include "platform/assert.h"
10 #include "platform/globals.h" 10 #include "platform/globals.h"
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 bool bytes_written = file_stream->WriteFully(buffer, num_bytes); 235 bool bytes_written = file_stream->WriteFully(buffer, num_bytes);
236 ASSERT(bytes_written); 236 ASSERT(bytes_written);
237 } 237 }
238 238
239 239
240 void DartUtils::CloseFile(void* stream) { 240 void DartUtils::CloseFile(void* stream) {
241 delete reinterpret_cast<File*>(stream); 241 delete reinterpret_cast<File*>(stream);
242 } 242 }
243 243
244 244
245 // Writes string into socket. 245 static Dart_Handle SingleArgDart_Invoke(Dart_Handle arg, Dart_Handle lib,
246 // Return < 0 indicates an error. 246 const char* method) {
247 // Return >= 0 number of bytes written. 247 const int kNumArgs = 1;
248 static intptr_t SocketWriteString(intptr_t socket, const char* str, 248 Dart_Handle dart_args[kNumArgs];
249 intptr_t len) { 249 dart_args[0] = arg;
250 int r; 250 return Dart_Invoke(lib, DartUtils::NewString(method), kNumArgs, dart_args);
251 intptr_t cursor = 0;
252 do {
253 r = Socket::Write(socket, &str[cursor], len);
254 if (r < 0) {
255 return r;
256 }
257 cursor += r;
258 len -= r;
259 } while (len > 0);
260 ASSERT(len == 0);
261 return cursor;
262 } 251 }
263 252
264 253
265 static uint8_t* SocketReadUntilEOF(intptr_t socket, intptr_t* response_len) {
266 const intptr_t kInitialBufferSize = 16 * KB;
267 intptr_t buffer_size = kInitialBufferSize;
268 uint8_t* buffer = reinterpret_cast<uint8_t*>(malloc(buffer_size));
269 ASSERT(buffer != NULL);
270 intptr_t buffer_cursor = 0;
271 do {
272 int bytes_read = Socket::Read(socket, &buffer[buffer_cursor],
273 buffer_size - buffer_cursor - 1);
274 if (bytes_read < 0) {
275 free(buffer);
276 return NULL;
277 }
278
279 buffer_cursor += bytes_read;
280
281 if (bytes_read == 0) {
282 *response_len = buffer_cursor;
283 buffer[buffer_cursor] = '\0';
284 break;
285 }
286
287 // There is still more data to be read, check that we have room in the
288 // buffer for more data.
289 if (buffer_cursor == buffer_size - 1) {
290 // Buffer is full. Increase buffer size.
291 buffer_size *= 2;
292 buffer = reinterpret_cast<uint8_t*>(realloc(buffer, buffer_size));
293 ASSERT(buffer != NULL);
294 }
295 } while (true);
296 return buffer;
297 }
298
299
300 static bool HttpGetRequestOkay(const char* response) {
301 static const char* kOkayReply = "HTTP/1.0 200 OK";
302 static const intptr_t kOkayReplyLen = strlen(kOkayReply);
303 return (strncmp(response, kOkayReply, kOkayReplyLen) == 0);
304 }
305
306
307 static const uint8_t* HttpRequestGetPayload(const char* response) {
308 const char* split = strstr(response, "\r\n\r\n");
309 if (split != NULL) {
310 return reinterpret_cast<const uint8_t*>(split+4);
311 }
312 return NULL;
313 }
314
315
316 // TODO(iposva): Allocate from the zone instead of leaking error string 254 // TODO(iposva): Allocate from the zone instead of leaking error string
317 // here. On the other hand the binary is about the exit anyway. 255 // here. On the other hand the binary is about the exit anyway.
318 #define SET_ERROR_MSG(error_msg, format, ...) \ 256 #define SET_ERROR_MSG(error_msg, format, ...) \
319 intptr_t len = snprintf(NULL, 0, format, __VA_ARGS__); \ 257 intptr_t len = snprintf(NULL, 0, format, __VA_ARGS__); \
320 char *msg = reinterpret_cast<char*>(malloc(len + 1)); \ 258 char *msg = reinterpret_cast<char*>(malloc(len + 1)); \
321 snprintf(msg, len + 1, format, __VA_ARGS__); \ 259 snprintf(msg, len + 1, format, __VA_ARGS__); \
322 *error_msg = msg 260 *error_msg = msg
323 261
324 262
325 static const uint8_t* HttpGetRequest(const char* host, const char* path, 263 Dart_Handle MakeHttpRequest(Dart_Handle uri, Dart_Handle builtin_lib,
326 int port, intptr_t* response_len, 264 uint8_t** buffer, intptr_t* buffer_len) {
327 const char** error_msg) { 265 ASSERT(buffer != NULL);
328 OSError* error = NULL; 266 ASSERT(buffer_len != NULL);
siva 2013/05/31 21:02:02 ASSERT(!Dart_HasLivePorts());
Cutch 2013/05/31 21:38:34 Done.
329 SocketAddresses* addresses = Socket::LookupAddress(host, -1, &error); 267 SingleArgDart_Invoke(uri, builtin_lib, "_makeHttpRequest");
330 if (addresses == NULL || addresses->count() == 0) { 268 // Run until all ports to isolate are closed.
331 SET_ERROR_MSG(error_msg, "Unable to resolve %s", host); 269 Dart_Handle result = Dart_RunLoop();
332 return NULL;
333 }
334
335 int preferred_address = 0;
336 for (int i = 0; i < addresses->count(); i++) {
337 SocketAddress* address = addresses->GetAt(i);
338 if (address->GetType() == SocketAddress::ADDRESS_LOOPBACK_IP_V4) {
339 // Prefer the IP_V4 loop back.
340 preferred_address = i;
341 break;
342 }
343 }
344
345 RawAddr addr = addresses->GetAt(preferred_address)->addr();
346 intptr_t tcp_client = Socket::Create(addr);
347 if (tcp_client < 0) {
348 SET_ERROR_MSG(error_msg, "Unable to create socket to %s:%d", host, port);
349 return NULL;
350 }
351 Socket::Connect(tcp_client, addr, port);
352 if (tcp_client < 0) {
353 SET_ERROR_MSG(error_msg, "Unable to connect to %s:%d", host, port);
354 return NULL;
355 }
356 // Send get request.
357 {
358 const char* format =
359 "GET %s HTTP/1.0\r\nUser-Agent: Dart VM\r\nHost: %s\r\n\r\n";
360 intptr_t len = snprintf(NULL, 0, format, path, host);
361 char* get_request = reinterpret_cast<char*>(malloc(len + 1));
362 snprintf(get_request, len + 1, format, path, host);
363 intptr_t r = SocketWriteString(tcp_client, get_request, len);
364 free(get_request);
365 if (r < len) {
366 SET_ERROR_MSG(error_msg, "Unable to write to %s:%d - %d", host, port,
367 static_cast<int>(r));
368 Socket::Close(tcp_client);
369 return NULL;
370 }
371 ASSERT(r == len);
372 }
373 // Consume response.
374 uint8_t* response = SocketReadUntilEOF(tcp_client, response_len);
375 // Close socket.
376 Socket::Close(tcp_client);
377 if (response == NULL) {
378 SET_ERROR_MSG(error_msg, "Unable to read from %s:%d", host, port);
379 return NULL;
380 }
381 if (HttpGetRequestOkay(reinterpret_cast<const char*>(response)) == false) {
382 SET_ERROR_MSG(error_msg, "Invalid HTTP response from %s:%d", host, port);
383 free(response);
384 return NULL;
385 }
386 return response;
387 }
388
389
390 static Dart_Handle ParseHttpUri(const char* script_uri, const char** host_str,
391 int64_t* port_int, const char** path_str) {
392 ASSERT(script_uri != NULL);
393 ASSERT(host_str != NULL);
394 ASSERT(port_int != NULL);
395 ASSERT(path_str != NULL);
396 Dart_Handle result;
397 Dart_Handle uri = DartUtils::NewString(script_uri);
398 Dart_Handle builtin_lib =
399 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary);
400 Dart_Handle path = DartUtils::PathFromUri(uri, builtin_lib);
401 if (Dart_IsError(path)) {
402 return path;
403 }
404 Dart_Handle host = DartUtils::DomainFromUri(uri, builtin_lib);
405 if (Dart_IsError(host)) {
406 return host;
407 }
408 Dart_Handle port = DartUtils::PortFromUri(uri, builtin_lib);
409 if (Dart_IsError(port)) {
410 return port;
411 }
412 result = Dart_StringToCString(path, path_str);
413 if (Dart_IsError(result)) { 270 if (Dart_IsError(result)) {
414 return result; 271 return result;
415 } 272 }
416 result = Dart_StringToCString(host, host_str); 273 intptr_t responseCode =
274 DartUtils::GetIntegerField(builtin_lib, "_httpRequestResponseCode");
275 Dart_Handle responseStatus =
276 Dart_GetField(builtin_lib,
277 DartUtils::NewString("_httpRequestStatusString"));
278 if (Dart_IsError(responseStatus)) {
279 return responseStatus;
280 }
siva 2013/05/31 21:02:02 Can the get field of _httpRequestStatusString be
Cutch 2013/05/31 21:38:34 Done.
281 Dart_Handle responseList =
282 Dart_GetField(builtin_lib, DartUtils::NewString("_httpRequestResponse"));
283 if (Dart_IsError(responseList)) {
284 return responseList;
285 }
siva 2013/05/31 21:02:02 This get field code could be moved below the if (r
Cutch 2013/05/31 21:38:34 Done.
286 if (responseCode != 200) {
287 // Return error.
288 if (Dart_IsNull(responseStatus)) {
289 return Dart_Error("HTTP error.");
290 }
291 return Dart_Error(DartUtils::GetStringValue(responseStatus));
292 }
293 // Query list length.
294 result = Dart_ListLength(responseList, buffer_len);
417 if (Dart_IsError(result)) { 295 if (Dart_IsError(result)) {
296 *buffer_len = 0;
297 *buffer = NULL;
418 return result; 298 return result;
419 } 299 }
420 if (DartUtils::GetInt64Value(port, port_int) == false) { 300 // Get payload as bytes.
421 return Dart_Error("Invalid port"); 301 *buffer = reinterpret_cast<uint8_t*>(malloc(*buffer_len));
302 result = Dart_ListGetAsBytes(responseList, 0, *buffer, *buffer_len);
303 if (Dart_IsError(result)) {
304 free(*buffer);
305 *buffer_len = 0;
306 *buffer = NULL;
307 return result;
422 } 308 }
423 return result; 309 return result;
424 } 310 }
425 311
426 312
427 Dart_Handle DartUtils::ReadStringFromHttp(const char* script_uri) { 313 Dart_Handle DartUtils::ReadStringFromHttp(const char* script_uri) {
428 const char* host_str = NULL; 314 Dart_Handle uri = NewString(script_uri);
429 int64_t port_int = 0; 315 if (Dart_IsError(uri)) {
430 const char* path_str = NULL; 316 return uri;
431 Dart_Handle result = ParseHttpUri(script_uri, &host_str, &port_int, 317 }
432 &path_str); 318 Dart_Handle builtin_lib =
319 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary);
320 uint8_t* buffer;
321 intptr_t bufferLen;
322 Dart_Handle result = MakeHttpRequest(uri, builtin_lib, &buffer, &bufferLen);
433 if (Dart_IsError(result)) { 323 if (Dart_IsError(result)) {
434 return result; 324 return result;
435 } 325 }
436 const char* error_msg = NULL; 326 Dart_Handle str = Dart_NewStringFromUTF8(buffer,
437 intptr_t len; 327 bufferLen);
438 const uint8_t* text_buffer = HttpGetRequest(host_str, path_str, port_int, 328 free(buffer);
439 &len, &error_msg);
440 if (text_buffer == NULL) {
441 return Dart_Error(error_msg);
442 }
443 const uint8_t* payload = HttpRequestGetPayload(
444 reinterpret_cast<const char*>(text_buffer));
445 if (payload == NULL) {
446 return Dart_Error("Invalid HTTP response.");
447 }
448 // Subtract HTTP response from length.
449 len -= (payload-text_buffer);
450 ASSERT(len >= 0);
451 Dart_Handle str = Dart_NewStringFromUTF8(payload, len);
452 return str; 329 return str;
453 } 330 }
454 331
455 332
456 static const uint8_t* ReadFileFully(const char* filename, 333 static const uint8_t* ReadFileFully(const char* filename,
457 intptr_t* file_len, 334 intptr_t* file_len,
458 const char** error_msg) { 335 const char** error_msg) {
459 void* stream = DartUtils::OpenFile(filename, false); 336 void* stream = DartUtils::OpenFile(filename, false);
460 if (stream == NULL) { 337 if (stream == NULL) {
461 SET_ERROR_MSG(error_msg, "Unable to open file: %s", filename); 338 SET_ERROR_MSG(error_msg, "Unable to open file: %s", filename);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 Dart_Handle dart_args[kNumArgs]; 382 Dart_Handle dart_args[kNumArgs];
506 dart_args[0] = script_uri; 383 dart_args[0] = script_uri;
507 dart_args[1] = (IsWindowsHost() ? Dart_True() : Dart_False()); 384 dart_args[1] = (IsWindowsHost() ? Dart_True() : Dart_False());
508 return Dart_Invoke(builtin_lib, 385 return Dart_Invoke(builtin_lib,
509 NewString("_filePathFromUri"), 386 NewString("_filePathFromUri"),
510 kNumArgs, 387 kNumArgs,
511 dart_args); 388 dart_args);
512 } 389 }
513 390
514 391
515 static Dart_Handle SingleArgDart_Invoke(Dart_Handle arg, Dart_Handle lib,
516 const char* method) {
517 const int kNumArgs = 1;
518 Dart_Handle dart_args[kNumArgs];
519 dart_args[0] = arg;
520 return Dart_Invoke(lib, DartUtils::NewString(method), kNumArgs, dart_args);
521 }
522
523 Dart_Handle DartUtils::PathFromUri(Dart_Handle script_uri, 392 Dart_Handle DartUtils::PathFromUri(Dart_Handle script_uri,
524 Dart_Handle builtin_lib) { 393 Dart_Handle builtin_lib) {
525 return SingleArgDart_Invoke(script_uri, builtin_lib, "_pathFromHttpUri"); 394 return SingleArgDart_Invoke(script_uri, builtin_lib, "_pathFromHttpUri");
526 } 395 }
527 396
528 397
529 Dart_Handle DartUtils::DomainFromUri(Dart_Handle script_uri, 398 Dart_Handle DartUtils::DomainFromUri(Dart_Handle script_uri,
530 Dart_Handle builtin_lib) { 399 Dart_Handle builtin_lib) {
531 return SingleArgDart_Invoke(script_uri, builtin_lib, "_domainFromHttpUri"); 400 return SingleArgDart_Invoke(script_uri, builtin_lib, "_domainFromHttpUri");
532 } 401 }
siva 2013/05/31 21:02:02 Are these functions PathFromUri, DomainFromUri sti
Cutch 2013/05/31 21:38:34 Removed.
533 402
534 403
535 Dart_Handle DartUtils::PortFromUri(Dart_Handle script_uri, 404 Dart_Handle DartUtils::PortFromUri(Dart_Handle script_uri,
536 Dart_Handle builtin_lib) { 405 Dart_Handle builtin_lib) {
537 return SingleArgDart_Invoke(script_uri, builtin_lib, "_portFromHttpUri"); 406 return SingleArgDart_Invoke(script_uri, builtin_lib, "_portFromHttpUri");
538 } 407 }
539 408
540 409
541 Dart_Handle DartUtils::ResolveUri(Dart_Handle library_url, 410 Dart_Handle DartUtils::ResolveUri(Dart_Handle library_url,
542 Dart_Handle url, 411 Dart_Handle url,
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
658 } 527 }
659 528
660 529
661 void DartUtils::WriteMagicNumber(File* file) { 530 void DartUtils::WriteMagicNumber(File* file) {
662 // Write a magic number and version information into the snapshot file. 531 // Write a magic number and version information into the snapshot file.
663 bool bytes_written = file->WriteFully(magic_number, sizeof(magic_number)); 532 bool bytes_written = file->WriteFully(magic_number, sizeof(magic_number));
664 ASSERT(bytes_written); 533 ASSERT(bytes_written);
665 } 534 }
666 535
667 536
537
538
siva 2013/05/31 21:02:02 blank lines?
Cutch 2013/05/31 21:38:34 Done.
668 Dart_Handle DartUtils::LoadScriptHttp(const char* script_uri, 539 Dart_Handle DartUtils::LoadScriptHttp(const char* script_uri,
siva 2013/05/31 21:02:02 I think this should probably be (Dart_Handle scrip
Cutch 2013/05/31 21:38:34 Done. Will finish the cleanup in the next CL.
669 Dart_Handle builtin_lib) { 540 Dart_Handle builtin_lib) {
670 Dart_Handle uri = NewString(script_uri); 541 Dart_Handle uri = NewString(script_uri);
671 if (Dart_IsError(uri)) { 542 if (Dart_IsError(uri)) {
672 return uri; 543 return uri;
673 } 544 }
674 const char* host_str = NULL; 545 intptr_t len = 0;
675 int64_t port_int = 0; 546 uint8_t* buffer = NULL;
676 const char* path_str = NULL; 547 Dart_Handle result = MakeHttpRequest(uri, builtin_lib, &buffer, &len);
677 Dart_Handle result = ParseHttpUri(script_uri, &host_str, &port_int,
678 &path_str);
679 if (Dart_IsError(result)) { 548 if (Dart_IsError(result)) {
680 return result; 549 return result;
681 } 550 }
682 const char* error_msg = NULL; 551 const uint8_t* payload = buffer;
683 intptr_t len;
684 const uint8_t* text_buffer;
685 text_buffer = HttpGetRequest(host_str, path_str, port_int, &len,
686 &error_msg);
687 if (text_buffer == NULL) {
688 return Dart_Error(error_msg);
689 }
690 const uint8_t* payload = HttpRequestGetPayload(
691 reinterpret_cast<const char*>(text_buffer));
692 if (payload == NULL) {
693 return Dart_Error("Invalid HTTP response.");
694 }
695 // Subtract HTTP response from length.
696 len -= (payload-text_buffer);
697 ASSERT(len >= 0);
698 // At this point we have received a valid HTTP 200 reply and
699 // payload points at the beginning of the script or snapshot.
700 bool is_snapshot = false; 552 bool is_snapshot = false;
701 payload = SniffForMagicNumber(payload, &len, &is_snapshot); 553 payload = SniffForMagicNumber(payload, &len, &is_snapshot);
702 if (is_snapshot) { 554 if (is_snapshot) {
703 return Dart_LoadScriptFromSnapshot(payload, len); 555 return Dart_LoadScriptFromSnapshot(payload, len);
704 } else { 556 } else {
705 Dart_Handle source = Dart_NewStringFromUTF8(payload, len); 557 Dart_Handle source = Dart_NewStringFromUTF8(payload, len);
706 if (Dart_IsError(source)) { 558 if (Dart_IsError(source)) {
707 return source; 559 return source;
708 } 560 }
709 return Dart_LoadScript(uri, source, 0, 0); 561 return Dart_LoadScript(uri, source, 0, 0);
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after
1103 new CObjectString(CObject::NewString(os_error->message())); 955 new CObjectString(CObject::NewString(os_error->message()));
1104 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); 956 CObjectArray* result = new CObjectArray(CObject::NewArray(3));
1105 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); 957 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError)));
1106 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); 958 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code())));
1107 result->SetAt(2, error_message); 959 result->SetAt(2, error_message);
1108 return result; 960 return result;
1109 } 961 }
1110 962
1111 } // namespace bin 963 } // namespace bin
1112 } // namespace dart 964 } // namespace dart
OLDNEW
« runtime/bin/builtin.dart ('K') | « runtime/bin/builtin.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698