PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/django/http/__init__.py

https://bitbucket.org/rattray/anonbox
Python | 691 lines | 646 code | 16 blank | 29 comment | 24 complexity | 0a841dc3a79e588ca7dda8fa3f825ceb MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. import datetime
  2. import os
  3. import re
  4. import time
  5. from pprint import pformat
  6. from urllib import urlencode, quote
  7. from urlparse import urljoin
  8. try:
  9. from cStringIO import StringIO
  10. except ImportError:
  11. from StringIO import StringIO
  12. try:
  13. # The mod_python version is more efficient, so try importing it first.
  14. from mod_python.util import parse_qsl
  15. except ImportError:
  16. try:
  17. # Python 2.6 and greater
  18. from urlparse import parse_qsl
  19. except ImportError:
  20. # Python 2.5, 2.4. Works on Python 2.6 but raises
  21. # PendingDeprecationWarning
  22. from cgi import parse_qsl
  23. import Cookie
  24. # httponly support exists in Python 2.6's Cookie library,
  25. # but not in Python 2.4 or 2.5.
  26. _morsel_supports_httponly = Cookie.Morsel._reserved.has_key('httponly')
  27. # Some versions of Python 2.7 and later won't need this encoding bug fix:
  28. _cookie_encodes_correctly = Cookie.SimpleCookie().value_encode(';') == (';', '"\\073"')
  29. # See ticket #13007, http://bugs.python.org/issue2193 and http://trac.edgewall.org/ticket/2256
  30. _tc = Cookie.SimpleCookie()
  31. _tc.load('f:oo')
  32. _cookie_allows_colon_in_names = 'Set-Cookie: f:oo=' in _tc.output()
  33. if _morsel_supports_httponly and _cookie_encodes_correctly and _cookie_allows_colon_in_names:
  34. SimpleCookie = Cookie.SimpleCookie
  35. else:
  36. if not _morsel_supports_httponly:
  37. class Morsel(Cookie.Morsel):
  38. def __setitem__(self, K, V):
  39. K = K.lower()
  40. if K == "httponly":
  41. if V:
  42. # The superclass rejects httponly as a key,
  43. # so we jump to the grandparent.
  44. super(Cookie.Morsel, self).__setitem__(K, V)
  45. else:
  46. super(Morsel, self).__setitem__(K, V)
  47. def OutputString(self, attrs=None):
  48. output = super(Morsel, self).OutputString(attrs)
  49. if "httponly" in self:
  50. output += "; httponly"
  51. return output
  52. class SimpleCookie(Cookie.SimpleCookie):
  53. if not _morsel_supports_httponly:
  54. def __set(self, key, real_value, coded_value):
  55. M = self.get(key, Morsel())
  56. M.set(key, real_value, coded_value)
  57. dict.__setitem__(self, key, M)
  58. def __setitem__(self, key, value):
  59. rval, cval = self.value_encode(value)
  60. self.__set(key, rval, cval)
  61. if not _cookie_encodes_correctly:
  62. def value_encode(self, val):
  63. # Some browsers do not support quoted-string from RFC 2109,
  64. # including some versions of Safari and Internet Explorer.
  65. # These browsers split on ';', and some versions of Safari
  66. # are known to split on ', '. Therefore, we encode ';' and ','
  67. # SimpleCookie already does the hard work of encoding and decoding.
  68. # It uses octal sequences like '\\012' for newline etc.
  69. # and non-ASCII chars. We just make use of this mechanism, to
  70. # avoid introducing two encoding schemes which would be confusing
  71. # and especially awkward for javascript.
  72. # NB, contrary to Python docs, value_encode returns a tuple containing
  73. # (real val, encoded_val)
  74. val, encoded = super(SimpleCookie, self).value_encode(val)
  75. encoded = encoded.replace(";", "\\073").replace(",","\\054")
  76. # If encoded now contains any quoted chars, we need double quotes
  77. # around the whole string.
  78. if "\\" in encoded and not encoded.startswith('"'):
  79. encoded = '"' + encoded + '"'
  80. return val, encoded
  81. if not _cookie_allows_colon_in_names:
  82. def load(self, rawdata, ignore_parse_errors=False):
  83. if ignore_parse_errors:
  84. self.bad_cookies = []
  85. self._BaseCookie__set = self._loose_set
  86. super(SimpleCookie, self).load(rawdata)
  87. if ignore_parse_errors:
  88. self._BaseCookie__set = self._strict_set
  89. for key in self.bad_cookies:
  90. del self[key]
  91. _strict_set = Cookie.BaseCookie._BaseCookie__set
  92. def _loose_set(self, key, real_value, coded_value):
  93. try:
  94. self._strict_set(key, real_value, coded_value)
  95. except Cookie.CookieError:
  96. self.bad_cookies.append(key)
  97. dict.__setitem__(self, key, None)
  98. class CompatCookie(SimpleCookie):
  99. def __init__(self, *args, **kwargs):
  100. super(CompatCookie, self).__init__(*args, **kwargs)
  101. import warnings
  102. warnings.warn("CompatCookie is deprecated, use django.http.SimpleCookie instead.",
  103. PendingDeprecationWarning)
  104. from django.utils.datastructures import MultiValueDict, ImmutableList
  105. from django.utils.encoding import smart_str, iri_to_uri, force_unicode
  106. from django.utils.http import cookie_date
  107. from django.http.multipartparser import MultiPartParser
  108. from django.conf import settings
  109. from django.core.files import uploadhandler
  110. from utils import *
  111. RESERVED_CHARS="!*'();:@&=+$,/?%#[]"
  112. absolute_http_url_re = re.compile(r"^https?://", re.I)
  113. class Http404(Exception):
  114. pass
  115. class HttpRequest(object):
  116. """A basic HTTP request."""
  117. # The encoding used in GET/POST dicts. None means use default setting.
  118. _encoding = None
  119. _upload_handlers = []
  120. def __init__(self):
  121. self.GET, self.POST, self.COOKIES, self.META, self.FILES = {}, {}, {}, {}, {}
  122. self.path = ''
  123. self.path_info = ''
  124. self.method = None
  125. def __repr__(self):
  126. return '<HttpRequest\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % \
  127. (pformat(self.GET), pformat(self.POST), pformat(self.COOKIES),
  128. pformat(self.META))
  129. def get_host(self):
  130. """Returns the HTTP host using the environment or request headers."""
  131. # We try three options, in order of decreasing preference.
  132. if 'HTTP_X_FORWARDED_HOST' in self.META:
  133. host = self.META['HTTP_X_FORWARDED_HOST']
  134. elif 'HTTP_HOST' in self.META:
  135. host = self.META['HTTP_HOST']
  136. else:
  137. # Reconstruct the host using the algorithm from PEP 333.
  138. host = self.META['SERVER_NAME']
  139. server_port = str(self.META['SERVER_PORT'])
  140. if server_port != (self.is_secure() and '443' or '80'):
  141. host = '%s:%s' % (host, server_port)
  142. return host
  143. def get_full_path(self):
  144. # RFC 3986 requires query string arguments to be in the ASCII range.
  145. # Rather than crash if this doesn't happen, we encode defensively.
  146. return '%s%s' % (self.path, self.META.get('QUERY_STRING', '') and ('?' + iri_to_uri(self.META.get('QUERY_STRING', ''))) or '')
  147. def build_absolute_uri(self, location=None):
  148. """
  149. Builds an absolute URI from the location and the variables available in
  150. this request. If no location is specified, the absolute URI is built on
  151. ``request.get_full_path()``.
  152. """
  153. if not location:
  154. location = self.get_full_path()
  155. if not absolute_http_url_re.match(location):
  156. current_uri = '%s://%s%s' % (self.is_secure() and 'https' or 'http',
  157. self.get_host(), self.path)
  158. location = urljoin(current_uri, location)
  159. return iri_to_uri(location)
  160. def is_secure(self):
  161. return os.environ.get("HTTPS") == "on"
  162. def is_ajax(self):
  163. return self.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
  164. def _set_encoding(self, val):
  165. """
  166. Sets the encoding used for GET/POST accesses. If the GET or POST
  167. dictionary has already been created, it is removed and recreated on the
  168. next access (so that it is decoded correctly).
  169. """
  170. self._encoding = val
  171. if hasattr(self, '_get'):
  172. del self._get
  173. if hasattr(self, '_post'):
  174. del self._post
  175. def _get_encoding(self):
  176. return self._encoding
  177. encoding = property(_get_encoding, _set_encoding)
  178. def _initialize_handlers(self):
  179. self._upload_handlers = [uploadhandler.load_handler(handler, self)
  180. for handler in settings.FILE_UPLOAD_HANDLERS]
  181. def _set_upload_handlers(self, upload_handlers):
  182. if hasattr(self, '_files'):
  183. raise AttributeError("You cannot set the upload handlers after the upload has been processed.")
  184. self._upload_handlers = upload_handlers
  185. def _get_upload_handlers(self):
  186. if not self._upload_handlers:
  187. # If thre are no upload handlers defined, initialize them from settings.
  188. self._initialize_handlers()
  189. return self._upload_handlers
  190. upload_handlers = property(_get_upload_handlers, _set_upload_handlers)
  191. def parse_file_upload(self, META, post_data):
  192. """Returns a tuple of (POST QueryDict, FILES MultiValueDict)."""
  193. self.upload_handlers = ImmutableList(
  194. self.upload_handlers,
  195. warning = "You cannot alter upload handlers after the upload has been processed."
  196. )
  197. parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)
  198. return parser.parse()
  199. def _get_raw_post_data(self):
  200. if not hasattr(self, '_raw_post_data'):
  201. if self._read_started:
  202. raise Exception("You cannot access raw_post_data after reading from request's data stream")
  203. try:
  204. content_length = int(self.META.get('CONTENT_LENGTH', 0))
  205. except (ValueError, TypeError):
  206. # If CONTENT_LENGTH was empty string or not an integer, don't
  207. # error out. We've also seen None passed in here (against all
  208. # specs, but see ticket #8259), so we handle TypeError as well.
  209. content_length = 0
  210. if content_length:
  211. self._raw_post_data = self.read(content_length)
  212. else:
  213. self._raw_post_data = self.read()
  214. self._stream = StringIO(self._raw_post_data)
  215. return self._raw_post_data
  216. raw_post_data = property(_get_raw_post_data)
  217. def _mark_post_parse_error(self):
  218. self._post = QueryDict('')
  219. self._files = MultiValueDict()
  220. self._post_parse_error = True
  221. def _load_post_and_files(self):
  222. # Populates self._post and self._files
  223. if self.method != 'POST':
  224. self._post, self._files = QueryDict('', encoding=self._encoding), MultiValueDict()
  225. return
  226. if self._read_started:
  227. self._mark_post_parse_error()
  228. return
  229. if self.META.get('CONTENT_TYPE', '').startswith('multipart'):
  230. self._raw_post_data = ''
  231. try:
  232. self._post, self._files = self.parse_file_upload(self.META, self)
  233. except:
  234. # An error occured while parsing POST data. Since when
  235. # formatting the error the request handler might access
  236. # self.POST, set self._post and self._file to prevent
  237. # attempts to parse POST data again.
  238. # Mark that an error occured. This allows self.__repr__ to
  239. # be explicit about it instead of simply representing an
  240. # empty POST
  241. self._mark_post_parse_error()
  242. raise
  243. else:
  244. self._post, self._files = QueryDict(self.raw_post_data, encoding=self._encoding), MultiValueDict()
  245. ## File-like and iterator interface.
  246. ##
  247. ## Expects self._stream to be set to an appropriate source of bytes by
  248. ## a corresponding request subclass (WSGIRequest or ModPythonRequest).
  249. ## Also when request data has already been read by request.POST or
  250. ## request.raw_post_data, self._stream points to a StringIO instance
  251. ## containing that data.
  252. def read(self, *args, **kwargs):
  253. self._read_started = True
  254. return self._stream.read(*args, **kwargs)
  255. def readline(self, *args, **kwargs):
  256. self._read_started = True
  257. return self._stream.readline(*args, **kwargs)
  258. def xreadlines(self):
  259. while True:
  260. buf = self.readline()
  261. if not buf:
  262. break
  263. yield buf
  264. __iter__ = xreadlines
  265. def readlines(self):
  266. return list(iter(self))
  267. class QueryDict(MultiValueDict):
  268. """
  269. A specialized MultiValueDict that takes a query string when initialized.
  270. This is immutable unless you create a copy of it.
  271. Values retrieved from this class are converted from the given encoding
  272. (DEFAULT_CHARSET by default) to unicode.
  273. """
  274. # These are both reset in __init__, but is specified here at the class
  275. # level so that unpickling will have valid values
  276. _mutable = True
  277. _encoding = None
  278. def __init__(self, query_string, mutable=False, encoding=None):
  279. MultiValueDict.__init__(self)
  280. if not encoding:
  281. # *Important*: do not import settings any earlier because of note
  282. # in core.handlers.modpython.
  283. from django.conf import settings
  284. encoding = settings.DEFAULT_CHARSET
  285. self.encoding = encoding
  286. for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True
  287. self.appendlist(force_unicode(key, encoding, errors='replace'),
  288. force_unicode(value, encoding, errors='replace'))
  289. self._mutable = mutable
  290. def _get_encoding(self):
  291. if self._encoding is None:
  292. # *Important*: do not import settings at the module level because
  293. # of the note in core.handlers.modpython.
  294. from django.conf import settings
  295. self._encoding = settings.DEFAULT_CHARSET
  296. return self._encoding
  297. def _set_encoding(self, value):
  298. self._encoding = value
  299. encoding = property(_get_encoding, _set_encoding)
  300. def _assert_mutable(self):
  301. if not self._mutable:
  302. raise AttributeError("This QueryDict instance is immutable")
  303. def __setitem__(self, key, value):
  304. self._assert_mutable()
  305. key = str_to_unicode(key, self.encoding)
  306. value = str_to_unicode(value, self.encoding)
  307. MultiValueDict.__setitem__(self, key, value)
  308. def __delitem__(self, key):
  309. self._assert_mutable()
  310. super(QueryDict, self).__delitem__(key)
  311. def __copy__(self):
  312. result = self.__class__('', mutable=True, encoding=self.encoding)
  313. for key, value in dict.items(self):
  314. dict.__setitem__(result, key, value)
  315. return result
  316. def __deepcopy__(self, memo):
  317. import django.utils.copycompat as copy
  318. result = self.__class__('', mutable=True, encoding=self.encoding)
  319. memo[id(self)] = result
  320. for key, value in dict.items(self):
  321. dict.__setitem__(result, copy.deepcopy(key, memo), copy.deepcopy(value, memo))
  322. return result
  323. def setlist(self, key, list_):
  324. self._assert_mutable()
  325. key = str_to_unicode(key, self.encoding)
  326. list_ = [str_to_unicode(elt, self.encoding) for elt in list_]
  327. MultiValueDict.setlist(self, key, list_)
  328. def setlistdefault(self, key, default_list=()):
  329. self._assert_mutable()
  330. if key not in self:
  331. self.setlist(key, default_list)
  332. return MultiValueDict.getlist(self, key)
  333. def appendlist(self, key, value):
  334. self._assert_mutable()
  335. key = str_to_unicode(key, self.encoding)
  336. value = str_to_unicode(value, self.encoding)
  337. MultiValueDict.appendlist(self, key, value)
  338. def update(self, other_dict):
  339. self._assert_mutable()
  340. f = lambda s: str_to_unicode(s, self.encoding)
  341. if hasattr(other_dict, 'lists'):
  342. for key, valuelist in other_dict.lists():
  343. for value in valuelist:
  344. MultiValueDict.update(self, {f(key): f(value)})
  345. else:
  346. d = dict([(f(k), f(v)) for k, v in other_dict.items()])
  347. MultiValueDict.update(self, d)
  348. def pop(self, key, *args):
  349. self._assert_mutable()
  350. return MultiValueDict.pop(self, key, *args)
  351. def popitem(self):
  352. self._assert_mutable()
  353. return MultiValueDict.popitem(self)
  354. def clear(self):
  355. self._assert_mutable()
  356. MultiValueDict.clear(self)
  357. def setdefault(self, key, default=None):
  358. self._assert_mutable()
  359. key = str_to_unicode(key, self.encoding)
  360. default = str_to_unicode(default, self.encoding)
  361. return MultiValueDict.setdefault(self, key, default)
  362. def copy(self):
  363. """Returns a mutable copy of this object."""
  364. return self.__deepcopy__({})
  365. def urlencode(self, safe=None):
  366. """
  367. Returns an encoded string of all query string arguments.
  368. :arg safe: Used to specify characters which do not require quoting, for
  369. example::
  370. >>> q = QueryDict('', mutable=True)
  371. >>> q['next'] = '/a&b/'
  372. >>> q.urlencode()
  373. 'next=%2Fa%26b%2F'
  374. >>> q.urlencode(safe='/')
  375. 'next=/a%26b/'
  376. """
  377. output = []
  378. if safe:
  379. encode = lambda k, v: '%s=%s' % ((quote(k, safe), quote(v, safe)))
  380. else:
  381. encode = lambda k, v: urlencode({k: v})
  382. for k, list_ in self.lists():
  383. k = smart_str(k, self.encoding)
  384. output.extend([encode(k, smart_str(v, self.encoding))
  385. for v in list_])
  386. return '&'.join(output)
  387. def parse_cookie(cookie):
  388. if cookie == '':
  389. return {}
  390. if not isinstance(cookie, Cookie.BaseCookie):
  391. try:
  392. c = SimpleCookie()
  393. c.load(cookie, ignore_parse_errors=True)
  394. except Cookie.CookieError:
  395. # Invalid cookie
  396. return {}
  397. else:
  398. c = cookie
  399. cookiedict = {}
  400. for key in c.keys():
  401. cookiedict[key] = c.get(key).value
  402. return cookiedict
  403. class BadHeaderError(ValueError):
  404. pass
  405. class HttpResponse(object):
  406. """A basic HTTP response, with content and dictionary-accessed headers."""
  407. status_code = 200
  408. def __init__(self, content='', mimetype=None, status=None,
  409. content_type=None):
  410. # _headers is a mapping of the lower-case name to the original case of
  411. # the header (required for working with legacy systems) and the header
  412. # value. Both the name of the header and its value are ASCII strings.
  413. self._headers = {}
  414. self._charset = settings.DEFAULT_CHARSET
  415. if mimetype:
  416. content_type = mimetype # For backwards compatibility
  417. if not content_type:
  418. content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,
  419. self._charset)
  420. if not isinstance(content, basestring) and hasattr(content, '__iter__'):
  421. self._container = content
  422. self._is_string = False
  423. else:
  424. self._container = [content]
  425. self._is_string = True
  426. self.cookies = SimpleCookie()
  427. if status:
  428. self.status_code = status
  429. self['Content-Type'] = content_type
  430. def __str__(self):
  431. """Full HTTP message, including headers."""
  432. return '\n'.join(['%s: %s' % (key, value)
  433. for key, value in self._headers.values()]) \
  434. + '\n\n' + self.content
  435. def _convert_to_ascii(self, *values):
  436. """Converts all values to ascii strings."""
  437. for value in values:
  438. if isinstance(value, unicode):
  439. try:
  440. value = value.encode('us-ascii')
  441. except UnicodeError, e:
  442. e.reason += ', HTTP response headers must be in US-ASCII format'
  443. raise
  444. else:
  445. value = str(value)
  446. if '\n' in value or '\r' in value:
  447. raise BadHeaderError("Header values can't contain newlines (got %r)" % (value))
  448. yield value
  449. def __setitem__(self, header, value):
  450. header, value = self._convert_to_ascii(header, value)
  451. self._headers[header.lower()] = (header, value)
  452. def __delitem__(self, header):
  453. try:
  454. del self._headers[header.lower()]
  455. except KeyError:
  456. pass
  457. def __getitem__(self, header):
  458. return self._headers[header.lower()][1]
  459. def has_header(self, header):
  460. """Case-insensitive check for a header."""
  461. return self._headers.has_key(header.lower())
  462. __contains__ = has_header
  463. def items(self):
  464. return self._headers.values()
  465. def get(self, header, alternate):
  466. return self._headers.get(header.lower(), (None, alternate))[1]
  467. def set_cookie(self, key, value='', max_age=None, expires=None, path='/',
  468. domain=None, secure=False, httponly=False):
  469. """
  470. Sets a cookie.
  471. ``expires`` can be a string in the correct format or a
  472. ``datetime.datetime`` object in UTC. If ``expires`` is a datetime
  473. object then ``max_age`` will be calculated.
  474. """
  475. self.cookies[key] = value
  476. if expires is not None:
  477. if isinstance(expires, datetime.datetime):
  478. delta = expires - expires.utcnow()
  479. # Add one second so the date matches exactly (a fraction of
  480. # time gets lost between converting to a timedelta and
  481. # then the date string).
  482. delta = delta + datetime.timedelta(seconds=1)
  483. # Just set max_age - the max_age logic will set expires.
  484. expires = None
  485. max_age = max(0, delta.days * 86400 + delta.seconds)
  486. else:
  487. self.cookies[key]['expires'] = expires
  488. if max_age is not None:
  489. self.cookies[key]['max-age'] = max_age
  490. # IE requires expires, so set it if hasn't been already.
  491. if not expires:
  492. self.cookies[key]['expires'] = cookie_date(time.time() +
  493. max_age)
  494. if path is not None:
  495. self.cookies[key]['path'] = path
  496. if domain is not None:
  497. self.cookies[key]['domain'] = domain
  498. if secure:
  499. self.cookies[key]['secure'] = True
  500. if httponly:
  501. self.cookies[key]['httponly'] = True
  502. def delete_cookie(self, key, path='/', domain=None):
  503. self.set_cookie(key, max_age=0, path=path, domain=domain,
  504. expires='Thu, 01-Jan-1970 00:00:00 GMT')
  505. def _get_content(self):
  506. if self.has_header('Content-Encoding'):
  507. return ''.join(self._container)
  508. return smart_str(''.join(self._container), self._charset)
  509. def _set_content(self, value):
  510. self._container = [value]
  511. self._is_string = True
  512. content = property(_get_content, _set_content)
  513. def __iter__(self):
  514. self._iterator = iter(self._container)
  515. return self
  516. def next(self):
  517. chunk = self._iterator.next()
  518. if isinstance(chunk, unicode):
  519. chunk = chunk.encode(self._charset)
  520. return str(chunk)
  521. def close(self):
  522. if hasattr(self._container, 'close'):
  523. self._container.close()
  524. # The remaining methods partially implement the file-like object interface.
  525. # See http://docs.python.org/lib/bltin-file-objects.html
  526. def write(self, content):
  527. if not self._is_string:
  528. raise Exception("This %s instance is not writable" % self.__class__)
  529. self._container.append(content)
  530. def flush(self):
  531. pass
  532. def tell(self):
  533. if not self._is_string:
  534. raise Exception("This %s instance cannot tell its position" % self.__class__)
  535. return sum([len(chunk) for chunk in self._container])
  536. class HttpResponseRedirect(HttpResponse):
  537. status_code = 302
  538. def __init__(self, redirect_to):
  539. super(HttpResponseRedirect, self).__init__()
  540. self['Location'] = iri_to_uri(redirect_to)
  541. class HttpResponsePermanentRedirect(HttpResponse):
  542. status_code = 301
  543. def __init__(self, redirect_to):
  544. super(HttpResponsePermanentRedirect, self).__init__()
  545. self['Location'] = iri_to_uri(redirect_to)
  546. class HttpResponseNotModified(HttpResponse):
  547. status_code = 304
  548. class HttpResponseBadRequest(HttpResponse):
  549. status_code = 400
  550. class HttpResponseNotFound(HttpResponse):
  551. status_code = 404
  552. class HttpResponseForbidden(HttpResponse):
  553. status_code = 403
  554. class HttpResponseNotAllowed(HttpResponse):
  555. status_code = 405
  556. def __init__(self, permitted_methods):
  557. super(HttpResponseNotAllowed, self).__init__()
  558. self['Allow'] = ', '.join(permitted_methods)
  559. class HttpResponseGone(HttpResponse):
  560. status_code = 410
  561. class HttpResponseServerError(HttpResponse):
  562. status_code = 500
  563. # A backwards compatible alias for HttpRequest.get_host.
  564. def get_host(request):
  565. return request.get_host()
  566. # It's neither necessary nor appropriate to use
  567. # django.utils.encoding.smart_unicode for parsing URLs and form inputs. Thus,
  568. # this slightly more restricted function.
  569. def str_to_unicode(s, encoding):
  570. """
  571. Converts basestring objects to unicode, using the given encoding. Illegally
  572. encoded input characters are replaced with Unicode "unknown" codepoint
  573. (\ufffd).
  574. Returns any non-basestring objects without change.
  575. """
  576. if isinstance(s, str):
  577. return unicode(s, encoding, 'replace')
  578. else:
  579. return s