Update Files

This commit is contained in:
2025-01-22 16:18:30 +01:00
parent ed4603cf95
commit a36294b518
16718 changed files with 2960346 additions and 0 deletions

View File

@ -0,0 +1,123 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys;
private enum FileKind {
kdir;
kfile;
kother(k:String);
}
@:coreApi
class FileSystem {
public static function exists(path:String):Bool {
return sys_exists(untyped (makeCompatiblePath(path)).__s);
}
public static function rename(path:String, newPath:String):Void {
untyped sys_rename(path.__s, newPath.__s);
}
public static function stat(path:String):FileStat {
var s:FileStat = sys_stat(untyped (makeCompatiblePath(path)).__s);
s.atime = untyped Date.new1(s.atime);
s.mtime = untyped Date.new1(s.mtime);
s.ctime = untyped Date.new1(s.ctime);
return s;
}
public static function fullPath(relPath:String):String {
return new String(file_full_path(untyped relPath.__s));
}
public static function absolutePath(relPath:String):String {
if (haxe.io.Path.isAbsolute(relPath))
return relPath;
return haxe.io.Path.join([Sys.getCwd(), relPath]);
}
static function kind(path:String):FileKind {
var k = new String(sys_file_type(untyped (makeCompatiblePath(path)).__s));
return switch (k) {
case "file": kfile;
case "dir": kdir;
default: kother(k);
}
}
public static inline function isDirectory(path:String):Bool {
return kind(path) == kdir;
}
public static function createDirectory(path:String):Void {
var path = haxe.io.Path.addTrailingSlash(path);
var _p = null;
var parts = [];
while (path != (_p = haxe.io.Path.directory(path))) {
parts.unshift(path);
path = _p;
}
for (part in parts) {
if (part.charCodeAt(part.length - 1) != ":".code && !exists(part))
sys_create_dir(untyped part.__s, 493);
}
}
public static function deleteFile(path:String):Void {
file_delete(untyped path.__s);
}
public static function deleteDirectory(path:String):Void {
sys_remove_dir(untyped path.__s);
}
public static function readDirectory(path:String):Array<String> {
var l:Array<Dynamic> = sys_read_dir(untyped path.__s);
var a = new Array();
while (l != null) {
a.push(new String(l[0]));
l = l[1];
}
return a;
}
private static inline function makeCompatiblePath(path:String):String {
return if (path.charCodeAt(1) == ":".code && path.length <= 3) {
haxe.io.Path.addTrailingSlash(path);
} else if (path == "/") {
"/";
} else {
haxe.io.Path.removeTrailingSlashes(path);
}
}
private static var sys_exists = neko.Lib.load("std", "sys_exists", 1);
private static var file_delete = neko.Lib.load("std", "file_delete", 1);
private static var sys_rename = neko.Lib.load("std", "sys_rename", 2);
private static var sys_stat = neko.Lib.load("std", "sys_stat", 1);
private static var sys_file_type = neko.Lib.load("std", "sys_file_type", 1);
private static var sys_create_dir = neko.Lib.load("std", "sys_create_dir", 2);
private static var sys_remove_dir = neko.Lib.load("std", "sys_remove_dir", 1);
private static var sys_read_dir = neko.Lib.load("std", "sys_read_dir", 1);
private static var file_full_path = neko.Lib.load("std", "file_full_path", 1);
}

View File

@ -0,0 +1,213 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.db;
private class D {
static function load(fun, args):Dynamic {
return neko.Lib.load(lib, fun, args);
}
static var lib = try {
neko.Lib.load("mysql5", "connect", 1);
"mysql5";
} catch (e:Dynamic) "mysql";
public static var connect = load("connect", 1);
public static var select_db = load("select_db", 2);
public static var request = load("request", 2);
public static var close = load("close", 1);
public static var escape = load("escape", 2);
public static var set_conv_funs = load("set_conv_funs", 4);
public static var result_get_length = load("result_get_length", 1);
public static var result_get_nfields = load("result_get_nfields", 1);
public static var result_next = load("result_next", 1);
public static var result_get = load("result_get", 2);
public static var result_get_int = load("result_get_int", 2);
public static var result_get_float = load("result_get_float", 2);
public static var result_fields_names = neko.Lib.loadLazy(lib, "result_get_fields_names", 1);
}
private class MysqlResultSet implements sys.db.ResultSet {
public var length(get, null):Int;
public var nfields(get, null):Int;
private var __r:Dynamic;
private var cache:Dynamic;
public function new(r) {
__r = r;
}
private function get_length() {
return D.result_get_length(__r);
}
private function get_nfields() {
return D.result_get_nfields(__r);
}
public function hasNext() {
if (cache == null)
cache = next();
return (cache != null);
}
public function next():Dynamic {
var c = cache;
if (c != null) {
cache = null;
return c;
}
c = D.result_next(__r);
return c;
}
public function results():List<Dynamic> {
var l = new List();
while (hasNext())
l.add(next());
return l;
}
public function getResult(n:Int) {
return new String(D.result_get(__r, n));
}
public function getIntResult(n:Int):Int {
return D.result_get_int(__r, n);
}
public function getFloatResult(n:Int):Float {
return D.result_get_float(__r, n);
}
public function getFieldsNames():Array<String> {
var a = D.result_fields_names(__r);
untyped {
var i = 0;
var l = __dollar__asize(a);
while (i < l) {
a[i] = new String(a[i]);
i += 1;
}
a = Array.new1(cast a, l);
}
return a;
}
}
private class MysqlConnection implements sys.db.Connection {
private var __c:Dynamic;
public function new(c) {
__c = c;
D.set_conv_funs(c, function(s) return new String(s), function(d) return untyped Date.new1(d), function(b) return haxe.io.Bytes.ofData(b));
}
public function request(s:String):sys.db.ResultSet {
try {
var r = D.request(this.__c, untyped s.__s);
return new MysqlResultSet(r);
} catch (e:Dynamic) {
untyped if (__dollar__typeof(e) == __dollar__tobject && __dollar__typeof(e.msg) == __dollar__tstring)
e = e.msg;
untyped __dollar__rethrow(e);
return null;
}
}
public function close() {
D.close(__c);
}
public function escape(s:String) {
return new String(D.escape(__c, untyped s.__s));
}
public function quote(s:String) {
return "'" + escape(s) + "'";
}
public function addValue(s:StringBuf, v:Dynamic) {
var t = untyped __dollar__typeof(v);
if (untyped (t == __dollar__tint || t == __dollar__tnull))
s.add(v);
else if (untyped t == __dollar__tbool)
s.addChar(if (v) "1".code else "0".code);
else {
s.addChar("'".code);
s.add(escape(Std.string(v)));
s.addChar("'".code);
}
}
public function lastInsertId() {
return request("SELECT LAST_INSERT_ID()").getIntResult(0);
}
public function dbName() {
return "MySQL";
}
public function startTransaction() {
request("START TRANSACTION");
}
public function commit() {
request("COMMIT");
}
public function rollback() {
request("ROLLBACK");
}
private static var __use_date = Date;
}
@:coreApi class Mysql {
public static function connect(params:{
host:String,
?port:Int,
user:String,
pass:String,
?socket:String,
?database:String
}):sys.db.Connection {
var o = untyped {
host: params.host.__s,
port: if (params.port == null) 3306 else params.port,
user: params.user.__s,
pass: params.pass.__s,
socket: if (params.socket == null) null else params.socket.__s
};
var c = D.connect(o);
if (params.database != null) {
try {
D.select_db(c, untyped params.database.__s);
} catch (e:Dynamic) {
D.close(c);
neko.Lib.rethrow(e);
}
}
return new MysqlConnection(c);
}
}

