-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookie_framework_example.rs
More file actions
282 lines (248 loc) · 10 KB
/
cookie_framework_example.rs
File metadata and controls
282 lines (248 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use ignitia::{Cookie, Error, Request, Response, Result, Router, SameSite, Server};
use std::net::SocketAddr;
use tracing_subscriber;
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
let router = Router::new()
.get("/", home)
.get("/set", set_cookies)
.get("/get", get_cookies)
.get("/secure", set_secure_cookie)
.get("/remove", remove_cookie)
.post("/login", login)
.get("/dashboard", dashboard)
.get("/logout", logout);
let addr: SocketAddr = "127.0.0.1:3006".parse().unwrap();
let server = Server::new(router, addr);
println!("🍪 Framework Cookie Server running on http://{}", addr);
println!("📋 Built-in Cookie Functionality Demo");
println!("🔗 Try: http://127.0.0.1:3006/");
server.ignitia().await.unwrap();
Ok(())
}
async fn home(req: Request) -> Result<Response> {
let cookies = req.cookies();
let cookie_count = cookies.len();
let mut cookie_list = String::new();
for (name, value) in cookies.all() {
cookie_list.push_str(&format!("<tr><td>{}</td><td>{}</td></tr>", name, value));
}
if cookie_list.is_empty() {
cookie_list = "<tr><td colspan='2'><em>No cookies found</em></td></tr>".to_string();
}
let html = format!(
r#"
<!DOCTYPE html>
<html>
<head>
<title>🍪 Built-in Cookie Framework</title>
<style>
body {{ font-family: Arial, sans-serif; margin: 40px; }}
.container {{ max-width: 900px; margin: 0 auto; }}
.cookie-display {{ background: #f0f8ff; padding: 20px; border-radius: 10px; margin: 20px 0; }}
.links {{ display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 10px; margin: 20px 0; }}
.links a {{ display: block; padding: 12px; background: #007acc; color: white; text-decoration: none; border-radius: 5px; text-align: center; }}
.links a:hover {{ background: #005c99; }}
table {{ width: 100%; border-collapse: collapse; margin: 15px 0; }}
th, td {{ padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }}
th {{ background: #f2f2f2; }}
.form-section {{ background: #f8f9fa; padding: 20px; border-radius: 10px; margin: 20px 0; }}
input, button {{ padding: 10px; margin: 5px; border: 1px solid #ddd; border-radius: 5px; }}
button {{ background: #28a745; color: white; border: none; cursor: pointer; }}
button:hover {{ background: #218838; }}
</style>
</head>
<body>
<div class="container">
<h1>🍪 Built-in Cookie Framework</h1>
<p><strong>Framework Feature:</strong> Cookies are now built into your mini web framework!</p>
<div class="cookie-display">
<h2>Current Cookies ({} total):</h2>
<table>
<thead>
<tr><th>Name</th><th>Value</th></tr>
</thead>
<tbody>{}</tbody>
</table>
</div>
<div class="links">
<a href="/set">🍪 Set Demo Cookies</a>
<a href="/get">📋 Get Cookies (JSON)</a>
<a href="/secure">🔒 Set Secure Cookie</a>
<a href="/remove">🗑️ Remove Cookies</a>
<a href="/dashboard">👤 Dashboard (Protected)</a>
</div>
<div class="form-section">
<h3>🔐 Login Form</h3>
<form action="/login" method="POST">
<input type="text" name="username" placeholder="Username" required>
<input type="password" name="password" placeholder="Password" required>
<button type="submit">Login</button>
</form>
<p><em>Try any username/password combo</em></p>
</div>
<div class="form-section">
<h3>🛠️ Framework Features Demonstrated:</h3>
<ul>
<li>✅ <code>request.cookies()</code> - Get all cookies</li>
<li>✅ <code>request.cookie("name")</code> - Get specific cookie</li>
<li>✅ <code>response.add_cookie(cookie)</code> - Add cookie to response</li>
<li>✅ <code>Cookie::new("name", "value")</code> - Create cookies</li>
<li>✅ Cookie attributes: path, domain, secure, http_only, same_site</li>
<li>✅ <code>Cookie::removal("name")</code> - Remove cookies</li>
</ul>
</div>
</div>
</body>
</html>
"#,
cookie_count, cookie_list
);
Ok(Response::html(html))
}
async fn set_cookies(_req: Request) -> Result<Response> {
let response = Response::html(
r#"
<h1>✅ Demo Cookies Set!</h1>
<p>Multiple cookies with different attributes have been set using the built-in framework:</p>
<ul>
<li><strong>demo_cookie</strong> - Basic cookie (1 hour)</li>
<li><strong>user_pref</strong> - User preference (1 day)</li>
<li><strong>session_temp</strong> - Session cookie (browser close)</li>
<li><strong>theme</strong> - Theme setting (30 days)</li>
</ul>
<a href="/">← Back to Home</a>
"#,
);
// Using the built-in cookie functionality
let cookies = vec![
Cookie::new("demo_cookie", "framework_test")
.path("/")
.max_age(3600), // 1 hour
Cookie::new("user_pref", "dark_mode")
.path("/")
.max_age(86400) // 1 day
.same_site(SameSite::Lax),
Cookie::new("session_temp", "temp_session").path("/"), // No max_age = session cookie
Cookie::new("theme", "blue")
.path("/")
.max_age(2592000) // 30 days
.http_only(),
];
Ok(response.add_cookies(cookies))
}
async fn get_cookies(req: Request) -> Result<Response> {
let cookies = req.cookies();
Ok(Response::json(cookies.all()))
}
async fn set_secure_cookie(_req: Request) -> Result<Response> {
let response = Response::html(
r#"
<h1>🔒 Secure Cookie Set!</h1>
<p>A secure, HTTP-only cookie has been set with SameSite=Strict.</p>
<p>This cookie demonstrates security best practices.</p>
<a href="/">← Back to Home</a>
"#,
);
let secure_cookie = Cookie::new("secure_token", "super_secret_value")
.path("/")
.max_age(1800) // 30 minutes
.secure()
.http_only()
.same_site(SameSite::Strict);
Ok(response.add_cookie(secure_cookie))
}
async fn remove_cookie(_req: Request) -> Result<Response> {
let response = Response::html(
r#"
<h1>🗑️ Cookies Removed!</h1>
<p>Demo cookies have been removed using the framework's removal functionality.</p>
<a href="/">← Back to Home</a>
"#,
);
// Remove multiple cookies
Ok(response
.remove_cookie("demo_cookie")
.remove_cookie("user_pref")
.remove_cookie("session_temp"))
}
async fn login(req: Request) -> Result<Response> {
// Simple form parsing
let body = String::from_utf8(req.body.to_vec())
.map_err(|_| Error::BadRequest("Invalid form data".into()))?;
let mut username = String::new();
for pair in body.split('&') {
if let Some((key, value)) = pair.split_once('=') {
if key == "username" {
username = value.replace('+', " ");
break;
}
}
}
if username.is_empty() {
return Ok(Response::html(
r#"
<h1>❌ Login Failed!</h1>
<p>Username is required.</p>
<a href="/">← Back to Home</a>
"#,
));
}
let response = Response::html(format!(
r#"
<h1>✅ Login Successful!</h1>
<p>Welcome, <strong>{}</strong>!</p>
<p>A session cookie has been set using the framework.</p>
<a href="/dashboard">Go to Dashboard</a> | <a href="/">Home</a>
"#,
username
));
// Create a session cookie
let session_cookie = Cookie::new("user_session", &username)
.path("/")
.max_age(3600) // 1 hour
.http_only()
.same_site(SameSite::Lax);
Ok(response.add_cookie(session_cookie))
}
async fn dashboard(req: Request) -> Result<Response> {
// Check for session using built-in cookie functionality
if let Some(username) = req.cookie("user_session") {
Ok(Response::html(format!(
r#"
<h1>👤 Dashboard</h1>
<p>Welcome back, <strong>{}</strong>!</p>
<p>This protected page was accessed using framework cookies.</p>
<div style="background: #d4edda; padding: 15px; border-radius: 5px; margin: 20px 0;">
<h3>🍪 Session Info:</h3>
<p><strong>Username:</strong> {}</p>
<p><strong>Authentication:</strong> ✅ Valid session cookie</p>
<p><strong>Method:</strong> Framework built-in cookies</p>
</div>
<a href="/logout">Logout</a> | <a href="/">Home</a>
"#,
username, username
)))
} else {
Ok(Response::html(
r#"
<h1>🔒 Access Denied</h1>
<p>Please log in to access the dashboard.</p>
<p>This protection is implemented using framework cookies.</p>
<a href="/">← Login</a>
"#,
))
}
}
async fn logout(_req: Request) -> Result<Response> {
let response = Response::html(
r#"
<h1>👋 Logged Out!</h1>
<p>You have been logged out. Session cookie cleared.</p>
<a href="/">← Back to Home</a>
"#,
);
// Remove the session cookie
Ok(response.remove_cookie("user_session"))
}