Skip to content

Commit 686049d

Browse files
committed
Applications now write the response body with a Writer object.
1 parent 6442b7c commit 686049d

File tree

5 files changed

+65
-19
lines changed

5 files changed

+65
-19
lines changed

examples/hello.ooc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
use web, fastcgi
22

3-
import io/Reader
43
import text/StringBuffer
54
import web/[Application]
65
import fastcgi/Server
76

87

98
HelloApplication: class extends Application {
10-
processRequest: func -> Reader {
9+
processRequest: func {
1110
"Request path: %s" format(request path) println()
1211
"Request method: %s" format(request method) println()
1312
"Request remote addr: %s port %d" format(request remoteAddress, request remotePort) println()
1413

1514
response setHeader("Content-type", "text/html")
16-
out := BufferReader new()
17-
out buffer() append("<html><body><h1>Hello world from ooc!!!</h1></body></html>")
18-
return out
15+
out := response body()
16+
out write("<html><body><h1>Hello world from ooc!!!</h1></body></html>")
1917
}
2018
}
2119

examples/sendfile.ooc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import fastcgi/Server
55
import io/[Reader, FileReader]
66

77

8+
//TODO: update this to use the new body() writer
89
SendFile: class extends Application {
910
processRequest: func -> Reader {
1011
response setHeader("Content-type", "text/plain; charset=utf-8")

servers/fastcgi/fastcgi/Server.ooc

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use web, fastcgi
22
import web/[Server, Application]
33
import fastcgi/fcgx
4+
import io/Writer
45

56

67
/**
@@ -40,17 +41,7 @@ FCGIServer: class extends Server {
4041
// dispatch request to Application
4142
application request = request
4243
application response = FCGIResponse new(fcgi out)
43-
responseBody := application processRequest()
44-
45-
// send response body
46-
if (responseBody) {
47-
FCGX putString("\r\n", fcgi out)
48-
while (responseBody hasNext()) {
49-
if (FCGX putChar(responseBody read(), fcgi out) == -1) {
50-
Exception new(This, "Write error while sending response body!") throw()
51-
}
52-
}
53-
}
44+
application processRequest()
5445
}
5546

5647
return true
@@ -71,14 +62,47 @@ FCGIRequest: class extends Request {
7162

7263
FCGIResponse: class extends Response {
7364
stream: FCGXStream*
65+
bodyWriter: Writer
7466

75-
init: func(=stream) {}
67+
init: func(=stream) { bodyWriter = null }
7668

7769
setStatus: func(code: Int, value: String) {
70+
if(bodyWriter) Exception new("May not set status code once body() has been called") throw()
7871
FCGX putString("Status: %d %s\r\n" format(code, value), stream)
7972
}
8073

8174
setHeader: func(name: String, value: String) {
75+
if(bodyWriter) Exception new("May not set header once body() has been called") throw()
8276
FCGX putString("%s: %s\r\n" format(name, value), stream)
8377
}
78+
79+
body: func -> Writer {
80+
if (!bodyWriter) {
81+
FCGX putString("\r\n", stream) // terminate headers
82+
bodyWriter = FCGIBodyWriter new(stream)
83+
}
84+
return bodyWriter
85+
}
86+
}
87+
88+
FCGIBodyWriter: class extends Writer {
89+
stream: FCGXStream*
90+
91+
init: func(=stream) {}
92+
93+
close: func {}
94+
95+
write: func ~chr (chr: Char) {
96+
if (FCGX putChar(chr, stream) == -1) {
97+
Exception new("Error while sending response body byte") throw()
98+
}
99+
}
100+
101+
write: func(chars: String, length: SizeT) -> SizeT {
102+
bytes := FCGX putStringWithLength(chars, length, stream)
103+
if (bytes == -1) {
104+
Exception new("Error while sending response body string") throw()
105+
}
106+
return bytes
107+
}
84108
}

servers/fastcgi/fastcgi/fcgx.ooc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ FCGX: cover {
2727
getString: extern(FCGX_GetStr) static func(String, Int, FCGXStream*) -> Int
2828
getLine: extern(FCGX_GetLine) static func(String, Int, FCGXStream*) -> String
2929
putChar: extern(FCGX_PutChar) static func(Char, FCGXStream*) -> Int
30+
putStringWithLength: extern(FCGX_PutStr) static func(String, Int, FCGXStream*) -> Int
3031
putString: extern(FCGX_PutS) static func(String, FCGXStream*) -> Int
3132
getParam: extern(FCGX_GetParam) static func(String, Pointer) -> String
3233

web/Application.ooc

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import io/Reader
1+
import io/Writer
22

33

44
Request: abstract class {
@@ -8,18 +8,40 @@ Request: abstract class {
88
remoteHost: String
99
remotePort: String
1010

11+
/**
12+
Returns the value for the HTTP request header for the given key.
13+
*/
1114
getHeader: abstract func(name: String) -> String
1215
}
1316

1417
Response: abstract class {
18+
/**
19+
Set the HTTP response status code and message.
20+
*/
1521
setStatus: abstract func(code: Int, message: String)
22+
23+
/**
24+
Set a HTTP response header.
25+
*/
1626
setHeader: abstract func(name: String, value: String)
27+
28+
/**
29+
Returns a writer object used for sending
30+
the response body.
31+
32+
Note: once you call this method, not further
33+
headers way be sent or the status code/message changed.
34+
*/
35+
body: abstract func -> Writer
1736
}
1837

1938
Application: abstract class {
2039
request: Request
2140
response: Response
2241

23-
processRequest: abstract func -> Reader
42+
/**
43+
Invoked by the server when ever there is a request to be processed.
44+
*/
45+
processRequest: abstract func
2446
}
2547

0 commit comments

Comments
 (0)