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

Source Code for Module gluon.http

 1  """ 
 2  This file is part of web2py Web Framework (Copyrighted, 2007) 
 3  Developed by Massimo Di Pierro <mdipierro@cs.depaul.edu> 
 4  License: GPL v2 
 5  """ 
 6   
 7  __all__=['HTTP','redirect'] 
 8   
 9  defined_status = { 
10      # Successful 
11      200: 'OK', 
12      201: 'CREATED', 
13      202: 'ACCEPTED', 
14      203: 'NON-AUTHORITATIVE INFORMATION', 
15      204: 'NO CONTENT', 
16      205: 'RESET CONTENT', 
17      206: 'PARTIAL CONTENT', 
18      # Redirection 
19      301: 'MOVED PERMANENTLY', 
20      302: 'FOUND', 
21      303: 'SEE OTHER', 
22      304: 'NOT MODIFIED', 
23      305: 'USE PROXY', 
24      307: 'TEMPORARY REDIRECT', 
25      # Client error 
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: 'LENGHT 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      } 
44   
45 -class HTTP:
46 - def __init__(self,status,body='',**headers):
47 if status in defined_status: 48 self.status = "%d %s" % (status, defined_status[status]) 49 else: 50 self.status=str(status)+' ' 51 self.body=body 52 if not headers.has_key('Content-Type'): 53 headers['Content-Type']='text/html' 54 self.headers=headers
55 - def to(self,responder):
56 headers=[] 57 for k,v in self.headers.items(): 58 if isinstance(v,list): 59 for item in v: headers.append((k,str(item))) 60 else: headers.append((k,str(v))) 61 responder(self.status,headers) 62 if hasattr(self.body,'__iter__') and not isinstance(self.body,str): 63 return self.body 64 body=str(self.body) 65 self.headers['Content-Length']=len(body) 66 return [body]
67
68 -def redirect(location,how=303):
69 raise HTTP(how, 70 'You are being redirected <a href="%s">here</a>' % location, 71 Location=location) 72 73 """ 74 examples: 75 raise HTTP(301,Location='http://www.google.com') 76 redirect('/'+request.application+'/default/index') 77 """ 78