View File

@ -0,0 +1,199 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.db;
private class SqliteConnection implements Connection {
var c:Dynamic;
public function new(file:String) {
c = _connect(untyped file.__s);
}
public function close() {
_close(c);
}
public function request(s:String):ResultSet {
try {
return new SqliteResultSet(_request(c, untyped s.__s));
} catch (e:String) {
throw "Error while executing " + s + " (" + e + ")";
}
}
public function escape(s:String) {
return s.split("'").join("''");
}
public function quote(s:String) {
if (s.indexOf("\000") >= 0)
return "x'" + new String(untyped _encode(s.__s, "0123456789ABCDEF".__s)) + "'";
return "'" + s.split("'").join("''") + "'";
}
public function addValue(s:StringBuf, v:Dynamic) {
var t = untyped __dollar__typeof(v);
if (untyped (t == __dollar__tint || t == __dollar__tnull))
s.add(v);
else if (untyped t == __dollar__tbool)
s.add(if (v) 1 else 0);
else
s.add(quote(Std.string(v)));
}
public function lastInsertId() {
return _last_id(c);
}
public function dbName() {
return "SQLite";
}
public function startTransaction() {
request("BEGIN TRANSACTION");
}
public function commit() {
request("COMMIT");
}
public function rollback() {
request("ROLLBACK");
}
static var _encode = neko.Lib.load("std", "base_encode", 2);
static var _connect = neko.Lib.load("sqlite", "connect", 1);
static var _close = neko.Lib.load("sqlite", "close", 1);
static var _request = neko.Lib.load("sqlite", "request", 2);
static var _last_id = neko.Lib.load("sqlite", "last_insert_id", 1);
}
private class SqliteResultSet implements ResultSet {
public var length(get, null):Int;
public var nfields(get, null):Int;
var r:Dynamic;
var cache:List<Dynamic>;
public function new(r) {
cache = new List();
this.r = r;
hasNext(); // execute the request
}
function get_length() {
if (nfields != 0) {
while (true) {
var c = doNext();
if (c == null)
break;
cache.add(c);
}
return cache.length;
}
return result_get_length(r);
}
function get_nfields() {
return result_get_nfields(r);
}
public function hasNext() {
var c = next();
if (c == null)
return false;
cache.push(c);
return true;
}
public function next():Dynamic {
var c = cache.pop();
if (c != null)
return c;
return doNext();
}
private function doNext():Dynamic {
var c = result_next(r);
if (c == null)
return null;
untyped {
var f = __dollar__objfields(c);
var i = 0;
var l = __dollar__asize(f);
while (i < l) {
var v = __dollar__objget(c, f[i]);
if (__dollar__typeof(v) == __dollar__tstring)
__dollar__objset(c, f[i], new String(v));
i = i + 1;
}
}
return c;
}
public function results():List<Dynamic> {
var l = new List();
while (true) {
var c = next();
if (c == null)
break;
l.add(c);
}
return l;
}
public function getResult(n:Int) {
return new String(result_get(r, n));
}
public function getIntResult(n:Int):Int {
return result_get_int(r, n);
}
public function getFloatResult(n:Int):Float {
return result_get_float(r, n);
}
public function getFieldsNames():Array<String> {
if(hasNext()) {
return switch cache.first() {
case null: null;
case row: Reflect.fields(row);
}
}
return null;
}
static var result_next = neko.Lib.load("sqlite", "result_next", 1);
static var result_get_length = neko.Lib.load("sqlite", "result_get_length", 1);
static var result_get_nfields = neko.Lib.load("sqlite", "result_get_nfields", 1);
static var result_get = neko.Lib.load("sqlite", "result_get", 2);
static var result_get_int = neko.Lib.load("sqlite", "result_get_int", 2);
static var result_get_float = neko.Lib.load("sqlite", "result_get_float", 2);
}
@:coreApi class Sqlite {
public static function open(file:String):Connection {
return new SqliteConnection(file);
}
}

View File

