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 /// Helpers for dealing with HTTP. | 5 /// Helpers for dealing with HTTP. |
6 library pub.http; | 6 library pub.http; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:json' as json; | 10 import 'dart:json' as json; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 // to pass along 400 responses from the token endpoint. | 54 // to pass along 400 responses from the token endpoint. |
55 var tokenRequest = urisEqual( | 55 var tokenRequest = urisEqual( |
56 streamedResponse.request.url, oauth2.tokenEndpoint); | 56 streamedResponse.request.url, oauth2.tokenEndpoint); |
57 if (status < 400 || status == 401 || (status == 400 && tokenRequest)) { | 57 if (status < 400 || status == 401 || (status == 400 && tokenRequest)) { |
58 return streamedResponse; | 58 return streamedResponse; |
59 } | 59 } |
60 | 60 |
61 return http.Response.fromStream(streamedResponse).then((response) { | 61 return http.Response.fromStream(streamedResponse).then((response) { |
62 throw new PubHttpException(response); | 62 throw new PubHttpException(response); |
63 }); | 63 }); |
64 }).catchError((asyncError) { | 64 }).catchError((error) { |
65 if (asyncError.error is SocketIOException && | 65 if (error is SocketIOException && |
66 asyncError.error.osError != null) { | 66 error.osError != null) { |
67 if (asyncError.error.osError.errorCode == 8 || | 67 if (error.osError.errorCode == 8 || |
68 asyncError.error.osError.errorCode == -2 || | 68 error.osError.errorCode == -2 || |
69 asyncError.error.osError.errorCode == -5 || | 69 error.osError.errorCode == -5 || |
70 asyncError.error.osError.errorCode == 11001 || | 70 error.osError.errorCode == 11001 || |
71 asyncError.error.osError.errorCode == 11004) { | 71 error.osError.errorCode == 11004) { |
72 throw 'Could not resolve URL "${request.url.origin}".'; | 72 throw 'Could not resolve URL "${request.url.origin}".'; |
73 } else if (asyncError.error.osError.errorCode == -12276) { | 73 } else if (error.osError.errorCode == -12276) { |
74 throw 'Unable to validate SSL certificate for ' | 74 throw 'Unable to validate SSL certificate for ' |
75 '"${request.url.origin}".'; | 75 '"${request.url.origin}".'; |
76 } | 76 } |
77 } | 77 } |
78 throw asyncError; | 78 throw error; |
79 }), HTTP_TIMEOUT, 'fetching URL "${request.url}"'); | 79 }), HTTP_TIMEOUT, 'fetching URL "${request.url}"'); |
80 } | 80 } |
81 | 81 |
82 /// Logs the fact that [request] was sent, and information about it. | 82 /// Logs the fact that [request] was sent, and information about it. |
83 void _logRequest(http.BaseRequest request) { | 83 void _logRequest(http.BaseRequest request) { |
84 var requestLog = new StringBuffer(); | 84 var requestLog = new StringBuffer(); |
85 requestLog.writeln("HTTP ${request.method} ${request.url}"); | 85 requestLog.writeln("HTTP ${request.method} ${request.url}"); |
86 request.headers.forEach((name, value) => | 86 request.headers.forEach((name, value) => |
87 requestLog.writeln(_logField(name, value))); | 87 requestLog.writeln(_logField(name, value))); |
88 | 88 |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 | 193 |
194 /// Exception thrown when an HTTP operation fails. | 194 /// Exception thrown when an HTTP operation fails. |
195 class PubHttpException implements Exception { | 195 class PubHttpException implements Exception { |
196 final http.Response response; | 196 final http.Response response; |
197 | 197 |
198 const PubHttpException(this.response); | 198 const PubHttpException(this.response); |
199 | 199 |
200 String toString() => 'HTTP error ${response.statusCode}: ' | 200 String toString() => 'HTTP error ${response.statusCode}: ' |
201 '${response.reasonPhrase}'; | 201 '${response.reasonPhrase}'; |
202 } | 202 } |
OLD | NEW |