LEFT | RIGHT |
1 // Copyright (C) 2008 Google Inc. | 1 // Copyright (C) 2008 Google Inc. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 buffer.write(barr, 0, n); | 181 buffer.write(barr, 0, n); |
182 } | 182 } |
183 } finally { | 183 } finally { |
184 stream.close(); | 184 stream.close(); |
185 } | 185 } |
186 byte[] content = buffer.toByteArray(); | 186 byte[] content = buffer.toByteArray(); |
187 return new FetchedData(content, contentType, contentCharSet); | 187 return new FetchedData(content, contentType, contentCharSet); |
188 } | 188 } |
189 | 189 |
190 public void registerHandlers(BuildInfo buildInfo) { | 190 public void registerHandlers(BuildInfo buildInfo) { |
191 handlers.add(new JsHandler(buildInfo)); | 191 UriCallback retriever = new UriCallback() { |
192 handlers.add(new ImageHandler()); | |
193 handlers.add(new GadgetHandler(buildInfo, new UriCallback() { | |
194 public Reader retrieve(ExternalReference extref, String mimeType) | 192 public Reader retrieve(ExternalReference extref, String mimeType) |
195 throws UriCallbackException { | 193 throws UriCallbackException { |
196 try { | 194 try { |
197 FetchedData data = fetch(extref.getUri()); | 195 FetchedData data = fetch(extref.getUri()); |
198 if (data == null) { return null; } | 196 if (data == null) { return null; } |
199 return new InputStreamReader( | 197 return new InputStreamReader( |
200 new ByteArrayInputStream(data.content), data.charSet); | 198 new ByteArrayInputStream(data.content), data.charSet); |
201 } catch (IOException ex) { | 199 } catch (IOException ex) { |
202 throw new UriCallbackException(extref, ex); | 200 throw new UriCallbackException(extref, ex); |
203 } | 201 } |
204 } | 202 } |
205 | 203 |
206 public URI rewrite(ExternalReference extref, String mimeType) { | 204 public URI rewrite(ExternalReference extref, String mimeType) { |
207 return null; | 205 return null; |
208 } | 206 } |
209 })); | 207 }; |
| 208 handlers.add(new JsHandler(buildInfo)); |
| 209 handlers.add(new ImageHandler()); |
| 210 handlers.add(new GadgetHandler(buildInfo, retriever)); |
210 handlers.add(new InnocentHandler()); | 211 handlers.add(new InnocentHandler()); |
211 handlers.add(new HtmlHandler(buildInfo, host)); | 212 handlers.add(new HtmlHandler(buildInfo, host, retriever)); |
212 } | 213 } |
213 | 214 |
214 private Pair<String, String> applyHandler(URI uri, | 215 private Pair<String, String> applyHandler( |
215 Transform t, String contentType, String charSet, | 216 URI uri, Transform t, String contentType, String charSet, |
216 byte[] content, OutputStream response) | 217 byte[] content, OutputStream response) |
217 throws UnsupportedContentTypeException { | 218 throws UnsupportedContentTypeException { |
218 for (ContentHandler handler : handlers) { | 219 for (ContentHandler handler : handlers) { |
219 if (handler.canHandle(uri, t, contentType, typeCheck)) { | 220 if (handler.canHandle(uri, t, contentType, typeCheck)) { |
220 return | 221 return handler.apply(uri, t, contentType, charSet, content, response); |
221 handler.apply(uri, t, contentType, charSet, content, response); | |
222 } | 222 } |
223 } | 223 } |
224 throw new UnsupportedContentTypeException(); | 224 throw new UnsupportedContentTypeException(); |
225 } | 225 } |
226 | 226 |
227 public static final class FetchedData { | 227 public static final class FetchedData { |
228 final byte[] content; | 228 final byte[] content; |
229 final String contentType; | 229 final String contentType; |
230 final String charSet; | 230 final String charSet; |
231 FetchedData(byte[] content, String contentType, String charSet) { | 231 FetchedData(byte[] content, String contentType, String charSet) { |
232 this.content = content; | 232 this.content = content; |
233 this.contentType = contentType; | 233 this.contentType = contentType; |
234 this.charSet = charSet; | 234 this.charSet = charSet; |
235 } | 235 } |
236 } | 236 } |
237 | 237 |
238 // Used to protect against header splitting attacks. | 238 // Used to protect against header splitting attacks. |
239 private static boolean containsNewline(String s) { | 239 private static boolean containsNewline(String s) { |
240 return s.indexOf('\n') >= 0 || s.indexOf('\r') >= 0; | 240 return s.indexOf('\n') >= 0 || s.indexOf('\r') >= 0; |
241 } | 241 } |
242 | 242 |
243 public static enum Transform { | 243 public static enum Transform { |
244 INNOCENT, | 244 INNOCENT, |
245 VALIJA, | 245 VALIJA, |
246 CAJITA; | 246 CAJITA; |
247 } | 247 } |
248 } | 248 } |
LEFT | RIGHT |