OLD | NEW |
(Empty) | |
| 1 library googleapis_beta.common_internal; |
| 2 |
| 3 import "dart:async"; |
| 4 import "dart:convert"; |
| 5 import "dart:collection" as collection; |
| 6 |
| 7 import "package:crypto/crypto.dart" as crypto; |
| 8 import "../common/common.dart" as common_external; |
| 9 import "package:http/http.dart" as http; |
| 10 |
| 11 const String USER_AGENT_STRING = |
| 12 'google-api-dart-client googleapis_beta/0.1.0'; |
| 13 |
| 14 const CONTENT_TYPE_JSON_UTF8 = 'application/json; charset=utf-8'; |
| 15 |
| 16 /** |
| 17 * Base class for all API clients, offering generic methods for |
| 18 * HTTP Requests to the API |
| 19 */ |
| 20 class ApiRequester { |
| 21 final http.Client _httpClient; |
| 22 final String _rootUrl; |
| 23 final String _basePath; |
| 24 |
| 25 ApiRequester(this._httpClient, this._rootUrl, this._basePath) { |
| 26 assert(_rootUrl.endsWith('/')); |
| 27 } |
| 28 |
| 29 |
| 30 /** |
| 31 * Sends a HTTPRequest using [method] (usually GET or POST) to [requestUrl] |
| 32 * using the specified [urlParams] and [queryParams]. Optionally include a |
| 33 * [body] and/or [uploadMedia] in the request. |
| 34 * |
| 35 * If [uploadMedia] was specified [downloadOptions] must be |
| 36 * [DownloadOptions.Metadata] or `null`. |
| 37 * |
| 38 * If [downloadOptions] is [DownloadOptions.Metadata] the result will be |
| 39 * decoded as JSON. |
| 40 * |
| 41 * If [downloadOptions] is `null` the result will be a Future completing with |
| 42 * `null`. |
| 43 * |
| 44 * Otherwise the result will be downloaded as a [common_external.Media] |
| 45 */ |
| 46 Future request(String requestUrl, String method, |
| 47 {String body, Map queryParams, |
| 48 common_external.Media uploadMedia, |
| 49 common_external.UploadOptions uploadOptions, |
| 50 common_external.DownloadOptions downloadOptions: |
| 51 common_external.DownloadOptions.Metadata}) { |
| 52 if (uploadMedia != null && |
| 53 downloadOptions != common_external.DownloadOptions.Metadata) { |
| 54 throw new ArgumentError('When uploading a [Media] you cannot download a ' |
| 55 '[Media] at the same time!'); |
| 56 } |
| 57 common_external.ByteRange downloadRange; |
| 58 if (downloadOptions is common_external.PartialDownloadOptions && |
| 59 !downloadOptions.isFullDownload) { |
| 60 downloadRange = downloadOptions.range; |
| 61 } |
| 62 |
| 63 return _request(requestUrl, method, body, queryParams, |
| 64 uploadMedia, uploadOptions, |
| 65 downloadOptions, |
| 66 downloadRange) |
| 67 .then(_validateResponse).then((http.StreamedResponse response) { |
| 68 if (downloadOptions == null) { |
| 69 // If no download options are given, the response is of no interest |
| 70 // and we will drain the stream. |
| 71 return response.stream.drain(); |
| 72 } else if (downloadOptions == common_external.DownloadOptions.Metadata) { |
| 73 // Downloading JSON Metadata |
| 74 var stringStream = _decodeStreamAsText(response); |
| 75 if (stringStream != null) { |
| 76 return stringStream.join('').then((String bodyString) { |
| 77 if (bodyString == '') return null; |
| 78 return JSON.decode(bodyString); |
| 79 }); |
| 80 } else { |
| 81 throw new common_external.ApiRequestError( |
| 82 "Unable to read response with content-type " |
| 83 "${response.headers['content-type']}."); |
| 84 } |
| 85 } else { |
| 86 // Downloading Media. |
| 87 var contentType = response.headers['content-type']; |
| 88 if (contentType == null) { |
| 89 throw new common_external.ApiRequestError( |
| 90 "No 'content-type' header in media response."); |
| 91 } |
| 92 var contentLength; |
| 93 try { |
| 94 contentLength = int.parse(response.headers['content-length']); |
| 95 } catch (_) { |
| 96 // We silently ignore errors here. If no content-length was specified |
| 97 // we use `null`. |
| 98 // Please note that the code below still asserts the content-length |
| 99 // is correct for range downloads. |
| 100 } |
| 101 |
| 102 if (downloadRange != null) { |
| 103 if (contentLength != downloadRange.length) { |
| 104 throw new common_external.ApiRequestError( |
| 105 "Content length of response does not match requested range " |
| 106 "length."); |
| 107 } |
| 108 var contentRange = response.headers['content-range']; |
| 109 var expected = 'bytes ${downloadRange.start}-${downloadRange.end}/'; |
| 110 if (contentRange == null || !contentRange.startsWith(expected)) { |
| 111 throw new common_external.ApiRequestError("Attempting partial " |
| 112 "download but got invalid 'Content-Range' header " |
| 113 "(was: $contentRange, expected: $expected)."); |
| 114 } |
| 115 } |
| 116 |
| 117 return new common_external.Media( |
| 118 response.stream, contentLength, contentType: contentType); |
| 119 } |
| 120 }); |
| 121 } |
| 122 |
| 123 Future _request(String requestUrl, String method, |
| 124 String body, Map queryParams, |
| 125 common_external.Media uploadMedia, |
| 126 common_external.UploadOptions uploadOptions, |
| 127 common_external.DownloadOptions downloadOptions, |
| 128 common_external.ByteRange downloadRange) { |
| 129 bool downloadAsMedia = |
| 130 downloadOptions != null && |
| 131 downloadOptions != common_external.DownloadOptions.Metadata; |
| 132 |
| 133 if (queryParams == null) queryParams = {}; |
| 134 |
| 135 if (uploadMedia != null) { |
| 136 if (uploadOptions is common_external.ResumableUploadOptions) { |
| 137 queryParams['uploadType'] = const ['resumable']; |
| 138 } else if (body == null) { |
| 139 queryParams['uploadType'] = const ['media']; |
| 140 } else { |
| 141 queryParams['uploadType'] = const ['multipart']; |
| 142 } |
| 143 } |
| 144 |
| 145 if (downloadAsMedia) { |
| 146 queryParams['alt'] = const ['media']; |
| 147 } else if (downloadOptions != null) { |
| 148 queryParams['alt'] = const ['json']; |
| 149 } |
| 150 |
| 151 var path; |
| 152 if (requestUrl.startsWith('/')) { |
| 153 path ="$_rootUrl${requestUrl.substring(1)}"; |
| 154 } else { |
| 155 path ="$_rootUrl${_basePath.substring(1)}$requestUrl"; |
| 156 } |
| 157 |
| 158 bool containsQueryParameter = path.contains('?'); |
| 159 addQueryParameter(String name, String value) { |
| 160 name = Escaper.escapeQueryComponent(name); |
| 161 value = Escaper.escapeQueryComponent(value); |
| 162 if (containsQueryParameter) { |
| 163 path = '$path&$name=$value'; |
| 164 } else { |
| 165 path = '$path?$name=$value'; |
| 166 } |
| 167 containsQueryParameter = true; |
| 168 } |
| 169 queryParams.forEach((String key, List<String> values) { |
| 170 for (var value in values) { |
| 171 addQueryParameter(key, value); |
| 172 } |
| 173 }); |
| 174 |
| 175 var uri = Uri.parse(path); |
| 176 |
| 177 Future simpleUpload() { |
| 178 var bodyStream = uploadMedia.stream; |
| 179 var request = new RequestImpl(method, uri, bodyStream); |
| 180 request.headers.addAll({ |
| 181 'user-agent' : USER_AGENT_STRING, |
| 182 'content-type' : uploadMedia.contentType, |
| 183 'content-length' : '${uploadMedia.length}' |
| 184 }); |
| 185 return _httpClient.send(request); |
| 186 } |
| 187 |
| 188 Future simpleRequest() { |
| 189 var length = 0; |
| 190 var bodyController = new StreamController<List<int>>(); |
| 191 if (body != null) { |
| 192 var bytes = UTF8.encode(body); |
| 193 bodyController.add(bytes); |
| 194 length = bytes.length; |
| 195 } |
| 196 bodyController.close(); |
| 197 |
| 198 var headers; |
| 199 if (downloadRange != null) { |
| 200 headers = { |
| 201 'user-agent' : USER_AGENT_STRING, |
| 202 'content-type' : CONTENT_TYPE_JSON_UTF8, |
| 203 'content-length' : '$length', |
| 204 'range' : 'bytes=${downloadRange.start}-${downloadRange.end}', |
| 205 }; |
| 206 } else { |
| 207 headers = { |
| 208 'user-agent' : USER_AGENT_STRING, |
| 209 'content-type' : CONTENT_TYPE_JSON_UTF8, |
| 210 'content-length' : '$length', |
| 211 }; |
| 212 } |
| 213 |
| 214 var request = new RequestImpl(method, uri, bodyController.stream); |
| 215 request.headers.addAll(headers); |
| 216 return _httpClient.send(request); |
| 217 } |
| 218 |
| 219 if (uploadMedia != null) { |
| 220 // Three upload types: |
| 221 // 1. Resumable: Upload of data + metdata with multiple requests. |
| 222 // 2. Simple: Upload of media. |
| 223 // 3. Multipart: Upload of data + metadata. |
| 224 |
| 225 if (uploadOptions is common_external.ResumableUploadOptions) { |
| 226 var helper = new ResumableMediaUploader( |
| 227 _httpClient, uploadMedia, body, uri, method, uploadOptions); |
| 228 return helper.upload(); |
| 229 } |
| 230 |
| 231 if (uploadMedia.length == null) { |
| 232 throw new ArgumentError( |
| 233 'For non-resumable uploads you need to specify the length of the ' |
| 234 'media to upload.'); |
| 235 } |
| 236 |
| 237 if (body == null) { |
| 238 return simpleUpload(); |
| 239 } else { |
| 240 var uploader = new MultipartMediaUploader( |
| 241 _httpClient, uploadMedia, body, uri, method); |
| 242 return uploader.upload(); |
| 243 } |
| 244 } |
| 245 return simpleRequest(); |
| 246 } |
| 247 } |
| 248 |
| 249 |
| 250 /** |
| 251 * Does media uploads using the multipart upload protocol. |
| 252 */ |
| 253 class MultipartMediaUploader { |
| 254 static final _boundary = '314159265358979323846'; |
| 255 static final _base64Encoder = new Base64Encoder(); |
| 256 |
| 257 final http.Client _httpClient; |
| 258 final common_external.Media _uploadMedia; |
| 259 final Uri _uri; |
| 260 final String _body; |
| 261 final String _method; |
| 262 |
| 263 MultipartMediaUploader( |
| 264 this._httpClient, this._uploadMedia, this._body, this._uri, this._method); |
| 265 |
| 266 Future<http.StreamedResponse> upload() { |
| 267 var base64MediaStream = |
| 268 _uploadMedia.stream.transform(_base64Encoder).transform(ASCII.encoder); |
| 269 var base64MediaStreamLength = |
| 270 Base64Encoder.lengthOfBase64Stream(_uploadMedia.length); |
| 271 |
| 272 // NOTE: We assume that [_body] is encoded JSON without any \r or \n in it. |
| 273 // This guarantees us that [_body] cannot contain a valid multipart |
| 274 // boundary. |
| 275 var bodyHead = |
| 276 '--$_boundary\r\n' |
| 277 "Content-Type: $CONTENT_TYPE_JSON_UTF8\r\n\r\n" |
| 278 + _body + |
| 279 '\r\n--$_boundary\r\n' |
| 280 "Content-Type: ${_uploadMedia.contentType}\r\n" |
| 281 "Content-Transfer-Encoding: base64\r\n\r\n"; |
| 282 var bodyTail = '\r\n--$_boundary--'; |
| 283 |
| 284 var totalLength = |
| 285 bodyHead.length + base64MediaStreamLength + bodyTail.length; |
| 286 |
| 287 var bodyController = new StreamController<List<int>>(); |
| 288 bodyController.add(UTF8.encode(bodyHead)); |
| 289 bodyController.addStream(base64MediaStream).then((_) { |
| 290 bodyController.add(UTF8.encode(bodyTail)); |
| 291 }).catchError((error, stack) { |
| 292 bodyController.addError(error, stack); |
| 293 }).then((_) { |
| 294 bodyController.close(); |
| 295 }); |
| 296 |
| 297 var headers = { |
| 298 'user-agent' : USER_AGENT_STRING, |
| 299 'content-type' : "multipart/related; boundary=\"$_boundary\"", |
| 300 'content-length' : '$totalLength' |
| 301 }; |
| 302 var bodyStream = bodyController.stream; |
| 303 var request = new RequestImpl(_method, _uri, bodyStream); |
| 304 request.headers.addAll(headers); |
| 305 return _httpClient.send(request); |
| 306 } |
| 307 } |
| 308 |
| 309 |
| 310 /** |
| 311 * Base64 encodes a stream of bytes. |
| 312 */ |
| 313 class Base64Encoder implements StreamTransformer<List<int>, String> { |
| 314 static int lengthOfBase64Stream(int lengthOfByteStream) { |
| 315 return ((lengthOfByteStream + 2) ~/ 3) * 4; |
| 316 } |
| 317 |
| 318 Stream<String> bind(Stream<List<int>> stream) { |
| 319 StreamController<String> controller; |
| 320 |
| 321 // Holds between 0 and 3 bytes and is used as a buffer. |
| 322 List<int> remainingBytes = []; |
| 323 |
| 324 void onData(List<int> bytes) { |
| 325 if ((remainingBytes.length + bytes.length) < 3) { |
| 326 remainingBytes.addAll(bytes); |
| 327 return; |
| 328 } |
| 329 int start; |
| 330 if (remainingBytes.length == 0) { |
| 331 start = 0; |
| 332 } else if (remainingBytes.length == 1) { |
| 333 remainingBytes.add(bytes[0]); |
| 334 remainingBytes.add(bytes[1]); |
| 335 start = 2; |
| 336 } else if (remainingBytes.length == 2) { |
| 337 remainingBytes.add(bytes[0]); |
| 338 start = 1; |
| 339 } |
| 340 |
| 341 // Convert & Send bytes from buffer (if necessary). |
| 342 if (remainingBytes.length > 0) { |
| 343 controller.add(crypto.CryptoUtils.bytesToBase64(remainingBytes)); |
| 344 remainingBytes.clear(); |
| 345 } |
| 346 |
| 347 int chunksOf3 = (bytes.length - start) ~/ 3; |
| 348 int end = start + 3 * chunksOf3; |
| 349 int remaining = bytes.length - end; |
| 350 |
| 351 // Convert & Send main bytes. |
| 352 if (start == 0 && end == bytes.length) { |
| 353 // Fast path if [bytes] are devisible by 3. |
| 354 controller.add(crypto.CryptoUtils.bytesToBase64(bytes)); |
| 355 } else { |
| 356 controller.add( |
| 357 crypto.CryptoUtils.bytesToBase64(bytes.sublist(start, end))); |
| 358 |
| 359 // Buffer remaining bytes if necessary. |
| 360 if (end < bytes.length) { |
| 361 remainingBytes.addAll(bytes.sublist(end)); |
| 362 } |
| 363 } |
| 364 } |
| 365 |
| 366 void onError(error, stack) { |
| 367 controller.addError(error, stack); |
| 368 } |
| 369 |
| 370 void onDone() { |
| 371 if (remainingBytes.length > 0) { |
| 372 controller.add(crypto.CryptoUtils.bytesToBase64(remainingBytes)); |
| 373 remainingBytes.clear(); |
| 374 } |
| 375 controller.close(); |
| 376 } |
| 377 |
| 378 var subscription; |
| 379 controller = new StreamController<String>( |
| 380 onListen: () { |
| 381 subscription = stream.listen( |
| 382 onData, onError: onError, onDone: onDone); |
| 383 }, |
| 384 onPause: () { |
| 385 subscription.pause(); |
| 386 }, |
| 387 onResume: () { |
| 388 subscription.resume(); |
| 389 }, |
| 390 onCancel: () { |
| 391 subscription.cancel(); |
| 392 }); |
| 393 return controller.stream; |
| 394 } |
| 395 } |
| 396 |
| 397 |
| 398 // TODO: Buffer less if we know the content length in advance. |
| 399 /** |
| 400 * Does media uploads using the resumable upload protocol. |
| 401 */ |
| 402 class ResumableMediaUploader { |
| 403 final http.Client _httpClient; |
| 404 final common_external.Media _uploadMedia; |
| 405 final Uri _uri; |
| 406 final String _body; |
| 407 final String _method; |
| 408 final common_external.ResumableUploadOptions _options; |
| 409 |
| 410 ResumableMediaUploader( |
| 411 this._httpClient, this._uploadMedia, this._body, this._uri, this._method, |
| 412 this._options); |
| 413 |
| 414 /** |
| 415 * Returns the final [http.StreamedResponse] if the upload succeded and |
| 416 * completes with an error otherwise. |
| 417 * |
| 418 * The returned response stream has not been listened to. |
| 419 */ |
| 420 Future<http.StreamedResponse> upload() { |
| 421 return _startSession().then((Uri uploadUri) { |
| 422 StreamSubscription subscription; |
| 423 |
| 424 var completer = new Completer<http.StreamedResponse>(); |
| 425 bool completed = false; |
| 426 |
| 427 var chunkStack = new ChunkStack(_options.chunkSize); |
| 428 subscription = _uploadMedia.stream.listen((List<int> bytes) { |
| 429 chunkStack.addBytes(bytes); |
| 430 |
| 431 // Upload all but the last chunk. |
| 432 // The final send will be done in the [onDone] handler. |
| 433 if (chunkStack.length > 1) { |
| 434 // Pause the input stream. |
| 435 subscription.pause(); |
| 436 |
| 437 // Upload all chunks except the last one. |
| 438 var fullChunks = chunkStack.removeSublist(0, chunkStack.length - 1); |
| 439 Future.forEach(fullChunks, |
| 440 (c) => _uploadChunkDrained(uploadUri, c)).then((_) { |
| 441 // All chunks uploaded, we can continue consuming data. |
| 442 subscription.resume(); |
| 443 }).catchError((error, stack) { |
| 444 subscription.cancel(); |
| 445 completed = true; |
| 446 completer.completeError(error, stack); |
| 447 }); |
| 448 } |
| 449 }, onError: (error, stack) { |
| 450 subscription.cancel(); |
| 451 if (!completed) { |
| 452 completed = true; |
| 453 completer.completeError(error, stack); |
| 454 } |
| 455 }, onDone: () { |
| 456 if (!completed) { |
| 457 chunkStack.finalize(); |
| 458 |
| 459 var lastChunk; |
| 460 if (chunkStack.totalByteLength > 0) { |
| 461 assert(chunkStack.length == 1); |
| 462 lastChunk = chunkStack.removeSublist(0, chunkStack.length).first; |
| 463 } else { |
| 464 lastChunk = new ResumableChunk([], 0, 0); |
| 465 } |
| 466 var end = lastChunk.endOfChunk; |
| 467 |
| 468 // Validate that we have the correct number of bytes if length was |
| 469 // specified. |
| 470 if (_uploadMedia.length != null) { |
| 471 if (end < _uploadMedia.length) { |
| 472 completer.completeError(new common_external.ApiRequestError( |
| 473 'Received less bytes than indicated by [Media.length].')); |
| 474 return; |
| 475 } else if (end > _uploadMedia.length) { |
| 476 completer.completeError( |
| 477 'Received more bytes than indicated by [Media.length].'); |
| 478 return; |
| 479 } |
| 480 } |
| 481 |
| 482 // Upload last chunk and *do not drain the response* but complete |
| 483 // with it. |
| 484 _uploadChunkResumable(uploadUri, lastChunk, lastChunk: true) |
| 485 .then((response) { |
| 486 completer.complete(response); |
| 487 }).catchError((error, stack) { |
| 488 completer.completeError(error, stack); |
| 489 }); |
| 490 } |
| 491 }); |
| 492 |
| 493 return completer.future; |
| 494 }); |
| 495 } |
| 496 |
| 497 /** |
| 498 * Starts a resumable upload. |
| 499 * |
| 500 * Returns the [Uri] which should be used for uploading all content. |
| 501 */ |
| 502 Future<Uri> _startSession() { |
| 503 var length = 0; |
| 504 var bytes; |
| 505 if (_body != null) { |
| 506 bytes = UTF8.encode(_body); |
| 507 length = bytes.length; |
| 508 } |
| 509 var bodyStream = _bytes2Stream(bytes); |
| 510 |
| 511 var request = new RequestImpl(_method, _uri, bodyStream); |
| 512 request.headers.addAll({ |
| 513 'user-agent' : USER_AGENT_STRING, |
| 514 'content-type' : CONTENT_TYPE_JSON_UTF8, |
| 515 'content-length' : '$length', |
| 516 'x-upload-content-type' : _uploadMedia.contentType, |
| 517 'x-upload-content-length' : '${_uploadMedia.length}', |
| 518 }); |
| 519 |
| 520 return _httpClient.send(request).then((http.StreamedResponse response) { |
| 521 return response.stream.drain().then((_) { |
| 522 var uploadUri = response.headers['location']; |
| 523 if (response.statusCode != 200 || uploadUri == null) { |
| 524 throw new common_external.ApiRequestError( |
| 525 'Invalid response for resumable upload attempt ' |
| 526 '(status was: ${response.statusCode})'); |
| 527 } |
| 528 return Uri.parse(uploadUri); |
| 529 }); |
| 530 }); |
| 531 } |
| 532 |
| 533 /** |
| 534 * Uploads [chunk], retries upon server errors. The response stream will be |
| 535 * drained. |
| 536 */ |
| 537 Future _uploadChunkDrained(Uri uri, ResumableChunk chunk) { |
| 538 return _uploadChunkResumable(uri, chunk).then((response) { |
| 539 return response.stream.drain(); |
| 540 }); |
| 541 } |
| 542 |
| 543 /** |
| 544 * Does repeated attempts to upload [chunk]. |
| 545 */ |
| 546 Future _uploadChunkResumable(Uri uri, |
| 547 ResumableChunk chunk, |
| 548 {bool lastChunk: false}) { |
| 549 tryUpload(int attemptsLeft) { |
| 550 return _uploadChunk(uri, chunk, lastChunk: lastChunk) |
| 551 .then((http.StreamedResponse response) { |
| 552 var status = response.statusCode; |
| 553 if (attemptsLeft > 0 && |
| 554 (status == 500 || (502 <= status && status < 504))) { |
| 555 return response.stream.drain().then((_) { |
| 556 // Delay the next attempt. Default backoff function is exponential. |
| 557 int failedAttemts = _options.numberOfAttempts - attemptsLeft; |
| 558 var duration = _options.backoffFunction(failedAttemts); |
| 559 if (duration == null) { |
| 560 throw new common_external.DetailedApiRequestError( |
| 561 status, |
| 562 'Resumable upload: Uploading a chunk resulted in status ' |
| 563 '$status. Maximum number of retries reached.'); |
| 564 } |
| 565 |
| 566 return new Future.delayed(duration).then((_) { |
| 567 return tryUpload(attemptsLeft - 1); |
| 568 }); |
| 569 }); |
| 570 } else if (!lastChunk && status != 308) { |
| 571 return response.stream.drain().then((_) { |
| 572 throw new common_external.DetailedApiRequestError( |
| 573 status, |
| 574 'Resumable upload: Uploading a chunk resulted in status ' |
| 575 '$status instead of 308.'); |
| 576 }); |
| 577 } else if (lastChunk && status != 201 && status != 200) { |
| 578 return response.stream.drain().then((_) { |
| 579 throw new common_external.DetailedApiRequestError( |
| 580 status, |
| 581 'Resumable upload: Uploading a chunk resulted in status ' |
| 582 '$status instead of 200 or 201.'); |
| 583 }); |
| 584 } else { |
| 585 return response; |
| 586 } |
| 587 }); |
| 588 } |
| 589 |
| 590 return tryUpload(_options.numberOfAttempts - 1); |
| 591 } |
| 592 |
| 593 /** |
| 594 * Uploads [length] bytes in [byteArrays] and ensures the upload was |
| 595 * successful. |
| 596 * |
| 597 * Content-Range: [start ... (start + length)[ |
| 598 * |
| 599 * Returns the returned [http.StreamedResponse] or completes with an error if |
| 600 * the upload did not succeed. The response stream will not be listened to. |
| 601 */ |
| 602 Future _uploadChunk(Uri uri, ResumableChunk chunk, {bool lastChunk: false}) { |
| 603 // If [uploadMedia.length] is null, we do not know the length. |
| 604 var mediaTotalLength = _uploadMedia.length; |
| 605 if (mediaTotalLength == null || lastChunk) { |
| 606 if (lastChunk) { |
| 607 mediaTotalLength = '${chunk.endOfChunk}'; |
| 608 } else { |
| 609 mediaTotalLength = '*'; |
| 610 } |
| 611 } |
| 612 |
| 613 var headers = { |
| 614 'user-agent' : USER_AGENT_STRING, |
| 615 'content-type' : _uploadMedia.contentType, |
| 616 'content-length' : '${chunk.length}', |
| 617 'content-range' : |
| 618 'bytes ${chunk.offset}-${chunk.endOfChunk - 1}/$mediaTotalLength', |
| 619 }; |
| 620 |
| 621 var stream = _listOfBytes2Stream(chunk.byteArrays); |
| 622 var request = new RequestImpl('PUT', uri, stream); |
| 623 request.headers.addAll(headers); |
| 624 return _httpClient.send(request); |
| 625 } |
| 626 |
| 627 Stream<List<int>> _bytes2Stream(List<int> bytes) { |
| 628 var bodyController = new StreamController<List<int>>(); |
| 629 if (bytes != null) { |
| 630 bodyController.add(bytes); |
| 631 } |
| 632 bodyController.close(); |
| 633 return bodyController.stream; |
| 634 } |
| 635 |
| 636 Stream<List<int>> _listOfBytes2Stream(List<List<int>> listOfBytes) { |
| 637 var controller = new StreamController(); |
| 638 for (var array in listOfBytes) { |
| 639 controller.add(array); |
| 640 } |
| 641 controller.close(); |
| 642 return controller.stream; |
| 643 } |
| 644 } |
| 645 |
| 646 |
| 647 /** |
| 648 * Represents a stack of [ResumableChunk]s. |
| 649 */ |
| 650 class ChunkStack { |
| 651 final int _chunkSize; |
| 652 final List<ResumableChunk> _chunkStack = []; |
| 653 |
| 654 // Currently accumulated data. |
| 655 List<List<int>> _byteArrays = []; |
| 656 int _length = 0; |
| 657 int _totalLength = 0; |
| 658 int _offset = 0; |
| 659 |
| 660 bool _finalized = false; |
| 661 |
| 662 ChunkStack(this._chunkSize); |
| 663 |
| 664 int get length => _chunkStack.length; |
| 665 |
| 666 int get totalByteLength => _offset; |
| 667 |
| 668 /** |
| 669 * Returns the chunks [from] ... [to] and deletes it from the stack. |
| 670 */ |
| 671 List<ResumableChunk> removeSublist(int from, int to) { |
| 672 var sublist = _chunkStack.sublist(from, to); |
| 673 _chunkStack.removeRange(from, to); |
| 674 return sublist; |
| 675 } |
| 676 |
| 677 /** |
| 678 * Adds [bytes] to the buffer. If the buffer is larger than the given chunk |
| 679 * size a new [ResumableChunk] will be created. |
| 680 */ |
| 681 void addBytes(List<int> bytes) { |
| 682 if (_finalized) { |
| 683 throw new StateError('ChunkStack has already been finalized.'); |
| 684 } |
| 685 |
| 686 var remaining = _chunkSize - _length; |
| 687 |
| 688 if (bytes.length >= remaining) { |
| 689 var left = bytes.sublist(0, remaining); |
| 690 var right = bytes.sublist(remaining); |
| 691 |
| 692 _byteArrays.add(left); |
| 693 _length += left.length; |
| 694 |
| 695 _chunkStack.add(new ResumableChunk(_byteArrays, _offset, _length)); |
| 696 |
| 697 _byteArrays = []; |
| 698 _offset += _length; |
| 699 _length = 0; |
| 700 |
| 701 addBytes(right); |
| 702 } else if (bytes.length > 0) { |
| 703 _byteArrays.add(bytes); |
| 704 _length += bytes.length; |
| 705 } |
| 706 } |
| 707 |
| 708 /** |
| 709 * Finalizes this [ChunkStack] and creates the last chunk (may have less bytes |
| 710 * than the chunk size, but not zero). |
| 711 */ |
| 712 void finalize() { |
| 713 if (_finalized) { |
| 714 throw new StateError('ChunkStack has already been finalized.'); |
| 715 } |
| 716 _finalized = true; |
| 717 |
| 718 if (_length > 0) { |
| 719 _chunkStack.add(new ResumableChunk(_byteArrays, _offset, _length)); |
| 720 _offset += _length; |
| 721 } |
| 722 } |
| 723 } |
| 724 |
| 725 |
| 726 /** |
| 727 * Represents a chunk of data that will be transferred in one http request. |
| 728 */ |
| 729 class ResumableChunk { |
| 730 final List<List<int>> byteArrays; |
| 731 final int offset; |
| 732 final int length; |
| 733 |
| 734 /** |
| 735 * Index of the next byte after this chunk. |
| 736 */ |
| 737 int get endOfChunk => offset + length; |
| 738 |
| 739 ResumableChunk(this.byteArrays, this.offset, this.length); |
| 740 } |
| 741 |
| 742 class RequestImpl extends http.BaseRequest { |
| 743 final Stream<List<int>> _stream; |
| 744 |
| 745 RequestImpl(String method, Uri url, [Stream<List<int>> stream]) |
| 746 : _stream = stream == null ? new Stream.fromIterable([]) : stream, |
| 747 super(method, url); |
| 748 |
| 749 http.ByteStream finalize() { |
| 750 super.finalize(); |
| 751 return new http.ByteStream(_stream); |
| 752 } |
| 753 } |
| 754 |
| 755 |
| 756 class Escaper { |
| 757 // Character class definitions from RFC 6570 |
| 758 // (see http://tools.ietf.org/html/rfc6570) |
| 759 // ALPHA = %x41-5A / %x61-7A ; A-Z / a-z |
| 760 // DIGIT = %x30-39 ; 0 |
| 761 // HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F" |
| 762 // pct-encoded = "%" HEXDIG HEXDIG |
| 763 // unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" |
| 764 // reserved = gen-delims / sub-delims |
| 765 // gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" |
| 766 // sub-delims = "!" / "$" / "&" / "'" / "(" / ")" |
| 767 // / "*" / "+" / "," / ";" / "=" |
| 768 |
| 769 // NOTE: Uri.encodeQueryComponent() does the following: |
| 770 // ... |
| 771 // Then the resulting bytes are "percent-encoded". This transforms spaces |
| 772 // (U+0020) to a plus sign ('+') and all bytes that are not the ASCII decimal |
| 773 // digits, letters or one of '-._~' are written as a percent sign '%' |
| 774 // followed by the two-digit hexadecimal representation of the byte. |
| 775 // ... |
| 776 |
| 777 // NOTE: Uri.encodeFull() does the following: |
| 778 // ... |
| 779 // All characters except uppercase and lowercase letters, digits and the |
| 780 // characters !#$&'()*+,-./:;=?@_~ are percent-encoded. |
| 781 // ... |
| 782 |
| 783 static String ecapeVariableReserved(String name) { |
| 784 // ... perform variable expansion, as defined in Section 3.2.1, with the |
| 785 // allowed characters being those in the set |
| 786 // (unreserved / reserved / pct-encoded) |
| 787 |
| 788 // NOTE: The chracters [ and ] need (according to URI Template spec) not be |
| 789 // percent encoded. The dart implementation does percent-encode [ and ]. |
| 790 // This gives us in effect a conservative encoding, since the server side |
| 791 // must interpret percent-encoded parts anyway due to arbitrary unicode. |
| 792 |
| 793 // NOTE: This is broken in the discovery protocol. It allows ? and & to be |
| 794 // expanded via URI Templates which may generate completely bogus URIs. |
| 795 // TODO/FIXME: Should we change this to _encodeUnreserved() as well |
| 796 // (disadvantage, slashes get encoded at this point)? |
| 797 return Uri.encodeFull(name); |
| 798 } |
| 799 |
| 800 static String ecapePathComponent(String name) { |
| 801 // For each defined variable in the variable-list, append "/" to the |
| 802 // result string and then perform variable expansion, as defined in |
| 803 // Section 3.2.1, with the allowed characters being those in the |
| 804 // *unreserved set*. |
| 805 return _encodeUnreserved(name); |
| 806 } |
| 807 |
| 808 static String ecapeVariable(String name) { |
| 809 // ... perform variable expansion, as defined in Section 3.2.1, with the |
| 810 // allowed characters being those in the *unreserved set*. |
| 811 return _encodeUnreserved(name); |
| 812 } |
| 813 |
| 814 static String escapeQueryComponent(String name) { |
| 815 // This method will not be used by UriTemplate, but rather for encoding |
| 816 // normal query name/value pairs. |
| 817 |
| 818 // NOTE: For safety reasons we use '%20' instead of '+' here as well. |
| 819 // TODO/FIXME: Should we do this? |
| 820 return _encodeUnreserved(name); |
| 821 } |
| 822 |
| 823 static String _encodeUnreserved(String name) { |
| 824 // The only difference between dart's [Uri.encodeQueryComponent] and the |
| 825 // encoding defined by RFC 6570 for the above-defined unreserved character |
| 826 // set is the encoding of space. |
| 827 // Dart's Uri class will convert spaces to '+' which we replace by '%20'. |
| 828 return Uri.encodeQueryComponent(name).replaceAll('+', '%20'); |
| 829 } |
| 830 } |
| 831 |
| 832 |
| 833 Future<http.StreamedResponse> _validateResponse( |
| 834 http.StreamedResponse response) { |
| 835 var statusCode = response.statusCode; |
| 836 |
| 837 // TODO: We assume that status codes between [200..400[ are OK. |
| 838 // Can we assume this? |
| 839 if (statusCode < 200 || statusCode >= 400) { |
| 840 throwGeneralError() { |
| 841 throw new common_external.ApiRequestError( |
| 842 'No error details. Http status was: ${response.statusCode}.'); |
| 843 } |
| 844 |
| 845 // Some error happened, try to decode the response and fetch the error. |
| 846 Stream<String> stringStream = _decodeStreamAsText(response); |
| 847 if (stringStream != null) { |
| 848 return stringStream.transform(JSON.decoder).first.then((json) { |
| 849 if (json is Map && json['error'] is Map) { |
| 850 var error = json['error']; |
| 851 var code = error['code']; |
| 852 var message = error['message']; |
| 853 throw new common_external.DetailedApiRequestError(code, message); |
| 854 } else { |
| 855 throwGeneralError(); |
| 856 } |
| 857 }); |
| 858 } else { |
| 859 throwGeneralError(); |
| 860 } |
| 861 } |
| 862 |
| 863 return new Future.value(response); |
| 864 } |
| 865 |
| 866 |
| 867 Stream<String> _decodeStreamAsText(http.StreamedResponse response) { |
| 868 // TODO: Correctly handle the response content-types, using correct |
| 869 // decoder. |
| 870 // Currently we assume that the api endpoint is responding with json |
| 871 // encoded in UTF8. |
| 872 String contentType = response.headers['content-type']; |
| 873 if (contentType != null && |
| 874 contentType.toLowerCase().startsWith('application/json')) { |
| 875 return response.stream.transform(new Utf8Decoder(allowMalformed: true)); |
| 876 } else { |
| 877 return null; |
| 878 } |
| 879 } |
| 880 |
| 881 Map mapMap(Map source, [Object convert(Object source) = null]) { |
| 882 assert(source != null); |
| 883 var result = new collection.LinkedHashMap(); |
| 884 source.forEach((String key, value) { |
| 885 assert(key != null); |
| 886 if(convert == null) { |
| 887 result[key] = value; |
| 888 } else { |
| 889 result[key] = convert(value); |
| 890 } |
| 891 }); |
| 892 return result; |
| 893 } |
| 894 |
OLD | NEW |