~ubuntu-branches/ubuntu/trusty/swift/trusty-updates

« back to all changes in this revision

Viewing changes to swift/common/middleware/staticweb.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, James Page, Chuck Short
  • Date: 2013-08-13 10:37:13 UTC
  • mfrom: (1.2.21)
  • Revision ID: package-import@ubuntu.com-20130813103713-1ctbx4zifyljs2aq
Tags: 1.9.1-0ubuntu1
[ James Page ]
* d/control: Update VCS fields for new branch locations.

[ Chuck Short ]
* New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
125
125
from swift.proxy.controllers.base import get_container_info
126
126
 
127
127
 
 
128
def ensure_utf8_bytes(value):
 
129
    if isinstance(value, unicode):
 
130
        value = value.encode('utf-8')
 
131
    return value
 
132
 
 
133
 
128
134
def quote(value, safe='/'):
129
135
    """
130
136
    Patched version of urllib.quote that encodes utf-8 strings before quoting
131
137
    """
132
 
    if isinstance(value, unicode):
133
 
        value = value.encode('utf-8')
134
 
    return urllib_quote(value, safe)
 
138
    return urllib_quote(ensure_utf8_bytes(value), safe)
135
139
 
136
140
 
137
141
class _StaticWebContext(WSGIContext):
152
156
        self.container = container
153
157
        self.obj = obj
154
158
        self.app = staticweb.app
155
 
        self.cache_timeout = staticweb.cache_timeout
156
159
        self.agent = '%(orig)s StaticWeb'
157
160
        # Results from the last call to self._get_container_info.
158
161
        self._index = self._error = self._listings = self._listings_css = \
273
276
                    '   </tr>\n'
274
277
        for item in listing:
275
278
            if 'subdir' in item:
276
 
                if isinstance(item['subdir'], unicode):
277
 
                    subdir = item['subdir'].encode('utf-8')
278
 
                else:
279
 
                    subdir = item['subdir']
 
279
                subdir = ensure_utf8_bytes(item['subdir'])
280
280
                if prefix:
281
281
                    subdir = subdir[len(prefix):]
282
282
                body += '   <tr class="item subdir">\n' \
287
287
                        (quote(subdir), cgi.escape(subdir))
288
288
        for item in listing:
289
289
            if 'name' in item:
290
 
                if isinstance(item['name'], unicode):
291
 
                    name = item['name'].encode('utf-8')
292
 
                else:
293
 
                    name = item['name']
 
290
                name = ensure_utf8_bytes(item['name'])
294
291
                if prefix:
295
292
                    name = name[len(prefix):]
 
293
                content_type = ensure_utf8_bytes(item['content_type'])
 
294
                bytes = ensure_utf8_bytes(human_readable(item['bytes']))
 
295
                last_modified = (cgi.escape(item['last_modified']).
 
296
                                 split('.')[0].replace('T', ' '))
296
297
                body += '   <tr class="item %s">\n' \
297
298
                        '    <td class="colname"><a href="%s">%s</a></td>\n' \
298
299
                        '    <td class="colsize">%s</td>\n' \
299
300
                        '    <td class="coldate">%s</td>\n' \
300
301
                        '   </tr>\n' % \
301
302
                        (' '.join('type-' + cgi.escape(t.lower(), quote=True)
302
 
                                  for t in item['content_type'].split('/')),
 
303
                                  for t in content_type.split('/')),
303
304
                         quote(name), cgi.escape(name),
304
 
                         human_readable(item['bytes']),
305
 
                         cgi.escape(item['last_modified']).split('.')[0].
306
 
                            replace('T', ' '))
 
305
                         bytes, ensure_utf8_bytes(last_modified))
307
306
        body += '  </table>\n' \
308
307
                ' </body>\n' \
309
308
                '</html>\n'
350
349
        status_int = self._get_status_int()
351
350
        if status_int == HTTP_NOT_FOUND:
352
351
            return self._listing(env, start_response)
353
 
        elif not is_success(self._get_status_int()) or \
 
352
        elif not is_success(self._get_status_int()) and \
354
353
                not is_redirection(self._get_status_int()):
355
354
            return self._error_response(resp, env, start_response)
356
355
        start_response(self._response_status, self._response_headers,
447
446
        self.app = app
448
447
        #: The filter configuration dict.
449
448
        self.conf = conf
450
 
        #: The seconds to cache the x-container-meta-web-* headers.,
451
 
        self.cache_timeout = int(conf.get('cache_timeout', 300))
452
449
 
453
450
    def __call__(self, env, start_response):
454
451
        """
477
474
 
478
475
 
479
476
def filter_factory(global_conf, **local_conf):
480
 
    """ Returns a Static Web WSGI filter for use with paste.deploy. """
 
477
    """Returns a Static Web WSGI filter for use with paste.deploy."""
481
478
    conf = global_conf.copy()
482
479
    conf.update(local_conf)
483
480