[downloadable] fix filename in HTTP header (closes #2522325, #2522324)
Before this changeset we use the `filename` header with utf8 encoded filename
all the time.
However RFC6266 says:
The parameters "filename" and "filename*" differ only in that "filename*"
uses the encoding defined in [RFC5987], allowing the use of characters not
present in the ISO-8859-1 character set ([ISO-8859-1]).
Therefore, we alter the code to:
1. Use `filename` and `ascii` encoding whenever possible,
2. use `filename*` with `utf8` encoding otherwise (with a filename fallback for
old browser)
We also switch the `content-disposition` value to attachement if filename is
specified, this will result as a mandatory download according to RFC6266. This
mandatory download is the expected behavior.
We changes the filename encoding to RFC5987 which is simpler, supported by all
and modern browser (including IE from version 6) and does not suffer from the
continuation issue. (see ticket #2522324 for details)
# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr## This file is part of CubicWeb.## CubicWeb is free software: you can redistribute it and/or modify it under the# terms of the GNU Lesser General Public License as published by the Free# Software Foundation, either version 2.1 of the License, or (at your option)# any later version.## CubicWeb is distributed in the hope that it will be useful, but WITHOUT# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more# details.## You should have received a copy of the GNU Lesser General Public License along# with CubicWeb. If not, see <http://www.gnu.org/licenses/>."""allways executed before all others in server migrationit should only include low level schema changes"""from__future__importwith_statementfromcubicweb.server.sessionimporthooks_controlfromcubicweb.serverimportschemaserialasssapplcubicwebversion,cubicwebversion=versions_map['cubicweb']def_add_relation_definition_no_perms(subjtype,rtype,objtype):rschema=fsschema.rschema(rtype)rdef=rschema.rdefs[(subjtype,objtype)]rdef.rtype=schema.rschema(rtype)rdef.subject=schema.eschema(subjtype)rdef.object=schema.eschema(objtype)ss.execschemarql(rql,rdef,ss.rdef2rql(rdef,CSTRMAP,groupmap=None))commit(ask_confirm=False)ifapplcubicwebversion<=(3,13,0)andcubicwebversion>=(3,13,1):sql('ALTER TABLE entities ADD asource VARCHAR(64)')sql('UPDATE entities SET asource=cw_name ''FROM cw_CWSource, cw_source_relation ''WHERE entities.eid=cw_source_relation.eid_from AND cw_source_relation.eid_to=cw_CWSource.cw_eid')commit()ifschema['TZDatetime'].eidisNone:add_entity_type('TZDatetime',auto=False)ifschema['TZTime'].eidisNone:add_entity_type('TZTime',auto=False)ifapplcubicwebversion<=(3,14,0)andcubicwebversion>=(3,14,0):if'require_permission'inschemaandnot'localperms'inrepo.config.cubes():fromcubicwebimportExecutionErrortry:add_cube('localperms',update_database=False)exceptImportError:raiseExecutionError('In cubicweb 3.14, CWPermission and related stuff ''has been moved to cube localperms. Install it first.')ifapplcubicwebversion==(3,6,0)andcubicwebversion>=(3,6,0):CSTRMAP=dict(rql('Any T, X WHERE X is CWConstraintType, X name T',ask_confirm=False))_add_relation_definition_no_perms('CWAttribute','update_permission','CWGroup')_add_relation_definition_no_perms('CWAttribute','update_permission','RQLExpression')rql('SET X update_permission Y WHERE X is CWAttribute, X add_permission Y')drop_relation_definition('CWAttribute','add_permission','CWGroup')drop_relation_definition('CWAttribute','add_permission','RQLExpression')drop_relation_definition('CWAttribute','delete_permission','CWGroup')drop_relation_definition('CWAttribute','delete_permission','RQLExpression')elifapplcubicwebversion<(3,6,0)andcubicwebversion>=(3,6,0):CSTRMAP=dict(rql('Any T, X WHERE X is CWConstraintType, X name T',ask_confirm=False))session.set_cnxset()permsdict=ss.deserialize_ertype_permissions(session)withhooks_control(session,session.HOOKS_ALLOW_ALL,'integrity'):forrschemainrepo.schema.relations():rpermsdict=permsdict.get(rschema.eid,{})forrdefinrschema.rdefs.values():foractioninrdef.ACTIONS:actperms=[]forsomethinginrpermsdict.get(action=='update'and'add'oraction,()):ifisinstance(something,tuple):actperms.append(rdef.rql_expression(*something))else:# group nameactperms.append(something)rdef.set_action_permissions(action,actperms)foractionin('read','add','delete'):_add_relation_definition_no_perms('CWRelation','%s_permission'%action,'CWGroup')_add_relation_definition_no_perms('CWRelation','%s_permission'%action,'RQLExpression')foractionin('read','update'):_add_relation_definition_no_perms('CWAttribute','%s_permission'%action,'CWGroup')_add_relation_definition_no_perms('CWAttribute','%s_permission'%action,'RQLExpression')foractionin('read','add','delete'):rql('SET X %s_permission Y WHERE X is CWRelation, ''RT %s_permission Y, X relation_type RT, Y is CWGroup'%(action,action))rql('INSERT RQLExpression Y: Y exprtype YET, Y mainvars YMV, Y expression YEX, ''X %s_permission Y WHERE X is CWRelation, ''X relation_type RT, RT %s_permission Y2, Y2 exprtype YET, ''Y2 mainvars YMV, Y2 expression YEX'%(action,action))rql('SET X read_permission Y WHERE X is CWAttribute, ''RT read_permission Y, X relation_type RT, Y is CWGroup')rql('INSERT RQLExpression Y: Y exprtype YET, Y mainvars YMV, Y expression YEX, ''X read_permission Y WHERE X is CWAttribute, ''X relation_type RT, RT read_permission Y2, Y2 exprtype YET, ''Y2 mainvars YMV, Y2 expression YEX')rql('SET X update_permission Y WHERE X is CWAttribute, ''RT add_permission Y, X relation_type RT, Y is CWGroup')rql('INSERT RQLExpression Y: Y exprtype YET, Y mainvars YMV, Y expression YEX, ''X update_permission Y WHERE X is CWAttribute, ''X relation_type RT, RT add_permission Y2, Y2 exprtype YET, ''Y2 mainvars YMV, Y2 expression YEX')foractionin('read','add','delete'):drop_relation_definition('CWRType','%s_permission'%action,'CWGroup',commit=False)drop_relation_definition('CWRType','%s_permission'%action,'RQLExpression')sync_schema_props_perms('read_permission',syncperms=False)# fix read_permission cardinalityifapplcubicwebversion<(3,9,6)andcubicwebversion>=(3,9,6)andnot'CWUniqueTogetherConstraint'inschema:add_entity_type('CWUniqueTogetherConstraint')ifnot('CWUniqueTogetherConstraint','CWRType')inschema['relations'].rdefs:add_relation_definition('CWUniqueTogetherConstraint','relations','CWRType')rql('SET C relations RT WHERE C relations RDEF, RDEF relation_type RT')commit()drop_relation_definition('CWUniqueTogetherConstraint','relations','CWAttribute')drop_relation_definition('CWUniqueTogetherConstraint','relations','CWRelation')ifapplcubicwebversion<(3,4,0)andcubicwebversion>=(3,4,0):withhooks_control(session,session.HOOKS_ALLOW_ALL,'integrity'):session.set_shared_data('do-not-insert-cwuri',True)add_relation_type('cwuri')base_url=session.base_url()foreid,inrql('Any X',ask_confirm=False):type,source,extid=session.describe(eid)ifsource=='system':rql('SET X cwuri %(u)s WHERE X eid %(x)s',{'x':eid,'u':base_url+u'eid/%s'%eid})isession.commit()session.set_shared_data('do-not-insert-cwuri',False)ifapplcubicwebversion<(3,5,0)andcubicwebversion>=(3,5,0):# check that migration is not doomedrset=rql('Any X,Y WHERE X transition_of E, Y transition_of E, ''X name N, Y name N, NOT X identity Y',ask_confirm=False)ifrset:fromlogilab.common.shellutilsimportASKifnotASK.confirm('Migration will fail because of transitions with the same name. ''Continue anyway ?'):importsyssys.exit(1)# proceed with migrationadd_entity_type('Workflow')add_entity_type('BaseTransition')add_entity_type('WorkflowTransition')add_entity_type('SubWorkflowExitPoint')# drop explicit 'State allowed_transition Transition' since it should be# infered due to yams inheritance. However we've to disable the schema# sync hook first to avoid to destroy existing data...try:fromcubicweb.hooksimportsyncschemarepo.vreg.unregister(syncschema.AfterDelRelationTypeHook)try:drop_relation_definition('State','allowed_transition','Transition')finally:repo.vreg.register(syncschema.AfterDelRelationTypeHook)exceptImportError:# syncschema is in CW >= 3.6 onlyfromcubicweb.server.schemahooksimportafter_del_relation_typerepo.hm.unregister_hook(after_del_relation_type,'after_delete_relation','relation_type')try:drop_relation_definition('State','allowed_transition','Transition')finally:repo.hm.register_hook(after_del_relation_type,'after_delete_relation','relation_type')schema.rebuild_infered_relations()# need to be explicitly called once everything is in placeforetinrql('DISTINCT Any ET,ETN WHERE S state_of ET, ET name ETN',ask_confirm=False).entities():wf=add_workflow(u'default %s workflow'%et.name,et.name,ask_confirm=False)rql('SET S state_of WF WHERE S state_of ET, ET eid %(et)s, WF eid %(wf)s',{'et':et.eid,'wf':wf.eid},'et',ask_confirm=False)rql('SET T transition_of WF WHERE T transition_of ET, ET eid %(et)s, WF eid %(wf)s',{'et':et.eid,'wf':wf.eid},'et',ask_confirm=False)rql('SET WF initial_state S WHERE ET initial_state S, ET eid %(et)s, WF eid %(wf)s',{'et':et.eid,'wf':wf.eid},'et',ask_confirm=False)rql('DELETE TrInfo TI WHERE NOT TI from_state S')rql('SET TI by_transition T WHERE TI from_state FS, TI to_state TS, ''FS allowed_transition T, T destination_state TS')commit()drop_relation_definition('State','state_of','CWEType')drop_relation_definition('Transition','transition_of','CWEType')drop_relation_definition('CWEType','initial_state','State')sync_schema_props_perms()ifapplcubicwebversion<(3,2,2)andcubicwebversion>=(3,2,1):frombase64importb64encodefortablein('entities','deleted_entities'):foreid,extidinsql('SELECT eid, extid FROM %s WHERE extid is NOT NULL'%table,ask_confirm=False):sql('UPDATE %s SET extid=%%(extid)s WHERE eid=%%(eid)s'%table,{'extid':b64encode(extid),'eid':eid},ask_confirm=False)commit()ifapplcubicwebversion<(3,2,0)andcubicwebversion>=(3,2,0):add_cube('card',update_database=False)