Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Request now supports reading the body content.

See example/echo.ooc
  • Loading branch information...
commit d381e07578d2143821c906e042065d367fae8a9c 1 parent ded4fe1
@joshthecoder authored
View
18 examples/echo.ooc
@@ -0,0 +1,18 @@
+use web, fastcgi
+
+import web/Application
+import fastcgi/Server
+import text/StringBuffer
+
+EchoApplication: class extends Application {
+ processRequest: func {
+ response setHeader("Content-type", "plain/text; charset=utf-8")
+ response body() write(request body())
+ }
+}
+
+main: func {
+ server := FCGIServer new(":8000")
+ server setApplication(EchoApplication new())
+ server run()
+}
View
47 servers/fastcgi/fastcgi/Server.ooc
@@ -1,7 +1,7 @@
use web, fastcgi
import web/[Server, Application]
import fastcgi/fcgx
-import io/Writer
+import io/[Reader, Writer]
/**
@@ -31,7 +31,7 @@ FCGIServer: class extends Server {
// Begin listening for requests
while (FCGX accept(fcgi&) == 0) {
// parse and setup request
- request := FCGIRequest new(fcgi envp)
+ request := FCGIRequest new(fcgi envp, fcgi in)
request path = FCGX getParam("REQUEST_URI", fcgi envp)
request method = FCGX getParam("REQUEST_METHOD", fcgi envp)
request remoteAddress = FCGX getParam("REMOTE_ADDR", fcgi envp)
@@ -50,14 +50,19 @@ FCGIServer: class extends Server {
FCGIRequest: class extends Request {
_envp: Pointer
+ stream: FCGXStream*
- init: func(=_envp) {}
+ init: func(=_envp, =stream) {}
getHeader: func(name: String) -> String {
value := FCGX getParam("HTTP_%s" format(name toUpper()), _envp)
if (value != null) return value clone()
else return value
}
+
+ body: func -> Reader {
+ FCGIBodyReader new(stream)
+ }
}
FCGIResponse: class extends Response {
@@ -85,6 +90,42 @@ FCGIResponse: class extends Response {
}
}
+FCGIBodyReader: class extends Reader {
+ stream: FCGXStream*
+
+ init: func(=stream) {}
+
+ read: func ~string(chars: String, length: Int) -> SizeT {
+ bytesRead := FCGX getString(chars, length, stream)
+ if (bytesRead == -1) Exception new("Error while reading string") throw()
+ marker += bytesRead
+ return bytesRead
+ }
+
+ read: func(chars: String, offset: Int, count: Int) -> SizeT {
+ skip(offset - marker)
+ return read(chars, count)
+ }
+
+ read: func ~char -> Char {
+ c := FCGX getChar(stream)
+ if (c == -1) Exception new("Error while reading byte") throw()
+ marker += 1
+ return c
+ }
+
+ hasNext: func -> Bool {
+ c := FCGX getChar(stream)
+ if (c == -1) return false
+ FCGX unGetChar(c, stream)
+ return true
+ }
+
+ rewind: func(offset: Int) { Exception new("Rewind not supported") throw() }
+ mark: func -> Long { marker }
+ reset: func(marker: Long) { Exception new("Reset not supported") throw() }
+}
+
FCGIBodyWriter: class extends Writer {
stream: FCGXStream*
View
7 web/Application.ooc
@@ -1,4 +1,4 @@
-import io/Writer
+import io/[Reader, Writer]
Request: abstract class {
@@ -12,6 +12,11 @@ Request: abstract class {
Returns the value for the HTTP request header for the given key.
*/
getHeader: abstract func(name: String) -> String
+
+ /**
+ Returns a Reader object for reading the request body.
+ */
+ body: abstract func -> Reader
}
Response: abstract class {
Please sign in to comment.
Something went wrong with that request. Please try again.