vendor/tramline

changeset 32:ad965873c5a6 gracinet-fix-range

#2322: if multiple lines received, tram id is the last ; the first are forwarded
author Georges Racinet on purity.racinet.fr <georges@racinet.fr>
date Sat, 15 Jan 2011 16:43:45 +0100
parents 234396237d81
children 5f66349d184c
files src/tramline/core.py
diffstat 1 files changed, 19 insertions(+), 5 deletions(-) [+]
line diff
     1.1 --- a/src/tramline/core.py
     1.2 +++ b/src/tramline/core.py
     1.3 @@ -48,7 +48,9 @@
     1.4                             tramline_repository_path(req))):
     1.5          if not os.path.isdir(p):
     1.6              os.mkdir(p)
     1.7 -            if i == 3 and group_write(req):
     1.8 +            import sys
     1.9 +            sys.stderr.write('creating path: %r\n' % p)
    1.10 +	    if i == 3 and group_write(req):
    1.11                  # drwxrwsr-x
    1.12                  # (set-group-ID-on-execution bit)
    1.13                  os.chmod(p, 02775)
    1.14 @@ -205,7 +207,17 @@
    1.15      while s:
    1.16          data.append(s)
    1.17          s = filter.read()
    1.18 -    file_id = ''.join(data)
    1.19 +    data = ''.join(data)
    1.20 +
    1.21 +    # if multiple lines, the id is the last, first lines have to be forwarded
    1.22 +    split = data.rsplit('\n', 1)
    1.23 +    if len(split) == 1:
    1.24 +        file_id = data
    1.25 +        prepend = ''
    1.26 +    else:
    1.27 +        file_id = split[1]
    1.28 +        prepend = split[0] + '\n'
    1.29 +        
    1.30      p = id_to_path(tramline_path(filter.req), file_id)
    1.31  
    1.32      log('file id:' + file_id, filter.req)
    1.33 @@ -223,13 +235,15 @@
    1.34  	else:
    1.35             log("Unparsable Range header " + h, filter.req)
    1.36      elif status == 200:
    1.37 -	serve_file(p, filter)
    1.38 +	serve_file(p, filter, prepend=prepend)
    1.39  
    1.40 -def serve_file(p, filter):
    1.41 +def serve_file(p, filter, prepend=''):
    1.42      """Serve a whole file."""
    1.43      # XXX what if file doesn't exist? 404?
    1.44      size = os.stat(p).st_size
    1.45 -    filter.req.headers_out['content-length'] = str(size)
    1.46 +    if prepend:
    1.47 +        filter.write(prepend)
    1.48 +    filter.req.headers_out['content-length'] = str(size + len(prepend))
    1.49      f = open(p, 'rb')
    1.50      dump_file_range(f, 0, size-1, filter)
    1.51      f.close()