@ -0,0 +1,77 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.io;
enum FileHandle {}
@:coreApi class File {
public static function getContent(path:String):String {
return new String(file_contents(untyped path.__s));
}
public static function getBytes(path:String):haxe.io.Bytes {
return neko.Lib.bytesReference(getContent(path));
}
public static function saveContent(path:String, content:String):Void {
var f = write(path);
f.writeString(content);
f.close();
}
public static function saveBytes(path:String, bytes:haxe.io.Bytes):Void {
var f = write(path);
f.write(bytes);
f.close();
}
public static function read(path:String, binary:Bool = true):FileInput {
return untyped new FileInput(file_open(path.__s, (if (binary) "rb" else "r").__s));
}
public static function write(path:String, binary:Bool = true):FileOutput {
return untyped new FileOutput(file_open(path.__s, (if (binary) "wb" else "w").__s));
}
public static function append(path:String, binary:Bool = true):FileOutput {
return untyped new FileOutput(file_open(path.__s, (if (binary) "ab" else "a").__s));
}
public static function update(path:String, binary:Bool = true):FileOutput {
if (!FileSystem.exists(path)) {
write(path).close();
}
return untyped new FileOutput(file_open(path.__s, (if (binary) "rb+" else "r+").__s));
}
public static function copy(srcPath:String, dstPath:String):Void {
var s = read(srcPath, true);
var d = write(dstPath, true);
d.writeInput(s);
s.close();
d.close();
}
private static var file_contents = neko.Lib.load("std", "file_contents", 1);
private static var file_open = neko.Lib.load("std", "file_open", 2);
}

View File

@ -0,0 +1,83 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.io;
@:coreApi class FileInput extends haxe.io.Input {
private var __f:File.FileHandle;
function new(f:File.FileHandle):Void {
__f = f;
}
public override function readByte():Int {
return try {
file_read_char(__f);
} catch (e:Dynamic) {
if (untyped __dollar__typeof(e) == __dollar__tarray)
throw new haxe.io.Eof();
else
throw haxe.io.Error.Custom(e);
}
}
public override function readBytes(s:haxe.io.Bytes, p:Int, l:Int):Int {
return try {
file_read(__f, s.getData(), p, l);
} catch (e:Dynamic) {
if (untyped __dollar__typeof(e) == __dollar__tarray)
throw new haxe.io.Eof();
else
throw haxe.io.Error.Custom(e);
}
}
public override function close():Void {
super.close();
file_close(__f);
}
public function seek(p:Int, pos:FileSeek):Void {
file_seek(__f, p, switch (pos) {
case SeekBegin: 0;
case SeekCur: 1;
case SeekEnd: 2;
});
}
public function tell():Int {
return file_tell(__f);
}
public function eof():Bool {
return file_eof(__f);
}
private static var file_eof = neko.Lib.load("std", "file_eof", 1);
private static var file_read = neko.Lib.load("std", "file_read", 4);
private static var file_read_char = neko.Lib.load("std", "file_read_char", 1);
private static var file_close = neko.Lib.load("std", "file_close", 1);
private static var file_seek = neko.Lib.load("std", "file_seek", 3);
private static var file_tell = neko.Lib.load("std", "file_tell", 1);
}

View File

@ -0,0 +1,71 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.io;
@:coreApi class FileOutput extends haxe.io.Output {
private var __f:File.FileHandle;
function new(f:File.FileHandle):Void {
__f = f;
}
public override function writeByte(c:Int):Void {
try
file_write_char(__f, c)
catch (e:Dynamic)
throw haxe.io.Error.Custom(e);
}
public override function writeBytes(s:haxe.io.Bytes, p:Int, l:Int):Int {
return try file_write(__f, s.getData(), p, l) catch (e:Dynamic) throw haxe.io.Error.Custom(e);
}
public override function flush():Void {
file_flush(__f);
}
public override function close():Void {
super.close();
file_close(__f);
}
public function seek(p:Int, pos:FileSeek):Void {
file_seek(__f, p, switch (pos) {
case SeekBegin: 0;
case SeekCur: 1;
case SeekEnd: 2;
});
}
public function tell():Int {
return file_tell(__f);
}
private static var file_close = neko.Lib.load("std", "file_close", 1);
private static var file_seek = neko.Lib.load("std", "file_seek", 3);
private static var file_tell = neko.Lib.load("std", "file_tell", 1);
private static var file_flush = neko.Lib.load("std", "file_flush", 1);
private static var file_write = neko.Lib.load("std", "file_write", 4);
private static var file_write_char = neko.Lib.load("std", "file_write_char", 2);
}

View File

@ -0,0 +1,124 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.io;
private class Stdin extends haxe.io.Output {
var p:Dynamic;
var buf:haxe.io.Bytes;
public function new(p:Dynamic) {
this.p = p;
buf = haxe.io.Bytes.alloc(1);
}
public override function close() {
super.close();
_stdin_close(p);
}
public override function writeByte(c) {
buf.set(0, c);
writeBytes(buf, 0, 1);
}
public override function writeBytes(buf:haxe.io.Bytes, pos:Int, len:Int):Int {
try {
return _stdin_write(p, buf.getData(), pos, len);
} catch (e:Dynamic) {
throw new haxe.io.Eof();
}
}
static var _stdin_write = neko.Lib.load("std", "process_stdin_write", 4);
static var _stdin_close = neko.Lib.load("std", "process_stdin_close", 1);
}
private class Stdout extends haxe.io.Input {
var p:Dynamic;
var out:Bool;
var buf:haxe.io.Bytes;
public function new(p:Dynamic, out) {
this.p = p;
this.out = out;
buf = haxe.io.Bytes.alloc(1);
}
public override function readByte() {
if (readBytes(buf, 0, 1) == 0)
throw haxe.io.Error.Blocked;
return buf.get(0);
}
public override function readBytes(str:haxe.io.Bytes, pos:Int, len:Int):Int {
try {
return (out ? _stdout_read : _stderr_read)(p, str.getData(), pos, len);
} catch (e:Dynamic) {
throw new haxe.io.Eof();
}
}
static var _stdout_read = neko.Lib.load("std", "process_stdout_read", 4);
static var _stderr_read = neko.Lib.load("std", "process_stderr_read", 4);
}
@:coreApi class Process {
var p:Dynamic;
public var stdout(default, null):haxe.io.Input;
public var stderr(default, null):haxe.io.Input;
public var stdin(default, null):haxe.io.Output;
public function new(cmd:String, ?args:Array<String>, ?detached:Bool):Void {
if (detached)
throw "Detached process is not supported on this platform";
p = try _run(untyped cmd.__s, neko.Lib.haxeToNeko(args)) catch (e:Dynamic) throw "Process creation failure : " + cmd;
stdin = new Stdin(p);
stdout = new Stdout(p, true);
stderr = new Stdout(p, false);
}
public function getPid():Int {
return _pid(p);
}
public function exitCode(block:Bool = true):Null<Int> {
if (block == false)
throw "Non blocking exitCode() not supported on this platform";
return _exit(p);
}
public function close():Void {
_close(p);
}
public function kill():Void {
_kill(p);
}
static var _run = neko.Lib.load("std", "process_run", 2);
static var _exit = neko.Lib.load("std", "process_exit", 1);
static var _pid = neko.Lib.load("std", "process_pid", 1);
static var _close = neko.Lib.loadLazy("std", "process_close", 1);
static var _kill = neko.Lib.loadLazy("std", "process_kill", 1);
}

