Package web2py :: Package gluon :: Module http
[hide private]
[frames] | no frames]

Source Code for Module web2py.gluon.http

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  """ 
  5  This file is part of the web2py Web Framework 
  6  Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu> 
  7  License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html) 
  8  """ 
  9   
 10  __all__ = ['HTTP', 'redirect'] 
 11   
 12  defined_status = { 
 13      200: 'OK', 
 14      201: 'CREATED', 
 15      202: 'ACCEPTED', 
 16      203: 'NON-AUTHORITATIVE INFORMATION', 
 17      204: 'NO CONTENT', 
 18      205: 'RESET CONTENT', 
 19      206: 'PARTIAL CONTENT', 
 20      301: 'MOVED PERMANENTLY', 
 21      302: 'FOUND', 
 22      303: 'SEE OTHER', 
 23      304: 'NOT MODIFIED', 
 24      305: 'USE PROXY', 
 25      307: 'TEMPORARY REDIRECT', 
 26      400: 'BAD REQUEST', 
 27      401: 'UNAUTHORIZED', 
 28      403: 'FORBIDDEN', 
 29      404: 'NOT FOUND', 
 30      405: 'METHOD NOT ALLOWED', 
 31      406: 'NOT ACCEPTABLE', 
 32      407: 'PROXY AUTHENTICATION REQUIRED', 
 33      408: 'REQUEST TIMEOUT', 
 34      409: 'CONFLICT', 
 35      410: 'GONE', 
 36      411: 'LENGTH REQUIRED', 
 37      412: 'PRECONDITION FAILED', 
 38      413: 'REQUEST ENTITY TOO LARGE', 
 39      414: 'REQUEST-URI TOO LONG', 
 40      415: 'UNSUPPORTED MEDIA TYPE', 
 41      416: 'REQUESTED RANGE NOT SATISFIABLE', 
 42      417: 'EXPECTATION FAILED', 
 43      500: 'INTERNAL SERVER ERROR', 
 44      501: 'NOT IMPLEMENTED', 
 45      502: 'BAD GATEWAY', 
 46      503: 'SERVICE UNAVAILABLE', 
 47      504: 'GATEWAY TIMEOUT', 
 48      505: 'HTTP VERSION NOT SUPPORTED', 
 49      } 
 50   
 51  # If web2py is executed with python2.4 we need 
 52  # to use Exception instead of BaseException 
 53   
 54  try: 
 55      BaseException 
 56  except NameError: 
 57      BaseException = Exception 
 58   
 59   
60 -class HTTP(BaseException):
61
62 - def __init__( 63 self, 64 status, 65 body='', 66 **headers 67 ):
68 self.status = status 69 self.body = body 70 self.headers = headers
71
72 - def to(self, responder):
73 if self.status in defined_status: 74 status = '%d %s' % (self.status, defined_status[self.status]) 75 else: 76 status = str(self.status) + ' ' 77 if not 'Content-Type' in self.headers: 78 self.headers['Content-Type'] = 'text/html; charset=UTF-8' 79 body = self.body 80 if status[:1] == '4': 81 if not body: 82 body = status 83 if isinstance(body, str): 84 if len(body)<512 and self.headers['Content-Type'].startswith('text/html'): 85 body += '<!-- %s //-->' % ('x'*512) ### trick IE 86 self.headers['Content-Length'] = len(body) 87 headers = [] 88 for (k, v) in self.headers.items(): 89 if isinstance(v, list): 90 for item in v: 91 headers.append((k, str(item))) 92 else: 93 headers.append((k, str(v))) 94 responder(status, headers) 95 if hasattr(body, '__iter__') and not isinstance(self.body, str): 96 return body 97 return [str(body)]
98 99 @property
100 - def message(self):
101 ''' 102 compose a message describing this exception 103 104 "status defined_status [web2py_error]" 105 106 message elements that are not defined are omitted 107 ''' 108 msg = '%(status)d' 109 if self.status in defined_status: 110 msg = '%(status)d %(defined_status)s' 111 if 'web2py_error' in self.headers: 112 msg += ' [%(web2py_error)s]' 113 return msg % dict(status=self.status, 114 defined_status=defined_status.get(self.status), 115 web2py_error=self.headers.get('web2py_error'))
116
117 - def __str__(self):
118 "stringify me" 119 return self.message
120 121
122 -def redirect(location, how=303):
123 location = location.replace('\r', '%0D').replace('\n', '%0A') 124 raise HTTP(how, 125 'You are being redirected <a href="%s">here</a>' % location, 126 Location=location)
127