18
18
RecentlyUsedContainer = urllib3 ._collections .RecentlyUsedContainer
19
19
20
20
21
+ class UnixHTTPResponse (httplib .HTTPResponse , object ):
22
+ def __init__ (self , sock , * args , ** kwargs ):
23
+ disable_buffering = kwargs .pop ('disable_buffering' , False )
24
+ super (UnixHTTPResponse , self ).__init__ (sock , * args , ** kwargs )
25
+ if disable_buffering is True :
26
+ # We must first create a new pointer then close the old one
27
+ # to avoid closing the underlying socket.
28
+ new_fp = sock .makefile ('rb' , 0 )
29
+ self .fp .close ()
30
+ self .fp = new_fp
31
+
32
+
21
33
class UnixHTTPConnection (httplib .HTTPConnection , object ):
34
+
22
35
def __init__ (self , base_url , unix_socket , timeout = 60 ):
23
36
super (UnixHTTPConnection , self ).__init__ (
24
37
'localhost' , timeout = timeout
25
38
)
26
39
self .base_url = base_url
27
40
self .unix_socket = unix_socket
28
41
self .timeout = timeout
42
+ self .disable_buffering = False
29
43
30
44
def connect (self ):
31
45
sock = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
32
46
sock .settimeout (self .timeout )
33
47
sock .connect (self .unix_socket )
34
48
self .sock = sock
35
49
50
+ def putheader (self , header , * values ):
51
+ super (UnixHTTPConnection , self ).putheader (header , * values )
52
+ if header == 'Connection' and 'Upgrade' in values :
53
+ self .disable_buffering = True
36
54
37
- class AttachHTTPResponse (httplib .HTTPResponse ):
38
- '''
39
- A HTTPResponse object that doesn't use a buffered fileobject.
40
- '''
41
- def __init__ (self , sock , * args , ** kwargs ):
42
- # Delegate to super class
43
- httplib .HTTPResponse .__init__ (self , sock , * args , ** kwargs )
44
-
45
- # Override fp with a fileobject that doesn't buffer
46
- self .fp = sock .makefile ('rb' , 0 )
47
-
55
+ def response_class (self , sock , * args , ** kwargs ):
56
+ if self .disable_buffering :
57
+ kwargs ['disable_buffering' ] = True
48
58
49
- class AttachUnixHTTPConnection (UnixHTTPConnection ):
50
- '''
51
- A HTTPConnection that returns responses that don't used buffering.
52
- '''
53
- response_class = AttachHTTPResponse
59
+ return UnixHTTPResponse (sock , * args , ** kwargs )
54
60
55
61
56
62
class UnixHTTPConnectionPool (urllib3 .connectionpool .HTTPConnectionPool ):
@@ -63,17 +69,9 @@ def __init__(self, base_url, socket_path, timeout=60, maxsize=10):
63
69
self .timeout = timeout
64
70
65
71
def _new_conn (self ):
66
- # Special case for attach url, as we do a http upgrade to tcp and
67
- # a buffered connection can cause data loss.
68
- path = urllib3 .util .parse_url (self .base_url ).path
69
- if path .endswith ('attach' ):
70
- return AttachUnixHTTPConnection (
71
- self .base_url , self .socket_path , self .timeout
72
- )
73
- else :
74
- return UnixHTTPConnection (
75
- self .base_url , self .socket_path , self .timeout
76
- )
72
+ return UnixHTTPConnection (
73
+ self .base_url , self .socket_path , self .timeout
74
+ )
77
75
78
76
79
77
class UnixAdapter (requests .adapters .HTTPAdapter ):
0 commit comments