mirror of
https://github.com/openSUSE/osc.git
synced 2025-08-24 07:08:53 +02:00
save and reuse HTTP server cookies, speeding HTTP requests up about 5 times (in
our iChain setup anyway...)
This commit is contained in:
21
osc/conf.py
21
osc/conf.py
@@ -58,6 +58,7 @@ DEFAULTS = { 'apisrv': 'api.opensuse.org',
|
|||||||
],
|
],
|
||||||
|
|
||||||
'http_debug': '0',
|
'http_debug': '0',
|
||||||
|
'cookiejar': '~/.osc_cookiejar',
|
||||||
|
|
||||||
# switched off for now (testing)
|
# switched off for now (testing)
|
||||||
'do_commits': '0',
|
'do_commits': '0',
|
||||||
@@ -108,12 +109,16 @@ Make sure that it has a [general] section.
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
cookiejar = None
|
||||||
|
|
||||||
def init_basicauth(config):
|
def init_basicauth(config):
|
||||||
"""initialize urllib2 with the credentials for Basic Authentication"""
|
"""initialize urllib2 with the credentials for Basic Authentication"""
|
||||||
|
|
||||||
from osc.core import __version__
|
from osc.core import __version__
|
||||||
import urllib2
|
import os, urllib2
|
||||||
|
import cookielib
|
||||||
|
|
||||||
|
global cookiejar
|
||||||
|
|
||||||
if config['http_debug'] == '1':
|
if config['http_debug'] == '1':
|
||||||
# brute force
|
# brute force
|
||||||
@@ -124,7 +129,19 @@ def init_basicauth(config):
|
|||||||
authhandler = urllib2.HTTPBasicAuthHandler( \
|
authhandler = urllib2.HTTPBasicAuthHandler( \
|
||||||
urllib2.HTTPPasswordMgrWithDefaultRealm())
|
urllib2.HTTPPasswordMgrWithDefaultRealm())
|
||||||
|
|
||||||
opener = urllib2.build_opener(authhandler)
|
cookie_file = os.path.expanduser(config['cookiejar'])
|
||||||
|
cookiejar = cookielib.LWPCookieJar(cookie_file)
|
||||||
|
try:
|
||||||
|
cookiejar.load(ignore_discard=True)
|
||||||
|
except IOError:
|
||||||
|
try:
|
||||||
|
open(cookie_file, 'w').close()
|
||||||
|
os.chmod(cookie_file, 0600)
|
||||||
|
except:
|
||||||
|
#print 'Unable to create cookiejar file: \'%s\'. Using RAM-based cookies.' % cookie_file
|
||||||
|
cookiejar = cookielib.CookieJar()
|
||||||
|
|
||||||
|
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar), authhandler)
|
||||||
urllib2.install_opener(opener)
|
urllib2.install_opener(opener)
|
||||||
|
|
||||||
opener.addheaders = [('User-agent', 'osc/%s' % __version__)]
|
opener.addheaders = [('User-agent', 'osc/%s' % __version__)]
|
||||||
|
@@ -674,6 +674,9 @@ def http_request(method, url, data=None, file=None):
|
|||||||
|
|
||||||
fd = urllib2.urlopen(req, data=data)
|
fd = urllib2.urlopen(req, data=data)
|
||||||
|
|
||||||
|
if hasattr(conf.cookiejar, 'save'):
|
||||||
|
conf.cookiejar.save(ignore_discard=True)
|
||||||
|
|
||||||
if filefd: filefd.close()
|
if filefd: filefd.close()
|
||||||
return fd
|
return fd
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user