Commits

Kirill Simonov committed 4071197

/:json: Use content type application/javascript instead of application/json
to avoid the browser opening a Save As dialog.

Also, dropped support for /:jsonex.

  • Participants
  • Parent commits a399eee

Comments (0)

Files changed (6)

File src/htsql/fmt/json.py

 
 class JSONRenderer(Renderer):
 
-    # Note: see `http://www.ietf.org/rfc/rfc4627.txt`.
-    name = 'application/json'
-    aliases = ['json', 'jsonex']
+    # The specification `http://www.ietf.org/rfc/rfc4627.txt` recommends
+    # `application/json`, but we use `application/javascript` to prevent
+    # the browser from opening "Save As" window.
+    name = 'application/javascript'
+    aliases = ['js', 'application/json', 'json']
 
     def render(self, product):
         status = self.generate_status(product)
     def generate_headers(self, product):
         filename = str(product.profile.segment.syntax)
         filename = filename.replace('\\', '\\\\').replace('"', '\\"')
-        return [('Content-Type', 'application/json'),
+        return [('Content-Type', 'application/javascript'),
                 ('Content-Disposition',
-                 'attachment; filename="(%s).json"' % filename)]
+                 'inline; filename="(%s).js"' % filename)]
 
     def generate_body(self, product):
         titles = [escape(entitle(element.binding))

File test/regress/output/mssql.yaml

         - uri: /school/:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, attachment; filename="(school).json"]
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, inline; filename="(school).js"]
           body: |
             {
               "meta": [
         - uri: /school/:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, attachment; filename="(school).json"]
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, inline; filename="(school).js"]
           body: |
             {
               "meta": [
         - uri: /school
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, attachment; filename="(school).json"]
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, inline; filename="(school).js"]
           body: |
             {
               "meta": [
             '# of Departments'} /:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, 'attachment; filename="((school:as(''List of Schools'')){name:as(Name),count(department):as(''#
-              of Departments'')}).json"']
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, 'inline; filename="((school:as(''List of Schools'')){name:as(Name),count(department):as(''#
+              of Departments'')}).js"']
           body: |
             {
               "meta": [
             /:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, 'attachment; filename="({null(),true(),false(),60,2.125,271828e-5,''HTSQL'',date(''2010-04-15'')}).json"']
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, 'inline; filename="({null(),true(),false(),60,2.125,271828e-5,''HTSQL'',date(''2010-04-15'')}).js"']
           body: |
             {
               "meta": [
         - uri: /school?false()/:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, 'attachment; filename="(school?false()).json"']
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, 'inline; filename="(school?false()).js"']
           body: |
             {
               "meta": [

File test/regress/output/mysql.yaml

         - uri: /school/:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, attachment; filename="(school).json"]
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, inline; filename="(school).js"]
           body: |
             {
               "meta": [
         - uri: /school/:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, attachment; filename="(school).json"]
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, inline; filename="(school).js"]
           body: |
             {
               "meta": [
         - uri: /school
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, attachment; filename="(school).json"]
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, inline; filename="(school).js"]
           body: |
             {
               "meta": [
             '# of Departments'} /:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, 'attachment; filename="((school:as(''List of Schools'')){name:as(Name),count(department):as(''#
-              of Departments'')}).json"']
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, 'inline; filename="((school:as(''List of Schools'')){name:as(Name),count(department):as(''#
+              of Departments'')}).js"']
           body: |
             {
               "meta": [
             /:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, 'attachment; filename="({null(),true(),false(),60,2.125,271828e-5,''HTSQL'',date(''2010-04-15'')}).json"']
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, 'inline; filename="({null(),true(),false(),60,2.125,271828e-5,''HTSQL'',date(''2010-04-15'')}).js"']
           body: |
             {
               "meta": [
         - uri: /school?false()/:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, 'attachment; filename="(school?false()).json"']
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, 'inline; filename="(school?false()).js"']
           body: |
             {
               "meta": [

File test/regress/output/oracle.yaml

         - uri: /school/:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, attachment; filename="(school).json"]
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, inline; filename="(school).js"]
           body: |
             {
               "meta": [
         - uri: /school/:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, attachment; filename="(school).json"]
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, inline; filename="(school).js"]
           body: |
             {
               "meta": [
         - uri: /school
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, attachment; filename="(school).json"]
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, inline; filename="(school).js"]
           body: |
             {
               "meta": [
             '# of Departments'} /:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, 'attachment; filename="((school:as(''List of Schools'')){name:as(Name),count(department):as(''#
-              of Departments'')}).json"']
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, 'inline; filename="((school:as(''List of Schools'')){name:as(Name),count(department):as(''#
+              of Departments'')}).js"']
           body: |
             {
               "meta": [
             /:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, 'attachment; filename="({null(),true(),false(),60,2.125,271828e-5,''HTSQL'',date(''2010-04-15'')}).json"']
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, 'inline; filename="({null(),true(),false(),60,2.125,271828e-5,''HTSQL'',date(''2010-04-15'')}).js"']
           body: |
             {
               "meta": [
         - uri: /school?false()/:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, 'attachment; filename="(school?false()).json"']
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, 'inline; filename="(school?false()).js"']
           body: |
             {
               "meta": [

File test/regress/output/pgsql.yaml

         - uri: /school/:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, attachment; filename="(school).json"]
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, inline; filename="(school).js"]
           body: |
             {
               "meta": [
         - uri: /school/:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, attachment; filename="(school).json"]
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, inline; filename="(school).js"]
           body: |
             {
               "meta": [
         - uri: /school
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, attachment; filename="(school).json"]
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, inline; filename="(school).js"]
           body: |
             {
               "meta": [
             '# of Departments'} /:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, 'attachment; filename="((school:as(''List of Schools'')){name:as(Name),count(department):as(''#
-              of Departments'')}).json"']
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, 'inline; filename="((school:as(''List of Schools'')){name:as(Name),count(department):as(''#
+              of Departments'')}).js"']
           body: |
             {
               "meta": [
             /:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, 'attachment; filename="({null(),true(),false(),60,2.125,271828e-5,''HTSQL'',date(''2010-04-15'')}).json"']
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, 'inline; filename="({null(),true(),false(),60,2.125,271828e-5,''HTSQL'',date(''2010-04-15'')}).js"']
           body: |
             {
               "meta": [
         - uri: /school?false()/:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, 'attachment; filename="(school?false()).json"']
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, 'inline; filename="(school?false()).js"']
           body: |
             {
               "meta": [

File test/regress/output/sqlite.yaml

         - uri: /school/:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, attachment; filename="(school).json"]
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, inline; filename="(school).js"]
           body: |
             {
               "meta": [
         - uri: /school/:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, attachment; filename="(school).json"]
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, inline; filename="(school).js"]
           body: |
             {
               "meta": [
         - uri: /school
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, attachment; filename="(school).json"]
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, inline; filename="(school).js"]
           body: |
             {
               "meta": [
             '# of Departments'} /:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, 'attachment; filename="((school:as(''List of Schools'')){name:as(Name),count(department):as(''#
-              of Departments'')}).json"']
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, 'inline; filename="((school:as(''List of Schools'')){name:as(Name),count(department):as(''#
+              of Departments'')}).js"']
           body: |
             {
               "meta": [
             /:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, 'attachment; filename="({null(),true(),false(),60,2.125,271828e-5,''HTSQL'',date(''2010-04-15'')}).json"']
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, 'inline; filename="({null(),true(),false(),60,2.125,271828e-5,''HTSQL'',date(''2010-04-15'')}).js"']
           body: |
             {
               "meta": [
         - uri: /school?false()/:json
           status: 200 OK
           headers:
-          - [Content-Type, application/json]
-          - [Content-Disposition, 'attachment; filename="(school?false()).json"']
+          - [Content-Type, application/javascript]
+          - [Content-Disposition, 'inline; filename="(school?false()).js"']
           body: |
             {
               "meta": [