Repository: ipython/ipython

Issue:
/files/ only serving attachments
https://github.com/ipython/ipython/pull/6624 changes the behavior of serving html files from '/files/'.
Instead of displaying a html page, I get the option to download it as file.

Before I got as a response:

``` Python
import requests
r = requests.get('http://localhost:8888/files/Untitled10.html')
print(r.headers)

{'Last-Modified': 'Sat, 23 Aug 2014 16:03:02 GMT', 
'Set-Cookie': 'username-localhost-8888=; expires=Thu, 31 Oct 2013 22:14:47 GMT; Path=/',
'Content-Type': 'text/html', 'X-Frame-Options': 'SAMEORIGIN', 
'Server': 'TornadoServer/4.0.2', 'Date': 'Fri, 31 Oct 2014 22:14:47 GMT', 
'Content-Length': '237903', 'Accept-Ranges': 'bytes'}
```

Now I get

``` Python
r = requests.get('http://localhost:8888/files/Untitled10.html')
print(r.headers)

{'Date': 'Fri, 31 Oct 2014 22:17:27 GMT', 
'Transfer-Encoding': 'chunked', 'X-Frame-Options': 'SAMEORIGIN', 
'Set-Cookie': 'username-localhost-8888=; expires=Thu, 31 Oct 2013 22:17:27 GMT; Path=/',
 'Server': 'TornadoServer/4.0.2', 'Content-Type': 'text/html', 
'Content-Disposition': 'attachment; filename="Untitled10.html"'}
```

Is this an intended new behavior ?



Discussion:

Resolving patch:
diff --git a/IPython/html/files/handlers.py b/IPython/html/files/handlers.py
--- a/IPython/html/files/handlers.py
+++ b/IPython/html/files/handlers.py
@@ -24,7 +24,10 @@ def get(self, path):
 
         path, name = os.path.split(path)
         model = cm.get_model(name, path)
-
+        
+        if self.get_argument("download", False):
+            self.set_header('Content-Disposition','attachment; filename="%s"' % name)
+        
         if model['type'] == 'notebook':
             self.set_header('Content-Type', 'application/json')
         else:
@@ -32,8 +35,6 @@ def get(self, path):
             if cur_mime is not None:
                 self.set_header('Content-Type', cur_mime)
         
-        self.set_header('Content-Disposition','attachment; filename="%s"' % name)
-
         if model['format'] == 'base64':
             b64_bytes = model['content'].encode('ascii')
             self.write(base64.decodestring(b64_bytes))


Test patch:
diff --git a/IPython/html/tests/test_files.py b/IPython/html/tests/test_files.py
--- a/IPython/html/tests/test_files.py
+++ b/IPython/html/tests/test_files.py
@@ -98,7 +98,23 @@ def test_contents_manager(self):
         self.assertEqual(r.status_code, 200)
         self.assertEqual(r.headers['content-type'], 'text/plain')
         self.assertEqual(r.text, 'foobar')
+    
+    def test_download(self):
+        nbdir = self.notebook_dir.name
+        base = self.base_url()
+        
+        text = 'hello'
+        with open(pjoin(nbdir, 'test.txt'), 'w') as f:
+            f.write(text)
+        
+        r = requests.get(url_path_join(base, 'files', 'test.txt'))
+        disposition = r.headers.get('Content-Disposition', '')
+        self.assertNotIn('attachment', disposition)
 
+        r = requests.get(url_path_join(base, 'files', 'test.txt') + '?download=1')
+        disposition = r.headers.get('Content-Disposition', '')
+        self.assertIn('attachment', disposition)
+        self.assertIn('filename="test.txt"', disposition)
 
     def test_old_files_redirect(self):
         """pre-2.0 'files/' prefixed links are properly redirected"""