OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 part of dart.io; | |
6 | |
7 class _HttpBodyHandlerTransformer | |
8 extends StreamEventTransformer<HttpRequest, HttpRequestBody> { | |
9 final Encoding _defaultEncoding; | |
10 _HttpBodyHandlerTransformer(this._defaultEncoding); | |
11 | |
12 void handleData(HttpRequest request, EventSink<HttpRequestBody> sink) { | |
13 _HttpBodyHandler.processRequest(request, _defaultEncoding) | |
14 .then(sink.add, onError: sink.addError); | |
15 } | |
16 } | |
17 | |
18 class _HttpBodyHandler { | |
19 static Future<HttpRequestBody> processRequest( | |
20 HttpRequest request, | |
21 Encoding defaultEncoding) { | |
22 return process(request, request.headers, defaultEncoding) | |
23 .then((body) => new _HttpRequestBody(request, body), | |
24 onError: (error) { | |
25 // Try to send BAD_REQUEST response. | |
26 request.response.statusCode = HttpStatus.BAD_REQUEST; | |
27 request.response.close(); | |
28 request.response.done.catchError((_) {}); | |
29 throw error; | |
30 }); | |
31 } | |
32 | |
33 static Future<HttpClientResponseBody> processResponse( | |
34 HttpClientResponse response, | |
35 Encoding defaultEncoding) { | |
36 return process(response, response.headers, defaultEncoding) | |
37 .then((body) => new _HttpClientResponseBody(response, body)); | |
38 } | |
39 | |
40 static Future<HttpBody> process(Stream<List<int>> stream, | |
41 HttpHeaders headers, | |
42 Encoding defaultEncoding) { | |
43 ContentType contentType = headers.contentType; | |
44 | |
45 Future<HttpBody> asBinary() { | |
46 return stream | |
47 .fold(new BytesBuilder(), (builder, data) => builder..add(data)) | |
48 .then((builder) => new _HttpBody(contentType, | |
49 "binary", | |
50 builder.takeBytes())); | |
51 } | |
52 | |
53 Future<HttpBody> asText(Encoding defaultEncoding) { | |
54 var encoding; | |
55 var charset = contentType.charset; | |
56 if (charset != null) encoding = Encoding.fromName(charset); | |
57 if (encoding == null) encoding = defaultEncoding; | |
58 return stream | |
59 .transform(new StringDecoder(encoding)) | |
60 .fold(new StringBuffer(), (buffer, data) => buffer..write(data)) | |
61 .then((buffer) => new _HttpBody(contentType, | |
62 "text", | |
63 buffer.toString())); | |
64 } | |
65 | |
66 Future<HttpBody> asFormData() { | |
67 return stream | |
68 .transform(new MimeMultipartTransformer( | |
69 contentType.parameters['boundary'])) | |
70 .map((HttpMultipartFormData.parse)) | |
71 .map((multipart) { | |
72 var future; | |
73 if (multipart.isText) { | |
74 future = multipart | |
75 .fold(new StringBuffer(), (b, s) => b..write(s)) | |
76 .then((b) => b.toString()); | |
77 } else { | |
78 future = multipart | |
79 .fold(new BytesBuilder(), (b, d) => b..add(d)) | |
80 .then((b) => b.takeBytes()); | |
81 } | |
82 return future.then((data) { | |
83 var filename = | |
84 multipart.contentDisposition.parameters['filename']; | |
85 if (filename != null) { | |
86 data = new _HttpBodyFileUpload(multipart.contentType, | |
87 filename, | |
88 data); | |
89 } | |
90 return [multipart.contentDisposition.parameters['name'], data]; | |
91 }); | |
92 }) | |
93 .fold([], (l, f) => l..add(f)) | |
94 .then(Future.wait) | |
95 .then((parts) { | |
96 Map<String, dynamic> map = new Map<String, dynamic>(); | |
97 for (var part in parts) { | |
98 map[part[0]] = part[1]; // Override existing entries. | |
99 } | |
100 return new _HttpBody(contentType, 'form', map); | |
101 }); | |
102 } | |
103 | |
104 if (contentType == null) { | |
105 return asBinary(); | |
106 } | |
107 | |
108 switch (contentType.primaryType) { | |
109 case "text": | |
110 return asText(Encoding.ASCII); | |
111 | |
112 case "application": | |
113 switch (contentType.subType) { | |
114 case "json": | |
115 return asText(Encoding.UTF_8) | |
116 .then((body) => new _HttpBody(contentType, | |
117 "json", | |
118 JSON.parse(body.body))); | |
119 | |
120 case "x-www-form-urlencoded": | |
121 return asText(Encoding.ASCII) | |
122 .then((body) { | |
123 var map = Uri.splitQueryString(body.body, | |
124 decode: (s) => _decodeString(s, defaultEncoding)); | |
125 var result = {}; | |
126 for (var key in map.keys) { | |
127 result[key] = map[key]; | |
128 } | |
129 return new _HttpBody(contentType, "form", result); | |
130 }); | |
131 | |
132 default: | |
133 break; | |
134 } | |
135 break; | |
136 | |
137 case "multipart": | |
138 switch (contentType.subType) { | |
139 case "form-data": | |
140 return asFormData(); | |
141 | |
142 default: | |
143 break; | |
144 } | |
145 break; | |
146 | |
147 default: | |
148 break; | |
149 } | |
150 | |
151 return asBinary(); | |
152 } | |
153 } | |
154 | |
155 class _HttpBodyFileUpload implements HttpBodyFileUpload { | |
156 final ContentType contentType; | |
157 final String filename; | |
158 final dynamic content; | |
159 _HttpBodyFileUpload(this.contentType, this.filename, this.content); | |
160 } | |
161 | |
162 class _HttpBody implements HttpBody { | |
163 final ContentType contentType; | |
164 final String type; | |
165 final dynamic body; | |
166 | |
167 _HttpBody(ContentType this.contentType, | |
168 String this.type, | |
169 dynamic this.body); | |
170 } | |
171 | |
172 class _HttpRequestBody extends _HttpBody implements HttpRequestBody { | |
173 final String method; | |
174 final Uri uri; | |
175 final HttpHeaders headers; | |
176 final HttpResponse response; | |
177 | |
178 _HttpRequestBody(HttpRequest request, HttpBody body) | |
179 : super(body.contentType, body.type, body.body), | |
180 method = request.method, | |
181 uri = request.uri, | |
182 headers = request.headers, | |
183 response = request.response; | |
184 } | |
185 | |
186 class _HttpClientResponseBody | |
187 extends _HttpBody implements HttpClientResponseBody { | |
188 final HttpClientResponse response; | |
189 | |
190 _HttpClientResponseBody(HttpClientResponse response, HttpBody body) | |
191 : super(body.contentType, body.type, body.body), | |
192 this.response = response; | |
193 | |
194 int get statusCode => response.statusCode; | |
195 | |
196 String get reasonPhrase => response.reasonPhrase; | |
197 | |
198 HttpHeaders get headers => response.headers; | |
199 } | |
OLD | NEW |