View File

@ -0,0 +1,57 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.net;
@:coreApi
@:keepInit
class Host {
public var host(default, null):String;
public var ip(default, null):Int;
public function new(name:String):Void {
host = name;
ip = host_resolve(untyped name.__s);
}
public function toString():String {
return new String(host_to_string(ip));
}
public function reverse():String {
return new String(host_reverse(ip));
}
public static function localhost():String {
return new String(host_local());
}
static function __init__():Void {
neko.Lib.load("std", "socket_init", 0)();
}
private static var host_resolve = neko.Lib.load("std", "host_resolve", 1);
private static var host_reverse = neko.Lib.load("std", "host_reverse", 1);
private static var host_to_string = neko.Lib.load("std", "host_to_string", 1);
private static var host_local = neko.Lib.load("std", "host_local", 0);
}

View File

@ -0,0 +1,284 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.net;
import haxe.io.Error;
@:callable
@:coreType
abstract SocketHandle {}
private class SocketOutput extends haxe.io.Output {
var __s:SocketHandle;
public function new(s) {
__s = s;
}
public override function writeByte(c:Int) {
try {
socket_send_char(__s, c);
} catch (e:Dynamic) {
if (e == "Blocking")
throw Blocked;
else if (e == "EOF")
throw new haxe.io.Eof();
else
throw Custom(e);
}
}
public override function writeBytes(buf:haxe.io.Bytes, pos:Int, len:Int):Int {
return try {
socket_send(__s, buf.getData(), pos, len);
} catch (e:Dynamic) {
if (e == "Blocking")
throw Blocked;
else
throw Custom(e);
}
}
public override function close() {
super.close();
if (__s != null)
socket_close(__s);
}
private static var socket_close = neko.Lib.load("std", "socket_close", 1);
private static var socket_send_char = neko.Lib.load("std", "socket_send_char", 2);
private static var socket_send = neko.Lib.load("std", "socket_send", 4);
}
private class SocketInput extends haxe.io.Input {
var __s:SocketHandle;
public function new(s) {
__s = s;
}
public override function readByte():Int {
return try {
socket_recv_char(__s);
} catch (e:Dynamic) {
if (e == "Blocking")
throw Blocked;
else if (__s == null)
throw Custom(e);
else
throw new haxe.io.Eof();
}
}
public override function readBytes(buf:haxe.io.Bytes, pos:Int, len:Int):Int {
var r;
try {
r = socket_recv(__s, buf.getData(), pos, len);
} catch (e:Dynamic) {
if (e == "Blocking")
throw Blocked;
else
throw Custom(e);
}
if (r == 0)
throw new haxe.io.Eof();
return r;
}
public override function close() {
super.close();
if (__s != null)
socket_close(__s);
}
private static var socket_recv = neko.Lib.load("std", "socket_recv", 4);
private static var socket_recv_char = neko.Lib.load("std", "socket_recv_char", 1);
private static var socket_close = neko.Lib.load("std", "socket_close", 1);
}
@:coreApi
class Socket {
private var __s:SocketHandle;
public var input(default, null):haxe.io.Input;
public var output(default, null):haxe.io.Output;
public var custom:Dynamic;
public function new():Void {
init();
}
private function init():Void {
if (__s == null)
__s = socket_new(false);
input = new SocketInput(__s);
output = new SocketOutput(__s);
}
public function close():Void {
socket_close(__s);
untyped {
input.__s = null;
output.__s = null;
}
input.close();
output.close();
}
public function read():String {
return new String(socket_read(__s));
}
public function write(content:String):Void {
socket_write(__s, untyped content.__s);
}
public function connect(host:Host, port:Int):Void {
try {
socket_connect(__s, host.ip, port);
} catch (s:String) {
if (s == "std@socket_connect")
throw "Failed to connect on " + host.toString() + ":" + port;
else if (s == "Blocking") {
// Do nothing, this is not a real error, it simply indicates
// that a non-blocking connect is in progress
} else
neko.Lib.rethrow(s);
}
}
public function listen(connections:Int):Void {
socket_listen(__s, connections);
}
public function shutdown(read:Bool, write:Bool):Void {
socket_shutdown(__s, read, write);
}
public function bind(host:Host, port:Int):Void {
socket_bind(__s, host.ip, port);
}
public function accept():Socket {
var c = socket_accept(__s);
var s = Type.createEmptyInstance(Socket);
s.__s = c;
s.input = new SocketInput(c);
s.output = new SocketOutput(c);
return s;
}
public function peer():{host:Host, port:Int} {
var a:Dynamic = socket_peer(__s);
if (a == null) {
return null;
}
var h = new Host("127.0.0.1");
untyped h.ip = a[0];
return {host: h, port: a[1]};
}
public function host():{host:Host, port:Int} {
var a:Dynamic = socket_host(__s);
if (a == null) {
return null;
}
var h = new Host("127.0.0.1");
untyped h.ip = a[0];
return {host: h, port: a[1]};
}
public function setTimeout(timeout:Float):Void {
socket_set_timeout(__s, timeout);
}
public function waitForRead():Void {
select([this], null, null, null);
}
public function setBlocking(b:Bool):Void {
socket_set_blocking(__s, b);
}
public function setFastSend(b:Bool):Void {
socket_set_fast_send(__s, b);
}
public static function select(read:Array<Socket>, write:Array<Socket>, others:Array<Socket>,
?timeout:Float):{read:Array<Socket>, write:Array<Socket>, others:Array<Socket>} {
var c = untyped __dollar__hnew(1);
var f = function(a:Array<Socket>) {
if (a == null)
return null;
untyped {
var r = __dollar__amake(a.length);
var i = 0;
while (i < a.length) {
r[i] = a[i].__s;
__dollar__hadd(c, a[i].__s, a[i]);
i += 1;
}
return r;
}
}
var neko_array = socket_select(f(read), f(write), f(others), timeout);
var g = function(a):Array<Socket> {
if (a == null)
return null;
var r = new Array();
var i = 0;
while (i < untyped __dollar__asize(a)) {
var t = untyped __dollar__hget(c, a[i], null);
if (t == null)
throw "Socket object not found.";
r[i] = t;
i += 1;
}
return r;
}
return {
read: g(neko_array[0]),
write: g(neko_array[1]),
others: g(neko_array[2])
};
}
private static var socket_new = neko.Lib.load("std", "socket_new", 1);
private static var socket_close = neko.Lib.load("std", "socket_close", 1);
private static var socket_write = neko.Lib.load("std", "socket_write", 2);
private static var socket_read = neko.Lib.load("std", "socket_read", 1);
private static var socket_connect = neko.Lib.load("std", "socket_connect", 3);
private static var socket_listen = neko.Lib.load("std", "socket_listen", 2);
private static var socket_select = neko.Lib.load("std", "socket_select", 4);
private static var socket_bind = neko.Lib.load("std", "socket_bind", 3);
private static var socket_accept = neko.Lib.load("std", "socket_accept", 1);
private static var socket_peer = neko.Lib.load("std", "socket_peer", 1);
private static var socket_host = neko.Lib.load("std", "socket_host", 1);
private static var socket_set_timeout = neko.Lib.load("std", "socket_set_timeout", 2);
private static var socket_shutdown = neko.Lib.load("std", "socket_shutdown", 3);
private static var socket_set_blocking = neko.Lib.load("std", "socket_set_blocking", 2);
private static var socket_set_fast_send = neko.Lib.loadLazy("std", "socket_set_fast_send", 2);
}

