def _send_request(self, request, debug = False): '''sends the provided request which was created by _build_request()''' request_headers_with_auth = request['headers'].copy() request_headers_with_auth['Authorization'] = 'Basic {}'.format(self._get_auth_token()) conn = HTTPSConnection('api.github.com') if debug: conn.set_debuglevel(999) conn.request( request['method'], request['path'], body = json.dumps(request['payload']).encode('utf-8'), headers = request_headers_with_auth) return conn.getresponse()
def _send_request(self, request, debug=False): '''sends the provided request which was created by _build_request()''' request_headers_with_auth = request['headers'].copy() request_headers_with_auth['Authorization'] = 'Basic {}'.format( self._get_auth_token()) conn = HTTPSConnection('api.github.com') if debug: conn.set_debuglevel(999) conn.request(request['method'], request['path'], body=json.dumps(request['payload']).encode('utf-8'), headers=request_headers_with_auth) return conn.getresponse()
def _query_http(self, request_method, request_path, request_json_payload=None, log_error=True, debug=False): if self._dry_run: logger.info('[DRY RUN] {} https://{}{}'.format( request_method, self._CCM_HOST, request_path)) if request_json_payload: logger.info('[DRY RUN] Payload: {}'.format( pprint.pformat(request_json_payload))) return None conn = HTTPSConnection(self._CCM_HOST) if debug: conn.set_debuglevel(999) request_headers = self._http_headers.copy() if request_json_payload: request_body = json.dumps(request_json_payload).encode('utf-8') request_headers['Content-Type'] = 'application/json' else: request_body = None conn.request(request_method, request_path, body=request_body, headers=request_headers) response = conn.getresponse() if log_error and (response.status < 200 or response.status >= 300): logger.error('Got {} response to HTTP request:'.format( response.status)) logger.error('Request: {} https://{}{}'.format( request_method, self._CCM_HOST, request_path)) logger.error('Response:') logger.error(' - Status: {} {}'.format(response.status, str(response.msg).strip())) logger.error(' - Headers: {}'.format( pprint.pformat(response.getheaders()))) logger.error(' - Body: {}'.format(pprint.pformat( response.read()))) return None elif debug: logger.debug('{}: {}'.format(response.status, str(response.msg).strip())) logger.debug(pprint.pformat(response.getheaders())) return response
def _query_http(self, request_method, request_path, request_json_payload=None, log_error=True, debug=False): parsed_url = urlparse(self._dcos_url) if parsed_url.scheme == 'https': # EE clusters are often self-signed, disable validation: conn = HTTPSConnection(parsed_url.hostname, context=ssl._create_unverified_context()) elif parsed_url.scheme == 'http': conn = HTTPConnection(parsed_url.hostname) else: raise Exception('Unsupported protocol: {} (from url={})'.format( parsed_url.scheme, self._dcos_url)) if debug: conn.set_debuglevel(999) request_headers = {} if request_json_payload: request_body = json.dumps(request_json_payload).encode('utf-8') request_headers['Content-Type'] = 'application/json' else: request_body = None conn.request(request_method, request_path, body=request_body, headers=request_headers) response = conn.getresponse() if log_error and (response.status < 200 or response.status >= 300): logger.error('Got {} response to HTTP request:'.format( response.status)) logger.error('Request: {} {}'.format(request_method, request_path)) logger.error('Response: {} {}'.format(response.status, str(response.msg).strip())) logger.error(pprint.pformat(response.getheaders())) logger.error(pprint.pformat(response.read())) return None elif debug: logger.debug('{}: {}'.format(response.status, str(response.msg).strip())) logger.debug(pprint.pformat(response.getheaders())) return response
def _query_http( self, request_method, request_path, request_json_payload=None, log_error=True, debug=False): parsed_url = urlparse(self._dcos_url) if parsed_url.scheme == 'https': # EE clusters are often self-signed, disable validation: conn = HTTPSConnection(parsed_url.hostname, context=ssl._create_unverified_context()) elif parsed_url.scheme == 'http': conn = HTTPConnection(parsed_url.hostname) else: raise Exception('Unsupported protocol: {} (from url={})'.format( parsed_url.scheme, self._dcos_url)) if debug: conn.set_debuglevel(999) request_headers = {} if request_json_payload: request_body = json.dumps(request_json_payload).encode('utf-8') request_headers['Content-Type'] = 'application/json' else: request_body = None conn.request( request_method, request_path, body = request_body, headers = request_headers) response = conn.getresponse() if log_error and (response.status < 200 or response.status >= 300): logger.error('Got {} response to HTTP request:'.format(response.status)) logger.error('Request: {} {}'.format(request_method, request_path)) logger.error('Response: {} {}'.format(response.status, str(response.msg).strip())) logger.error(pprint.pformat(response.getheaders())) logger.error(pprint.pformat(response.read())) return None elif debug: logger.debug('{}: {}'.format(response.status, str(response.msg).strip())) logger.debug(pprint.pformat(response.getheaders())) return response
def _create_universe_pr(self, branch, commitmsg_path): if self._dry_run: logger.info('[DRY RUN] Skipping creation of PR against branch {}'.format(branch)) return None headers = { 'User-Agent': 'release_builder.py', 'Content-Type': 'application/json', 'Authorization': 'Basic {}'.format(self._github_token)} payload = { 'title': self._pr_title, 'head': branch, 'base': 'version-3.x', 'body': open(commitmsg_path).read()} conn = HTTPSConnection('api.github.com') conn.set_debuglevel(999) conn.request( 'POST', '/repos/mesosphere/universe/pulls', body = json.dumps(payload).encode('utf-8'), headers = headers) return conn.getresponse()
def _query_http(self, request_method, request_path, request_json_payload=None, log_error=True, debug=False): if self._dry_run: logger.info('[DRY RUN] {} https://{}{}'.format(request_method, self._CCM_HOST, request_path)) if request_json_payload: logger.info('[DRY RUN] Payload: {}'.format(pprint.pformat(request_json_payload))) return None conn = HTTPSConnection(self._CCM_HOST) if debug: conn.set_debuglevel(999) request_headers = self._http_headers.copy() if request_json_payload: request_body = json.dumps(request_json_payload).encode('utf-8') request_headers['Content-Type'] = 'application/json' else: request_body = None conn.request( request_method, request_path, body = request_body, headers = request_headers) response = conn.getresponse() if log_error and (response.status < 200 or response.status >= 300): logger.error('Got {} response to HTTP request:'.format(response.status)) logger.error('Request: {} https://{}{}'.format(request_method, self._CCM_HOST, request_path)) logger.error('Response:') logger.error(' - Status: {} {}'.format(response.status, str(response.msg).strip())) logger.error(' - Headers: {}'.format(pprint.pformat(response.getheaders()))) logger.error(' - Body: {}'.format(pprint.pformat(response.read()))) return None elif debug: logger.debug('{}: {}'.format(response.status, str(response.msg).strip())) logger.debug(pprint.pformat(response.getheaders())) return response
def connectOpenAPIServer(): global conn, server conn = HTTPSConnection(server) conn.set_debuglevel(1)
def connectOpenAPIServer(): global conn, server conn = HTTPSConnection(server) #https프로토콜을 사용하기 위한 핸들러 conn.set_debuglevel(1)
def _send_request(self, method, domain, path, extra_headers, body=None): # Create the http object h = HTTPSConnection(domain) if self.debug_mode: h.set_debuglevel(10) # Construct the request headers headers = self._get_headers(extra_headers) # In "raw mode", create the equivalent curl(1) command for this request # and save it in the session provided in the constructor curl = '' if self.raw_mode and self.session: curl = 'curl -v -X "%s" ' % method for header_name, header_value in six.iteritems(headers): curl += '-H "%s: %s" ' % (header_name, header_value) if body: # we want to turn ' into '"'"' for curl output so we need to do this! match = re.compile("'") curl_body = match.sub("'\"'\"'", body) if method == "POST" or method == "PUT": curl += "--data '%s' " % curl_body # escape the url in double-quotes because it might contain & characters curl += '"https://%s%s"' % (domain, path) self.session['curl_command'] = curl # Perform the request (with retries) and get the response headers and content retries = RETRY_LIMIT; response = ''; response_status = 0; response_text = '' for n in range(retries): try: h.request(method, path, body, headers) except gaierror: # if we got a socket exception, try again time.sleep(5) continue response = h.getresponse() response_status = response.status response_text = response.read().decode('utf-8') if response_status == 504: # gateway timeout error: sleep then retry (up to retry limit) time.sleep(5) else: # go on break if self.raw_mode and self.session: self.session['response_status'] = response_status self.session['response_text'] = response_text if self.debug_mode: print ("DEBUG: response status is %d, full response is" % response_status) print (response_text) # Bad Request gives an error in text, not JSON if response_status == 400: self._relay_error(response_status, response_text) if response_status == 201: # 201 Created means we get the response as a Location: header if 'location' not in response.msg: self._relay_error(response_status, "Received 201 Created response but there's no Location: header. Response text is %s" % response_text) return response.msg['location'] # 422 is "unprocessable entity" but more details are given in the JS response # so we should use that instead if response_status != 200 and response_status != 422: self._relay_error(response_status, response.reason + " " + str(response_text)) js = None if not response_text or response_text == '': return response_text if response_status == 422: self._relay_error(response_status, response_text) js = json.JSONDecoder().decode(response_text) if response_status == 422: if 'message' in js: if 'code' in js: code = js['code'] else: code = response_status self._relay_error(code, js['message']) else: # if we didn't get a JSON payload (eg Create RFP), just report the whole response self._relay_error(response_status, response_text) return js
def retry_using_http_auth(self, target, req, infourl, auth_methods, headers): connection = headers.get('connection') # ntlm secures a socket, so we must use the same socket for the complete handshake headers = dict(req.headers) headers.update(req.unredirected_hdrs) url = req.get_full_url() host = req.host if not host: raise URLError('no host given') user, pw = self.passwd.find_user_password(None, target) domain = None if user is not None and '\\' in user: domain, user = user.split('\\', 1) certificate = infourl_to_ssl_certificate(infourl) try: more, method, payload = self.create_auth1_message( domain, user, pw, url, auth_methods, certificate) except AuthenticationError: self.logger.warning('No way to perform authentication: URL=%s', url) return None self.logger.debug('Selected auth method=%s payload=%s more=%s', method, payload, more) headers.update({ self.auth_header_request: ' '.join([method, payload]), 'Connection': 'keep-alive' if (more or self.auth_is_proxy) else 'close' }) h = None if url.startswith('https://'): try: old_ssl_context = infourl_to_ssl_context(infourl) self.logger.debug('Reuse SSL Context: %s', old_ssl_context) except Exception as e: self.logger.exception('SSL Context not found') old_ssl_context = None h = HTTPSConnection(host, context=old_ssl_context) else: h = HTTPConnection(host) if self._debuglevel or self.logger.getEffectiveLevel( ) == logging.DEBUG: h.set_debuglevel(1) if connection and connection.lower() == 'close': self.logger.debug('New connection required: host=%s', host) infourl.close() infourl = None else: h.sock = infourl_to_sock(infourl) self.logger.debug('Reuse connection socket %s', h.sock) # We must keep the connection because NTLM authenticates the # connection, not single requests headers = dict((name.title(), val) for name, val in headers.items()) self.logger.debug('Send request, headers=%s', headers) payload = None if sys.version_info.major > 2: selector = req.selector else: selector = req.get_selector() h.request(req.get_method(), selector, req.data, headers) response = h.getresponse() if response.getheader('set-cookie'): # this is important for some web applications that store authentication-related # info in cookies (it took a long time to figure out) headers['Cookie'] = response.getheader('set-cookie') # some Exchange servers send two WWW-Authenticate headers, one with the NTLM challenge # and another with the 'Negotiate' keyword - make sure we operate on the right one expected_header = self.auth_header_response.lower() for header, value in response.getheaders(): if header.lower() != expected_header: self.logger.debug('Not matched header: %s = %s (!= %s)', header.lower(), value, expected_header) continue match = re.match(r'^(?:{}\s+)?([a-zA-Z].*)$'.format(method), value, re.IGNORECASE) if not match: self.logger.debug('Not matched value: %s = %s (method=%s)', header, value, method) continue payload, = match.groups() self.logger.debug('Found auth header: %s = %s', header, payload) break if more: if not payload: self.logger.error( 'Auth header response not found, Status=%s URL=%s', response.status, url) return None self.logger.debug('Step2: Method: %s, Payload: %s', method, payload) try: more, method, payload = self.create_auth2_message(payload) except AuthenticationError as e: self.logger.error('Step2: Authentication failed (%s)', e) return None if more: self.logger.debug('Step2: Method: %s, Response Payload: %s', method, payload) headers[self.auth_header_request] = ' '.join([method, payload]) try: consume_response_body(response) if sys.version_info.major > 2: selector = req.selector else: selector = req.get_selector() h.request(req.get_method(), selector, req.data, headers) # none of the configured handlers are triggered, for example # redirect-responses are not handled! response = h.getresponse() except socket.error as err: self.logger.exception('') raise URLError(err) else: self.logger.debug('Step2: Method: %s, Continuation not required', method) infourl = make_infourl(response, req) if infourl.code == self.auth_code: self.logger.warning('Authentication failed: URL=%s, CODE=%s', req.get_full_url(), infourl.code) else: self.logger.info('Authentication OK: URL=%s', req.get_full_url()) return infourl