Skip to content

Commit

Permalink
request.uri => request.path
Browse files Browse the repository at this point in the history
  • Loading branch information
trikko committed May 28, 2024
1 parent 66931ea commit cb19aa3
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 80 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ void homepage(Request request, Output output)
// This endpoint shows a private page: it's protected by the auth endpoint below.
@endpoint @route!"/private/profile"
void user(Request request, Output output)
{
output ~= "Hello user!";
void user(Request request, Output output)
{
output ~= "Hello user!";
}
// This endpoint shows a private page: it's protected by the auth endpoint below.
@endpoint
@endpoint
void blah(Request request, Output output)
{
// Same as marking this endpoint with @route!"/private/dump"
if (request.uri != "/private/dump")
// Same as marking this endpoint with @route!"/private/dump"
if (request.path != "/private/dump")
return;
output ~= request.dump();
Expand All @@ -71,7 +71,7 @@ void blah(Request request, Output output)
// This endpoint simply checks if the user and password are correct for all the private pages.
// Since it has a higher priority than the previous endpoints, it will be called first.
@priority(10)
@endpoint @route!(r => r.uri.startsWith("/private/"))
@endpoint @route!(r => r.path.startsWith("/private/"))
void auth(Request request, Output output)
{
if (request.user != "user" || request.password != "password")
Expand All @@ -90,7 +90,7 @@ void auth(Request request, Output output)
void requestLog(Request request)
{
// There's no http output, so the next endpoint will be called.
info("Request: ", request.uri);
info("Request: ", request.path);
}
```

Expand All @@ -102,7 +102,7 @@ void requestLog(Request request)
// Accept a new connection only if the request URI is "/echo"
// Every websocket will start a new indipendent process
@onWebSocketUpgrade bool onUpgrade(Request req) {
return req.uri == "/echo";
return req.path == "/echo";
}
// Handle the WebSocket connection
Expand Down Expand Up @@ -135,11 +135,11 @@ It's pretty easy. Just add these lines inside your nginx configuration:
server {
listen 80 default_server;
listen [::]:80 default_server;
location /your_path/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:8080;
}
...
Expand All @@ -165,10 +165,10 @@ server {
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://your_upstream_name;
}
...
...
}
Expand Down
2 changes: 1 addition & 1 deletion examples/04_html_dom/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void example(Request request, Output output)
{
// Skip if the request is not for the root
// You can also use @route!("/") to achieve the same result
if (request.uri != "/")
if (request.path != "/")
return;

// Read the query parameter, default to "https://trikko.github.io/serverino"
Expand Down
2 changes: 1 addition & 1 deletion examples/05_websocket_echo/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mixin ServerinoMain;

// Accept a new connection only if the request URI is "/echo"
@onWebSocketUpgrade bool onUpgrade(Request req) {
return req.uri == "/echo";
return req.path == "/echo";
}

// Handle the WebSocket connection
Expand Down
2 changes: 1 addition & 1 deletion init-exec/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mixin ServerinoMain;
void example(Request request, Output output)
{
// Probably you want to delete this spam.
info("Hello, log! There's a new incoming request. Url: ", request.uri);
info("Hello, log! There's a new incoming request. Url: ", request.path);
output ~= request.dump(true);
}
Expand Down
2 changes: 1 addition & 1 deletion source/serverino/communicator.d
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ package class Communicator
}

// Assign a worker to the communicator
void setWorker(ref WorkerInfo worker)
void setWorker(WorkerInfo worker)
{
this.worker = worker;
worker.communicator = this;
Expand Down
12 changes: 6 additions & 6 deletions source/serverino/config.d
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import serverino.interfaces : Request;
+ Example:
+ ---
+ @endpoint
+ @route!(x => x.uri.startsWith("/api"))
+ @route!(x => x.path.startsWith("/api"))
+ void api(Request r, Output o) { ... }
+ ---
+/
Expand All @@ -59,24 +59,24 @@ public struct route(alias T)
static bool apply(const Request r) { return T(r); }
}

private template compareUri(string _uri)
private template comparePath(string _uri)
{
import std.uri : encode;
enum compareUri = (const Request r){
enum comparePath = (const Request r){
static assert(_uri[0] == '/', "Every route must begin with a '/'");
return r.uri == _uri.encode();
return r.path == _uri.encode();
};
}

/++ UDA. You can use to filter requests using a uri.
/++ UDA. You can use to filter requests using a path.
+ Example:
+ ---
+ @endpoint
+ @route!"/hello.html"
+ void api(Request r, Output o) { ... }
+ ---
+/
public alias route(string uri) = route!(r => compareUri!uri(r));
public alias route(string path) = route!(r => comparePath!path(r));

/++
Struct used to setup serverino.
Expand Down
2 changes: 1 addition & 1 deletion source/serverino/daemon.d
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ package:
writeCanary();
scope(exit) removeCanary();

info("Daemon started.");
info("Daemon started using ", Backend == Backend.EPOLL ? "epoll" : "select", " backend.");
now = CoarseTime.currTime;

version(Posix)
Expand Down
10 changes: 6 additions & 4 deletions source/serverino/interfaces.d
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ enum HttpVersion
+ ---
+ void handler(Request request, Output output)
+ {
+ info("You asked for ", request.uri, " with method ", request.method, " and params ", request.get.data);
+ info("You asked for ", request.path, " with method ", request.method, " and params ", request.get.data);
+ }
+ ---
+/
Expand Down Expand Up @@ -144,7 +144,7 @@ struct Request
output ~= "\n";
output ~= "Request:\n";
output ~= format(" • Method: %s\n", method.to!string);
output ~= format("Uri: %s\n", uri);
output ~= format("Path: %s\n", path);

if (!_internal._user.empty)
output ~= format(" • Authorization: user => `%s` password => `%s`\n",_internal._user,_internal._password.map!(x=>'*'));
Expand Down Expand Up @@ -271,8 +271,10 @@ struct Request
/// Cookies received from user
@safe @nogc @property nothrow public auto cookie() const { return SafeAccess!string(_internal._cookie); }

/// The uri requested by user
@safe @nogc @property nothrow public const(string) uri() const { return _internal._uri; }
/// The path requested by user
@safe @nogc @property nothrow public const(string) path() const { return _internal._uri; }

deprecated("Use `request.path` instead") alias uri = path;

/// Which worker is processing this request?
@safe @nogc @property nothrow public auto worker() const { return _internal._worker; }
Expand Down
2 changes: 1 addition & 1 deletion source/serverino/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ OTHER DEALINGS IN THE SOFTWARE.
+ Every function decorated with `serverino.config.endpoint` is an endpoint. They are called in order of priority assigned with
+ `serverino.config.priority` (default is 0). The first endpoint that write something to the output is the one that will respond to the request.
+
+ The `serverino.config.route` attribute can be used to filter the requests that are passed to the endpoint, using a uri or a `bool delegate(Request r)` argument.
+ The `serverino.config.route` attribute can be used to filter the requests that are passed to the endpoint, using a path or a `bool delegate(Request r)` argument.
+ In this example, only requests to `/hello` are passed to the `hello` endpoint. The `serverino.config.route` attribute can be used multiple times to specify multiple routes also using a delegate.
+/
module serverino;
Expand Down
31 changes: 0 additions & 31 deletions source/serverino/versionlog.d

This file was deleted.

35 changes: 34 additions & 1 deletion source/serverino/worker.d
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ struct Worker

version(debugRequest)
{
log("-- REQ: ", request.uri);
log("-- REQ: ", request.path);
log("-- PARSING STATUS: ", request._internal._parsingStatus);

try { log("-- REQ: ", request); }
Expand Down Expand Up @@ -839,6 +839,39 @@ struct Worker
{
bool callUntilIsDirty(FunctionPriority[] taggedHandlers)()
{
static if (untaggedHandlers !is null && untaggedHandlers.length > 0)
{
static bool untaggedWarningShown = false;

if (!untaggedWarningShown)
{
untaggedWarningShown = true;

string[] untagged;

static foreach(ff; untaggedHandlers)
{
{
mixin(`import ` ~ untaggedHandlers[0].mod ~ ";");
alias currentMod = mixin(untaggedHandlers[0].mod);
alias f = __traits(getMember,currentMod, untaggedHandlers[0].name);
import std.traits : hasUDA;
static if (hasUDA!(f, priority) || hasUDA!(f, route))
{
import std.algorithm : canFind;
static if(!taggedHandlers.canFind!(a => a.name == ff.name))
untagged ~= "`" ~ ff.mod ~ "." ~ ff.name ~ "`";

}
}
}

import std.logger : critical;
foreach(u; untagged)
critical("Function ", u, " is not tagged with `@endpoint`. It will be ignored.");
}
}

static foreach(ff; taggedHandlers)
{
{
Expand Down
22 changes: 11 additions & 11 deletions tests/test-01/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ void test_routing_1(Request r, Output o)
}

@endpoint @priority(10000)
@route!(r => r.uri == "/world_routing")
@route!(r => r.uri == "/blah_routing")
@route!(r => r.path == "/world_routing")
@route!(r => r.path == "/blah_routing")
void test_routing_2(Request r, Output o)
{
JSONValue v = parseJSON(`{"route" : "world"}`);
Expand All @@ -171,7 +171,7 @@ void test_routing_3(Request r, Output o)
@endpoint @priority(5)
void json(Request r, Output o)
{
if (r.uri != "/json/dump/test") return;
if (r.path != "/json/dump/test") return;

o.addHeader("content-type", "application/json");

Expand All @@ -183,7 +183,7 @@ void json(Request r, Output o)
v.object["headers"] = r.header.data.byKeyValue.map!(x => x.key ~ ":" ~ x.value).array;
v.object["method"] = r.method.to!string.toUpper;
v.object["host"] = r.host;
v.object["uri"] = r.uri;
v.object["path"] = r.path;
v.object["username"] = r.user;
v.object["password"] = r.password;
v.object["content-type"] = r.body.contentType;
Expand All @@ -205,14 +205,14 @@ void json(Request r, Output o)
@endpoint @priority(5)
void test_1(Request r, Output o)
{
if (r.uri == "/401")
if (r.path == "/401")
o.status = 401;
}

@endpoint @priority(5)
void test_2(Request r, Output o)
{
if (r.uri != "/test_get") return;
if (r.path != "/test_get") return;

o.addHeader("content-type", "text-plain");
o ~= r.get.read("hello", "world");
Expand All @@ -222,7 +222,7 @@ void test_2(Request r, Output o)
@endpoint @priority(5)
void test_3(Request r, Output o)
{
if (r.uri != "/long") return;
if (r.path != "/long") return;

import core.thread;

Expand Down Expand Up @@ -260,7 +260,7 @@ void test()
auto j = parseJSON(content);

assert(j["method"].str == "POST");
assert(j["uri"].str == "/json/dump/test");
assert(j["path"].str == "/json/dump/test");
assert(j["host"].str == "localhost:8080");

assert(j["get"].array.map!(x=>x.str).array.sort.array == ["hello:123", "world:"]);
Expand Down Expand Up @@ -305,7 +305,7 @@ Content-Type: application/json\r
auto j = parseJSON(content);

assert(j["method"].str == "POST");
assert(j["uri"].str == "/json/dump/test");
assert(j["path"].str == "/json/dump/test");
assert(j["host"].str == "127.0.0.1:8080");

assert(j["get"].array.map!(x=>x.str).array.sort.array == []);
Expand Down Expand Up @@ -483,7 +483,7 @@ Content-Disposition: form-data; name=\"field1\"\r

auto j = parseJSON(content);
assert(j["method"].str == "POST");
assert(j["uri"].str == "/json/dump/test");
assert(j["path"].str == "/json/dump/test");
assert(j["host"].str == "127.0.0.1:8080");
assert(j["username"].str == string.init);
assert(j["password"].str == string.init);
Expand Down Expand Up @@ -522,7 +522,7 @@ Content-Type: application/json\r
auto j = parseJSON(content);

assert(j["method"].str == "POST");
assert(j["uri"].str == "/json/dump/test");
assert(j["path"].str == "/json/dump/test");
assert(j["host"].str == "127.0.0.1:8080");

assert(j["get"].array.map!(x=>x.str).array.sort.array == []);
Expand Down
Loading

0 comments on commit cb19aa3

Please sign in to comment.