Commit 59fa7f23 authored by Bat's avatar Bat
Browse files

Merge branch 'master' into 'master'

fix #60



See merge request !18
parents 8b777512 192cd954
Pipeline #9447 passed with stage
in 9 minutes and 31 seconds
using Valse;
/*
/**
* Controller on /
*/
public class HomeController : Controller {
......@@ -9,10 +9,11 @@ public class HomeController : Controller {
this.add_action ("truc", this.truc);
this.add_action ("wala", this.wala);
this.add_action ("oupa", this.deleted);
this.add_complete_action ("cookies", this.cookies);
this.error_controller = new CustomErrorController ();
}
/*
/**
* The home page.
*
* Accessible at /
......@@ -21,7 +22,7 @@ public class HomeController : Controller {
return page ("views/home.html");
}
/*
/**
* A page to test the router.
*
* Accessible at /truc
......@@ -30,7 +31,7 @@ public class HomeController : Controller {
return page ("views/truc.html");
}
/*
/**
* Wala-related tests
*
* Accessible at /wala
......@@ -45,19 +46,37 @@ public class HomeController : Controller {
}));
}
/**
* Used to test custom error pages
*/
public Response deleted () {
return error (404, "The page you are looking for doesn't exist anymore.");
}
/**
* Used for testing cookies
*/
public Response cookies (Request req, Response response) {
Response res = new Response ();
if (req.cookies["test"] != null) {
res = template ("<h1>Cookie is set !</h1><p>It's the %s nd time you visit this page".printf (req.cookies["test"]));
res.cookies["test"] = (int.parse (req.cookies["test"]) + 1).to_string ();
} else {
res = template ("<h1>Try to refresh...</h1>");
res.cookies["test"] = "2";
}
return res;
}
}
/*
/**
* A model used in KillTheMod tests.
*/
public class Page : Object {
public string title { get; set; }
}
/*
/**
* Anoher model used in KillTheMod tests.
*/
public class Site : Object {
......
......@@ -43,12 +43,6 @@ public class UserController : Controller {
Many<User> usrs = new Many<User>.wrap (base_arr);
UserList list = new UserList ();
list.users = usrs;
//var model = new HashMap<string, Object> ();
//model ["mod"] = list;
return page ("views/user/list.wala", model ("users", usrs));
}
......
......@@ -52,6 +52,11 @@ namespace Valse {
*/
public HashMap<string, string> headers { get; set; default = new HashMap<string, string> (); }
/**
* The cookies stored by the web browser.
*/
public HashMap<string, string> cookies { get; set; default = new HashMap<string, string> (); }
/**
* Creates a new request
*/
......@@ -81,6 +86,13 @@ namespace Valse {
}
}
this.headers [res] = v;
if (res == "Cookie") {
foreach (string cookie in v.split (";")) {
cookie = cookie.strip ();
this.cookies [cookie.split ("=")[0]] = cookie.split ("=")[1];
}
}
} else if (k == "REQUEST_METHOD"){
// request's method (GET, POST ...)
this.method = v;
......
......@@ -37,6 +37,11 @@ namespace Valse {
*/
public string body { get; set; }
/**
* The cookies to set.
*/
public HashMap<string, string> cookies { get; set; default = new HashMap<string, string> (); }
/**
* Create an empty response, that should be modified before sending.
*/
......
......@@ -164,6 +164,11 @@ namespace Valse {
foreach (var hdr in res.headers.entries) {
response += "%s: %s\r\n".printf (hdr.key, hdr.value);
}
foreach (var cookie in res.cookies.entries) {
response += "Set-Cookie: %s=%s\r\n".printf (cookie.key, cookie.value);
}
response += "\r\n";
if (!res.is_data) {
response += res.body.replace ("\n", "\r\n");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment