def _check_url(cls, url): """ Function will check given url and try to verify if it's a valid link. Sometimes it may happened that mercurial will issue basic auth request that can cause whole API to hang when used from python or other external calls. On failures it'll raise urllib2.HTTPError, return code 200 if url is valid or True if it's a local path """ from mercurial.util import url as Url # those authnadlers are patched for python 2.6.5 bug an # infinit looping when given invalid resources from mercurial.url import httpbasicauthhandler, httpdigestauthhandler # check first if it's not an local url if os.path.isdir(url) or url.startswith('file:'): return True if('+' in url[:url.find('://')]): url = url[url.find('+') + 1:] handlers = [] test_uri, authinfo = Url(url).authinfo() if authinfo: #create a password manager passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm() passmgr.add_password(*authinfo) handlers.extend((httpbasicauthhandler(passmgr), httpdigestauthhandler(passmgr))) o = urllib2.build_opener(*handlers) o.addheaders = [('Content-Type', 'application/mercurial-0.1'), ('Accept', 'application/mercurial-0.1')] q = {"cmd": 'between'} q.update({'pairs': "%s-%s" % ('0' * 40, '0' * 40)}) qs = '?%s' % urllib.urlencode(q) cu = "%s%s" % (test_uri, qs) req = urllib2.Request(cu, None, {}) try: resp = o.open(req) return resp.code == 200 except Exception, e: # means it cannot be cloned raise urllib2.URLError("[%s] %s" % (url, e))
def _check_url(cls, url): """ Function will check given url and try to verify if it's a valid link. Sometimes it may happened that mercurial will issue basic auth request that can cause whole API to hang when used from python or other external calls. On failures it'll raise urllib2.HTTPError, return code 200 if url is valid or True if it's a local path """ from mercurial.util import url as Url # those authnadlers are patched for python 2.6.5 bug an # infinit looping when given invalid resources from mercurial.url import httpbasicauthhandler, httpdigestauthhandler # check first if it's not an local url if os.path.isdir(url) or url.startswith('file:'): return True if ('+' in url[:url.find('://')]): url = url[url.find('+') + 1:] handlers = [] test_uri, authinfo = Url(url).authinfo() if authinfo: #create a password manager passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm() passmgr.add_password(*authinfo) handlers.extend((httpbasicauthhandler(passmgr), httpdigestauthhandler(passmgr))) o = urllib2.build_opener(*handlers) o.addheaders = [('Content-Type', 'application/mercurial-0.1'), ('Accept', 'application/mercurial-0.1')] q = {"cmd": 'between'} q.update({'pairs': "%s-%s" % ('0' * 40, '0' * 40)}) qs = '?%s' % urllib.urlencode(q) cu = "%s%s" % (test_uri, qs) req = urllib2.Request(cu, None, {}) try: resp = o.open(req) return resp.code == 200 except Exception, e: # means it cannot be cloned raise urllib2.URLError("[%s] %s" % (url, e))
def _check_url(cls, url): """ Functon will check given url and try to verify if it's a valid link. Sometimes it may happened that mercurial will issue basic auth request that can cause whole API to hang when used from python or other external calls. On failures it'll raise urllib2.HTTPError """ from mercurial.util import url as Url # those authnadlers are patched for python 2.6.5 bug an # infinit looping when given invalid resources from mercurial.url import httpbasicauthhandler, httpdigestauthhandler # check first if it's not an local url if os.path.isdir(url) or url.startswith('file:'): return True if('+' in url[:url.find('://')]): url = url[url.find('+') + 1:] handlers = [] test_uri, authinfo = Url(url).authinfo() if not test_uri.endswith('info/refs'): test_uri = test_uri.rstrip('/') + '/info/refs' if authinfo: #create a password manager passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm() passmgr.add_password(*authinfo) handlers.extend((httpbasicauthhandler(passmgr), httpdigestauthhandler(passmgr))) o = urllib2.build_opener(*handlers) o.addheaders = [('User-Agent', 'git/1.7.8.0')] # fake some git q = {"service": 'git-upload-pack'} qs = '?%s' % urllib.urlencode(q) cu = "%s%s" % (test_uri, qs) req = urllib2.Request(cu, None, {}) try: resp = o.open(req) return resp.code == 200 except Exception, e: # means it cannot be cloned raise urllib2.URLError("[%s] %s" % (url, e))