Skip to content

Commit c9ef277

Browse files
authored
Merge pull request #13980 from ziglang/std.net
networking: delete std.x; add std.crypto.tls and std.http.Client
2 parents 8bd734d + 7178451 commit c9ef277

38 files changed

+3917
-3707
lines changed

lib/std/Url.zig

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
scheme: []const u8,
2+
host: []const u8,
3+
path: []const u8,
4+
port: ?u16,
5+
6+
/// TODO: redo this implementation according to RFC 1738. This code is only a
7+
/// placeholder for now.
8+
pub fn parse(s: []const u8) !Url {
9+
var scheme_end: usize = 0;
10+
var host_start: usize = 0;
11+
var host_end: usize = 0;
12+
var path_start: usize = 0;
13+
var port_start: usize = 0;
14+
var port_end: usize = 0;
15+
var state: enum {
16+
scheme,
17+
scheme_slash1,
18+
scheme_slash2,
19+
host,
20+
port,
21+
path,
22+
} = .scheme;
23+
24+
for (s) |b, i| switch (state) {
25+
.scheme => switch (b) {
26+
':' => {
27+
state = .scheme_slash1;
28+
scheme_end = i;
29+
},
30+
else => {},
31+
},
32+
.scheme_slash1 => switch (b) {
33+
'/' => {
34+
state = .scheme_slash2;
35+
},
36+
else => return error.InvalidUrl,
37+
},
38+
.scheme_slash2 => switch (b) {
39+
'/' => {
40+
state = .host;
41+
host_start = i + 1;
42+
},
43+
else => return error.InvalidUrl,
44+
},
45+
.host => switch (b) {
46+
':' => {
47+
state = .port;
48+
host_end = i;
49+
port_start = i + 1;
50+
},
51+
'/' => {
52+
state = .path;
53+
host_end = i;
54+
path_start = i;
55+
},
56+
else => {},
57+
},
58+
.port => switch (b) {
59+
'/' => {
60+
port_end = i;
61+
state = .path;
62+
path_start = i;
63+
},
64+
else => {},
65+
},
66+
.path => {},
67+
};
68+
69+
const port_slice = s[port_start..port_end];
70+
const port = if (port_slice.len == 0) null else try std.fmt.parseInt(u16, port_slice, 10);
71+
72+
return .{
73+
.scheme = s[0..scheme_end],
74+
.host = s[host_start..host_end],
75+
.path = s[path_start..],
76+
.port = port,
77+
};
78+
}
79+
80+
const Url = @This();
81+
const std = @import("std.zig");
82+
const testing = std.testing;
83+
84+
test "basic" {
85+
const parsed = try parse("https://ziglang.org/download");
86+
try testing.expectEqualStrings("https", parsed.scheme);
87+
try testing.expectEqualStrings("ziglang.org", parsed.host);
88+
try testing.expectEqualStrings("/download", parsed.path);
89+
try testing.expectEqual(@as(?u16, null), parsed.port);
90+
}
91+
92+
test "with port" {
93+
const parsed = try parse("http://example:1337/");
94+
try testing.expectEqualStrings("http", parsed.scheme);
95+
try testing.expectEqualStrings("example", parsed.host);
96+
try testing.expectEqualStrings("/", parsed.path);
97+
try testing.expectEqual(@as(?u16, 1337), parsed.port);
98+
}

lib/std/c.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ pub extern "c" fn sendto(
206206
dest_addr: ?*const c.sockaddr,
207207
addrlen: c.socklen_t,
208208
) isize;
209-
pub extern "c" fn sendmsg(sockfd: c.fd_t, msg: *const std.x.os.Socket.Message, flags: c_int) isize;
209+
pub extern "c" fn sendmsg(sockfd: c.fd_t, msg: *const c.msghdr_const, flags: u32) isize;
210210