View File

@ -0,0 +1,67 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.net;
import haxe.io.Error;
@:coreApi
class UdpSocket extends Socket {
private override function init():Void {
__s = Socket.socket_new(true);
super.init();
}
public function sendTo(buf:haxe.io.Bytes, pos:Int, len:Int, addr:Address):Int {
return try {
socket_send_to(__s, buf.getData(), pos, len, addr);
} catch (e:Dynamic) {
if (e == "Blocking")
throw Blocked;
else
throw Custom(e);
}
}
public function readFrom(buf:haxe.io.Bytes, pos:Int, len:Int, addr:Address):Int {
var r;
try {
r = socket_recv_from(__s, buf.getData(), pos, len, addr);
} catch (e:Dynamic) {
if (e == "Blocking")
throw Blocked;
else
throw Custom(e);
}
if (r == 0)
throw new haxe.io.Eof();
return r;
}
public function setBroadcast(b:Bool):Void {
socket_set_broadcast(__s, b);
}
static var socket_recv_from = neko.Lib.loadLazy("std", "socket_recv_from", 5);
static var socket_send_to = neko.Lib.loadLazy("std", "socket_send_to", 5);
static var socket_set_broadcast = neko.Lib.loadLazy("std", "socket_set_broadcast", 2);
}

View File

@ -0,0 +1,150 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.ssl;
@:coreApi
class Certificate {
var __h:Null<Certificate>;
var __x:Dynamic;
@:allow(sys.ssl.Socket)
function new(x:Dynamic, ?h:Certificate) {
__x = x;
__h = h;
}
public static function loadFile(file:String):Certificate {
return new Certificate(cert_load_file(untyped file.__s));
}
public static function loadPath(path:String):Certificate {
return new Certificate(cert_load_path(untyped path.__s));
}
public static function fromString(str:String):Certificate {
return new Certificate(cert_add_pem(null, untyped str.__s));
}
public static function loadDefaults():Certificate {
var x = cert_load_defaults();
if (x != null)
return new Certificate(x);
var defPaths = null;
switch (Sys.systemName()) {
case "Linux":
defPaths = [
"/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu/Gentoo etc.
"/etc/pki/tls/certs/ca-bundle.crt", // Fedora/RHEL
"/etc/ssl/ca-bundle.pem", // OpenSUSE
"/etc/pki/tls/cacert.pem", // OpenELEC
"/etc/ssl/certs", // SLES10/SLES11
"/system/etc/security/cacerts" // Android
];
case "BSD":
defPaths = [
"/usr/local/share/certs/ca-root-nss.crt", // FreeBSD/DragonFly
"/etc/ssl/cert.pem", // OpenBSD
"/etc/openssl/certs/ca-certificates.crt", // NetBSD
];
case "Android":
defPaths = ["/system/etc/security/cacerts"];
default:
}
if (defPaths != null) {
for (path in defPaths) {
if (sys.FileSystem.exists(path)) {
if (sys.FileSystem.isDirectory(path))
return loadPath(path);
else
return loadFile(path);
}
}
}
return null;
}
public var commonName(get, null):Null<String>;
public var altNames(get, null):Array<String>;
public var notBefore(get, null):Date;
public var notAfter(get, null):Date;
function get_commonName():Null<String> {
return subject("CN");
}
function get_altNames():Array<String> {
var l:Dynamic = cert_get_altnames(__x);
var a = new Array<String>();
while (l != null) {
a.push(new String(l[0]));
l = l[1];
}
return a;
}
public function subject(field:String):Null<String> {
var s = cert_get_subject(__x, untyped field.__s);
return s == null ? null : new String(cast s);
}
public function issuer(field:String):Null<String> {
var s = cert_get_issuer(__x, untyped field.__s);
return s == null ? null : new String(cast s);
}
function get_notBefore():Date {
var a = cert_get_notbefore(__x);
return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
}
function get_notAfter():Date {
var a = cert_get_notafter(__x);
return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
}
public function next():Null<Certificate> {
var n = cert_get_next(__x);
return n == null ? null : new Certificate(n, __h == null ? this : __h);
}
public function add(pem:String):Void {
cert_add_pem(__x, untyped pem.__s);
}
public function addDER(der:haxe.io.Bytes):Void {
cert_add_der(__x, der.getData());
}
private static var cert_load_defaults = neko.Lib.loadLazy("ssl", "cert_load_defaults", 0);
private static var cert_load_file = neko.Lib.loadLazy("ssl", "cert_load_file", 1);
private static var cert_load_path = neko.Lib.loadLazy("ssl", "cert_load_path", 1);
private static var cert_get_subject = neko.Lib.loadLazy("ssl", "cert_get_subject", 2);
private static var cert_get_issuer = neko.Lib.loadLazy("ssl", "cert_get_issuer", 2);
private static var cert_get_altnames = neko.Lib.loadLazy("ssl", "cert_get_altnames", 1);
private static var cert_get_notbefore = neko.Lib.loadLazy("ssl", "cert_get_notbefore", 1);
private static var cert_get_notafter = neko.Lib.loadLazy("ssl", "cert_get_notafter", 1);
private static var cert_get_next = neko.Lib.loadLazy("ssl", "cert_get_next", 1);
private static var cert_add_pem = neko.Lib.loadLazy("ssl", "cert_add_pem", 2);
private static var cert_add_der = neko.Lib.loadLazy("ssl", "cert_add_der", 2);
}

