Skip to content

Commit

Permalink
Merge pull request #13980 from ziglang/std.net
Browse files Browse the repository at this point in the history
networking: delete std.x; add std.crypto.tls and std.http.Client
  • Loading branch information
andrewrk authored Jan 3, 2023
2 parents 8bd734d + 7178451 commit c9ef277
Show file tree
Hide file tree
Showing 38 changed files with 3,917 additions and 3,707 deletions.
98 changes: 98 additions & 0 deletions lib/std/Url.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
scheme: []const u8,
host: []const u8,
path: []const u8,
port: ?u16,

/// TODO: redo this implementation according to RFC 1738. This code is only a
/// placeholder for now.
pub fn parse(s: []const u8) !Url {
var scheme_end: usize = 0;
var host_start: usize = 0;
var host_end: usize = 0;
var path_start: usize = 0;
var port_start: usize = 0;
var port_end: usize = 0;
var state: enum {
scheme,
scheme_slash1,
scheme_slash2,
host,
port,
path,
} = .scheme;

for (s) |b, i| switch (state) {
.scheme => switch (b) {
':' => {
state = .scheme_slash1;
scheme_end = i;
},
else => {},
},
.scheme_slash1 => switch (b) {
'/' => {
state = .scheme_slash2;
},
else => return error.InvalidUrl,
},
.scheme_slash2 => switch (b) {
'/' => {
state = .host;
host_start = i + 1;
},
else => return error.InvalidUrl,
},
.host => switch (b) {
':' => {
state = .port;
host_end = i;
port_start = i + 1;
},
'/' => {
state = .path;
host_end = i;
path_start = i;
},
else => {},
},
.port => switch (b) {
'/' => {
port_end = i;
state = .path;
path_start = i;
},
else => {},
},
.path => {},
};

const port_slice = s[port_start..port_end];
const port = if (port_slice.len == 0) null else try std.fmt.parseInt(u16, port_slice, 10);

return .{
.scheme = s[0..scheme_end],
.host = s[host_start..host_end],
.path = s[path_start..],
.port = port,
};
}

const Url = @This();
const std = @import("std.zig");
const testing = std.testing;

test "basic" {
const parsed = try parse("https://ziglang.org/download");
try testing.expectEqualStrings("https", parsed.scheme);
try testing.expectEqualStrings("ziglang.org", parsed.host);
try testing.expectEqualStrings("/download", parsed.path);
try testing.expectEqual(@as(?u16, null), parsed.port);
}

test "with port" {
const parsed = try parse("http://example:1337/");
try testing.expectEqualStrings("http", parsed.scheme);
try testing.expectEqualStrings("example", parsed.host);
try testing.expectEqualStrings("/", parsed.path);
try testing.expectEqual(@as(?u16, 1337), parsed.port);
}
4 changes: 2 additions & 2 deletions lib/std/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ pub extern "c" fn sendto(
dest_addr: ?*const c.sockaddr,
addrlen: c.socklen_t,
) isize;
pub extern "c" fn sendmsg(sockfd: c.fd_t, msg: *const std.x.os.Socket.Message, flags: c_int) isize;
pub extern "c" fn sendmsg(sockfd: c.fd_t, msg: *const c.msghdr_const, flags: u32) isize;

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

