@@ -1064,83 +1064,33 @@ def connect(self):
1064
1064
from google .appengine .api .urlfetch import fetch
1065
1065
from google .appengine .api .urlfetch import InvalidURLError
1066
1066
1067
- class ResponseDict (dict ):
1068
- """Dictionary with a read() method; can pass off as httplib.HTTPResponse."""
1069
- def __init__ (self , * args , ** kwargs ):
1070
- self .content = kwargs .pop ('content' , None )
1071
- return super (ResponseDict , self ).__init__ (* args , ** kwargs )
1067
+ def _new_fixed_fetch (validate_certificate ):
1068
+ def fixed_fetch (url , payload = None , method = "GET" , headers = {}, allow_truncated = False , follow_redirects = True , deadline = 5 ):
1069
+ return fetch (url , payload = payload , method = method , headers = header , allow_truncated = allow_truncated , follow_redirects = follow_redirects , deadline = deadline , validate_certificate = validate_certificate )
1070
+ return fixed_fetch
1072
1071
1073
- def read ( self ):
1074
- return self . content
1072
+ class AppEngineHttpConnection ( httplib . HTTPConnection ):
1073
+ """Use httplib on App Engine, but compensate for its weirdness.
1075
1074
1076
-
1077
- class AppEngineHttpConnection (object ):
1078
- """Emulates an httplib.HTTPConnection object, but actually uses the Google
1079
- App Engine urlfetch library. This allows the timeout to be properly used on
1080
- Google App Engine, and avoids using httplib, which on Google App Engine is
1081
- just another wrapper around urlfetch.
1075
+ The parameters key_file, cert_file, proxy_info, ca_certs, and
1076
+ disable_ssl_certificate_validation are all dropped on the ground.
1082
1077
"""
1083
1078
def __init__ (self , host , port = None , key_file = None , cert_file = None ,
1084
1079
strict = None , timeout = None , proxy_info = None , ca_certs = None ,
1085
1080
disable_ssl_certificate_validation = False ):
1086
- self .host = host
1087
- self .port = port
1088
- self .timeout = timeout
1089
- if key_file or cert_file or proxy_info or ca_certs :
1090
- raise NotSupportedOnThisPlatform ()
1091
- self .response = None
1092
- self .scheme = 'http'
1093
- self .validate_certificate = not disable_ssl_certificate_validation
1094
- self .sock = True
1095
-
1096
- def request (self , method , url , body , headers ):
1097
- # Calculate the absolute URI, which fetch requires
1098
- netloc = self .host
1099
- if self .port :
1100
- netloc = '%s:%s' % (self .host , self .port )
1101
- absolute_uri = '%s://%s%s' % (self .scheme , netloc , url )
1102
- try :
1103
- try : # 'body' can be a stream.
1104
- body = body .read ()
1105
- except AttributeError :
1106
- pass
1107
- response = fetch (absolute_uri , payload = body , method = method ,
1108
- headers = headers , allow_truncated = False , follow_redirects = False ,
1109
- deadline = self .timeout ,
1110
- validate_certificate = self .validate_certificate )
1111
- self .response = ResponseDict (response .headers , content = response .content )
1112
- self .response ['status' ] = str (response .status_code )
1113
- self .response ['reason' ] = httplib .responses .get (response .status_code , 'Ok' )
1114
- self .response .status = response .status_code
1115
-
1116
- # Make sure the exceptions raised match the exceptions expected.
1117
- except InvalidURLError :
1118
- raise socket .gaierror ('' )
1119
-
1120
- def getresponse (self ):
1121
- if self .response :
1122
- return self .response
1123
- else :
1124
- raise httplib .HTTPException ()
1125
-
1126
- def set_debuglevel (self , level ):
1127
- pass
1128
-
1129
- def connect (self ):
1130
- pass
1131
-
1132
- def close (self ):
1133
- pass
1134
-
1081
+ httplib .HTTPConnection .__init__ (self , host , port = port , strict = strict ,
1082
+ timeout = timeout )
1135
1083
1136
- class AppEngineHttpsConnection (AppEngineHttpConnection ):
1084
+ class AppEngineHttpsConnection (httplib . HTTPSConnection ):
1137
1085
"""Same as AppEngineHttpConnection, but for HTTPS URIs."""
1138
1086
def __init__ (self , host , port = None , key_file = None , cert_file = None ,
1139
1087
strict = None , timeout = None , proxy_info = None , ca_certs = None ,
1140
1088
disable_ssl_certificate_validation = False ):
1141
- AppEngineHttpConnection .__init__ (self , host , port , key_file , cert_file ,
1142
- strict , timeout , proxy_info , ca_certs , disable_ssl_certificate_validation )
1143
- self .scheme = 'https'
1089
+ httplib .HTTPSConnection .__init__ (self , host , port = port ,
1090
+ key_file = key_file ,
1091
+ cert_file = cert_file , strict = strict ,
1092
+ timeout = timeout )
1093
+ self ._fetch = _new_fixed_fetch (not disable_ssl_certificate_validation )
1144
1094
1145
1095
# Update the connection classes to use the Googel App Engine specific ones.
1146
1096
SCHEME_TO_CONNECTION = {
@@ -1277,7 +1227,7 @@ def clear_credentials(self):
1277
1227
def _conn_request (self , conn , request_uri , method , body , headers ):
1278
1228
for i in range (RETRIES ):
1279
1229
try :
1280
- if conn .sock is None :
1230
+ if hasattr ( conn , 'sock' ) and conn .sock is None :
1281
1231
conn .connect ()
1282
1232
conn .request (method , request_uri , body , headers )
1283
1233
except socket .timeout :
@@ -1299,7 +1249,7 @@ def _conn_request(self, conn, request_uri, method, body, headers):
1299
1249
except httplib .HTTPException :
1300
1250
# Just because the server closed the connection doesn't apparently mean
1301
1251
# that the server didn't send a response.
1302
- if conn .sock is None :
1252
+ if hasattr ( conn , 'sock' ) and conn .sock is None :
1303
1253
if i < RETRIES - 1 :
1304
1254
conn .close ()
1305
1255
conn .connect ()
0 commit comments