View File

@ -0,0 +1,42 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.ssl;
@:coreApi
class Digest {
public static function make(data:haxe.io.Bytes, alg:DigestAlgorithm):haxe.io.Bytes {
return haxe.io.Bytes.ofData(dgst_make(data.getData(), untyped alg.__s));
}
public static function sign(data:haxe.io.Bytes, privKey:Key, alg:DigestAlgorithm):haxe.io.Bytes {
return haxe.io.Bytes.ofData(dgst_sign(data.getData(), @:privateAccess privKey.__k, untyped alg.__s));
}
public static function verify(data:haxe.io.Bytes, signature:haxe.io.Bytes, pubKey:Key, alg:DigestAlgorithm):Bool {
return dgst_verify(data.getData(), signature.getData(), @:privateAccess pubKey.__k, untyped alg.__s);
}
private static var dgst_make = neko.Lib.loadLazy("ssl", "dgst_make", 2);
private static var dgst_sign = neko.Lib.loadLazy("ssl", "dgst_sign", 3);
private static var dgst_verify = neko.Lib.loadLazy("ssl", "dgst_verify", 4);
}

View File

@ -0,0 +1,54 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.ssl;
private typedef PKEY = Dynamic;
@:coreApi
class Key {
private var __k:PKEY;
private function new(k:PKEY) {
__k = k;
}
public static function loadFile(file:String, ?isPublic:Bool, ?pass:String):Key {
var data = sys.io.File.getBytes(file);
var str = neko.Lib.stringReference(data);
if (str.indexOf("-----BEGIN ") >= 0)
return readPEM(str, isPublic == true, pass);
else
return readDER(data, isPublic == true);
}
public static function readPEM(data:String, isPublic:Bool, ?pass:String):Key {
return new Key(key_from_pem(untyped data.__s, isPublic, pass == null ? null : untyped pass.__s));
}
public static function readDER(data:haxe.io.Bytes, isPublic:Bool):Key {
return new Key(key_from_der(data.getData(), isPublic));
}
private static var key_from_pem = neko.Lib.loadLazy("ssl", "key_from_pem", 3);
private static var key_from_der = neko.Lib.loadLazy("ssl", "key_from_der", 2);
}

View File

