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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 /// Logs the fact that [request] was sent, and information about it. | 90 /// Logs the fact that [request] was sent, and information about it. |
91 void _logRequest(http.BaseRequest request) { | 91 void _logRequest(http.BaseRequest request) { |
92 var requestLog = new StringBuffer(); | 92 var requestLog = new StringBuffer(); |
93 requestLog.writeln("HTTP ${request.method} ${request.url}"); | 93 requestLog.writeln("HTTP ${request.method} ${request.url}"); |
94 request.headers.forEach((name, value) => | 94 request.headers.forEach((name, value) => |
95 requestLog.writeln(_logField(name, value))); | 95 requestLog.writeln(_logField(name, value))); |
96 | 96 |
97 if (request.method == 'POST') { | 97 if (request.method == 'POST') { |
98 var contentTypeString = request.headers[HttpHeaders.CONTENT_TYPE]; | 98 var contentTypeString = request.headers[HttpHeaders.CONTENT_TYPE]; |
99 if (contentTypeString == null) contentTypeString = ''; | 99 if (contentTypeString == null) contentTypeString = ''; |
100 var contentType = new ContentType.fromString(contentTypeString); | 100 var contentType = ContentType.parse(contentTypeString); |
101 if (request is http.MultipartRequest) { | 101 if (request is http.MultipartRequest) { |
102 requestLog.writeln(); | 102 requestLog.writeln(); |
103 requestLog.writeln("Body fields:"); | 103 requestLog.writeln("Body fields:"); |
104 request.fields.forEach((name, value) => | 104 request.fields.forEach((name, value) => |
105 requestLog.writeln(_logField(name, value))); | 105 requestLog.writeln(_logField(name, value))); |
106 | 106 |
107 // TODO(nweiz): make MultipartRequest.files readable, and log them? | 107 // TODO(nweiz): make MultipartRequest.files readable, and log them? |
108 } else if (request is http.Request) { | 108 } else if (request is http.Request) { |
109 if (contentType.value == 'application/x-www-form-urlencoded') { | 109 if (contentType.value == 'application/x-www-form-urlencoded') { |
110 requestLog.writeln(); | 110 requestLog.writeln(); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 | 200 |
201 /// Exception thrown when an HTTP operation fails. | 201 /// Exception thrown when an HTTP operation fails. |
202 class PubHttpException implements Exception { | 202 class PubHttpException implements Exception { |
203 final http.Response response; | 203 final http.Response response; |
204 | 204 |
205 const PubHttpException(this.response); | 205 const PubHttpException(this.response); |
206 | 206 |
207 String toString() => 'HTTP error ${response.statusCode}: ' | 207 String toString() => 'HTTP error ${response.statusCode}: ' |
208 '${response.reasonPhrase}'; | 208 '${response.reasonPhrase}'; |
209 } | 209 } |
OLD | NEW |