Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
urllib.request.HTTPRedirectHandler Class Reference
Inheritance diagram for urllib.request.HTTPRedirectHandler:
Inheritance graph
[legend]
Collaboration diagram for urllib.request.HTTPRedirectHandler:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def redirect_request
def http_error_302
def add_parent
def close
def __lt__

Public Attributes

 parent

Static Public Attributes

int max_repeats = 4
int max_redirections = 10
 http_error_301 = http_error_303http_error_307http_error_302
string inf_msg = "The HTTP server returned a redirect error that would "
int handler_order = 500

Detailed Description

Definition at line 497 of file request.py.


Member Function Documentation

def urllib.request.BaseHandler.__lt__ (   self,
  other 
) [inherited]

Definition at line 467 of file request.py.

00467 
00468     def __lt__(self, other):
00469         if not hasattr(other, "handler_order"):
00470             # Try to preserve the old behavior of having custom classes
00471             # inserted after default ones (works only for custom user
00472             # classes which are not aware of handler_order).
00473             return True
00474         return self.handler_order < other.handler_order
00475 

def urllib.request.BaseHandler.add_parent (   self,
  parent 
) [inherited]

Definition at line 460 of file request.py.

00460 
00461     def add_parent(self, parent):
00462         self.parent = parent

def urllib.request.BaseHandler.close (   self) [inherited]

Definition at line 463 of file request.py.

00463 
00464     def close(self):
00465         # Only exists for backwards compatibility
00466         pass

Here is the caller graph for this function:

def urllib.request.HTTPRedirectHandler.http_error_302 (   self,
  req,
  fp,
  code,
  msg,
  headers 
)

Definition at line 539 of file request.py.

00539 
00540     def http_error_302(self, req, fp, code, msg, headers):
00541         # Some servers (incorrectly) return multiple Location headers
00542         # (so probably same goes for URI).  Use first header.
00543         if "location" in headers:
00544             newurl = headers["location"]
00545         elif "uri" in headers:
00546             newurl = headers["uri"]
00547         else:
00548             return
00549 
00550         # fix a possible malformed URL
00551         urlparts = urlparse(newurl)
00552 
00553         # For security reasons we don't allow redirection to anything other
00554         # than http, https or ftp.
00555 
00556         if not urlparts.scheme in ('http', 'https', 'ftp'):
00557             raise HTTPError(newurl, code,
00558                             msg +
00559                             " - Redirection to url '%s' is not allowed" %
00560                             newurl,
00561                             headers, fp)
00562 
00563         if not urlparts.path:
00564             urlparts = list(urlparts)
00565             urlparts[2] = "/"
00566         newurl = urlunparse(urlparts)
00567 
00568         newurl = urljoin(req.full_url, newurl)
00569 
00570         # XXX Probably want to forget about the state of the current
00571         # request, although that might interact poorly with other
00572         # handlers that also use handler-specific request attributes
00573         new = self.redirect_request(req, fp, code, msg, headers, newurl)
00574         if new is None:
00575             return
00576 
00577         # loop detection
00578         # .redirect_dict has a key url if url was previously visited.
00579         if hasattr(req, 'redirect_dict'):
00580             visited = new.redirect_dict = req.redirect_dict
00581             if (visited.get(newurl, 0) >= self.max_repeats or
00582                 len(visited) >= self.max_redirections):
00583                 raise HTTPError(req.full_url, code,
00584                                 self.inf_msg + msg, headers, fp)
00585         else:
00586             visited = new.redirect_dict = req.redirect_dict = {}
00587         visited[newurl] = visited.get(newurl, 0) + 1
00588 
00589         # Don't close the fp until we are sure that we won't use it
00590         # with HTTPError.
00591         fp.read()
00592         fp.close()
00593 
00594         return self.parent.open(new, timeout=req.timeout)

Here is the call graph for this function:

Here is the caller graph for this function:

def urllib.request.HTTPRedirectHandler.redirect_request (   self,
  req,
  fp,
  code,
  msg,
  headers,
  newurl 
)
Return a Request or None in response to a redirect.

This is called by the http_error_30x methods when a
redirection response is received.  If a redirection should
take place, return a new Request to allow http_error_30x to
perform the redirect.  Otherwise, raise HTTPError if no-one
else should try to handle this url.  Return None if you can't
but another Handler might.

Definition at line 505 of file request.py.

00505 
00506     def redirect_request(self, req, fp, code, msg, headers, newurl):
00507         """Return a Request or None in response to a redirect.
00508 
00509         This is called by the http_error_30x methods when a
00510         redirection response is received.  If a redirection should
00511         take place, return a new Request to allow http_error_30x to
00512         perform the redirect.  Otherwise, raise HTTPError if no-one
00513         else should try to handle this url.  Return None if you can't
00514         but another Handler might.
00515         """
00516         m = req.get_method()
00517         if (not (code in (301, 302, 303, 307) and m in ("GET", "HEAD")
00518             or code in (301, 302, 303) and m == "POST")):
00519             raise HTTPError(req.full_url, code, msg, headers, fp)
00520 
00521         # Strictly (according to RFC 2616), 301 or 302 in response to
00522         # a POST MUST NOT cause a redirection without confirmation
00523         # from the user (of urllib.request, in this case).  In practice,
00524         # essentially all clients do redirect in this case, so we do
00525         # the same.
00526         # be conciliant with URIs containing a space
00527         newurl = newurl.replace(' ', '%20')
00528         CONTENT_HEADERS = ("content-length", "content-type")
00529         newheaders = dict((k, v) for k, v in req.headers.items()
00530                           if k.lower() not in CONTENT_HEADERS)
00531         return Request(newurl,
00532                        headers=newheaders,
00533                        origin_req_host=req.origin_req_host,
00534                        unverifiable=True)

Here is the caller graph for this function:


Member Data Documentation

int urllib.request.BaseHandler.handler_order = 500 [static, inherited]
urllib.request.HTTPRedirectHandler.http_error_301 = http_error_303http_error_307http_error_302 [static]

Definition at line 595 of file request.py.

string urllib.request.HTTPRedirectHandler.inf_msg = "The HTTP server returned a redirect error that would " [static]

Definition at line 597 of file request.py.

Definition at line 503 of file request.py.

Definition at line 500 of file request.py.

Definition at line 461 of file request.py.


The documentation for this class was generated from the following file: