[cpif] r141 - trunk/frontend-web

svn at argo.es svn at argo.es
Sat Jun 9 21:39:58 CEST 2007


Author: jcea
Date: Sat Jun  9 21:39:57 2007
New Revision: 141

Log:
Refactorizacion de codigo, preparandonos para
permitir el acceso no autentificado al foro.



Added:
   trunk/frontend-web/url_LOGIN.py
      - copied, changed from r138, /trunk/frontend-web/url_logout.py
Modified:
   trunk/frontend-web/servidor_web.py
   trunk/frontend-web/skins.py
   trunk/frontend-web/url_logout.py
   trunk/frontend-web/url_nuevo_hilo_POST.py
   trunk/frontend-web/url_nuevo_post_POST.py
   trunk/frontend-web/url_todo_leido.py

Modified: trunk/frontend-web/servidor_web.py
==============================================================================
--- trunk/frontend-web/servidor_web.py	(original)
+++ trunk/frontend-web/servidor_web.py	Sat Jun  9 21:39:57 2007
@@ -10,36 +10,6 @@
   class handler(BaseHTTPRequestHandler) :
     must_stop=False
 
-    @monitor
-    def elige_usuario(conn,self,path) :
-      if path[0]=="LOGIN" :
-        import cgi,database
-        path.pop(0)
-        ctype,pdict=cgi.parse_header(self.headers.getheader('content-type'))
-        cuerpo=cgi.FieldStorage(fp=self.rfile,headers=self.headers,environ={'REQUEST_METHOD':'POST'},keep_blank_values=1)
-        usuario=cuerpo.getfirst("usuario")
-        clave=cuerpo.getfirst("clave")
-        if usuario and clave :
-          resultado=database.usuario_verifica(conn,nick=usuario,clave=clave)
-          if resultado :
-            nick,cookie=resultado
-            if cookie :
-              cookie="cpif_auth=%s; path=/;" %cookie
-            return (cookie, (302,"/"+"/".join(path),"") )
-
-      return (None,(200,"text/html; charset=utf-8",
-"""
-<html><head></head>
-<body>
-<form action="/LOGIN/%s" method="post" enctype="multipart/form-data">
-<table>
-<tr><td>Usuario:</td><td><input type="text" name="usuario" size="25" value="" /></td></tr>
-<tr><td>Clave:</td><td><input type="password" name="clave" size="25" value="" /></td></tr>
-<tr><td colspan=2 align=right><input class="form-element" type="submit" name="submit" value="Log In"/></td></tr>
-</table>
-</form>
-</body></html>""" % ("/".join(path)) ))
-
     def do_GET(self) :
       global urls
       cookie=self.headers.get("cookie",None)
@@ -58,7 +28,7 @@
       try :
         path=self.path.split("/")[1:]
         if not cookie :
-          cookie,resultado=self.elige_usuario(path)
+          resultado=urls["LOGIN"](self,path,None)
         else :
           usuario,cookie=cookie
           resultado=urls.get(path[0],None)
@@ -76,21 +46,9 @@
         print >>sys.stderr,"EXCEPCION:",time.ctime()
         raise
 
-      estado=resultado[0]
-      if estado==301 or estado==302 :
-        self.send_response(estado)
-        self.send_header("Location",resultado[1])
-        self.send_header("Content-Type","text/html; charset=utf-8")
-      elif estado==401 :
-        cookie=resultado[1]
-        self.send_response(302)
-        self.send_header("Location","/")
-        self.send_header("Content-Type","text/html; charset=utf-8")
-      else :
-        self.send_response(estado)
-        self.send_header("Content-Type",resultado[1])
-      if cookie!=None :
-        self.send_header("Set-Cookie",cookie)
+      self.send_response(resultado[0])
+      for i,j in resultado[1].iteritems() :
+        self.send_header(i,j)
       self.end_headers()
       self.wfile.write(resultado[2])
 

Modified: trunk/frontend-web/skins.py
==============================================================================
--- trunk/frontend-web/skins.py	(original)
+++ trunk/frontend-web/skins.py	Sat Jun  9 21:39:57 2007
@@ -180,4 +180,4 @@
     return available_skins[self._skin]['footer'] % self._dict
 
   def web(self,code=200):
-    return code, "text/html; charset=utf-8", "\r\n".join([self._header(),self._body(),self._footer()])
+    return code, {"Content-Type":"text/html; charset=utf-8"}, "\r\n".join([self._header(),self._body(),self._footer()])

