def upload(self, subdir, ext, content_type, data, filename=None): host = str(random.choice(self.clconf("storage", ["storage"]))) tag = self.app().tag id = uuid4().hex if filename is None: url = str("/%s/%s/%s%s/%s/%s.%s" % (subdir, tag[0], tag[0], tag[1], tag, id, ext)) else: url = str("/%s/%s/%s%s/%s/%s-%s" % (subdir, tag[0], tag[0], tag[1], tag, id, filename)) uri = str("//" + host + url) cnn = HTTPConnection() cnn.connect((str(host), 80)) try: request = HTTPRequest() request.method = "PUT" request.path = url request.host = host request.body = data request.add_header("Content-type", content_type) request.add_header("Connection", "close") response = cnn.perform(request) if response.status_code != 201 and response.status_code != 204: raise StaticUploadError(self._("Error storing object {0}: {1}").format(uri, response.status)) finally: cnn.close() return (uri, url, host, id)
def upload_if_large(self, data): if type(data) == unicode: data = data.encode("utf-8") if len(data) < max_inline_chunk_len: return data req = self.req() url = str("/storage/%s%s/%s" % (random.choice(alphabet), random.choice(alphabet), uuid4().hex)) cnn = HTTPConnection() try: cnn.connect((req.host(), 80)) try: request = HTTPRequest() request.method = "PUT" request.path = url request.host = req.host() request.body = data request.add_header("Content-type", "application/octet-stream") request.add_header("Content-length", len(data)) response = cnn.perform(request) if response.status_code != 201: self.error("Error storing object: %s", response.status) # degradation return "" return '<!--# include file="%s" -->' % url finally: cnn.close() except IOError as e: self.exception(e)
def webdav_delete(self, url): "Downloads given URL and returns it" if url is None: return if type(url) == unicode: url = url.encode("utf-8") url_obj = urlparse.urlparse(url, "http", False) if url_obj.scheme != "http": self.error("Scheme '%s' is not supported", url_obj.scheme) elif url_obj.hostname is None: self.error("Empty hostname: %s", url) else: cnn = HTTPConnection() try: with Timeout.push(50): port = url_obj.port if port is None: port = 80 cnn.connect((url_obj.hostname, port)) request = HTTPRequest() request.method = "DELETE" request.path = url_obj.path + url_obj.query request.host = url_obj.hostname request.add_header("Connection", "close") cnn.perform(request) except TimeoutError: self.error("Timeout deleting %s", url) except Exception as e: self.error("Error deleting %s: %s", url, str(e)) finally: try: cnn.close() except Exception: pass
def static_upload_zip(self, subdir, zip, upload_list): host = str(random.choice(self.clconf("storage", ["storage"]))) id = uuid4().hex tag = self.app().tag url = str("/%s/%s/%s%s/%s/%s" % (subdir, tag[0], tag[0], tag[1], tag, id)) uri = str("//" + host + url) for ent in upload_list: cnn = HTTPConnection() cnn.connect((str(host), 80)) try: request = HTTPRequest() request.method = "PUT" request.path = str("%s/%s" % (url, ent["filename"])) request.host = host data = ent.get("data") if data is None and zip: data = zip.read(ent["zipname"]) if data is None and ent.get("path"): with open(ent.get("path")) as f: data = f.read() request.body = data request.add_header("Content-type", str(ent["content-type"])) request.add_header("Connection", "close") response = cnn.perform(request) if response.status_code != 201 and response.status_code != 204: raise StaticUploadError( self._("Error storing object {name}: {err}").format( name=ent["filename"], err=response.status)) finally: cnn.close() return uri
def upload(self, subdir, ext, content_type, data, filename=None): host = str(random.choice(self.clconf("storage", ["storage"]))) tag = self.app().tag id = uuid4().hex if filename is None: url = str("/%s/%s/%s%s/%s/%s.%s" % (subdir, tag[0], tag[0], tag[1], tag, id, ext)) else: url = str("/%s/%s/%s%s/%s/%s-%s" % (subdir, tag[0], tag[0], tag[1], tag, id, filename)) uri = str("//" + host + url) cnn = HTTPConnection() cnn.connect((str(host), 80)) try: request = HTTPRequest() request.method = "PUT" request.path = url request.host = host request.body = data request.add_header("Content-type", content_type) request.add_header("Connection", "close") response = cnn.perform(request) if response.status_code != 201 and response.status_code != 204: raise StaticUploadError( self._("Error storing object {0}: {1}").format( uri, response.status)) finally: cnn.close() return (uri, url, host, id)
def static_upload_zip(self, subdir, zip, upload_list): host = str(random.choice(self.clconf("storage", ["storage"]))) id = uuid4().hex tag = self.app().tag url = str("/%s/%s/%s%s/%s/%s" % (subdir, tag[0], tag[0], tag[1], tag, id)) uri = str("//" + host + url) for ent in upload_list: cnn = HTTPConnection() cnn.connect((str(host), 80)) try: request = HTTPRequest() request.method = "PUT" request.path = str("%s/%s" % (url, ent["filename"])) request.host = host data = ent.get("data") if data is None and zip: data = zip.read(ent["zipname"]) if data is None and ent.get("path"): with open(ent.get("path")) as f: data = f.read() request.body = data request.add_header("Content-type", str(ent["content-type"])) request.add_header("Connection", "close") response = cnn.perform(request) if response.status_code != 201 and response.status_code != 204: raise StaticUploadError(self._("Error storing object {name}: {err}").format(name=ent["filename"], err=response.status)) finally: cnn.close() return uri
def static_put(self, uri, content_type, data): if uri is None: raise StaticUploadError(self._("Invalid store URI")) if type(uri) == unicode: uri = uri.encode("utf-8") uri_obj = urlparse.urlparse(uri, "http", False) if uri_obj.hostname is None: raise StaticUploadError(self._("Empty hostname")) cnn = HTTPConnection() cnn.connect((uri_obj.hostname, 80)) try: request = HTTPRequest() request.method = "PUT" request.path = uri_obj.path request.host = uri_obj.hostname request.body = data request.add_header("Content-type", content_type) request.add_header("Connection", "close") response = cnn.perform(request) if response.status_code != 201 and response.status_code != 204: raise StaticUploadError( self._("Error storing object {0}: {1}").format( uri, response.status)) finally: cnn.close()
def get(self, path, host = None): """Returns a new :class:`HTTPRequest` with request.method = 'GET' and request.path = *path*. request.host will be set to the host used in :func:`connect`, or optionally you can specify a specific *host* just for this request. """ request = HTTPRequest() request.method = 'GET' request.path = path request.host = host or self._host return request
def get(self, path, host=None): """Returns a new :class:`HTTPRequest` with request.method = 'GET' and request.path = *path*. request.host will be set to the host used in :func:`connect`, or optionally you can specify a specific *host* just for this request. """ request = HTTPRequest() request.method = 'GET' request.path = path request.host = host or self._host return request
def post(self, path, body=None, host=None): """Returns a new :class:`HTTPRequest` with request.method = 'POST' and request.path = *path*. request.host will be set to the host used in :func:`connect`, or optionally you can specify a specific *host* just for this request. *body* is an optional string containing the data to post to the server. """ request = HTTPRequest() request.method = 'POST' request.path = path request.host = host or self._host if body is not None: request.body = body return request
def post(self, path, body = None, host = None): """Returns a new :class:`HTTPRequest` with request.method = 'POST' and request.path = *path*. request.host will be set to the host used in :func:`connect`, or optionally you can specify a specific *host* just for this request. *body* is an optional string containing the data to post to the server. """ request = HTTPRequest() request.method = 'POST' request.path = path request.host = host or self._host if body is not None: request.body = body return request
def delete(self): host = str(self.get("host")) url = str(self.get("url")) uri = str(self.get("uri")) cnn = HTTPConnection() cnn.connect((str(host), 80)) try: request = HTTPRequest() request.method = "DELETE" request.path = url request.host = host request.add_header("Connection", "close") cnn.perform(request) except Exception: pass finally: cnn.close()
def static_delete(self, uri): if uri is None: raise StaticUploadError(self._("Invalid delete URI")) if type(uri) == unicode: uri = uri.encode("utf-8") uri_obj = urlparse.urlparse(uri, "http", False) if uri_obj.hostname is None: raise StaticUploadError(self._("Empty hostname")) cnn = HTTPConnection() cnn.connect((uri_obj.hostname, 80)) try: request = HTTPRequest() request.method = "DELETE" request.path = uri_obj.path request.host = uri_obj.hostname request.add_header("Connection", "close") cnn.perform(request) finally: cnn.close()
def static_put(self, uri, content_type, data): if uri is None: raise StaticUploadError(self._("Invalid store URI")) if type(uri) == unicode: uri = uri.encode("utf-8") uri_obj = urlparse.urlparse(uri, "http", False) if uri_obj.hostname is None: raise StaticUploadError(self._("Empty hostname")) cnn = HTTPConnection() cnn.connect((uri_obj.hostname, 80)) try: request = HTTPRequest() request.method = "PUT" request.path = uri_obj.path request.host = uri_obj.hostname request.body = data request.add_header("Content-type", content_type) request.add_header("Connection", "close") response = cnn.perform(request) if response.status_code != 201 and response.status_code != 204: raise StaticUploadError(self._("Error storing object {0}: {1}").format(uri, response.status)) finally: cnn.close()