def _escape_to_lower(url): escaped_url = quote_plus(url) def toLowercase(match): return match.group(0).lower() escaped_url = re.sub(r'%..', toLowercase, escaped_url) return escaped_url
def get_headers(params, boundary): """Returns a dictionary with Content-Type and Content-Length headers for the multipart/form-data encoding of ``params``.""" headers = {} boundary = quote_plus(boundary) headers['Content-Type'] = "multipart/form-data; boundary=%s" % boundary headers['Content-Length'] = str(get_body_size(params, boundary)) return headers
def encode_and_quote(data): """If ``data`` is unicode, return quote_plus(data.encode("utf-8")) otherwise return quote_plus(data)""" if data is None: return None if isinstance(data, unicode): data = data.encode("utf-8") return quote_plus(data)
def multipart_encode(params, boundary=None, cb=None): """Encode ``params`` as multipart/form-data. ``params`` should be a sequence of (name, value) pairs or MultipartParam objects, or a mapping of names to values. Values are either strings parameter values, or file-like objects to use as the parameter value. The file-like objects must support .read() and either .fileno() or both .seek() and .tell(). If ``boundary`` is set, then it as used as the MIME boundary. Otherwise a randomly generated boundary will be used. In either case, if the boundary string appears in the parameter values a ValueError will be raised. If ``cb`` is set, it should be a callback which will get called as blocks of data are encoded. It will be called with (param, current, total), indicating the current parameter being encoded, the current amount encoded, and the total amount to encode. Returns a tuple of `datagen`, `headers`, where `datagen` is a generator that will yield blocks of data that make up the encoded parameters, and `headers` is a dictionary with the assoicated Content-Type and Content-Length headers. Examples: >>> datagen, headers = multipart_encode( [("key", "value1"), ("key", "value2")] ) >>> s = "".join(datagen) >>> assert "value2" in s and "value1" in s >>> p = MultipartParam("key", "value2") >>> datagen, headers = multipart_encode( [("key", "value1"), p] ) >>> s = "".join(datagen) >>> assert "value2" in s and "value1" in s >>> datagen, headers = multipart_encode( {"key": "value1"} ) >>> s = "".join(datagen) >>> assert "value2" not in s and "value1" in s """ if boundary is None: boundary = gen_boundary() else: boundary = quote_plus(boundary) headers = get_headers(params, boundary) params = MultipartParam.from_params(params) return multipart_yielder(params, boundary, cb), headers
def _escape_to_lower(url): escaped_url = quote_plus(url) escaped_url = re.sub(r'%..', lambda x: x.group(0).lower(), escaped_url) return escaped_url
def encode_and_quote(data): if data is None: return None return quote_plus(to_bytes(data))