Skip to content

Commit 6778dbc

Browse files
authored
httpz improvements (#9462)
1 parent f749c9f commit 6778dbc

File tree

2 files changed

+12
-28
lines changed

2 files changed

+12
-28
lines changed

frameworks/Zig/httpz/src/endpoints.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const template = "<!DOCTYPE html><html><head><title>Fortunes</title></head><body
1010

1111
pub const Global = struct {
1212
pool: *pg.Pool,
13-
prng: *std.rand.DefaultPrng,
13+
rand: *std.rand.Random,
1414
mutex: std.Thread.Mutex = .{},
1515
};
1616

@@ -41,7 +41,7 @@ pub fn db(global: *Global, _: *httpz.Request, res: *httpz.Response) !void {
4141
try setHeaders(res.arena, res);
4242

4343
global.mutex.lock();
44-
const random_number = 1 + (global.prng.random().uintAtMost(u32, 9999));
44+
const random_number = 1 + (global.rand.uintAtMostBiased(u32, 9999));
4545
global.mutex.unlock();
4646

4747
const world = getWorld(global.pool, random_number) catch |err| {

frameworks/Zig/httpz/src/main.zig

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ const pool = @import("pool.zig");
77

88
const endpoints = @import("endpoints.zig");
99

10-
const RndGen = std.rand.DefaultPrng;
11-
const Allocator = std.mem.Allocator;
12-
const Pool = pg.Pool;
13-
1410
var server: httpz.ServerCtx(*endpoints.Global, *endpoints.Global) = undefined;
1511

1612
pub fn main() !void {
@@ -26,28 +22,16 @@ pub fn main() !void {
2622

2723
var prng = std.rand.DefaultPrng.init(@as(u64, @bitCast(std.time.milliTimestamp())));
2824

25+
var rand = prng.random();
26+
2927
var global = endpoints.Global{
3028
.pool = pg_pool,
31-
.prng = &prng,
29+
.rand = &rand,
3230
};
3331

34-
var httpz_port: []u8 = undefined;
35-
var arg_string = try std.fmt.allocPrint(allocator, "{s}", .{"0"});
36-
defer allocator.free(arg_string);
37-
38-
var args = try std.process.argsWithAllocator(allocator);
39-
defer args.deinit();
40-
while (args.next()) |arg| {
41-
arg_string = try std.fmt.allocPrint(allocator, "{s}", .{arg});
32+
const args = try std.process.argsAlloc(allocator);
4233

43-
httpz_port = arg_string; // use arg
44-
}
45-
46-
var port = try std.fmt.parseInt(u16,httpz_port, 0);
47-
48-
if (port == 0) {
49-
port = 3000;
50-
}
34+
const port: u16 = if (args.len > 1) try std.fmt.parseInt(u16, args[1], 0) else 3000;
5135

5236
const workers = @as(u16, @intCast(16 * cpu_count));
5337

@@ -100,7 +84,7 @@ pub fn main() !void {
10084
.buffer_size = 8192,
10185
},
10286
.request = .{
103-
// Maximum request body size that we'll process. We can allocate up
87+
// Maximum request body size that we'll process. We can allocate up
10488
// to this much memory per request for the body. Internally, we might
10589
// keep this memory around for a number of requests as an optimization.
10690
.max_body_size = 1_048_576,
@@ -109,17 +93,17 @@ pub fn main() !void {
10993
// this space, else the request will be rejected.
11094
.buffer_size = 4_096,
11195

112-
// Maximum number of headers to accept.
96+
// Maximum number of headers to accept.
11397
// Additional headers will be silently ignored.
11498
.max_header_count = 32,
11599

116100
// Maximum number of URL parameters to accept.
117101
// Additional parameters will be silently ignored.
118-
.max_param_count = 10,
102+
.max_param_count = 0,
119103

120104
// Maximum number of query string parameters to accept.
121105
// Additional parameters will be silently ignored.
122-
.max_query_count = 32,
106+
.max_query_count = 0,
123107

124108
// Maximum number of x-www-form-urlencoded fields to support.
125109
// Additional parameters will be silently ignored. This must be
@@ -132,7 +116,7 @@ pub fn main() !void {
132116
// set to a value greater than 0 (the default) if you're going
133117
// to use the req.multiFormData() method.
134118
.max_multiform_count = 0,
135-
},
119+
},
136120
}, &global);
137121
defer server.deinit();
138122

0 commit comments

Comments
 (0)