Beispiel #1
0
def make_response(uri, environ):
    
    res = Response(conditional_response=True)
    
    # check if the host is online. If not raise an http error
    if not pingSMB( parseSMBuri(uri)["host"] ):
        return HTTPGatewayTimeout("Host is currently offline. You may try again at a later time.")
    
    try:
        f = c.open(uri)
    except smbc.NoEntryError:
        return HTTPNotFound("The file you requested is no longer available!")
    
    fs = f.fstat()
    filesize = fs[6]
    last_modified = fs[8] # mtime
    filename = uri.split("/")[-1]
    req = Request(environ)
    log.debug("Incoming request: \n" + str(req))
    
    if req.range:
        log.debug("begin ranged transfer")
        res.status_int = 206
        res.content_range = req.range.content_range(filesize)
        (start, stop) = req.range.range_for_length(filesize)
        
        log.debug("filesize: " + str(filesize))
        log.debug("start: " + str(start)  + "   stop: " + str(stop))
        log.debug("Content-Range: " + str(res.content_range))
        
        res.app_iter = FileIterable(uri, start=start, stop=stop)
        res.content_length = stop - start
    
    else:
        log.debug("begin normal transfer")
        res.app_iter = FileIterable(uri)
        res.content_length = filesize
    
    log.debug("Content-Length: " + str(res.content_length))
    
    res.server_protocol = "HTTP/1.1"
    # Make sure the file gets downloaded and not played live
    res.content_type = "application/octet-stream"
    res.last_modified = last_modified
    res.etag = '%s-%s-%s' % (fs[8], fs[6], hash(f))
    res.headers.add("Content-Disposition", 'attachment; filename="%s"' % str(filename) )
    res.headers.add("Accept-Ranges", "bytes")
    return res