Commits

Ning Sun committed e6d8360

add environ impl

  • Participants
  • Parent commits 21925f2

Comments (0)

Files changed (3)

File java/info/sunng/wsgiservlet/WsgiBridgeServlet.java

 import org.python.core.PyType;
 import org.python.util.PythonInterpreter;
 
+import info.sunng.wsgiservlet.wsgi.Environ;
+import info.sunng.wsgiservlet.wsgi.StartResponse;
+
 public class WsgiBridgeServlet extends HttpServlet {
     
     protected PythonInterpreter pyinterp;
     
     @Override
     public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
+        Environ environ = new Environ(req);
+        StartResponse start_response = new StartResponse();
+        
         
     }
 

File java/info/sunng/wsgiservlet/wsgi/Environ.java

 
 package info.sunng.wsgiservlet.wsgi;
 
+import javax.servlet.http.HttpServletRequest;
+
+import org.python.core.PyBoolean;
 import org.python.core.PyDictionary;
+import org.python.core.PyFile;
+import org.python.core.PyInteger;
+import org.python.core.PyString;
+import org.python.core.PyTuple;
+import org.python.core.util.FileUtil;
 
 public class Environ extends PyDictionary {
     
+    protected static final PyInteger ZERO = new PyInteger(0);
+    
+    protected static final PyInteger ONE = new PyInteger(1);
+    
+    public Environ(HttpServletRequest request) {
+        initFromRequest(request);
+    }
+        
+    private void initFromRequest(HttpServletRequest request) {
+        
+        this.setValue("wsgi.version", new PyTuple(ONE, ZERO));
+        this.setValue("wsgi.url_scheme", request.getScheme())
+        this.setValue("wsgi.multithread", true);
+        this.setValue("wsgi.multiprocess", false);
+        this.setValue("wsgi.run_once", false);
+        this.setValue("wsgi.errors", new PyFile(/* TODO servlet output stream */));
+        
+        this.setValue("REQUEST_METHOD", request.getMethod());
+        this.setValue("SCRIPT_NAME", "");
+        this.setValue("PATH_INFO", request.getRequestURI());
+        this.setValue("QUERY_STRING", request.getQueryString());
+        this.setValue("CONTENT_TYPE", request.getHeader("Content-Type"));
+        this.setValue("CONTENT_LENGTH", request.getHeader("Content-Length"))
+        this.setValue("SERVER_NAME", request.getLocalName());
+        this.setValue("SERVER_PORT", request.getLocalPort());
+        this.setValue("REMOTE_ADDR", request.getRemoteHost());
+        this.setValue("SERVER_PROTOCOL", request.getProtocol());
+        this.setValue("wsgi.input", FileUtil.wrap(request.getInputStream()));
+        
+        initHeaders(request);
+    }
+    
+    private void initHeaders(HttpServletRequest request) {
+        Enumeration headerNames = request.getHeaderNames();
+        while(headerNames.hasMoreElements()){
+            String headerName = headerNames.nextElement();
+            String keyName = "HTTP_"+headerName.replace("-", "_");
+            this.setValue(keyName, request.getHeader(headerName));
+        }
+    }
+    
+    private void setValue(String name, PyObject value) {
+        this.__setitem__(new PyString(name), value);
+    }
+    
+    private void setValue(String name, String value) {
+        this.setValue(name, new PyString(value));
+    }
+    
+    private void setValue(String name, int value) {
+        this.setValue(name, new PyInteger(value));
+    }
+    
+    private void setValue(String name, boolean value){
+        this.setValue(name, new PyBoolean(value));
+    }
+    
 }

File java/info/sunng/wsgiservlet/wsgi/StartResponse.java

 import org.python.core.PyObject;
 
 public class StartResponse extends PyObject{
+    
+    @Override
+    public PyObject __call__(PyObject args[], String keywords[]) {
+        
+    }
 
 }