Refactor SimpleServer#mount.
@@ -57,16 +57,32 @@ var builtinSimpleServerInstanceMethods = []*BuiltInMethodObject{
path := args[0].(*StringObject).Value
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
- req := httpRequestClass.initializeInstance()
+ req := initRequest(r)
res := httpResponseClass.initializeInstance()
+ v.builtInMethodYield(blockFrame, req, res)
+
+ setupResponse(w, req, res)
+ })
+
+ return receiver
+ }
+ },
+ },
+}
+
+func initRequest(r *http.Request) *RObject {
+ req := httpRequestClass.initializeInstance()
+
req.InstanceVariables.set("@method", initializeString(r.Method))
req.InstanceVariables.set("@body", initializeString(""))
req.InstanceVariables.set("@path", initializeString(r.URL.Path))
req.InstanceVariables.set("@url", initializeString(r.URL.RequestURI()))
- v.builtInMethodYield(blockFrame, req, res)
+ return req
+}
+func setupResponse(w http.ResponseWriter, req, res *RObject) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8") // normal header
resStatus, ok := res.InstanceVariables.get("@status")
@@ -84,11 +100,7 @@ var builtinSimpleServerInstanceMethods = []*BuiltInMethodObject{
return
}
- io.WriteString(w, resBody.(*StringObject).Value)
- })
+ res.InstanceVariables.set("@request", req)
- return receiver
- }
- },
- },
+ io.WriteString(w, resBody.(*StringObject).Value)
}
\ No newline at end of file