211211
pub extern "c" fn recv(sockfd: c.fd_t, arg1: ?*anyopaque, arg2: usize, arg3: c_int) isize;
212212
pub extern "c" fn recvfrom(
@@ -217,7 +217,7 @@ pub extern "c" fn recvfrom(
217217
noalias src_addr: ?*c.sockaddr,
218218
noalias addrlen: ?*c.socklen_t,
219219
) isize;
220-
pub extern "c" fn recvmsg(sockfd: c.fd_t, msg: *std.x.os.Socket.Message, flags: c_int) isize;
220+
pub extern "c" fn recvmsg(sockfd: c.fd_t, msg: *c.msghdr, flags: u32) isize;
221221

222222
pub extern "c" fn kill(pid: c.pid_t, sig: c_int) c_int;
223223
pub extern "c" fn getdirentries(fd: c.fd_t, buf_ptr: [*]u8, nbytes: usize, basep: *i64) isize;

lib/std/c/darwin.zig

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,16 @@ pub const sockaddr = extern struct {
10071007
data: [14]u8,
10081008

10091009
pub const SS_MAXSIZE = 128;
1010-
pub const storage = std.x.os.Socket.Address.Native.Storage;
1010+
pub const storage = extern struct {
1011+
len: u8 align(8),
1012+
family: sa_family_t,
1013+
padding: [126]u8 = undefined,
1014+
1015+
comptime {
1016+
assert(@sizeOf(storage) == SS_MAXSIZE);
1017+
assert(@alignOf(storage) == 8);
1018+
}
1019+
};
10111020
pub const in = extern struct {
10121021
len: u8 = @sizeOf(in),
10131022
family: sa_family_t = AF.INET,

lib/std/c/dragonfly.zig

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const builtin = @import("builtin");
22
const std = @import("../std.zig");
3+
const assert = std.debug.assert;
34
const maxInt = std.math.maxInt;
45
const iovec = std.os.iovec;
56

@@ -478,11 +479,20 @@ pub const CLOCK = struct {
478479

479480
pub const sockaddr = extern struct {
480481
len: u8,
481-
family: u8,
482+
family: sa_family_t,
482483
data: [14]u8,
483484

484485
pub const SS_MAXSIZE = 128;
485-
pub const storage = std.x.os.Socket.Address.Native.Storage;
486+
pub const storage = extern struct {
487+
len: u8 align(8),
488+
family: sa_family_t,
489+
padding: [126]u8 = undefined,
490+
491+
comptime {
492+
assert(@sizeOf(storage) == SS_MAXSIZE);
493+
assert(@alignOf(storage) == 8);
494+
}
495+
};
486496

487497
pub const in = extern struct {
488498
len: u8 = @sizeOf(in),

lib/std/c/freebsd.zig

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const std = @import("../std.zig");
2+
const assert = std.debug.assert;
23
const builtin = @import("builtin");
34
const maxInt = std.math.maxInt;
45
const iovec = std.os.iovec;
@@ -404,7 +405,16 @@ pub const sockaddr = extern struct {
404405
data: [14]u8,
405406

406407
pub const SS_MAXSIZE = 128;
407-
pub const storage = std.x.os.Socket.Address.Native.Storage;
408+
pub const storage = extern struct {
409+
len: u8 align(8),
410+
family: sa_family_t,
411+
padding: [126]u8 = undefined,
412+
413+
comptime {
414+
assert(@sizeOf(storage) == SS_MAXSIZE);
415+
assert(@alignOf(storage) == 8);
416+
}
417+
};
408418

409419
pub const in = extern struct {
410420
len: u8 = @sizeOf(in),

lib/std/c/haiku.zig

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const std = @import("../std.zig");
2+
const assert = std.debug.assert;
23
const builtin = @import("builtin");
34
const maxInt = std.math.maxInt;
45
const iovec = std.os.iovec;
@@ -339,7 +340,16 @@ pub const sockaddr = extern struct {
339340
data: [14]u8,
340341

341342
pub const SS_MAXSIZE = 128;
342-
pub const storage = std.x.os.Socket.Address.Native.Storage;
343+
pub const storage = extern struct {
344+
len: u8 align(8),
345+
family: sa_family_t,
346+
padding: [126]u8 = undefined,
347+
348+
comptime {
349+
assert(@sizeOf(storage) == SS_MAXSIZE);
350+
assert(@alignOf(storage) == 8);
351+
}
352+
};
343353

344354
pub const in = extern struct {
345355
len: u8 = @sizeOf(in),

lib/std/c/netbsd.zig

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const std = @import("../std.zig");
2+
const assert = std.debug.assert;
23
const builtin = @import("builtin");
34
const maxInt = std.math.maxInt;
45
const iovec = std.os.iovec;
@@ -481,7 +482,16 @@ pub const sockaddr = extern struct {
481482
data: [14]u8,
482483

483484
pub const SS_MAXSIZE = 128;
484-
pub const storage = std.x.os.Socket.Address.Native.Storage;
485+
pub const storage = extern struct {
486+
len: u8 align(8),
487+
family: sa_family_t,
488+
padding: [126]u8 = undefined,
489+
490+
comptime {
491+
assert(@sizeOf(storage) == SS_MAXSIZE);
492+
assert(@alignOf(storage) == 8);
493+
}
494+
};
485495

486496
pub const in = extern struct {
487497
len: u8 = @sizeOf(in),

lib/std/c/openbsd.zig

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const std = @import("../std.zig");
2+
const assert = std.debug.assert;
23
const maxInt = std.math.maxInt;
34
const builtin = @import("builtin");
45
const iovec = std.os.iovec;
@@ -372,7 +373,16 @@ pub const sockaddr = extern struct {
372373
data: [14]u8,
373374

374375
pub const SS_MAXSIZE = 256;
375-
pub const storage = std.x.os.Socket.Address.Native.Storage;
376+
pub const storage = extern struct {
377+
len: u8 align(8),
378+
family: sa_family_t,
379+
padding: [254]u8 = undefined,
380+
381+
comptime {
382+
assert(@sizeOf(storage) == SS_MAXSIZE);
383+
assert(@alignOf(storage) == 8);
384+
}
385+
};
376386

377387
pub const in = extern struct {
378388
len: u8 = @sizeOf(in),

lib/std/c/solaris.zig

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const std = @import("../std.zig");
2+
const assert = std.debug.assert;
23
const builtin = @import("builtin");
34
const maxInt = std.math.maxInt;
45
const iovec = std.os.iovec;
@@ -435,7 +436,15 @@ pub const sockaddr = extern struct {
435436
data: [14]u8,
436437

437438
pub const SS_MAXSIZE = 256;
438-
pub const storage = std.x.os.Socket.Address.Native.Storage;
439+
pub const storage = extern struct {
440+
family: sa_family_t align(8),
441+
padding: [254]u8 = undefined,
442+
443+
comptime {
444+
assert(@sizeOf(storage) == SS_MAXSIZE);
445+
assert(@alignOf(storage) == 8);
446+
}
447+
};
439448

440449
pub const in = extern struct {
441450
family: sa_family_t = AF.INET,

lib/std/crypto.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ const std = @import("std.zig");
176176

177177
pub const errors = @import("crypto/errors.zig");
178178

179+
pub const tls = @import("crypto/tls.zig");
180+
pub const Certificate = @import("crypto/Certificate.zig");
181+
179182
test {
180183
_ = aead.aegis.Aegis128L;
181184
_ = aead.aegis.Aegis256;
@@ -264,6 +267,8 @@ test {
264267
_ = utils;
265268
_ = random;
266269
_ = errors;
270+
_ = tls;
271+
_ = Certificate;
267272
}
268273

269274
test "CSPRNG" {

0 commit comments

Comments
 (0)