[FileBackend] Work-around low header value limits in Swift.
authorAaron <aschulz@wikimedia.org>
Thu, 6 Sep 2012 17:15:10 +0000 (10:15 -0700)
committerAaron <aschulz@wikimedia.org>
Thu, 6 Sep 2012 17:27:43 +0000 (10:27 -0700)
* Also added sanity check to makeContentDisposition() and made the file name optional

Change-Id: Ie4bfef743d11227631606498f026e693dd8d21f3

includes/filebackend/FileBackend.php
includes/filebackend/SwiftFileBackend.php

index e59a13b..6b25982 100644 (file)
@@ -1105,17 +1105,27 @@ abstract class FileBackend {
        }
 
        /**
-        * Build a Content-Disposition header value per RFC 6266
+        * Build a Content-Disposition header value per RFC 6266.
         *
         * @param $type string One of (attachment, inline)
         * @param $filename string Suggested file name (should not contain slashes)
         * @return string
         * @since 1.20
         */
-       final public static function makeContentDisposition( $type, $filename ) {
+       final public static function makeContentDisposition( $type, $filename = '' ) {
+               $parts = array();
+
                $type = strtolower( $type );
-               $type = in_array( $type, array( 'inline', 'attachment' ) ) ? $type : 'inline';
-               return "$type; filename*=UTF-8''" . rawurlencode( basename( $filename ) );
+               if ( !in_array( $type, array( 'inline', 'attachment' ) ) ) {
+                       throw new MWException( "Invalid Content-Disposition type '$type'." );
+               }
+               $parts[] = $type;
+
+               if ( strlen( $filename ) ) {
+                       $parts[] = "filename*=UTF-8''" . rawurlencode( basename( $filename ) );
+               }
+
+               return implode( ';', $parts );
        }
 
        /**
index 7f91ae3..ab86107 100644 (file)
@@ -162,6 +162,24 @@ class SwiftFileBackend extends FileBackendStore {
                return false;
        }
 
+       /**
+        * @param $disposition string Content-Disposition header value
+        * @return string Truncated Content-Disposition header value to meet Swift limits
+        */
+       protected function truncDisp( $disposition ) {
+               $res = '';
+               foreach ( explode( ';', $disposition ) as $part ) {
+                       $part = trim( $part );
+                       $new  = ( $res === '' ) ? $part : "{$res};{$part}";
+                       if ( strlen( $new ) <= 255 ) {
+                               $res = $new;
+                       } else {
+                               break; // too long; sigh
+                       }
+               }
+               return $res;
+       }
+
        /**
         * @see FileBackendStore::doCreateInternal()
         * @return Status
@@ -212,7 +230,7 @@ class SwiftFileBackend extends FileBackendStore {
                        }
                        // Set the Content-Disposition header if requested
                        if ( isset( $params['disposition'] ) ) {
-                               $obj->headers['Content-Disposition'] = $params['disposition'];
+                               $obj->headers['Content-Disposition'] = $this->truncDisp( $params['disposition'] );
                        }
                        if ( !empty( $params['async'] ) ) { // deferred
                                $op = $obj->write_async( $params['content'] );
@@ -302,7 +320,7 @@ class SwiftFileBackend extends FileBackendStore {
                        }
                        // Set the Content-Disposition header if requested
                        if ( isset( $params['disposition'] ) ) {
-                               $obj->headers['Content-Disposition'] = $params['disposition'];
+                               $obj->headers['Content-Disposition'] = $this->truncDisp( $params['disposition'] );
                        }
                        if ( !empty( $params['async'] ) ) { // deferred
                                wfSuppressWarnings();
@@ -392,7 +410,7 @@ class SwiftFileBackend extends FileBackendStore {
                        $dstObj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
                        $hdrs = array(); // source file headers to override with new values
                        if ( isset( $params['disposition'] ) ) {
-                               $hdrs['Content-Disposition'] = $params['disposition'];
+                               $hdrs['Content-Disposition'] = $this->truncDisp( $params['disposition'] );
                        }
                        if ( !empty( $params['async'] ) ) { // deferred
                                $op = $sContObj->copy_object_to_async( $srcRel, $dContObj, $dstRel, null, $hdrs );
@@ -471,7 +489,7 @@ class SwiftFileBackend extends FileBackendStore {
                        $dstObj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
                        $hdrs = array(); // source file headers to override with new values
                        if ( isset( $params['disposition'] ) ) {
-                               $hdrs['Content-Disposition'] = $params['disposition'];
+                               $hdrs['Content-Disposition'] = $this->truncDisp( $params['disposition'] );
                        }
                        if ( !empty( $params['async'] ) ) { // deferred
                                $op = $sContObj->move_object_to_async( $srcRel, $dContObj, $dstRel, null, $hdrs );