Skip to main content

basic/
header.rs

1use afire::{Content, HeaderType, Method, Response, Server, Status};
2
3use crate::Example;
4
5// Read request headers and send a response with custom headers
6// You may want to read https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers to understand headers more
7
8pub struct Header;
9
10impl Example for Header {
11    fn name(&self) -> &'static str {
12        "header"
13    }
14    fn exec(&self) {
15        // Create a new Server instance on localhost port 8080
16        let mut server = Server::<()>::new("localhost", 8080)
17            // Define server wide default headers
18            // These will be send with every response
19            // If the same header is defined in the route it will be put before the default header
20            // Although it is not guaranteed to be the one picked by the client it usually is
21            // At the bottom of this file is a representation of the order of the headers
22            .default_header("X-Server-Header", "This is a server wide header")
23            // You can also use the HeaderType enum to define a header type
24            // This is also true with the .header method on the Response struct
25            .default_header(HeaderType::Server, "afire");
26
27        // Define a route to redirect to another website
28        server.route(Method::GET, "/", |_req| {
29            // Because this is all bout headers I have put the header vector here
30            let headers = vec![
31                // Tell the client what type of data we are sending
32                afire::Header::new(HeaderType::ContentType, "text/html"),
33                // Tell the client to redirect to another website
34                afire::Header::new(HeaderType::Location, "https://connorcode.com"),
35                // Custom header
36                afire::Header::new("X-Custom-Header", "This is a custom header"),
37            ];
38
39            // Define response body
40            // In this case this should only be seen if the client doesn't support redirects for some reason
41            let text = "<a href=\"https://connorcode.com\">connorcode</a>";
42
43            // The response code of 308 tells the client to redirect to the location specified in the header
44            // There are other response codes you can use too
45            // 301 -> Moved Permanently
46            // 302 -> Found
47            // 303 -> See Other
48            // 307 -> Temporary Redirect
49            // 308 -> Permanent Redirect
50            Response::new()
51                .status(Status::PermanentRedirect)
52                .text(text)
53                .headers(&headers)
54        });
55
56        // Now to define a route to handle client headers
57        // This will just echo the headers back to the client
58        server.route(Method::GET, "/headers", |req| {
59            // Get the headers from the request and make a html string
60            let body = req
61                .headers
62                .iter()
63                .fold(String::new(), |old, new| old + &format!("{:?}<br />", new));
64
65            // Create a response with the headers
66            Response::new().text(body).content(Content::HTML)
67        });
68
69        // You can now goto http://localhost:8080 you should see a redirect to https://connorcode.com
70        // And you can goto http://localhost:8080/headers to see the headers your client sent to the server
71
72        // Start the server
73        // This will block the current thread
74        server.start().unwrap();
75    }
76}
77
78// This is a representation of the order of the headers
79// The order is important because the client usually picks the first header it can find
80// Note: The default headers and ContentLength are only added if they are not already defined
81
82// -------------------
83// | Default Headers |
84// -------------------
85// |  ContentLength  |
86// -------------------
87// |  Route Headers  |
88// -------------------