@ -0,0 +1,316 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.ssl;
private typedef SocketHandle = Dynamic;
private typedef CTX = Dynamic;
private typedef SSL = Dynamic;
private class SocketInput extends haxe.io.Input {
@:allow(sys.ssl.Socket) private var __s:Socket;
public function new(s:Socket) {
this.__s = s;
}
public override function readByte() {
return try {
__s.handshake();
ssl_recv_char(@:privateAccess __s.ssl);
} catch (e:Dynamic) {
if (e == "Blocking")
throw haxe.io.Error.Blocked;
else if (__s == null)
throw haxe.io.Error.Custom(e);
else
throw new haxe.io.Eof();
}
}
public override function readBytes(buf:haxe.io.Bytes, pos:Int, len:Int):Int {
var r:Int;
if (__s == null)
throw "Invalid handle";
try {
__s.handshake();
r = ssl_recv(@:privateAccess __s.ssl, buf.getData(), pos, len);
} catch (e:Dynamic) {
if (e == "Blocking")
throw haxe.io.Error.Blocked;
else
throw haxe.io.Error.Custom(e);
}
if (r == 0)
throw new haxe.io.Eof();
return r;
}
public override function close() {
super.close();
if (__s != null)
__s.close();
}
private static var ssl_recv = neko.Lib.loadLazy("ssl", "ssl_recv", 4);
private static var ssl_recv_char = neko.Lib.loadLazy("ssl", "ssl_recv_char", 1);
}
private class SocketOutput extends haxe.io.Output {
@:allow(sys.ssl.Socket) private var __s:Socket;
public function new(s:Socket) {
this.__s = s;
}
public override function writeByte(c:Int) {
if (__s == null)
throw "Invalid handle";
try {
__s.handshake();
ssl_send_char(@:privateAccess __s.ssl, c);
} catch (e:Dynamic) {
if (e == "Blocking")
throw haxe.io.Error.Blocked;
else
throw haxe.io.Error.Custom(e);
}
}
public override function writeBytes(buf:haxe.io.Bytes, pos:Int, len:Int):Int {
return try {
__s.handshake();
ssl_send(@:privateAccess __s.ssl, buf.getData(), pos, len);
} catch (e:Dynamic) {
if (e == "Blocking")
throw haxe.io.Error.Blocked;
else
throw haxe.io.Error.Custom(e);
}
}
public override function close() {
super.close();
if (__s != null)
__s.close();
}
private static var ssl_send_char = neko.Lib.loadLazy("ssl", "ssl_send_char", 2);
private static var ssl_send = neko.Lib.loadLazy("ssl", "ssl_send", 4);
}
@:coreApi
class Socket extends sys.net.Socket {
public static var DEFAULT_VERIFY_CERT:Null<Bool> = true;
public static var DEFAULT_CA:Null<Certificate>;
private var ctx:CTX;
private var ssl:SSL;
public var verifyCert:Null<Bool>;
private var caCert:Null<Certificate>;
private var hostname:String;
private var ownCert:Null<Certificate>;
private var ownKey:Null<Key>;
private var altSNIContexts:Null<Array<{match:String->Bool, key:Key, cert:Certificate}>>;
private var sniCallback:Dynamic;
private var handshakeDone:Bool;
private override function init():Void {
__s = socket_new(false);
input = new SocketInput(this);
output = new SocketOutput(this);
if (DEFAULT_VERIFY_CERT && DEFAULT_CA == null) {
try {
DEFAULT_CA = Certificate.loadDefaults();
} catch (e:Dynamic) {}
}
verifyCert = DEFAULT_VERIFY_CERT;
caCert = DEFAULT_CA;
}
public override function connect(host:sys.net.Host, port:Int):Void {
try {
ctx = buildSSLContext(false);
ssl = ssl_new(ctx);
ssl_set_socket(ssl, __s);
handshakeDone = false;
if (hostname == null)
hostname = host.host;
if (hostname != null)
ssl_set_hostname(ssl, untyped hostname.__s);
socket_connect(__s, host.ip, port);
handshake();
} catch (s:String) {
if (s == "std@socket_connect")
throw "Failed to connect on " + host.host + ":" + port;
else
neko.Lib.rethrow(s);
} catch (e:Dynamic) {
neko.Lib.rethrow(e);
}
}
public function handshake():Void {
if (!handshakeDone) {
try {
ssl_handshake(ssl);
handshakeDone = true;
} catch (e:Dynamic) {
if (e == "Blocking")
throw haxe.io.Error.Blocked;
else
neko.Lib.rethrow(e);
}
}
}
public function setCA(cert:Certificate):Void {
caCert = cert;
}
public function setHostname(name:String):Void {
hostname = name;
}
public function setCertificate(cert:Certificate, key:Key):Void {
ownCert = cert;
ownKey = key;
}
public override function read():String {
handshake();
var b = ssl_read(ssl);
if (b == null)
return "";
return new String(cast b);
}
public override function write(content:String):Void {
handshake();
ssl_write(ssl, untyped content.__s);
}
public override function close():Void {
if (ssl != null)
ssl_close(ssl);
if (ctx != null)
conf_close(ctx);
if (altSNIContexts != null)
sniCallback = null;
socket_close(__s);
var input:SocketInput = cast input;
var output:SocketOutput = cast output;
@:privateAccess input.__s = output.__s = null;
input.close();
output.close();
}
public function addSNICertificate(cbServernameMatch:String->Bool, cert:Certificate, key:Key):Void {
if (altSNIContexts == null)
altSNIContexts = [];
altSNIContexts.push({match: cbServernameMatch, cert: cert, key: key});
}
public override function bind(host:sys.net.Host, port:Int):Void {
ctx = buildSSLContext(true);
socket_bind(__s, host.ip, port);
}
public override function accept():Socket {
var c = socket_accept(__s);
var ssl = ssl_new(ctx);
ssl_set_socket(ssl, c);
var s = Type.createEmptyInstance(sys.ssl.Socket);
s.__s = c;
s.ssl = ssl;
s.input = new SocketInput(s);
s.output = new SocketOutput(s);
s.handshakeDone = false;
return s;
}
public function peerCertificate():sys.ssl.Certificate {
var x = ssl_get_peer_certificate(ssl);
return x == null ? null : new sys.ssl.Certificate(x);
}
private function buildSSLContext(server:Bool):CTX {
var ctx:CTX = conf_new(server);
if (ownCert != null && ownKey != null)
conf_set_cert(ctx, @:privateAccess ownCert.__x, @:privateAccess ownKey.__k);
if (altSNIContexts != null) {
sniCallback = function(servername) {
var servername = new String(cast servername);
for (c in altSNIContexts) {
if (c.match(servername))
return @:privateAccess {
key:c.key.__k, cert:c.cert.__x
};
}
if (ownKey != null && ownCert != null)
return @:privateAccess {
key:ownKey.__k, cert:ownCert.__x
};
return null;
}
conf_set_servername_callback(ctx, sniCallback);
}
if (caCert != null)
conf_set_ca(ctx, caCert == null ? null : @:privateAccess caCert.__x);
conf_set_verify(ctx, verifyCert);
return ctx;
}
private static var ssl_new = neko.Lib.loadLazy("ssl", "ssl_new", 1);
private static var ssl_close = neko.Lib.loadLazy("ssl", "ssl_close", 1);
private static var ssl_handshake = neko.Lib.loadLazy("ssl", "ssl_handshake", 1);
private static var ssl_set_socket = neko.Lib.loadLazy("ssl", "ssl_set_socket", 2);
private static var ssl_set_hostname = neko.Lib.loadLazy("ssl", "ssl_set_hostname", 2);
private static var ssl_get_peer_certificate = neko.Lib.loadLazy("ssl", "ssl_get_peer_certificate", 1);
private static var ssl_read = neko.Lib.loadLazy("ssl", "ssl_read", 1);
private static var ssl_write = neko.Lib.loadLazy("ssl", "ssl_write", 2);
private static var conf_new = neko.Lib.loadLazy("ssl", "conf_new", 1);
private static var conf_close = neko.Lib.loadLazy("ssl", "conf_close", 1);
private static var conf_set_ca = neko.Lib.loadLazy("ssl", "conf_set_ca", 2);
private static var conf_set_verify = neko.Lib.loadLazy("ssl", "conf_set_verify", 2);
private static var conf_set_cert = neko.Lib.loadLazy("ssl", "conf_set_cert", 3);
private static var conf_set_servername_callback = neko.Lib.loadLazy("ssl", "conf_set_servername_callback", 2);
private static var socket_new = neko.Lib.load("std", "socket_new", 1);
private static var socket_close = neko.Lib.load("std", "socket_close", 1);
private static var socket_connect = neko.Lib.load("std", "socket_connect", 3);
private static var socket_bind = neko.Lib.load("std", "socket_bind", 3);
private static var socket_accept = neko.Lib.load("std", "socket_accept", 1);
}

View File

