Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit with a python scgi http server

  • Loading branch information...
commit c27d29bc220f4bce6049ae16eff3e7c112c9ef9d 0 parents
@fredreichbier authored
Showing with 129 additions and 0 deletions.
  1. +24 −0 LICENSE
  2. +28 −0 Netz.io
  3. +57 −0 NetzSCGI.io
  4. +12 −0 NetzTest.io
  5. +8 −0 serve.py
24 LICENSE
@@ -0,0 +1,24 @@
+Netz is licensed under the 2-clause BSD license:
+
+Copyright (c) 2009, Friedrich Weber
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+OF THE POSSIBILITY OF SUCH DAMAGE.
28 Netz.io
@@ -0,0 +1,28 @@
+Netz := Object clone do(
+ NotImplementedError := Exception clone
+
+ BaseRequest := Object clone do(
+ environment ::= nil
+
+ requestMethod := method(
+ environment at("REQUEST_METHOD") asUppercase
+ )
+
+ contentType := method(
+ environment at("CONTENT_TYPE")
+ )
+
+ isGet := method(requestMethod == "GET")
+ isPost := method(requestMethod == "POST")
+ isPut := method(requestMethod == "PUT")
+ isDelete := method(requestMethod == "DELETE")
+
+ path := method(
+ environment at("PATH_INFO")
+ )
+
+ writeln := method(line,
+ NotImplementedError raise("Request misses a `write` implementation!")
+ )
+ )
+)
57 NetzSCGI.io
@@ -0,0 +1,57 @@
+# contains parts of io-scgi <http://github.com/quag/io-scgi>
+# by Jonathan Wright
+
+Netz
+
+NetzSCGI := Object clone do(
+ Request := Netz BaseRequest clone do(
+ socket ::= nil
+
+ writeln := method(line,
+ if(line isNil not,
+ socket write(line)
+ )
+ socket write("\r\n")
+ )
+ )
+
+ SCGIServer := Server clone do(
+ application ::= nil
+
+ handleSocket := method(socket,
+ coroDo(handleSocketAsync(socket))
+ )
+
+ handleSocketAsync := method(socket,
+ writeln("[Got scgi request connection from ", socket ipAddress, "]")
+ while(socket isOpen,
+ if(socket streamReadNextChunk) then(
+ input := socket readBuffer
+
+ # get the header netstring
+ i := input findSeq(":")
+ count := input exSlice(0, i) asNumber
+ params := input exSlice(i + 1, i + 1 + count) split("\0")
+ rest := input exSlice(i + 1 + count + 1)
+
+ # set the header values
+ environ := Map clone
+ key := nil
+ params foreach(i, value,
+ if(i isEven,
+ key = value
+ ,
+ environ atPut(key, value)
+ )
+ )
+ # create a request object
+ request := NetzSCGI Request clone setEnvironment(environ) setSocket(socket)
+ application handleRequest(request)
+ socket close
+ break
+ )
+ )
+ writeln("[Closed ", socket ipAddress, "]")
+ )
+ )
+)
12 NetzTest.io
@@ -0,0 +1,12 @@
+NetzSCGI
+
+app := Object clone do(
+ handleRequest := method(req,
+ req writeln("Content-type: text/html")
+ req writeln
+ req writeln("<h1>Hi!</h1>")
+ )
+)
+
+s := NetzSCGI SCGIServer clone setApplication(app) setPort(4001)
+s start
8 serve.py
@@ -0,0 +1,8 @@
+from flup.client.scgi_app import SCGIApp
+from wsgiref.simple_server import make_server
+
+application = SCGIApp(connect=('127.0.0.1', 4001))
+httpd = make_server('', 8008, application)
+print 'Serving HTTP on port 8008 ...'
+httpd.serve_forever()
+
Please sign in to comment.
Something went wrong with that request. Please try again.