2017-12-02 08:42:21 +01:00
|
|
|
# Resources and Routes
|
|
|
|
|
|
|
|
All resources and routes register for specific application.
|
|
|
|
Application routes incoming requests based on route criteria which is defined during
|
|
|
|
resource registration or path prefix for simple handlers.
|
|
|
|
Internally *router* is a list of *resources*. Resource is an entry in *route table*
|
|
|
|
which corresponds to requested URL.
|
|
|
|
|
|
|
|
Prefix handler:
|
|
|
|
|
2017-12-04 22:32:05 +01:00
|
|
|
```rust
|
|
|
|
# extern crate actix_web;
|
|
|
|
# use actix_web::*;
|
2017-12-05 05:38:38 +01:00
|
|
|
#
|
2017-12-04 22:32:05 +01:00
|
|
|
fn index(req: HttpRequest) -> HttpResponse {
|
|
|
|
unimplemented!()
|
2017-12-02 08:42:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
Application::default("/")
|
2017-12-06 17:03:08 +01:00
|
|
|
.resource("/prefix", |r| r.f(index))
|
2017-12-02 08:42:21 +01:00
|
|
|
.finish();
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
In this example `index` get called for any url which starts with `/prefix`.
|
|
|
|
|
|
|
|
Application prefix combines with handler prefix i.e
|
|
|
|
|
2017-12-04 22:32:05 +01:00
|
|
|
```rust
|
|
|
|
# extern crate actix_web;
|
|
|
|
# use actix_web::*;
|
2017-12-05 05:38:38 +01:00
|
|
|
#
|
2017-12-04 22:32:05 +01:00
|
|
|
fn index(req: HttpRequest) -> HttpResponse {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2017-12-02 08:42:21 +01:00
|
|
|
fn main() {
|
|
|
|
Application::default("/app")
|
2017-12-06 17:03:08 +01:00
|
|
|
.resource("/prefix", |r| r.f(index))
|
2017-12-02 08:42:21 +01:00
|
|
|
.finish();
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
In this example `index` get called for any url which starts with`/app/prefix`.
|
|
|
|
|
|
|
|
Resource contains set of route for same endpoint. Route corresponds to handling
|
|
|
|
*HTTP method* by calling *web handler*. Resource select route based on *http method*,
|
|
|
|
if no route could be matched default response `HTTPMethodNotAllowed` get resturned.
|
|
|
|
|
2017-12-04 22:32:05 +01:00
|
|
|
```rust
|
|
|
|
# extern crate actix_web;
|
|
|
|
# use actix_web::*;
|
2017-12-05 05:38:38 +01:00
|
|
|
#
|
2017-12-02 08:42:21 +01:00
|
|
|
fn main() {
|
|
|
|
Application::default("/")
|
|
|
|
.resource("/prefix", |r| {
|
2017-12-04 23:07:53 +01:00
|
|
|
r.method(Method::GET).h(httpcodes::HTTPOk);
|
|
|
|
r.method(Method::POST).h(httpcodes::HTTPForbidden);
|
2017-12-02 08:42:21 +01:00
|
|
|
})
|
|
|
|
.finish();
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
[`ApplicationBuilder::resource()` method](../actix_web/dev/struct.ApplicationBuilder.html#method.resource)
|
|
|
|
accepts configuration function, resource could be configured at once.
|
|
|
|
Check [`Resource`](../actix-web/target/doc/actix_web/struct.Resource.html) documentation
|
|
|
|
for more information.
|
|
|
|
|
|
|
|
## Variable resources
|
|
|
|
|
|
|
|
Resource may have *variable path*also. For instance, a resource with the
|
|
|
|
path '/a/{name}/c' would match all incoming requests with paths such
|
|
|
|
as '/a/b/c', '/a/1/c', and '/a/etc/c'.
|
|
|
|
|
2017-12-02 19:17:15 +01:00
|
|
|
A *variable part* is specified in the form {identifier}, where the identifier can be
|
2017-12-02 08:42:21 +01:00
|
|
|
used later in a request handler to access the matched value for that part. This is
|
|
|
|
done by looking up the identifier in the `HttpRequest.match_info` object:
|
|
|
|
|
|
|
|
```rust
|
2017-12-04 22:32:05 +01:00
|
|
|
# extern crate actix_web;
|
2017-12-02 08:42:21 +01:00
|
|
|
use actix_web::*;
|
|
|
|
|
2017-12-03 23:22:04 +01:00
|
|
|
fn index(req: HttpRequest) -> String {
|
|
|
|
format!("Hello, {}", &req.match_info()["name"])
|
2017-12-02 08:42:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
Application::default("/")
|
2017-12-04 23:07:53 +01:00
|
|
|
.resource("/{name}", |r| r.method(Method::GET).f(index))
|
2017-12-02 08:42:21 +01:00
|
|
|
.finish();
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
By default, each part matches the regular expression `[^{}/]+`.
|
|
|
|
|
|
|
|
You can also specify a custom regex in the form `{identifier:regex}`:
|
|
|
|
|
2017-12-04 22:32:05 +01:00
|
|
|
```rust
|
|
|
|
# extern crate actix_web;
|
|
|
|
# use actix_web::*;
|
|
|
|
# fn index(req: HttpRequest) -> String {
|
|
|
|
# format!("Hello, {}", &req.match_info()["name"])
|
|
|
|
# }
|
2017-12-05 05:38:38 +01:00
|
|
|
#
|
2017-12-02 08:42:21 +01:00
|
|
|
fn main() {
|
|
|
|
Application::default("/")
|
2017-12-04 23:07:53 +01:00
|
|
|
.resource(r"{name:\d+}", |r| r.method(Method::GET).f(index))
|
2017-12-02 08:42:21 +01:00
|
|
|
.finish();
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2017-12-02 19:17:15 +01:00
|
|
|
Any matched parameter can be deserialized into specific type if this type
|
|
|
|
implements `FromParam` trait. For example most of standard integer types
|
|
|
|
implements `FromParam` trait. i.e.:
|
|
|
|
|
|
|
|
```rust
|
2017-12-05 05:38:38 +01:00
|
|
|
# extern crate actix_web;
|
2017-12-02 19:17:15 +01:00
|
|
|
use actix_web::*;
|
|
|
|
|
2017-12-03 23:22:04 +01:00
|
|
|
fn index(req: HttpRequest) -> Result<String> {
|
2017-12-02 19:17:15 +01:00
|
|
|
let v1: u8 = req.match_info().query("v1")?;
|
|
|
|
let v2: u8 = req.match_info().query("v2")?;
|
2017-12-03 23:22:04 +01:00
|
|
|
Ok(format!("Values {} {}", v1, v2))
|
2017-12-02 19:17:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
Application::default("/")
|
2017-12-06 17:03:08 +01:00
|
|
|
.resource(r"/a/{v1}/{v2}/", |r| r.f(index))
|
2017-12-02 19:17:15 +01:00
|
|
|
.finish();
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
For this example for path '/a/1/2/', values v1 and v2 will resolve to "1" and "2".
|
|
|
|
|
2017-12-04 22:32:05 +01:00
|
|
|
It is possible to match path tail with custom `.*` regex.
|
2017-12-02 08:42:21 +01:00
|
|
|
|
2017-12-05 05:38:38 +01:00
|
|
|
```rust
|
|
|
|
# extern crate actix_web;
|
|
|
|
# use actix_web::*;
|
|
|
|
#
|
|
|
|
# fn index(req: HttpRequest) -> HttpResponse {
|
|
|
|
# unimplemented!()
|
|
|
|
# }
|
2017-12-02 08:42:21 +01:00
|
|
|
fn main() {
|
|
|
|
Application::default("/")
|
2017-12-04 23:07:53 +01:00
|
|
|
.resource(r"/test/{tail:.*}", |r| r.method(Method::GET).f(index))
|
2017-12-02 08:42:21 +01:00
|
|
|
.finish();
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Above example would match all incoming requests with path such as
|
|
|
|
'/test/b/c', '/test/index.html', and '/test/etc/test'.
|
2017-12-02 19:17:15 +01:00
|
|
|
|
|
|
|
It is possible to create a `PathBuf` from a tail path parameter. The returned `PathBuf` is
|
|
|
|
percent-decoded. If a segment is equal to "..", the previous segment (if
|
|
|
|
any) is skipped.
|
|
|
|
|
|
|
|
For security purposes, if a segment meets any of the following conditions,
|
|
|
|
an `Err` is returned indicating the condition met:
|
|
|
|
|
|
|
|
* Decoded segment starts with any of: `.` (except `..`), `*`
|
|
|
|
* Decoded segment ends with any of: `:`, `>`, `<`
|
|
|
|
* Decoded segment contains any of: `/`
|
|
|
|
* On Windows, decoded segment contains any of: '\'
|
|
|
|
* Percent-encoding results in invalid UTF8.
|
|
|
|
|
|
|
|
As a result of these conditions, a `PathBuf` parsed from request path parameter is
|
2017-12-04 22:32:05 +01:00
|
|
|
safe to interpolate within, or use as a suffix of, a path without additional checks.
|
2017-12-02 19:17:15 +01:00
|
|
|
|
|
|
|
```rust
|
2017-12-04 22:32:05 +01:00
|
|
|
# extern crate actix_web;
|
2017-12-02 19:17:15 +01:00
|
|
|
use actix_web::*;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2017-12-03 23:22:04 +01:00
|
|
|
fn index(req: HttpRequest) -> Result<String> {
|
2017-12-02 19:17:15 +01:00
|
|
|
let path: PathBuf = req.match_info().query("tail")?;
|
2017-12-03 23:22:04 +01:00
|
|
|
Ok(format!("Path {:?}", path))
|
2017-12-02 19:17:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
Application::default("/")
|
2017-12-04 23:07:53 +01:00
|
|
|
.resource(r"/a/{tail:.*}", |r| r.method(Method::GET).f(index))
|
2017-12-02 19:17:15 +01:00
|
|
|
.finish();
|
|
|
|
}
|
|
|
|
```
|