def _dav_put(filepath, url, matrix_params, login, progress=None):
    """Upload `filepath` to given `url` (referring to a WebDAV collection)."""
    fileurl = _file_url(filepath, url)
    if matrix_params:
        fileurl += ';' + matrix_params
    sys.stdout.write("  Uploading %s: " % os.path.basename(filepath))
    sys.stdout.flush()
    size = os.path.getsize(filepath)

    with closing(open(filepath, 'r')) as handle:
        if progress:
            handle = dputhelper.FileWithProgress(handle, ptype=progress, progressf=sys.stdout, size=size)
        trace("HTTP PUT to URL: %s" % fileurl)

        try:
            conn = _url_connection(fileurl, "PUT")
            try:
                conn.putheader("Authorization", 'Basic %s' % login.encode('base64').replace('\n', '').strip())
                conn.putheader("Content-Length", str(size))
                conn.endheaders()

                conn.debuglevel = 0
                while True:
                    data = handle.read(CHUNK_SIZE)
                    if not data:
                        break
                    conn.send(data)
                conn.debuglevel = int(trace.debug)

                resp = conn.getresponse()
                if 200 <= resp.status <= 299:
                    print(" done.")
                #elif res.status == 401 and not auth_headers:
                    #print "need authentication."
                    #auth_headers = AuthHandlerHackAround(url, res.msg, pwman).get_auth_headers()
                elif resp.status == 401:
                    print(" unauthorized.")
                    raise urllib2.URLError("Upload failed as unauthorized (%s),"
                        " maybe wrong username or password?" % resp.reason)
                else:
                    print(" failed.")
                    raise urllib2.URLError("Unexpected HTTP status %d %s" % (resp.status, resp.reason))

                resp.read() # eat response body
            finally:
                conn.close()
        except httplib.HTTPException, exc:
            raise urllib2.URLError(exc)
dataset/ETHPy150Open jhermann/artifactory-debian/dput-webdav/webdav.py/_dav_put/original