@@ -3,13 +3,22 @@ const URL = @import("url");
3
3
const RespondOptions = std .http .Server .Request .RespondOptions ;
4
4
const Header = std .http .Header ;
5
5
6
- const Request = @import ("request.zig" );
7
- const Response = @import ("response.zig" );
8
- const Config = @import ("config.zig" );
9
- const Headers = @import ("headers.zig" );
10
- const Param = @import ("param.zig" );
11
-
12
- const HandlerFn = @import ("handler.zig" ).HandlerFn ;
6
+ const zinc = @import ("../zinc.zig" );
7
+ const Request = zinc .Request ;
8
+ const Response = zinc .Response ;
9
+ const Config = zinc .Config ;
10
+ const Headers = zinc .Headers ;
11
+ const Param = zinc .Param ;
12
+ const HandlerFn = zinc .HandlerFn ;
13
+ // const Context = zinc.Context;
14
+
15
+ // const Request = @import("request.zig");
16
+ // const Response = @import("response.zig");
17
+ // const Config = @import("config.zig");
18
+ // const Headers = @import("headers.zig");
19
+ // const Param = @import("param.zig");
20
+
21
+ // const HandlerFn = @import("handler.zig").HandlerFn;
13
22
14
23
pub const Context = @This ();
15
24
const Self = @This ();
@@ -30,10 +39,8 @@ query_map: ?std.StringHashMap(std.ArrayList([]const u8)) = null,
30
39
// Slice of optional function pointers
31
40
handlers : std .ArrayList (* const fn (* Self ) anyerror ! void ) = std .ArrayList (* const fn (* Self ) anyerror ! void ).init (std .heap .page_allocator ),
32
41
index : u8 = 0 , // Adjust the type based on your specific needs
33
- // index: usize = 0, // Adjust the type based on your specific needs
34
42
35
- // body_buffer_len: usize = 0,
36
- // query: ?std.Uri.Component = null,
43
+ // writer: std.io.AnyWriter = std.net.Stream.writer(),
37
44
38
45
pub fn deinit (self : * Self ) void {
39
46
self .headers .deinit ();
@@ -56,6 +63,9 @@ pub fn init(self: Self) ?Context {
56
63
.params = self .params ,
57
64
.query = self .query ,
58
65
.query_map = self .query_map ,
66
+ .handlers = self .handlers ,
67
+ .index = self .index ,
68
+ // .writer = try self.request.server_request.server.connection.stream.writer(),
59
69
};
60
70
}
61
71
@@ -172,15 +182,15 @@ pub inline fn next(self: *Context) anyerror!void {
172
182
self .index += 1 ;
173
183
174
184
if (self .index >= self .handlers .items .len ) return ;
175
-
176
- try self . handlers . items [ self . index ] (self );
185
+ const handler = self . handlers . items [ self . index ];
186
+ try handler (self );
177
187
178
188
self .index += 1 ;
179
189
}
180
190
181
191
pub fn redirect (self : * Self , http_status : std.http.Status , url : []const u8 ) anyerror ! void {
182
192
try self .headers .add ("Location" , url );
183
- try self .request .server_request .respond ("" , .{ .status = http_status , .reason = http_status .phrase (), .extra_headers = self .headers .items (), .keep_alive = false });
193
+ try self .request .req .respond ("" , .{ .status = http_status , .reason = http_status .phrase (), .extra_headers = self .headers .items (), .keep_alive = false });
184
194
}
185
195
186
196
pub const queryError = error {
@@ -247,47 +257,6 @@ pub fn queryMap(self: *Self, map_key: []const u8) ?std.StringHashMap(std.ArrayLi
247
257
return inner_map ;
248
258
}
249
259
250
- test "context query" {
251
- var req = Request .init (.{
252
- .target = "/query?id=1234&message=hello&message=world&ids[a]=1234&ids[b]=hello&ids[b]=world" ,
253
- });
254
-
255
- var ctx = Context .init (.{ .request = & req }).? ;
256
-
257
- var qm = ctx .getQueryMap () orelse {
258
- return try std .testing .expect (false );
259
- };
260
-
261
- try std .testing .expectEqualStrings (qm .get ("id" ).? .items [0 ], "1234" );
262
- try std .testing .expectEqualStrings (qm .get ("message" ).? .items [0 ], "hello" );
263
- try std .testing .expectEqualStrings (qm .get ("message" ).? .items [1 ], "world" );
264
-
265
- const idv = ctx .queryValues ("id" ) catch return try std .testing .expect (false );
266
- try std .testing .expectEqualStrings (idv .items [0 ], "1234" );
267
-
268
- const messages = ctx .queryArray ("message" ) catch return try std .testing .expect (false );
269
- try std .testing .expectEqualStrings (messages [0 ], "hello" );
270
- try std .testing .expectEqualStrings (messages [1 ], "world" );
271
-
272
- const ids : std .StringHashMap (std .ArrayList ([]const u8 )) = ctx .queryMap ("ids" ) orelse return try std .testing .expect (false );
273
- try std .testing .expectEqualStrings (ids .get ("a" ).? .items [0 ], "1234" );
274
- try std .testing .expectEqualStrings (ids .get ("b" ).? .items [0 ], "hello" );
275
- try std .testing .expectEqualStrings (ids .get ("b" ).? .items [1 ], "world" );
276
- }
277
-
278
- test "context query map" {
279
- var req = Request .init (.{
280
- .target = "/query?ids[a]=1234&ids[b]=hello&ids[b]=world" ,
281
- });
282
-
283
- var ctx = Context .init (.{ .request = & req }).? ;
284
-
285
- var ids : std .StringHashMap (std .ArrayList ([]const u8 )) = ctx .queryMap ("ids" ) orelse return try std .testing .expect (false );
286
- try std .testing .expectEqualStrings (ids .get ("a" ).? .items [0 ], "1234" );
287
- try std .testing .expectEqualStrings (ids .get ("b" ).? .items [0 ], "hello" );
288
- try std .testing .expectEqualStrings (ids .get ("b" ).? .items [1 ], "world" );
289
- }
290
-
291
260
/// Get the query values as a map.
292
261
/// e.g /post?name=foo&name=bar => getQueryMap() => {"name": ["foo", "bar"]}
293
262
pub fn getQueryMap (self : * Self ) ? std .StringHashMap (std .ArrayList ([]const u8 )) {
@@ -310,7 +279,7 @@ pub fn queryArray(self: *Self, name: []const u8) anyerror![][]const u8 {
310
279
}
311
280
312
281
pub fn getPostFormMap (self : * Self ) ? std .StringHashMap ([]const u8 ) {
313
- const req = self .request .server_request ;
282
+ const req = self .request .req ;
314
283
315
284
const content_type = req .head .content_type orelse return null ;
316
285
_ = std .mem .indexOf (u8 , content_type , "application/x-www-form-urlencoded" ) orelse return null ;
@@ -342,13 +311,10 @@ pub fn postFormMap(self: *Self, map_key: []const u8) ?std.StringHashMap([]const
342
311
var inner_map : std .StringHashMap ([]const u8 ) = std .StringHashMap ([]const u8 ).init (self .allocator );
343
312
344
313
while (qit .next ()) | kv | {
345
- // const decoded_key = self.allocator.alloc(u8, key.len) catch continue;
346
314
const trimmed_key = std .mem .trim (u8 , std .Uri .percentDecodeInPlace (@constCast (kv .key_ptr .* )), "" );
347
315
var splited_key = std .mem .splitSequence (u8 , trimmed_key , "[" );
348
316
if (splited_key .index == null ) continue ;
349
317
const key_name = splited_key .first ();
350
- // std.debug.print("key_name: {s}\n", .{key_name});
351
-
352
318
if (! std .mem .eql (u8 , key_name , map_key )) continue ;
353
319
354
320
const key_rest = splited_key .next ();
@@ -365,10 +331,13 @@ pub fn postFormMap(self: *Self, map_key: []const u8) ?std.StringHashMap([]const
365
331
}
366
332
367
333
pub fn handle (self : * Self ) anyerror ! void {
334
+ if (self .handlers .items .len == 0 ) try self .handlers .items [self .index ](self );
335
+
368
336
for (self .handlers .items ) | handler | {
369
- if (self .index >= self .handlers .items .len ) {
337
+ if (self .index > self .handlers .items .len ) {
370
338
continue ;
371
339
}
340
+
372
341
try handler (self );
373
342
}
374
343
}
0 commit comments