Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ownership on 'NextCallback' #191

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/modular-app/admin-router.vala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class AdminRouter : Router {
/**
* Verify the user credentials and perform an authentication.
*/
public bool authenticate (Request req, Response res, NextCallback next) throws Error {
public bool authenticate (Request req, Response res, owned NextCallback next) throws Error {
string @value;
req.lookup_signed_cookie ("session", ChecksumType.SHA256, "impossible to break".data, out @value);
if (@value == "admin") {
Expand Down Expand Up @@ -65,7 +65,7 @@ public class AdminRouter : Router {
/**
* Restricted content.
*/
public bool view (Request req, Response res, NextCallback next, Context ctx) throws Error {
public bool view (Request req, Response res, owned NextCallback next, Context ctx) throws Error {
res.headers.set_content_type ("text/plain", null);
return res.expand_utf8 ("Hello admin!");
}
Expand Down
2 changes: 1 addition & 1 deletion examples/modular-app/user-router.vala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class UserRouter : Router {
get ("/user/<int:id>", view);
}

public bool view (Request req, Response res, NextCallback next, Context ctx) throws Error {
public bool view (Request req, Response res, owned NextCallback next, Context ctx) throws Error {
res.headers.set_content_type ("text/plain", null);
return res.expand_utf8 ("Hello, user %s!".printf (ctx["id"].get_string ()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/valum/valum-forward.vala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Valum {
* continuation.
*/
[Version (since = "0.3")]
public bool forward (Request req, Response res, NextCallback next) throws Error {
public bool forward (Request req, Response res, owned NextCallback next) throws Error {
return next ();
}
}
4 changes: 2 additions & 2 deletions src/valum/valum-router.vala
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ namespace Valum {
private bool perform_routing (SequenceIter<Route> routes,
Request req,
Response res,
NextCallback next,
owned NextCallback next,
Context context) throws Informational,
Success,
Redirection,
Expand All @@ -362,7 +362,7 @@ namespace Valum {
if (node.next ().is_end ()) {
return next ();
} else {
return perform_routing (node.next (), req, res, next, local_context);
return perform_routing (node.next (), req, res, (owned) next, local_context);
}
}, local_context);
}
Expand Down
15 changes: 10 additions & 5 deletions src/valum/valum.vala
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,15 @@ namespace Valum {
* otherwise 'false'
*/
[Version (since = "0.1")]
public delegate bool HandlerCallback (Request req,
Response res,
NextCallback next,
Context context) throws Error;
public delegate bool HandlerCallback (Request req,
Response res,
owned NextCallback next,
Context context) throws Informational,
Success,
Redirection,
ClientError,
ServerError,
Error;

/**
* Define a type of {@link Valum.HandlerCallback} that forward a generic
Expand All @@ -76,7 +81,7 @@ namespace Valum {
[Version (since = "0.3")]
public delegate bool ForwardCallback<T> (Request req,
Response res,
NextCallback next,
owned NextCallback next,
Context context,
T @value) throws Error;

Expand Down
30 changes: 28 additions & 2 deletions tests/router-test.vala
Original file line number Diff line number Diff line change
Expand Up @@ -1015,13 +1015,39 @@ public int main (string[] args) {
var request = new Request (null, "GET", new Soup.URI ("http://localhost/"));
var response = new Response (request, Soup.Status.NOT_FOUND);

assert (router.handle (request, response));
});

Test.add_func ("/router/next_in_thread", () => {
var router = new Router ();
Thread<bool>? thread = null;

router.get ("/", (req, res, next) => {
thread = new Thread<bool> (null, () => {
try {
return next ();
} catch (Error err) {
assert_not_reached ();
}
});
return true;
});

router.get ("/", (req, res) => {
return true;
});

var request = new Request (null, "GET", new Soup.URI ("http://localhost/"));
var response = new Response (request, Soup.Status.NOT_FOUND);

try {
router.handle (request, response);
assert (router.handle (request, response));
} catch (Error err) {
assert_not_reached ();
}

assert (418 == response.status);
assert (thread != null);
assert (thread.join ());
});

Test.add_func ("/router/next/not_found", () => {
Expand Down