@ -0,0 +1,49 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.thread;
@:coreApi
class Deque<T> {
var q:Dynamic;
public function new() {
q = deque_create();
}
public function add(i:T):Void {
deque_add(q, i);
}
public function push(i:T):Void {
deque_push(q, i);
}
public function pop(block:Bool):Null<T> {
return deque_pop(q, block);
}
static var deque_create = neko.Lib.loadLazy("std", "deque_create", 0);
static var deque_add = neko.Lib.loadLazy("std", "deque_add", 2);
static var deque_push = neko.Lib.loadLazy("std", "deque_push", 2);
static var deque_pop = neko.Lib.loadLazy("std", "deque_pop", 2);
}

View File

@ -0,0 +1,44 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.thread;
@:coreApi
class Lock {
var l:Dynamic;
public function new() {
l = lock_create();
}
public function wait(?timeout:Float):Bool {
return lock_wait(l, timeout);
}
public function release():Void {
lock_release(l);
}
static var lock_create = neko.Lib.load("std", "lock_create", 0);
static var lock_release = neko.Lib.load("std", "lock_release", 1);
static var lock_wait = neko.Lib.load("std", "lock_wait", 2);
}

View File

@ -0,0 +1,49 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.thread;
@:coreApi
class Mutex {
var m:Dynamic;
public function new():Void {
m = mutex_create();
}
public function acquire():Void {
mutex_acquire(m);
}
public function tryAcquire():Bool {
return mutex_try(m);
}
public function release():Void {
mutex_release(m);
}
static var mutex_create = neko.Lib.loadLazy("std", "mutex_create", 0);
static var mutex_release = neko.Lib.loadLazy("std", "mutex_release", 1);
static var mutex_acquire = neko.Lib.loadLazy("std", "mutex_acquire", 1);
static var mutex_try = neko.Lib.loadLazy("std", "mutex_try", 1);
}

View File

@ -0,0 +1,188 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.thread;
private typedef ThreadImpl = HaxeThread;
abstract Thread(ThreadImpl) from ThreadImpl {
public var events(get,never):EventLoop;
public inline function sendMessage(msg:Dynamic):Void {
this.sendMessage(msg);
}
public static inline function current():Thread {
return HaxeThread.current();
}
public static inline function create(job:()->Void):Thread {
return HaxeThread.create(job, false);
}
public static inline function runWithEventLoop(job:()->Void):Void {
HaxeThread.runWithEventLoop(job);
}
public static inline function createWithEventLoop(job:()->Void):Thread {
return HaxeThread.create(job, true);
}
public static inline function readMessage(block:Bool):Dynamic {
return HaxeThread.readMessage(block);
}
function get_events():EventLoop {
if(this.events == null)
throw new NoEventLoopException();
return this.events;
}
@:keep
static function processEvents() {
HaxeThread.current().events.loop();
}
}
@:callable
@:coreType
private abstract NativeThreadHandle {}
private typedef ThreadHandle = NativeThreadHandle;
private class HaxeThread {
static var thread_create:(callb:(_:Dynamic)->Void, _:Dynamic)->ThreadHandle;
static var thread_current:()->ThreadHandle;
static var thread_send:(handle:ThreadHandle, msg:Dynamic)->Void;
static var thread_read_message:(block:Bool)->Dynamic;
static var mainThreadHandle:ThreadHandle;
static var mainThread:HaxeThread;
static var threads:Array<{thread:HaxeThread, handle:ThreadHandle}>;
static var threadsMutex:Mutex;
static function __init__() {
thread_create = neko.Lib.load("std", "thread_create", 2);
thread_current = neko.Lib.load("std", "thread_current", 0);
thread_send = neko.Lib.load("std", "thread_send", 2);
thread_read_message = neko.Lib.load("std", "thread_read_message", 1);
mainThreadHandle = thread_current();
mainThread = new HaxeThread(mainThreadHandle);
mainThread.events = new EventLoop();
threads = [];
threadsMutex = new Mutex();
}
public var events(default,null):Null<EventLoop>;
public var handle:ThreadHandle;
static public function current():HaxeThread {
var handle = thread_current();
if(handle == mainThreadHandle) {
return mainThread;
}
threadsMutex.acquire();
var thread = null;
for(item in threads) {
if(item.handle == handle) {
thread = item.thread;
break;
}
}
if(thread == null) {
thread = new HaxeThread(handle);
threads.push({thread:thread, handle:handle});
}
threadsMutex.release();
return thread;
}
public static function create(callb:()->Void, withEventLoop:Bool):Thread {
var item = {handle:null, thread:new HaxeThread(null)};
threadsMutex.acquire();
threads.push(item);
threadsMutex.release();
if(withEventLoop)
item.thread.events = new EventLoop();
item.handle = thread_create(_ -> {
if(item.thread.handle == null) {
item.handle = thread_current();
item.thread.handle = item.handle;
}
try {
callb();
if(withEventLoop)
item.thread.events.loop();
} catch(e) {
dropThread(item);
throw e;
}
dropThread(item);
}, null);
item.thread.handle = item.handle;
return item.thread;
}
public static function runWithEventLoop(job:()->Void):Void {
var thread = current();
if(thread.events == null) {
thread.events = new EventLoop();
try {
job();
thread.events.loop();
thread.events = null;
} catch(e) {
thread.events = null;
throw e;
}
} else {
job();
}
}
static function dropThread(deleteItem) {
threadsMutex.acquire();
for(i => item in threads) {
if(item == deleteItem) {
threads.splice(i, 1);
break;
}
}
threadsMutex.release();
}
public static inline function readMessage(block:Bool):Dynamic {
return thread_read_message(block);
}
public function new(handle:ThreadHandle) {
this.handle = handle;
}
public function sendMessage(msg:Dynamic) {
thread_send(handle, msg);
}
}

View File

@ -0,0 +1,47 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.thread;
@:coreApi
class Tls<T> {
var t:Dynamic;
public var value(get, set):T;
public function new() {
t = tls_create();
}
function get_value():T {
return tls_get(t);
}
function set_value(v:T):T {
tls_set(t, v);
return v;
}
static var tls_create = neko.Lib.load("std", "tls_create", 0);
static var tls_get = neko.Lib.load("std", "tls_get", 1);
static var tls_set = neko.Lib.load("std", "tls_set", 2);
}