def test_auth_token(self): headers = {} method = "POST" bucket = test_bucket key = test_key query_args = {'upload': None} add_auth_header(ak, sk, headers, method, bucket, key, query_args) print(headers['Authorization'])
def test_auth_token(self): headers = {} method = "POST" bucket = test_bucket key = test_key query_args = {'upload': None} add_auth_header(ak, sk, headers, method, bucket, key, query_args) print headers['Authorization']
def make_request(server, port, access_key_id, access_key_secret, method, bucket="", key="", query_args=None, headers=None, data="", metadata=None, call_fmt=CallingFormat.PATH, is_secure=False): if not headers: headers = {} #if not query_args: # query_args = {} if not metadata: metadata = {} path = "" if bucket: if call_fmt == CallingFormat.SUBDOMAIN: server = "%s.%s" % (bucket, server) elif call_fmt == CallingFormat.VANITY: server = bucket elif call_fmt == CallingFormat.PATH: path += "/%s" % bucket path += "/%s" % urllib.quote_plus(key.encode("utf-8")) if query_args: #path += "?" + query_args_hash_to_string(query_args) path += "?" + query_args host = "%s:%d" % (server, port) if (is_secure): connection = httplib.HTTPSConnection(host) else: connection = httplib.HTTPConnection(host) final_headers = merge_meta(headers, metadata) if method == "PUT" and "Content-Length" not in final_headers and not data: final_headers["Content-Length"] = "0" if method == "POST" and "Content-Length" not in final_headers and not data: final_headers["Content-Length"] = str(len(data)) add_auth_header(access_key_id, access_key_secret, final_headers, method, bucket, key, query_args) connection.request(method, path, data, final_headers) resp = connection.getresponse() return resp
def make_request(server, port, access_key_id, access_key_secret, method, bucket="", key="", query_args=None, headers=None, data="", metadata=None, call_fmt=CallingFormat.PATH, is_secure=False): if not headers: headers = {} #if not query_args: # query_args = {} if not metadata: metadata = {} path = "" if bucket: if call_fmt == CallingFormat.SUBDOMAIN: server = "%s.%s" % (bucket, server) elif call_fmt == CallingFormat.VANITY: server = bucket elif call_fmt == CallingFormat.PATH: path += "/%s" % bucket #TODO encode_key = urllib.quote_plus(key.encode('utf-8')) if '%20' in encode_key: encode_key = encode_key.replace('%20','+') if '%2A' in encode_key: encode_key = encode_key.replace('%2A','*') if '%7E' in encode_key: encode_key = encode_key.replace('%7E','~') if '%2F' in encode_key: encode_key = encode_key.replace('%2F','/') path += "/%s" % encode_key if query_args: #path += "?" + query_args_hash_to_string(query_args) path += "?" + query_args host = "%s:%d" % (server, port) if (is_secure): connection = httplib.HTTPSConnection(host) else: connection = httplib.HTTPConnection(host) final_headers = merge_meta(headers, metadata) if method == "PUT" and "Content-Length" not in final_headers and not data: final_headers["Content-Length"] = "0" if method.upper() == "POST" and "Content-Length" not in final_headers and not data: final_headers["Content-Length"] = str(len(data)) add_auth_header(access_key_id, access_key_secret, final_headers, method, bucket, key, query_args) connection.request(method, path, data, final_headers) resp = connection.getresponse() if resp.status >= 300 and resp.status < 400 and 'location' == query_args: loc = resp.getheader('location') if loc: reg = re.findall('http[s]{0,1}://(.*?)(:\d+){0,1}/', loc) if reg: new_server = reg[0][0] return make_request(new_server, port, access_key_id, access_key_secret, method, bucket, key, query_args, headers, data, metadata, call_fmt, is_secure) return resp
def make_request(server, port, access_key_id, access_key_secret, method, bucket="", key="", query_args=None, headers=None, data="", metadata=None, call_fmt=CallingFormat.PATH, is_secure=False, domain_mode=False, need_auth_header=True, timeout=10): if not headers: headers = {} #if not query_args: # query_args = {} if not metadata: metadata = {} path = "" if bucket and not domain_mode: if call_fmt == CallingFormat.SUBDOMAIN: server = "%s.%s" % (bucket, server) elif call_fmt == CallingFormat.VANITY: server = bucket elif call_fmt == CallingFormat.PATH: path += "/%s" % bucket #TODO encode_key = parse.quote_plus(key.encode('utf-8')) if '%20' in encode_key: encode_key = encode_key.replace('%20', '+') if '%2A' in encode_key: encode_key = encode_key.replace('%2A', '*') if '%7E' in encode_key: encode_key = encode_key.replace('%7E', '~') if '%2F' in encode_key: encode_key = encode_key.replace('%2F', '/') path += "/%s" % encode_key path = path.replace('//', '/%2F') if query_args: if isinstance(query_args, dict): path += "?" + query_args_hash_to_string(query_args) else: path += "?" + query_args host = "%s:%d" % (server, port) if (is_secure): connection = httpcli.HTTPSConnection(host) else: connection = httpcli.HTTPConnection(host) connection.timeout = timeout final_headers = merge_meta(headers, metadata) if method == "PUT" and "Content-Length" not in final_headers and not data: final_headers["Content-Length"] = "0" if method.upper( ) == "POST" and "Content-Length" not in final_headers and not data: final_headers["Content-Length"] = str(len(data)) if need_auth_header: add_auth_header(access_key_id, access_key_secret, final_headers, method, bucket, key, query_args) connection.request(method, path, data, final_headers) resp = connection.getresponse() if resp.status >= 300 and resp.status < 400: loc = resp.getheader('location') if loc: reg = re.findall('http[s]?://(.*?)(:\d+)?/', loc) if reg: new_server = reg[0][0] loc_parse = urlparse.urlparse(loc) if 'Signature' in loc_parse.query: connection_temp = httpcli.HTTPSConnection(new_server) connection_temp.request( 'GET', loc_parse.path + '?' + loc_parse.query) try: resp_temp = connection_temp.getresponse() return resp_temp except Exception as err: print(str(err)) else: return make_request(new_server, port, access_key_id, access_key_secret, method, bucket, key, query_args, headers, data, metadata, call_fmt, is_secure, need_auth_header=False) return resp