Skip to content

Commit

Permalink
Some changes, add TLS api.
Browse files Browse the repository at this point in the history
  • Loading branch information
Apprentice-Alchemist committed Jan 25, 2024
1 parent 86fbb34 commit 75b477a
Show file tree
Hide file tree
Showing 19 changed files with 156 additions and 38 deletions.
12 changes: 9 additions & 3 deletions std/python/_std/sys/fs/DirEntry.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package sys.fs;

extern class DirEntry {
function path():Path;
function metadata():Metadata;
import haxe.exceptions.NotImplementedException;

class DirEntry {
public function path():Path {
throw new NotImplementedException();
}
public function metadata():Metadata {
throw new NotImplementedException();
}
}
10 changes: 4 additions & 6 deletions std/python/_std/sys/fs/File.hx
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ class File {
throw new NotImplementedException();
}

final path:String;
final file:RawIOBase;

function new(path:String, file:RawIOBase) {
this.path = path;
this.file = file;
}

Expand All @@ -73,7 +71,7 @@ class File {
}

public function metadata():Metadata {
throw new NotImplementedException();
return new Metadata(Os.fstat(file.fileno()));
}

public function setPermissions(perm:Permissions):Void {
Expand All @@ -83,12 +81,12 @@ class File {
// TODO: technically these should all use a Int64 or even uint64 type, but `haxe.io.Bytes` uses an int length, so...

public function read(bytes:haxe.io.Bytes, bufferOffset:Int, bufferLength:Int):Int {
final view:Memoryview = python.Syntax.arrayAccess(new Memoryview(bytes.getData()), [bufferOffset, bufferOffset + bufferLength]);
final view:Memoryview = python.Syntax.arrayAccess(new Memoryview(bytes.getData()), bufferOffset, bufferOffset + bufferLength);
return file.readinto(view);
}

public function write(bytes:haxe.io.Bytes, bufferOffset:Int, bufferLength:Int):Int {
final view:Memoryview = python.Syntax.arrayAccess(new Memoryview(bytes.getData()), [bufferOffset, bufferOffset + bufferLength]);
final view:Memoryview = python.Syntax.arrayAccess(new Memoryview(bytes.getData()), bufferOffset, bufferOffset + bufferLength);
return file.write(view);
}

Expand All @@ -104,6 +102,6 @@ class File {
}

public function close():Void {
throw new NotImplementedException();
file.close();
}
}
61 changes: 46 additions & 15 deletions std/python/_std/sys/fs/Fs.hx
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
package sys.fs;

import haxe.exceptions.NotImplementedException;
import sys.fs.Metadata;
import python.lib.Os;

@:coreApi
extern class Fs {
static function metadata(path:Path):Metadata;
/**
Returns the metadata for `path` without following symlinks.
**/
static function symlinkMetadata(path:Path):Metadata;
static function setPermissions(path:Path, perm:Permissions):Void;
static function copy(from:Path, to:Path):Void;
static function rename(from:Path, to:Path):Void;
static function readDir(path:Path):Iterator<DirEntry>;
static function createDir(path:Path):Void;
static function createDirRec(path:Path):Void;
static function removeDir(path:Path):Void;
static function removeDirRec(path:Path):Void;
static function removeFile(path:Path):Void;
class Fs {
public static function metadata(path:Path):Metadata {
return new Metadata(Os.stat(path.toString()));
}

public static function symlinkMetadata(path:Path):Metadata {
return new Metadata(Os.lstat(path.toString()));
}

public static function setPermissions(path:Path, perm:Permissions):Void {
throw new NotImplementedException();
}

public static function copy(from:Path, to:Path):Void {
throw new NotImplementedException();
}

public static function rename(from:Path, to:Path):Void {
throw new NotImplementedException();
}

public static function readDir(path:Path):Iterator<DirEntry> {
throw new NotImplementedException();
}

public static function createDir(path:Path):Void {
throw new NotImplementedException();
}

public static function createDirRec(path:Path):Void {
throw new NotImplementedException();
}

public static function removeDir(path:Path):Void {
throw new NotImplementedException();
}

public static function removeDirRec(path:Path):Void {
throw new NotImplementedException();
}

public static function removeFile(path:Path):Void {
throw new NotImplementedException();
}
}
7 changes: 3 additions & 4 deletions std/python/_std/sys/fs/Metadata.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class Metadata {
final stat:Stat;

@:allow(sys.fs)
function new(path:String, followSymlinks:Bool) {
stat = followSymlinks ? Os.stat(path) : Os.lstat(path);
function new(stat:Stat) {
this.stat = stat;
}

public function isDir():Bool {
Expand All @@ -25,15 +25,14 @@ class Metadata {
}

public function size():haxe.Int64 {
return stat.st_size
return stat.st_size;
}

public function permissions():Permissions {
throw new NotImplementedException();
}

public function modified():Null<haxe.time.SystemTime> {
stat.
throw new NotImplementedException();
}

Expand Down
12 changes: 10 additions & 2 deletions std/python/_std/sys/fs/Permissions.hx
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package sys.fs;

@:coreApi
extern class Permissions {
class Permissions {
/**
Note that this does not affect the file, use `Fs.setPermissions`.
**/
var readonly(get, set):Bool;
public var readonly(get, set):Bool;

function get_readonly():Bool {
throw new haxe.exceptions.NotImplementedException();
}

function set_readonly(value:Bool):Bool {
throw new haxe.exceptions.NotImplementedException();
}
}
2 changes: 2 additions & 0 deletions std/python/lib/Os.hx
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,6 @@ extern class Os {
static function fdatasync(fd:Int):Void;

static function lstat(path:String):Stat;

static function fstat(fd:Int):Stat;
}
2 changes: 1 addition & 1 deletion std/sys/fs/Permissions.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sys.fs;
@:coreApi
extern class Permissions {
/**
Note that this does not affect the file, use `Fs.setPermissions`.
Note that changing this does not affect the file, use `Fs.setPermissions`.
**/
var readonly(get, set):Bool;
}
1 change: 0 additions & 1 deletion std/sys/net/DatagramSocket.hx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sys.net;

import haxe.io.Bytes;
import sys.net.IpAddress;

/**
A User Datagram Protocol socket.
Expand Down
28 changes: 28 additions & 0 deletions std/sys/net/Selector.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package sys.net;

import haxe.extern.EitherType;

typedef Selectable = EitherType<DatagramSocket, EitherType<TcpListener, TcpStream>>;

enum abstract Event(Int) {
var Read;
var Write;
var Other;

@:op(A | B) function and(b:Event):Event;
@:op(A & B) function or(b:Event):Event;
}

@:coreApi
extern class Selector {
function new();
function register<T:Selectable>(socket:T, ev:Event, data:Null<Dynamic>):SelectorKey;
function modify<T:Selectable>(socket:T, ev:Event, data:Null<Dynamic>):SelectorKey;
function unregister<T:Selectable>(socket:T):SelectorKey;

function wait(?timeout:Float):KeyValueIterator<SelectorKey, Event>;
}

extern class SelectorKey {
// var socket:
}
2 changes: 1 addition & 1 deletion std/sys/net/SocketAddress.hx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ enum SocketAddress {
}

class SocketAddressTools {
public static function getBytes(s:SocketAddress):haxe.io.Bytes {
public static function toBytes(s:SocketAddress):haxe.io.Bytes {
return switch s {
case Ipv4(i, _): i.toBytes();
case Ipv6(i, _, _, _): i.toBytes();
Expand Down
6 changes: 3 additions & 3 deletions std/sys/net/TcpListener.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ extern class TcpListener {
function localAddress():SocketAddress;
function accept():TcpStream;

public var ttl(get, set):Int;
public var timeToLive(get, set):Int;
public var nonBlocking(get, set):Bool;

function close():Void;

private function get_ttl():Int;
private function set_ttl(value:Int):Int;
private function get_timeToLive():Int;
private function set_timeToLive(value:Int):Int;
private function get_nonBlocking():Bool;
private function set_nonBlocking(value:Bool):Bool;
}
3 changes: 2 additions & 1 deletion std/sys/net/TcpStream.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import haxe.time.Duration;

@:coreApi
extern class TcpStream {
static function connect(address:SocketAddress, ?timeout:Duration):TcpStream;
static function connectByIp(address:SocketAddress, ?timeout:Duration):TcpStream;
static function connectByName(hostname:String, port:Int, ?timeout:Duration):TcpStream;
function peerAddress():SocketAddress;
function localAddress():SocketAddress;
function shutdown(how:Shutdown):Void;
Expand Down
2 changes: 1 addition & 1 deletion std/sys/net/UdpSocket.hx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ package sys.net;
/**
A UDP socket class
**/
@:deprecated("use UDPSocket (note the uppercase)")
@:deprecated("use DatagramSocket")
class UdpSocket extends Socket {
public function new() {
throw "Not available on this platform";
Expand Down
6 changes: 6 additions & 0 deletions std/sys/tls/Certificate.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package sys.tls;

extern class Certificate {
static function fromDer(bytes:haxe.io.Bytes):Certificate;
static function fromPem(bytes:haxe.io.Bytes):Certificate;
}
6 changes: 6 additions & 0 deletions std/sys/tls/Identity.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package sys.tls;

extern class Identity {
static function fromPkcs12(der:haxe.io.Bytes, ?pass:String):Identity;
static function fromPkcs8(pem:haxe.io.Bytes, key:haxe.io.Bytes):Identity;
}
7 changes: 7 additions & 0 deletions std/sys/tls/TlsListener.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sys.tls;

extern class TlsListener extends sys.net.TcpListener {
static function bind(address:SocketAddress, config:TlsListenerConfig):TlsListener;

override function accept():TlsStream;
}
5 changes: 5 additions & 0 deletions std/sys/tls/TlsListenerConfig.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package sys.tls;

@:structInit class TlsListenerConfig {
var identity:Identity;
}
11 changes: 11 additions & 0 deletions std/sys/tls/TlsStream.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package sys.tls;

import haxe.time.Duration;
import sys.net.SocketAddress;

extern class TlsStream extends sys.net.TcpStream {
static function connectByIp(address:SocketAddress, config:TlsStreamConfig, ?timeout:Duration):TlsStream;
static function connectByName(hostname:String, port:Int, config:TlsStreamConfig, ?timeout:Duration):TlsStream;

function peerCertificate():Null<Certificate>;
}
11 changes: 11 additions & 0 deletions std/sys/tls/TlsStreamConfig.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package sys.tls;

@:structInit class TlsStreamConfig {
var use_built_in_roots:Bool;
var extra_roots:Array<Certificate> = [];

/**
For Server Name Indication
**/
var hostname:Null<String> = null;
}

0 comments on commit 75b477a

Please sign in to comment.