Copied: trunk/frontend-web/url_LOGIN.py (from r138, /trunk/frontend-web/url_logout.py)
==============================================================================
--- /trunk/frontend-web/url_logout.py	(original)
+++ trunk/frontend-web/url_LOGIN.py	Sat Jun  9 21:39:57 2007
@@ -3,7 +3,34 @@
 from globales import monitor
 
 def gestiona_url(handler,path,usuario) :
-  if len(path)!=1 : return None
-  # Para borrar una cookie hay que mandarla con una fecha en el pasado.
-  return (401,"cpif_auth=X; path=/; expires=Thu, 01-01-1970 01:00:00 GMT","")
+  if path[0]=="LOGIN" :
+    import cgi,database
+    path.pop(0)
+    ctype,pdict=cgi.parse_header(handler.headers.getheader('content-type'))
+    cuerpo=cgi.FieldStorage(fp=handler.rfile,headers=handler.headers,environ={'REQUEST_METHOD':'POST'},keep_blank_values=1)
+    usuario=cuerpo.getfirst("usuario")
+    clave=cuerpo.getfirst("clave")
+    if usuario and clave :
+      @monitor
+      def login(conn,usuario,clave) :
+        return database.usuario_verifica(conn,nick=usuario,clave=clave)
+      resultado=login(usuario,clave)
+      if resultado :
+        nick,cookie=resultado
+        if cookie :
+          cookie="cpif_auth=%s; path=/;" %cookie
+        return (302,{"Set-Cookie":cookie,"Location":"/"+"/".join(path)},"")
+
+  return (200,{"Content-Type":"text/html; charset=utf-8"},
+"""
+<html><head></head>
+<body>
+<form action="/LOGIN/%s" method="post" enctype="multipart/form-data">
+<table>
+<tr><td>Usuario:</td><td><input type="text" name="usuario" size="25" value="" /></td></tr>
+<tr><td>Clave:</td><td><input type="password" name="clave" size="25" value="" /></td></tr>
+<tr><td colspan=2 align=right><input class="form-element" type="submit" name="submit" value="Log In"/></td></tr>
+</table>
+</form>
+</body></html>""" % ("/".join(path)) )
 

Modified: trunk/frontend-web/url_logout.py
==============================================================================
--- trunk/frontend-web/url_logout.py	(original)
+++ trunk/frontend-web/url_logout.py	Sat Jun  9 21:39:57 2007
@@ -5,5 +5,5 @@
 def gestiona_url(handler,path,usuario) :
   if len(path)!=1 : return None
   # Para borrar una cookie hay que mandarla con una fecha en el pasado.
-  return (401,"cpif_auth=X; path=/; expires=Thu, 01-01-1970 01:00:00 GMT","")
+  return (302,{"Set-Cookie":"cpif_auth=X; path=/; expires=Thu, 01-01-1970 01:00:00 GMT","Location":"/"},"")
 

Modified: trunk/frontend-web/url_nuevo_hilo_POST.py
==============================================================================
--- trunk/frontend-web/url_nuevo_hilo_POST.py	(original)
+++ trunk/frontend-web/url_nuevo_hilo_POST.py	Sat Jun  9 21:39:57 2007
@@ -20,5 +20,5 @@
   texto=cgi.escape(cuerpo.getfirst("texto"))
   texto=texto.replace("\r","").replace("\n","<br>\r\n")
   database.mensaje_add(conn,texto,usuario,titulo=titulo,metatag=metatag)
-  return (302,"/indice/%d" %metatag,"")
+  return (302,{"Location":"/indice/%d" %metatag},"")
 

Modified: trunk/frontend-web/url_nuevo_post_POST.py
==============================================================================
--- trunk/frontend-web/url_nuevo_post_POST.py	(original)
+++ trunk/frontend-web/url_nuevo_post_POST.py	Sat Jun  9 21:39:57 2007
@@ -20,5 +20,5 @@
   texto=cgi.escape(cuerpo.getfirst("texto"))
   texto=texto.replace("\r","").replace("\n","<br>\r\n")
   database.mensaje_add(conn,texto,usuario,hilo=hilo)
-  return (302,"/indice/%d" %metatag, "")
+  return (302,{"Location":"/indice/%d" %metatag}, "")
 

Modified: trunk/frontend-web/url_todo_leido.py
==============================================================================
--- trunk/frontend-web/url_todo_leido.py	(original)
+++ trunk/frontend-web/url_todo_leido.py	Sat Jun  9 21:39:57 2007
@@ -13,5 +13,5 @@
   if not database.user_has_metatag_access(conn,usuario,metatag) :
     return None
   database.marca_todo_leido(conn,database.normaliza_nick(usuario),metatag)
-  return (302,"/indice/%d" %metatag,"")
+  return (302,{"Location":"/indice/%d" %metatag},"")
 



More information about the cpif mailing list