pub extern "c" fn kill(pid: c.pid_t, sig: c_int) c_int;
pub extern "c" fn getdirentries(fd: c.fd_t, buf_ptr: [*]u8, nbytes: usize, basep: *i64) isize;
Expand Down
11 changes: 10 additions & 1 deletion lib/std/c/darwin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,16 @@ pub const sockaddr = extern struct {
data: [14]u8,

pub const SS_MAXSIZE = 128;
pub const storage = std.x.os.Socket.Address.Native.Storage;
pub const storage = extern struct {
len: u8 align(8),
family: sa_family_t,
padding: [126]u8 = undefined,

comptime {
assert(@sizeOf(storage) == SS_MAXSIZE);
assert(@alignOf(storage) == 8);
}
};
pub const in = extern struct {
len: u8 = @sizeOf(in),
family: sa_family_t = AF.INET,
Expand Down
14 changes: 12 additions & 2 deletions lib/std/c/dragonfly.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const builtin = @import("builtin");
const std = @import("../std.zig");
const assert = std.debug.assert;
const maxInt = std.math.maxInt;
const iovec = std.os.iovec;

Expand Down Expand Up @@ -478,11 +479,20 @@ pub const CLOCK = struct {

pub const sockaddr = extern struct {
len: u8,
family: u8,
family: sa_family_t,
data: [14]u8,

pub const SS_MAXSIZE = 128;
pub const storage = std.x.os.Socket.Address.Native.Storage;
pub const storage = extern struct {
len: u8 align(8),
family: sa_family_t,
padding: [126]u8 = undefined,

comptime {
assert(@sizeOf(storage) == SS_MAXSIZE);
assert(@alignOf(storage) == 8);
}
};

pub const in = extern struct {
len: u8 = @sizeOf(in),
Expand Down
12 changes: 11 additions & 1 deletion lib/std/c/freebsd.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("../std.zig");
const assert = std.debug.assert;
const builtin = @import("builtin");
const maxInt = std.math.maxInt;
const iovec = std.os.iovec;
Expand Down Expand Up @@ -404,7 +405,16 @@ pub const sockaddr = extern struct {
data: [14]u8,

pub const SS_MAXSIZE = 128;
pub const storage = std.x.os.Socket.Address.Native.Storage;
pub const storage = extern struct {
len: u8 align(8),
family: sa_family_t,
padding: [126]u8 = undefined,

comptime {
assert(@sizeOf(storage) == SS_MAXSIZE);
assert(@alignOf(storage) == 8);
}
};

pub const in = extern struct {
len: u8 = @sizeOf(in),
Expand Down
12 changes: 11 additions & 1 deletion lib/std/c/haiku.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("../std.zig");
const assert = std.debug.assert;
const builtin = @import("builtin");
const maxInt = std.math.maxInt;
const iovec = std.os.iovec;
Expand Down Expand Up @@ -339,7 +340,16 @@ pub const sockaddr = extern struct {
data: [14]u8,

pub const SS_MAXSIZE = 128;
pub const storage = std.x.os.Socket.Address.Native.Storage;
pub const storage = extern struct {
len: u8 align(8),
family: sa_family_t,
padding: [126]u8 = undefined,

comptime {
assert(@sizeOf(storage) == SS_MAXSIZE);
assert(@alignOf(storage) == 8);
}
};

pub const in = extern struct {
len: u8 = @sizeOf(in),
Expand Down
12 changes: 11 additions & 1 deletion lib/std/c/netbsd.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("../std.zig");
const assert = std.debug.assert;
const builtin = @import("builtin");
const maxInt = std.math.maxInt;
const iovec = std.os.iovec;
Expand Down Expand Up @@ -481,7 +482,16 @@ pub const sockaddr = extern struct {
data: [14]u8,

pub const SS_MAXSIZE = 128;
pub const storage = std.x.os.Socket.Address.Native.Storage;
pub const storage = extern struct {
len: u8 align(8),
family: sa_family_t,
padding: [126]u8 = undefined,

comptime {
assert(@sizeOf(storage) == SS_MAXSIZE);
assert(@alignOf(storage) == 8);
}
};

pub const in = extern struct {
len: u8 = @sizeOf(in),
Expand Down
12 changes: 11 additions & 1 deletion lib/std/c/openbsd.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("../std.zig");
const assert = std.debug.assert;
const maxInt = std.math.maxInt;
const builtin = @import("builtin");
const iovec = std.os.iovec;
Expand Down Expand Up @@ -372,7 +373,16 @@ pub const sockaddr = extern struct {
data: [14]u8,

pub const SS_MAXSIZE = 256;
pub const storage = std.x.os.Socket.Address.Native.Storage;
pub const storage = extern struct {
len: u8 align(8),
family: sa_family_t,
padding: [254]u8 = undefined,

comptime {
assert(@sizeOf(storage) == SS_MAXSIZE);
assert(@alignOf(storage) == 8);
}
};

pub const in = extern struct {
len: u8 = @sizeOf(in),
Expand Down
11 changes: 10 additions & 1 deletion lib/std/c/solaris.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("../std.zig");
const assert = std.debug.assert;
const builtin = @import("builtin");
const maxInt = std.math.maxInt;
const iovec = std.os.iovec;
Expand Down Expand Up @@ -435,7 +436,15 @@ pub const sockaddr = extern struct {
data: [14]u8,

pub const SS_MAXSIZE = 256;
pub const storage = std.x.os.Socket.Address.Native.Storage;
pub const storage = extern struct {
family: sa_family_t align(8),
padding: [254]u8 = undefined,

comptime {
assert(@sizeOf(storage) == SS_MAXSIZE);
assert(@alignOf(storage) == 8);
}
};

pub const in = extern struct {
family: sa_family_t = AF.INET,
Expand Down
5 changes: 5 additions & 0 deletions lib/std/crypto.zig
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ const std = @import("std.zig");

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

pub const tls = @import("crypto/tls.zig");
pub const Certificate = @import("crypto/Certificate.zig");

test {
_ = aead.aegis.Aegis128L;
_ = aead.aegis.Aegis256;
Expand Down Expand Up @@ -264,6 +267,8 @@ test {
_ = utils;
_ = random;
_ = errors;
_ = tls;
_ = Certificate;
}

test "CSPRNG" {
Expand Down
Loading

0 comments on commit c9ef277

Please sign in to comment.