--- getbinpkg.py.beforeme 2005-09-27 18:54:17.000000000 -0300 +++ getbinpkg.py 2005-09-27 18:56:41.000000000 -0300 @@ -111,11 +111,27 @@ ), } + port = None + proxy_host = os.environ.get("http_proxy") + if proxy_host: + # Do not send user/password here + address = baseurl + #address = protocol + "://" + host + "/" + address + hostport = string.split(proxy_host, ":") + if len(hostport) > 2: + raise ValueError, "Unable to interpret proxy:port from http_proxy." + elif len(hostport) == 2: + host = hostport[0] + port = hostport[1] + elif len(hostport) == 1: + host = hostport[0] + del hostport, proxy_host + if not conn: if protocol == "https": - conn = httplib.HTTPSConnection(host) + conn = httplib.HTTPSConnection(host, port) elif protocol == "http": - conn = httplib.HTTPConnection(host) + conn = httplib.HTTPConnection(host, port) elif protocol == "ftp": passive = 1 if(host[-1] == "*"): @@ -181,7 +197,7 @@ return None,int(str(e)[:4]),str(e) -def make_http_request(conn, address, params={}, headers={}, dest=None): +def make_http_request(conn, address, params={}, headers={}, dest=None, method="GET"): """(conn,address,params,headers) --- uses the conn object to request the data from address, performing Location forwarding and using the optional params and headers.""" @@ -192,7 +208,7 @@ try: if (rc != 0): conn,ignore,ignore,ignore,ignore = create_conn(address) - conn.request("GET", address, params, headers) + conn.request(method, address, params, headers) except SystemExit, e: raise except Exception, e: @@ -224,6 +240,8 @@ dest.write(response.read()) return "",0,"" + if method != "GET": + return response,0,"" return response.read(),0,"" @@ -303,7 +321,7 @@ return listing -def file_get_metadata(baseurl,conn=None, chunk_size=3000): +def file_get_metadata(baseurl,conn=None, chunk_size=3000, file_size=-1): """(baseurl[,connection]) -- Takes a base url to connect to and read from. URL should be in the for ://[:port] Connection is used for persistent connection instances.""" @@ -316,8 +334,19 @@ conn,protocol,address,params,headers = create_conn(baseurl, conn) if protocol in ["http","https"]: - headers["Range"] = "bytes=-"+str(chunk_size) - data,rc,msg = make_http_request(conn, address, params, headers) + if file_size==-1: + data,rc,msg = make_http_request(conn, address, params, headers, method="HEAD") + if data: + file_size = int(data.getheader("content-length")) + del data + # If it's still -1, we were unable to get file_size, fallback to old method + if file_size==-1: + headers["Range"] = "bytes=-"+str(chunk_size) + data,rc,msg = make_http_request(conn, address, params, headers) + else: + headers["Range"] = "bytes="+str(file_size-chunk_size)+"-" + data,rc,msg = make_http_request(conn, address, params, headers) + elif protocol in ["ftp"]: data,rc,msg = make_ftp_request(conn, address, -chunk_size) else: @@ -326,7 +355,7 @@ if data: xpaksize = xpak.decodeint(data[-8:-4]) if (xpaksize+8) > chunk_size: - myid = file_get_metadata(baseurl, conn, (xpaksize+8)) + myid = file_get_metadata(baseurl, conn, (xpaksize+8), file_size) if not keepconnection: conn.close() return myid