mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 15:14:08 +00:00
got rid of cImport
This commit is contained in:
parent
663a809f90
commit
cd48038ae6
4 changed files with 443 additions and 76 deletions
391
src/fio.zig
Normal file
391
src/fio.zig
Normal file
|
@ -0,0 +1,391 @@
|
||||||
|
pub const FIOBJ = usize;
|
||||||
|
pub const fio_url_s = extern struct {
|
||||||
|
scheme: fio_str_info_s,
|
||||||
|
user: fio_str_info_s,
|
||||||
|
password: fio_str_info_s,
|
||||||
|
host: fio_str_info_s,
|
||||||
|
port: fio_str_info_s,
|
||||||
|
path: fio_str_info_s,
|
||||||
|
query: fio_str_info_s,
|
||||||
|
target: fio_str_info_s,
|
||||||
|
};
|
||||||
|
pub extern fn fio_url_parse(url: [*c]const u8, length: usize) fio_url_s;
|
||||||
|
pub const struct_fio_start_args = extern struct {
|
||||||
|
threads: i16,
|
||||||
|
workers: i16,
|
||||||
|
};
|
||||||
|
pub const fio_start_args = struct_fio_start_args;
|
||||||
|
pub extern fn fio_start(args: struct_fio_start_args) void;
|
||||||
|
const struct_unnamed_37 = extern struct {
|
||||||
|
vtbl: ?*anyopaque,
|
||||||
|
flag: usize,
|
||||||
|
out_headers: FIOBJ,
|
||||||
|
};
|
||||||
|
pub const __time_t = c_long;
|
||||||
|
pub const time_t = __time_t;
|
||||||
|
pub const __syscall_slong_t = c_long;
|
||||||
|
pub const struct_timespec = extern struct {
|
||||||
|
tv_sec: __time_t,
|
||||||
|
tv_nsec: __syscall_slong_t,
|
||||||
|
};
|
||||||
|
pub const struct_http_settings_s = extern struct {
|
||||||
|
on_request: ?*const fn ([*c]http_s) callconv(.C) void,
|
||||||
|
on_upgrade: ?*const fn ([*c]http_s, [*c]u8, usize) callconv(.C) void,
|
||||||
|
on_response: ?*const fn ([*c]http_s) callconv(.C) void,
|
||||||
|
on_finish: ?*const fn ([*c]struct_http_settings_s) callconv(.C) void,
|
||||||
|
udata: ?*anyopaque,
|
||||||
|
public_folder: [*c]const u8,
|
||||||
|
public_folder_length: usize,
|
||||||
|
max_header_size: usize,
|
||||||
|
max_body_size: usize,
|
||||||
|
max_clients: isize,
|
||||||
|
tls: ?*anyopaque,
|
||||||
|
reserved1: isize,
|
||||||
|
reserved2: isize,
|
||||||
|
reserved3: isize,
|
||||||
|
ws_max_msg_size: usize,
|
||||||
|
timeout: u8,
|
||||||
|
ws_timeout: u8,
|
||||||
|
log: u8,
|
||||||
|
is_client: u8,
|
||||||
|
};
|
||||||
|
pub const http_settings_s = struct_http_settings_s;
|
||||||
|
pub const http_s = extern struct {
|
||||||
|
private_data: struct_unnamed_37,
|
||||||
|
received_at: struct_timespec,
|
||||||
|
method: FIOBJ,
|
||||||
|
status_str: FIOBJ,
|
||||||
|
version: FIOBJ,
|
||||||
|
status: usize,
|
||||||
|
path: FIOBJ,
|
||||||
|
query: FIOBJ,
|
||||||
|
headers: FIOBJ,
|
||||||
|
cookies: FIOBJ,
|
||||||
|
params: FIOBJ,
|
||||||
|
body: FIOBJ,
|
||||||
|
udata: ?*anyopaque,
|
||||||
|
}; // zig-cache/i/e0c8a6e617497ade13de512cbe191f23/include/http.h:153:12: warning: struct demoted to opaque type - has bitfield
|
||||||
|
pub const struct_fio_str_info_s = extern struct {
|
||||||
|
capa: usize,
|
||||||
|
len: usize,
|
||||||
|
data: [*c]u8,
|
||||||
|
};
|
||||||
|
pub const fio_str_info_s = struct_fio_str_info_s;
|
||||||
|
pub extern fn http_send_body(h: [*c]http_s, data: ?*anyopaque, length: usize) c_int;
|
||||||
|
|
||||||
|
pub extern fn fiobj_hash_new() FIOBJ;
|
||||||
|
pub extern fn fiobj_hash_set(hash: FIOBJ, key: FIOBJ, obj: FIOBJ) c_int;
|
||||||
|
pub extern fn fiobj_ary_push(ary: FIOBJ, obj: FIOBJ) void;
|
||||||
|
pub extern fn fiobj_float_new(num: f64) FIOBJ;
|
||||||
|
pub extern fn fiobj_num_new_bignum(num: isize) FIOBJ;
|
||||||
|
pub extern fn fiobj_free_wrapped(o: FIOBJ) callconv(.C) void;
|
||||||
|
pub fn fiobj_null() callconv(.C) FIOBJ {
|
||||||
|
return @bitCast(FIOBJ, @as(c_long, FIOBJ_T_NULL));
|
||||||
|
}
|
||||||
|
pub fn fiobj_true() callconv(.C) FIOBJ {
|
||||||
|
return @bitCast(FIOBJ, @as(c_long, FIOBJ_T_TRUE));
|
||||||
|
}
|
||||||
|
pub fn fiobj_false() callconv(.C) FIOBJ {
|
||||||
|
return @bitCast(FIOBJ, @as(c_long, FIOBJ_T_FALSE));
|
||||||
|
}
|
||||||
|
pub extern fn fiobj_ary_new2(capa: usize) FIOBJ;
|
||||||
|
pub extern fn fiobj_str_new(str: [*c]const u8, len: usize) FIOBJ;
|
||||||
|
pub extern fn fiobj_str_buf(capa: usize) FIOBJ;
|
||||||
|
|
||||||
|
pub inline fn FIOBJ_TYPE(obj: anytype) @TypeOf(fiobj_type(obj)) {
|
||||||
|
return fiobj_type(obj);
|
||||||
|
}
|
||||||
|
pub inline fn FIOBJ_TYPE_IS(obj: anytype, @"type": anytype) @TypeOf(fiobj_type_is(obj, @"type")) {
|
||||||
|
return fiobj_type_is(obj, @"type");
|
||||||
|
}
|
||||||
|
pub inline fn FIOBJ_IS_NULL(obj: anytype) @TypeOf(!(obj != 0) or (obj == @import("std").zig.c_translation.cast(FIOBJ, FIOBJ_T_NULL))) {
|
||||||
|
return !(obj != 0) or (obj == @import("std").zig.c_translation.cast(FIOBJ, FIOBJ_T_NULL));
|
||||||
|
}
|
||||||
|
pub const FIOBJ_INVALID = @as(c_int, 0);
|
||||||
|
pub const FIOBJECT_NUMBER_FLAG = @as(c_int, 1);
|
||||||
|
pub const FIOBJECT_PRIMITIVE_FLAG = @as(c_int, 6);
|
||||||
|
pub const FIOBJECT_STRING_FLAG = @as(c_int, 2);
|
||||||
|
pub const FIOBJECT_HASH_FLAG = @as(c_int, 4);
|
||||||
|
pub const FIOBJECT_TYPE_MASK = ~@import("std").zig.c_translation.cast(usize, @as(c_int, 7));
|
||||||
|
pub const FIOBJ_NUMBER_SIGN_MASK = ~@import("std").zig.c_translation.cast(usize, @as(c_int, 0)) >> @as(c_int, 1);
|
||||||
|
pub const FIOBJ_NUMBER_SIGN_BIT = ~FIOBJ_NUMBER_SIGN_MASK;
|
||||||
|
pub const FIOBJ_NUMBER_SIGN_EXCLUDE_BIT = FIOBJ_NUMBER_SIGN_BIT >> @as(c_int, 1);
|
||||||
|
pub inline fn FIOBJ_IS_ALLOCATED(o: anytype) @TypeOf(((o != 0) and ((o & FIOBJECT_NUMBER_FLAG) == @as(c_int, 0))) and ((o & FIOBJECT_PRIMITIVE_FLAG) != FIOBJECT_PRIMITIVE_FLAG)) {
|
||||||
|
return ((o != 0) and ((o & FIOBJECT_NUMBER_FLAG) == @as(c_int, 0))) and ((o & FIOBJECT_PRIMITIVE_FLAG) != FIOBJECT_PRIMITIVE_FLAG);
|
||||||
|
}
|
||||||
|
pub inline fn FIOBJ2PTR(o: anytype) ?*anyopaque {
|
||||||
|
return @import("std").zig.c_translation.cast(?*anyopaque, o & FIOBJECT_TYPE_MASK);
|
||||||
|
}
|
||||||
|
pub inline fn FIOBJECT2VTBL(o: anytype) @TypeOf(fiobj_type_vtable(o)) {
|
||||||
|
return fiobj_type_vtable(o);
|
||||||
|
}
|
||||||
|
pub inline fn FIOBJECT2HEAD(o: anytype) [*c]fiobj_object_header_s {
|
||||||
|
return @import("std").zig.c_translation.cast([*c]fiobj_object_header_s, FIOBJ2PTR(o));
|
||||||
|
}
|
||||||
|
pub inline fn fiobj_ary_entry(a: anytype, p: anytype) @TypeOf(fiobj_ary_index(a, p)) {
|
||||||
|
return fiobj_ary_index(a, p);
|
||||||
|
}
|
||||||
|
pub extern fn fiobj_ary_index(ary: FIOBJ, pos: i64) FIOBJ;
|
||||||
|
pub const FIOBJ_T_NUMBER: c_int = 1;
|
||||||
|
pub const FIOBJ_T_NULL: c_int = 6;
|
||||||
|
pub const FIOBJ_T_TRUE: c_int = 22;
|
||||||
|
pub const FIOBJ_T_FALSE: c_int = 38;
|
||||||
|
pub const FIOBJ_T_FLOAT: c_int = 39;
|
||||||
|
pub const FIOBJ_T_STRING: c_int = 40;
|
||||||
|
pub const FIOBJ_T_ARRAY: c_int = 41;
|
||||||
|
pub const FIOBJ_T_HASH: c_int = 42;
|
||||||
|
pub const FIOBJ_T_DATA: c_int = 43;
|
||||||
|
pub const FIOBJ_T_UNKNOWN: c_int = 44;
|
||||||
|
pub const fiobj_type_enum = u8;
|
||||||
|
pub const fiobj_object_vtable_s = extern struct {
|
||||||
|
class_name: [*c]const u8,
|
||||||
|
dealloc: ?*const fn (FIOBJ, ?*const fn (FIOBJ, ?*anyopaque) callconv(.C) void, ?*anyopaque) callconv(.C) void,
|
||||||
|
count: ?*const fn (FIOBJ) callconv(.C) usize,
|
||||||
|
is_true: ?*const fn (FIOBJ) callconv(.C) usize,
|
||||||
|
is_eq: ?*const fn (FIOBJ, FIOBJ) callconv(.C) usize,
|
||||||
|
each: ?*const fn (FIOBJ, usize, ?*const fn (FIOBJ, ?*anyopaque) callconv(.C) c_int, ?*anyopaque) callconv(.C) usize,
|
||||||
|
to_str: ?*const fn (FIOBJ) callconv(.C) fio_str_info_s,
|
||||||
|
to_i: ?*const fn (FIOBJ) callconv(.C) isize,
|
||||||
|
to_f: ?*const fn (FIOBJ) callconv(.C) f64,
|
||||||
|
};
|
||||||
|
pub const fiobj_object_header_s = extern struct {
|
||||||
|
type: fiobj_type_enum,
|
||||||
|
ref: u32,
|
||||||
|
};
|
||||||
|
pub fn fiobj_type_is(arg_o: FIOBJ, arg_type: fiobj_type_enum) callconv(.C) usize {
|
||||||
|
var o = arg_o;
|
||||||
|
var @"type" = arg_type;
|
||||||
|
while (true) {
|
||||||
|
switch (@bitCast(c_int, @as(c_uint, @"type"))) {
|
||||||
|
@as(c_int, 1) => return @bitCast(usize, @as(c_long, @boolToInt(((o & @bitCast(c_ulong, @as(c_long, @as(c_int, 1)))) != 0) or (@bitCast(c_int, @as(c_uint, @intToPtr([*c]fiobj_type_enum, o)[@intCast(c_uint, @as(c_int, 0))])) == FIOBJ_T_NUMBER)))),
|
||||||
|
@as(c_int, 6) => return @bitCast(usize, @as(c_long, @boolToInt(!(o != 0) or (o == fiobj_null())))),
|
||||||
|
@as(c_int, 22) => return @bitCast(usize, @as(c_long, @boolToInt(o == fiobj_true()))),
|
||||||
|
@as(c_int, 38) => return @bitCast(usize, @as(c_long, @boolToInt(o == fiobj_false()))),
|
||||||
|
@as(c_int, 40) => return @bitCast(usize, @as(c_long, @boolToInt(((true and ((o & @bitCast(c_ulong, @as(c_long, @as(c_int, 1)))) == @bitCast(c_ulong, @as(c_long, @as(c_int, 0))))) and ((o & @bitCast(c_ulong, @as(c_long, @as(c_int, 6)))) == @bitCast(c_ulong, @as(c_long, @as(c_int, 2))))) or (((@as(c_int, 2) == @as(c_int, 0)) and (((o != 0) and ((o & @bitCast(c_ulong, @as(c_long, @as(c_int, 1)))) == @bitCast(c_ulong, @as(c_long, @as(c_int, 0))))) and ((o & @bitCast(c_ulong, @as(c_long, @as(c_int, 6)))) != @bitCast(c_ulong, @as(c_long, @as(c_int, 6)))))) and (@bitCast(c_int, @as(c_uint, @ptrCast([*c]fiobj_type_enum, @alignCast(@import("std").meta.alignment([*c]fiobj_type_enum), @intToPtr(?*anyopaque, o & ~@bitCast(usize, @as(c_long, @as(c_int, 7))))))[@intCast(c_uint, @as(c_int, 0))])) == FIOBJ_T_STRING))))),
|
||||||
|
@as(c_int, 42) => {
|
||||||
|
if (true) {
|
||||||
|
return @bitCast(usize, @as(c_long, @boolToInt(((o & @bitCast(c_ulong, @as(c_long, @as(c_int, 1)))) == @bitCast(c_ulong, @as(c_long, @as(c_int, 0)))) and ((o & @bitCast(c_ulong, @as(c_long, @as(c_int, 6)))) == @bitCast(c_ulong, @as(c_long, @as(c_int, 4)))))));
|
||||||
|
}
|
||||||
|
return @bitCast(usize, @as(c_long, @boolToInt((((o != 0) and ((o & @bitCast(c_ulong, @as(c_long, @as(c_int, 1)))) == @bitCast(c_ulong, @as(c_long, @as(c_int, 0))))) and ((o & @bitCast(c_ulong, @as(c_long, @as(c_int, 6)))) != @bitCast(c_ulong, @as(c_long, @as(c_int, 6))))) and (@bitCast(c_int, @as(c_uint, @ptrCast([*c]fiobj_type_enum, @alignCast(@import("std").meta.alignment([*c]fiobj_type_enum), @intToPtr(?*anyopaque, o & ~@bitCast(usize, @as(c_long, @as(c_int, 7))))))[@intCast(c_uint, @as(c_int, 0))])) == @bitCast(c_int, @as(c_uint, @"type"))))));
|
||||||
|
},
|
||||||
|
@as(c_int, 39), @as(c_int, 41), @as(c_int, 43), @as(c_int, 44) => return @bitCast(usize, @as(c_long, @boolToInt((((o != 0) and ((o & @bitCast(c_ulong, @as(c_long, @as(c_int, 1)))) == @bitCast(c_ulong, @as(c_long, @as(c_int, 0))))) and ((o & @bitCast(c_ulong, @as(c_long, @as(c_int, 6)))) != @bitCast(c_ulong, @as(c_long, @as(c_int, 6))))) and (@bitCast(c_int, @as(c_uint, @ptrCast([*c]fiobj_type_enum, @alignCast(@import("std").meta.alignment([*c]fiobj_type_enum), @intToPtr(?*anyopaque, o & ~@bitCast(usize, @as(c_long, @as(c_int, 7))))))[@intCast(c_uint, @as(c_int, 0))])) == @bitCast(c_int, @as(c_uint, @"type")))))),
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return @bitCast(usize, @as(c_long, @boolToInt((((o != 0) and ((o & @bitCast(c_ulong, @as(c_long, @as(c_int, 1)))) == @bitCast(c_ulong, @as(c_long, @as(c_int, 0))))) and ((o & @bitCast(c_ulong, @as(c_long, @as(c_int, 6)))) != @bitCast(c_ulong, @as(c_long, @as(c_int, 6))))) and (@bitCast(c_int, @as(c_uint, @ptrCast([*c]fiobj_type_enum, @alignCast(@import("std").meta.alignment([*c]fiobj_type_enum), @intToPtr(?*anyopaque, o & ~@bitCast(usize, @as(c_long, @as(c_int, 7))))))[@intCast(c_uint, @as(c_int, 0))])) == @bitCast(c_int, @as(c_uint, @"type"))))));
|
||||||
|
}
|
||||||
|
pub fn fiobj_type(arg_o: FIOBJ) callconv(.C) fiobj_type_enum {
|
||||||
|
var o = arg_o;
|
||||||
|
if (!(o != 0)) return @bitCast(u8, @truncate(i8, FIOBJ_T_NULL));
|
||||||
|
if ((o & @bitCast(c_ulong, @as(c_long, @as(c_int, 1)))) != 0) return @bitCast(u8, @truncate(i8, FIOBJ_T_NUMBER));
|
||||||
|
if ((o & @bitCast(c_ulong, @as(c_long, @as(c_int, 6)))) == @bitCast(c_ulong, @as(c_long, @as(c_int, 6)))) return @bitCast(u8, @truncate(u8, o));
|
||||||
|
if (true and ((o & @bitCast(c_ulong, @as(c_long, @as(c_int, 6)))) == @bitCast(c_ulong, @as(c_long, @as(c_int, 2))))) return @bitCast(u8, @truncate(i8, FIOBJ_T_STRING));
|
||||||
|
if (true and ((o & @bitCast(c_ulong, @as(c_long, @as(c_int, 6)))) == @bitCast(c_ulong, @as(c_long, @as(c_int, 4))))) return @bitCast(u8, @truncate(i8, FIOBJ_T_HASH));
|
||||||
|
return @ptrCast([*c]fiobj_type_enum, @alignCast(@import("std").meta.alignment([*c]fiobj_type_enum), @intToPtr(?*anyopaque, o & ~@bitCast(usize, @as(c_long, @as(c_int, 7))))))[@intCast(c_uint, @as(c_int, 0))];
|
||||||
|
}
|
||||||
|
pub extern const FIOBJECT_VTABLE_NUMBER: fiobj_object_vtable_s;
|
||||||
|
pub extern const FIOBJECT_VTABLE_FLOAT: fiobj_object_vtable_s;
|
||||||
|
pub extern const FIOBJECT_VTABLE_STRING: fiobj_object_vtable_s;
|
||||||
|
pub extern const FIOBJECT_VTABLE_ARRAY: fiobj_object_vtable_s;
|
||||||
|
pub extern const FIOBJECT_VTABLE_HASH: fiobj_object_vtable_s;
|
||||||
|
pub extern const FIOBJECT_VTABLE_DATA: fiobj_object_vtable_s;
|
||||||
|
pub fn fiobj_type_vtable(arg_o: FIOBJ) callconv(.C) [*c]const fiobj_object_vtable_s {
|
||||||
|
var o = arg_o;
|
||||||
|
while (true) {
|
||||||
|
switch (@bitCast(c_int, @as(c_uint, fiobj_type(o)))) {
|
||||||
|
@as(c_int, 1) => return &FIOBJECT_VTABLE_NUMBER,
|
||||||
|
@as(c_int, 39) => return &FIOBJECT_VTABLE_FLOAT,
|
||||||
|
@as(c_int, 40) => return &FIOBJECT_VTABLE_STRING,
|
||||||
|
@as(c_int, 41) => return &FIOBJECT_VTABLE_ARRAY,
|
||||||
|
@as(c_int, 42) => return &FIOBJECT_VTABLE_HASH,
|
||||||
|
@as(c_int, 43) => return &FIOBJECT_VTABLE_DATA,
|
||||||
|
@as(c_int, 6), @as(c_int, 22), @as(c_int, 38), @as(c_int, 44) => return null,
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
pub extern fn fio_ltocstr(c_long) fio_str_info_s;
|
||||||
|
pub fn fiobj_obj2cstr(o: FIOBJ) callconv(.C) fio_str_info_s {
|
||||||
|
if (!(o != 0)) {
|
||||||
|
var ret: fio_str_info_s = fio_str_info_s{
|
||||||
|
.capa = @bitCast(usize, @as(c_long, @as(c_int, 0))),
|
||||||
|
.len = @bitCast(usize, @as(c_long, @as(c_int, 4))),
|
||||||
|
.data = @intToPtr([*c]u8, @ptrToInt("null")),
|
||||||
|
};
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
if ((o & @bitCast(c_ulong, @as(c_long, @as(c_int, 1)))) != 0) return fio_ltocstr(@bitCast(isize, o) >> @intCast(@import("std").math.Log2Int(isize), 1));
|
||||||
|
if ((o & @bitCast(c_ulong, @as(c_long, @as(c_int, 6)))) == @bitCast(c_ulong, @as(c_long, @as(c_int, 6)))) {
|
||||||
|
while (true) {
|
||||||
|
switch (@bitCast(c_int, @as(c_uint, @bitCast(u8, @truncate(u8, o))))) {
|
||||||
|
@as(c_int, 6) => {
|
||||||
|
{
|
||||||
|
var ret: fio_str_info_s = fio_str_info_s{
|
||||||
|
.capa = @bitCast(usize, @as(c_long, @as(c_int, 0))),
|
||||||
|
.len = @bitCast(usize, @as(c_long, @as(c_int, 4))),
|
||||||
|
.data = @intToPtr([*c]u8, @ptrToInt("null")),
|
||||||
|
};
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
@as(c_int, 38) => {
|
||||||
|
{
|
||||||
|
var ret: fio_str_info_s = fio_str_info_s{
|
||||||
|
.capa = @bitCast(usize, @as(c_long, @as(c_int, 0))),
|
||||||
|
.len = @bitCast(usize, @as(c_long, @as(c_int, 5))),
|
||||||
|
.data = @intToPtr([*c]u8, @ptrToInt("false")),
|
||||||
|
};
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
@as(c_int, 22) => {
|
||||||
|
{
|
||||||
|
var ret: fio_str_info_s = fio_str_info_s{
|
||||||
|
.capa = @bitCast(usize, @as(c_long, @as(c_int, 0))),
|
||||||
|
.len = @bitCast(usize, @as(c_long, @as(c_int, 4))),
|
||||||
|
.data = @intToPtr([*c]u8, @ptrToInt("true")),
|
||||||
|
};
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
else => break,
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fiobj_type_vtable(o).*.to_str.?(o);
|
||||||
|
}
|
||||||
|
pub const http_cookie_args_s = opaque {};
|
||||||
|
pub extern fn http_set_header(h: [*c]http_s, name: FIOBJ, value: FIOBJ) c_int;
|
||||||
|
pub extern fn http_set_header2(h: [*c]http_s, name: fio_str_info_s, value: fio_str_info_s) c_int;
|
||||||
|
pub extern fn http_set_cookie(h: [*c]http_s, http_cookie_args_s) c_int;
|
||||||
|
pub extern fn http_sendfile(h: [*c]http_s, fd: c_int, length: usize, offset: usize) c_int;
|
||||||
|
pub extern fn http_sendfile2(h: [*c]http_s, prefix: [*c]const u8, prefix_len: usize, encoded: [*c]const u8, encoded_len: usize) c_int;
|
||||||
|
pub extern fn http_send_error(h: [*c]http_s, error_code: usize) c_int;
|
||||||
|
pub extern fn http_finish(h: [*c]http_s) void;
|
||||||
|
pub extern fn http_push_data(h: [*c]http_s, data: ?*anyopaque, length: usize, mime_type: FIOBJ) c_int;
|
||||||
|
pub extern fn http_push_file(h: [*c]http_s, filename: FIOBJ, mime_type: FIOBJ) c_int;
|
||||||
|
pub const struct_http_pause_handle_s = opaque {};
|
||||||
|
pub const http_pause_handle_s = struct_http_pause_handle_s;
|
||||||
|
pub extern fn http_pause(h: [*c]http_s, task: ?*const fn (?*http_pause_handle_s) callconv(.C) void) void;
|
||||||
|
pub extern fn http_resume(http: ?*http_pause_handle_s, task: ?*const fn ([*c]http_s) callconv(.C) void, fallback: ?*const fn (?*anyopaque) callconv(.C) void) void;
|
||||||
|
pub extern fn http_paused_udata_get(http: ?*http_pause_handle_s) ?*anyopaque;
|
||||||
|
pub extern fn http_paused_udata_set(http: ?*http_pause_handle_s, udata: ?*anyopaque) ?*anyopaque;
|
||||||
|
pub extern fn http_listen(port: [*c]const u8, binding: [*c]const u8, struct_http_settings_s) isize;
|
||||||
|
pub extern fn http_connect(url: [*c]const u8, unix_address: [*c]const u8, struct_http_settings_s) isize;
|
||||||
|
pub extern fn http_settings(h: [*c]http_s) [*c]struct_http_settings_s;
|
||||||
|
pub extern fn http_peer_addr(h: [*c]http_s) fio_str_info_s;
|
||||||
|
pub extern fn http_hijack(h: [*c]http_s, leftover: [*c]fio_str_info_s) isize;
|
||||||
|
pub const struct_ws_s = opaque {};
|
||||||
|
pub const ws_s = struct_ws_s;
|
||||||
|
pub const websocket_settings_s = extern struct {
|
||||||
|
on_message: ?*const fn (?*ws_s, fio_str_info_s, u8) callconv(.C) void,
|
||||||
|
on_open: ?*const fn (?*ws_s) callconv(.C) void,
|
||||||
|
on_ready: ?*const fn (?*ws_s) callconv(.C) void,
|
||||||
|
on_shutdown: ?*const fn (?*ws_s) callconv(.C) void,
|
||||||
|
on_close: ?*const fn (isize, ?*anyopaque) callconv(.C) void,
|
||||||
|
udata: ?*anyopaque,
|
||||||
|
};
|
||||||
|
pub extern fn http_upgrade2ws(http: [*c]http_s, websocket_settings_s) c_int;
|
||||||
|
pub extern fn websocket_connect(url: [*c]const u8, settings: websocket_settings_s) c_int;
|
||||||
|
pub extern fn websocket_attach(uuid: isize, http_settings: [*c]http_settings_s, args: [*c]websocket_settings_s, data: ?*anyopaque, length: usize) void;
|
||||||
|
pub extern fn websocket_udata_get(ws: ?*ws_s) ?*anyopaque;
|
||||||
|
pub extern fn websocket_udata_set(ws: ?*ws_s, udata: ?*anyopaque) ?*anyopaque;
|
||||||
|
pub extern fn websocket_uuid(ws: ?*ws_s) isize;
|
||||||
|
pub extern fn websocket_is_client(ws: ?*ws_s) u8;
|
||||||
|
pub extern fn websocket_write(ws: ?*ws_s, msg: fio_str_info_s, is_text: u8) c_int;
|
||||||
|
pub extern fn websocket_close(ws: ?*ws_s) void; // zig-cache/i/e0c8a6e617497ade13de512cbe191f23/include/websockets.h:104:12: warning: struct demoted to opaque type - has bitfield
|
||||||
|
pub const struct_websocket_subscribe_s = opaque {};
|
||||||
|
pub extern fn websocket_subscribe(args: struct_websocket_subscribe_s) usize;
|
||||||
|
pub extern fn websocket_unsubscribe(ws: ?*ws_s, subscription_id: usize) void;
|
||||||
|
pub extern fn websocket_optimize4broadcasts(@"type": isize, enable: c_int) void;
|
||||||
|
pub const http_sse_s = struct_http_sse_s;
|
||||||
|
pub const struct_http_sse_s = extern struct {
|
||||||
|
on_open: ?*const fn ([*c]http_sse_s) callconv(.C) void,
|
||||||
|
on_ready: ?*const fn ([*c]http_sse_s) callconv(.C) void,
|
||||||
|
on_shutdown: ?*const fn ([*c]http_sse_s) callconv(.C) void,
|
||||||
|
on_close: ?*const fn ([*c]http_sse_s) callconv(.C) void,
|
||||||
|
udata: ?*anyopaque,
|
||||||
|
};
|
||||||
|
pub extern fn http_upgrade2sse(h: [*c]http_s, http_sse_s) c_int;
|
||||||
|
pub extern fn http_sse_set_timout(sse: [*c]http_sse_s, timeout: u8) void;
|
||||||
|
pub const fio_match_fn = ?*const fn (fio_str_info_s, fio_str_info_s) callconv(.C) c_int;
|
||||||
|
pub const struct_http_sse_subscribe_args = extern struct {
|
||||||
|
channel: fio_str_info_s,
|
||||||
|
on_message: ?*const fn ([*c]http_sse_s, fio_str_info_s, fio_str_info_s, ?*anyopaque) callconv(.C) void,
|
||||||
|
on_unsubscribe: ?*const fn (?*anyopaque) callconv(.C) void,
|
||||||
|
udata: ?*anyopaque,
|
||||||
|
match: fio_match_fn,
|
||||||
|
};
|
||||||
|
pub extern fn http_sse_subscribe(sse: [*c]http_sse_s, args: struct_http_sse_subscribe_args) usize;
|
||||||
|
pub extern fn http_sse_unsubscribe(sse: [*c]http_sse_s, subscription: usize) void;
|
||||||
|
pub const struct_http_sse_write_args = extern struct {
|
||||||
|
id: fio_str_info_s,
|
||||||
|
event: fio_str_info_s,
|
||||||
|
data: fio_str_info_s,
|
||||||
|
retry: isize,
|
||||||
|
};
|
||||||
|
pub extern fn http_sse_write(sse: [*c]http_sse_s, struct_http_sse_write_args) c_int;
|
||||||
|
pub extern fn http_sse2uuid(sse: [*c]http_sse_s) isize;
|
||||||
|
pub extern fn http_sse_close(sse: [*c]http_sse_s) c_int;
|
||||||
|
pub extern fn http_sse_dup(sse: [*c]http_sse_s) [*c]http_sse_s;
|
||||||
|
pub extern fn http_sse_free(sse: [*c]http_sse_s) void;
|
||||||
|
pub extern fn http_parse_body(h: [*c]http_s) c_int;
|
||||||
|
pub extern fn http_parse_query(h: [*c]http_s) void;
|
||||||
|
pub extern fn http_parse_cookies(h: [*c]http_s, is_url_encoded: u8) void;
|
||||||
|
pub extern fn http_add2hash(dest: FIOBJ, name: [*c]u8, name_len: usize, value: [*c]u8, value_len: usize, encoded: u8) c_int;
|
||||||
|
pub extern fn http_add2hash2(dest: FIOBJ, name: [*c]u8, name_len: usize, value: FIOBJ, encoded: u8) c_int;
|
||||||
|
pub extern fn http_status2str(status: usize) fio_str_info_s;
|
||||||
|
pub extern fn http_mimetype_register(file_ext: [*c]u8, file_ext_len: usize, mime_type_str: FIOBJ) void;
|
||||||
|
pub extern fn http_mimetype_find(file_ext: [*c]u8, file_ext_len: usize) FIOBJ;
|
||||||
|
pub extern fn http_mimetype_find2(url: FIOBJ) FIOBJ;
|
||||||
|
pub extern fn http_mimetype_clear() void;
|
||||||
|
pub extern var HTTP_HEADER_ACCEPT: FIOBJ;
|
||||||
|
pub extern var HTTP_HEADER_CACHE_CONTROL: FIOBJ;
|
||||||
|
pub extern var HTTP_HEADER_CONNECTION: FIOBJ;
|
||||||
|
pub extern var HTTP_HEADER_CONTENT_ENCODING: FIOBJ;
|
||||||
|
pub extern var HTTP_HEADER_CONTENT_LENGTH: FIOBJ;
|
||||||
|
pub extern var HTTP_HEADER_CONTENT_RANGE: FIOBJ;
|
||||||
|
pub extern var HTTP_HEADER_CONTENT_TYPE: FIOBJ;
|
||||||
|
pub extern var HTTP_HEADER_COOKIE: FIOBJ;
|
||||||
|
pub extern var HTTP_HEADER_DATE: FIOBJ;
|
||||||
|
pub extern var HTTP_HEADER_ETAG: FIOBJ;
|
||||||
|
pub extern var HTTP_HEADER_HOST: FIOBJ;
|
||||||
|
pub extern var HTTP_HEADER_LAST_MODIFIED: FIOBJ;
|
||||||
|
pub extern var HTTP_HEADER_ORIGIN: FIOBJ;
|
||||||
|
pub extern var HTTP_HEADER_SET_COOKIE: FIOBJ;
|
||||||
|
pub extern var HTTP_HEADER_UPGRADE: FIOBJ;
|
||||||
|
pub extern fn http_req2str(h: [*c]http_s) FIOBJ;
|
||||||
|
pub extern fn http_write_log(h: [*c]http_s) void;
|
||||||
|
pub extern fn http_gmtime(timer: time_t, tmbuf: [*c]struct_tm) [*c]struct_tm;
|
||||||
|
pub extern fn http_date2rfc7231(target: [*c]u8, tmbuf: [*c]struct_tm) usize;
|
||||||
|
pub extern fn http_date2rfc2109(target: [*c]u8, tmbuf: [*c]struct_tm) usize;
|
||||||
|
pub extern fn http_date2rfc2822(target: [*c]u8, tmbuf: [*c]struct_tm) usize;
|
||||||
|
pub fn http_date2str(arg_target: [*c]u8, arg_tmbuf: [*c]struct_tm) callconv(.C) usize {
|
||||||
|
var target = arg_target;
|
||||||
|
var tmbuf = arg_tmbuf;
|
||||||
|
return http_date2rfc7231(target, tmbuf);
|
||||||
|
}
|
||||||
|
pub extern fn http_time2str(target: [*c]u8, t: time_t) usize;
|
||||||
|
pub extern fn http_decode_url_unsafe(dest: [*c]u8, url_data: [*c]const u8) isize;
|
||||||
|
pub extern fn http_decode_url(dest: [*c]u8, url_data: [*c]const u8, length: usize) isize;
|
||||||
|
pub extern fn http_decode_path_unsafe(dest: [*c]u8, url_data: [*c]const u8) isize;
|
||||||
|
pub extern fn http_decode_path(dest: [*c]u8, url_data: [*c]const u8, length: usize) isize;
|
||||||
|
pub const http_url_s = fio_url_s;
|
||||||
|
|
||||||
|
pub const struct_tm = extern struct {
|
||||||
|
tm_sec: c_int,
|
||||||
|
tm_min: c_int,
|
||||||
|
tm_hour: c_int,
|
||||||
|
tm_mday: c_int,
|
||||||
|
tm_mon: c_int,
|
||||||
|
tm_year: c_int,
|
||||||
|
tm_wday: c_int,
|
||||||
|
tm_yday: c_int,
|
||||||
|
tm_isdst: c_int,
|
||||||
|
tm_gmtoff: c_long,
|
||||||
|
tm_zone: [*c]const u8,
|
||||||
|
};
|
|
@ -3,17 +3,17 @@
|
||||||
// easier / possible / more zig-like
|
// easier / possible / more zig-like
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const fio = @import("fio.zig");
|
||||||
const util = @import("util.zig");
|
const util = @import("util.zig");
|
||||||
|
|
||||||
pub const FIOBJ = usize;
|
|
||||||
pub const struct_mustache_s = opaque {};
|
pub const struct_mustache_s = opaque {};
|
||||||
pub const enum_mustache_error_en = c_uint;
|
pub const enum_mustache_error_en = c_uint;
|
||||||
pub const mustache_error_en = enum_mustache_error_en;
|
pub const mustache_error_en = enum_mustache_error_en;
|
||||||
|
|
||||||
pub const mustache_s = struct_mustache_s;
|
pub const mustache_s = struct_mustache_s;
|
||||||
pub extern fn fiobj_mustache_new(args: MustacheLoadArgs) ?*mustache_s;
|
pub extern fn fiobj_mustache_new(args: MustacheLoadArgs) ?*mustache_s;
|
||||||
pub extern fn fiobj_mustache_build(mustache: ?*mustache_s, data: FIOBJ) FIOBJ;
|
pub extern fn fiobj_mustache_build(mustache: ?*mustache_s, data: fio.FIOBJ) fio.FIOBJ;
|
||||||
pub extern fn fiobj_mustache_build2(dest: FIOBJ, mustache: ?*mustache_s, data: FIOBJ) FIOBJ;
|
pub extern fn fiobj_mustache_build2(dest: fio.FIOBJ, mustache: ?*mustache_s, data: fio.FIOBJ) fio.FIOBJ;
|
||||||
pub extern fn fiobj_mustache_free(mustache: ?*mustache_s) void;
|
pub extern fn fiobj_mustache_free(mustache: ?*mustache_s) void;
|
||||||
|
|
||||||
pub const MustacheLoadArgs = extern struct {
|
pub const MustacheLoadArgs = extern struct {
|
||||||
|
@ -76,29 +76,11 @@ pub fn MustacheNew(data: []const u8) MustacheError!*Mustache {
|
||||||
// pub extern fn fiobj_mustache_build(mustache: ?*mustache_s, data: FIOBJ) FIOBJ;
|
// pub extern fn fiobj_mustache_build(mustache: ?*mustache_s, data: FIOBJ) FIOBJ;
|
||||||
// pub extern fn fiobj_mustache_build2(dest: FIOBJ, mustache: ?*mustache_s, data: FIOBJ) FIOBJ;
|
// pub extern fn fiobj_mustache_build2(dest: FIOBJ, mustache: ?*mustache_s, data: FIOBJ) FIOBJ;
|
||||||
|
|
||||||
pub extern fn fiobj_hash_new() FIOBJ;
|
|
||||||
pub extern fn fiobj_hash_set(hash: FIOBJ, key: FIOBJ, obj: FIOBJ) c_int;
|
|
||||||
pub extern fn fiobj_ary_push(ary: FIOBJ, obj: FIOBJ) void;
|
|
||||||
pub extern fn fiobj_float_new(num: f64) FIOBJ;
|
|
||||||
pub extern fn fiobj_num_new_bignum(num: isize) FIOBJ;
|
|
||||||
pub extern fn fiobj_free_wrapped(o: FIOBJ) callconv(.C) void;
|
|
||||||
pub const FIOBJ_T_TRUE: c_int = 22;
|
|
||||||
pub const FIOBJ_T_FALSE: c_int = 38;
|
|
||||||
pub fn fiobj_true() callconv(.C) FIOBJ {
|
|
||||||
return @bitCast(FIOBJ, @as(c_long, FIOBJ_T_TRUE));
|
|
||||||
}
|
|
||||||
pub fn fiobj_false() callconv(.C) FIOBJ {
|
|
||||||
return @bitCast(FIOBJ, @as(c_long, FIOBJ_T_FALSE));
|
|
||||||
}
|
|
||||||
pub extern fn fiobj_ary_new2(capa: usize) FIOBJ;
|
|
||||||
pub extern fn fiobj_str_new(str: [*c]const u8, len: usize) FIOBJ;
|
|
||||||
pub extern fn fiobj_str_buf(capa: usize) FIOBJ;
|
|
||||||
|
|
||||||
const MustacheBuildResult = struct {
|
const MustacheBuildResult = struct {
|
||||||
fiobj_result: FIOBJ = 0,
|
fiobj_result: fio.FIOBJ = 0,
|
||||||
|
|
||||||
pub fn deinit(m: *const MustacheBuildResult) void {
|
pub fn deinit(m: *const MustacheBuildResult) void {
|
||||||
fiobj_free_wrapped(m.fiobj_result);
|
fio.fiobj_free_wrapped(m.fiobj_result);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn str(m: *const MustacheBuildResult) ?[]const u8 {
|
pub fn str(m: *const MustacheBuildResult) ?[]const u8 {
|
||||||
|
@ -121,17 +103,17 @@ pub fn MustacheBuild(mustache: *Mustache, data: anytype) MustacheBuildResult {
|
||||||
|
|
||||||
pub fn fiobjectify(
|
pub fn fiobjectify(
|
||||||
value: anytype,
|
value: anytype,
|
||||||
) FIOBJ {
|
) fio.FIOBJ {
|
||||||
const T = @TypeOf(value);
|
const T = @TypeOf(value);
|
||||||
switch (@typeInfo(T)) {
|
switch (@typeInfo(T)) {
|
||||||
.Float, .ComptimeFloat => {
|
.Float, .ComptimeFloat => {
|
||||||
return fiobj_float_new(value);
|
return fio.fiobj_float_new(value);
|
||||||
},
|
},
|
||||||
.Int, .ComptimeInt => {
|
.Int, .ComptimeInt => {
|
||||||
return fiobj_num_new_bignum(value);
|
return fio.fiobj_num_new_bignum(value);
|
||||||
},
|
},
|
||||||
.Bool => {
|
.Bool => {
|
||||||
return if (value) fiobj_true() else fiobj_false();
|
return if (value) fio.fiobj_true() else fio.fiobj_false();
|
||||||
},
|
},
|
||||||
.Null => {
|
.Null => {
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -144,7 +126,7 @@ pub fn fiobjectify(
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
.Enum => {
|
.Enum => {
|
||||||
return fiobj_num_new_bignum(@enumToInt(value));
|
return fio.fiobj_num_new_bignum(@enumToInt(value));
|
||||||
},
|
},
|
||||||
.Union => {
|
.Union => {
|
||||||
const info = @typeInfo(T).Union;
|
const info = @typeInfo(T).Union;
|
||||||
|
@ -160,21 +142,21 @@ pub fn fiobjectify(
|
||||||
},
|
},
|
||||||
.Struct => |S| {
|
.Struct => |S| {
|
||||||
// create a new fio hashmap
|
// create a new fio hashmap
|
||||||
var m = fiobj_hash_new();
|
var m = fio.fiobj_hash_new();
|
||||||
// std.debug.print("new struct\n", .{});
|
// std.debug.print("new struct\n", .{});
|
||||||
inline for (S.fields) |Field| {
|
inline for (S.fields) |Field| {
|
||||||
// don't include void fields
|
// don't include void fields
|
||||||
if (Field.type == void) continue;
|
if (Field.type == void) continue;
|
||||||
|
|
||||||
// std.debug.print(" new field: {s}\n", .{Field.name});
|
// std.debug.print(" new field: {s}\n", .{Field.name});
|
||||||
const fname = fiobj_str_new(util.toCharPtr(Field.name), Field.name.len);
|
const fname = fio.fiobj_str_new(util.toCharPtr(Field.name), Field.name.len);
|
||||||
// std.debug.print(" fiobj name : {any}\n", .{fname});
|
// std.debug.print(" fiobj name : {any}\n", .{fname});
|
||||||
const v = @field(value, Field.name);
|
const v = @field(value, Field.name);
|
||||||
// std.debug.print(" value: {any}\n", .{v});
|
// std.debug.print(" value: {any}\n", .{v});
|
||||||
const fvalue = fiobjectify(v);
|
const fvalue = fiobjectify(v);
|
||||||
// std.debug.print(" fiobj value: {any}\n", .{fvalue});
|
// std.debug.print(" fiobj value: {any}\n", .{fvalue});
|
||||||
_ = fiobj_hash_set(m, fname, fvalue);
|
_ = fio.fiobj_hash_set(m, fname, fvalue);
|
||||||
fiobj_free_wrapped(fname);
|
fio.fiobj_free_wrapped(fname);
|
||||||
}
|
}
|
||||||
return m;
|
return m;
|
||||||
},
|
},
|
||||||
|
@ -194,13 +176,13 @@ pub fn fiobjectify(
|
||||||
.Slice => {
|
.Slice => {
|
||||||
// std.debug.print("new slice\n", .{});
|
// std.debug.print("new slice\n", .{});
|
||||||
if (ptr_info.child == u8 and std.unicode.utf8ValidateSlice(value)) {
|
if (ptr_info.child == u8 and std.unicode.utf8ValidateSlice(value)) {
|
||||||
return fiobj_str_new(util.toCharPtr(value), value.len);
|
return fio.fiobj_str_new(util.toCharPtr(value), value.len);
|
||||||
}
|
}
|
||||||
|
|
||||||
var arr = fiobj_ary_new2(value.len);
|
var arr = fio.fiobj_ary_new2(value.len);
|
||||||
for (value) |x| {
|
for (value) |x| {
|
||||||
const v = fiobjectify(x);
|
const v = fiobjectify(x);
|
||||||
fiobj_ary_push(arr, v);
|
fio.fiobj_ary_push(arr, v);
|
||||||
}
|
}
|
||||||
return arr;
|
return arr;
|
||||||
},
|
},
|
||||||
|
|
12
src/util.zig
12
src/util.zig
|
@ -1,17 +1,13 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const fio = @import("fio.zig");
|
||||||
|
|
||||||
pub const Cutil = @cImport({
|
pub fn fio2str(o: fio.FIOBJ) ?[]const u8 {
|
||||||
@cInclude("http.h");
|
|
||||||
@cInclude("fio.h");
|
|
||||||
});
|
|
||||||
|
|
||||||
pub fn fio2str(o: Cutil.FIOBJ) ?[]const u8 {
|
|
||||||
if (o == 0) return null;
|
if (o == 0) return null;
|
||||||
const x: Cutil.fio_str_info_s = Cutil.fiobj_obj2cstr(o);
|
const x: fio.fio_str_info_s = fio.fiobj_obj2cstr(o);
|
||||||
return std.mem.span(x.data);
|
return std.mem.span(x.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn str2fio(s: []const u8) Cutil.fio_str_info_s {
|
pub fn str2fio(s: []const u8) fio.fio_str_info_s {
|
||||||
return .{
|
return .{
|
||||||
.data = toCharPtr(s),
|
.data = toCharPtr(s),
|
||||||
.len = s.len,
|
.len = s.len,
|
||||||
|
|
64
src/zap.zig
64
src/zap.zig
|
@ -2,11 +2,9 @@
|
||||||
// or maybe let's just make it zap directly...
|
// or maybe let's just make it zap directly...
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
pub const C = @cImport({
|
const fio = @import("fio.zig");
|
||||||
@cInclude("http.h");
|
|
||||||
@cInclude("fio.h");
|
|
||||||
});
|
|
||||||
|
|
||||||
|
pub usingnamespace @import("fio.zig");
|
||||||
pub usingnamespace @import("endpoint.zig");
|
pub usingnamespace @import("endpoint.zig");
|
||||||
pub usingnamespace @import("util.zig");
|
pub usingnamespace @import("util.zig");
|
||||||
pub usingnamespace @import("http.zig");
|
pub usingnamespace @import("http.zig");
|
||||||
|
@ -16,8 +14,8 @@ const util = @import("util.zig");
|
||||||
|
|
||||||
const _module = @This();
|
const _module = @This();
|
||||||
|
|
||||||
pub fn start(args: C.fio_start_args) void {
|
pub fn start(args: fio.fio_start_args) void {
|
||||||
C.fio_start(args);
|
fio.fio_start(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const ListenError = error{
|
pub const ListenError = error{
|
||||||
|
@ -41,12 +39,12 @@ pub const SimpleRequest = struct {
|
||||||
query: ?[]const u8,
|
query: ?[]const u8,
|
||||||
body: ?[]const u8,
|
body: ?[]const u8,
|
||||||
method: ?[]const u8,
|
method: ?[]const u8,
|
||||||
h: [*c]C.http_s,
|
h: [*c]fio.http_s,
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
pub fn sendBody(self: *const Self, body: []const u8) c_int {
|
pub fn sendBody(self: *const Self, body: []const u8) c_int {
|
||||||
return C.http_send_body(self.h, @intToPtr(
|
return fio.http_send_body(self.h, @intToPtr(
|
||||||
*anyopaque,
|
*anyopaque,
|
||||||
@ptrToInt(body.ptr),
|
@ptrToInt(body.ptr),
|
||||||
), body.len);
|
), body.len);
|
||||||
|
@ -54,7 +52,7 @@ pub const SimpleRequest = struct {
|
||||||
|
|
||||||
pub fn sendJson(self: *const Self, json: []const u8) c_int {
|
pub fn sendJson(self: *const Self, json: []const u8) c_int {
|
||||||
self.setContentType(.JSON);
|
self.setContentType(.JSON);
|
||||||
return C.http_send_body(self.h, @intToPtr(
|
return fio.http_send_body(self.h, @intToPtr(
|
||||||
*anyopaque,
|
*anyopaque,
|
||||||
@ptrToInt(json.ptr),
|
@ptrToInt(json.ptr),
|
||||||
), json.len);
|
), json.len);
|
||||||
|
@ -69,29 +67,29 @@ pub const SimpleRequest = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn setContentTypeFromPath(self: *const Self) void {
|
pub fn setContentTypeFromPath(self: *const Self) void {
|
||||||
_ = C.fiobj_hash_set(
|
_ = fio.fiobj_hash_set(
|
||||||
self.h.*.private_data.out_headers,
|
self.h.*.private_data.out_headers,
|
||||||
C.HTTP_HEADER_CONTENT_TYPE,
|
fio.HTTP_HEADER_CONTENT_TYPE,
|
||||||
C.http_mimetype_find2(self.h.*.path),
|
fio.http_mimetype_find2(self.h.*.path),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn setHeader(self: *const Self, name: []const u8, value: []const u8) void {
|
pub fn setHeader(self: *const Self, name: []const u8, value: []const u8) void {
|
||||||
const hname: C.fio_str_info_s = .{
|
const hname: fio.fio_str_info_s = .{
|
||||||
.data = util.toCharPtr(name),
|
.data = util.toCharPtr(name),
|
||||||
.len = name.len,
|
.len = name.len,
|
||||||
.capa = name.len,
|
.capa = name.len,
|
||||||
};
|
};
|
||||||
const vname: C.fio_str_info_s = .{
|
const vname: fio.fio_str_info_s = .{
|
||||||
.data = util.toCharPtr(value),
|
.data = util.toCharPtr(value),
|
||||||
.len = value.len,
|
.len = value.len,
|
||||||
.capa = value.len,
|
.capa = value.len,
|
||||||
};
|
};
|
||||||
_ = C.http_set_header2(self.h, hname, vname);
|
_ = fio.http_set_header2(self.h, hname, vname);
|
||||||
|
|
||||||
// Note to self:
|
// Note to self:
|
||||||
// const new_fiobj_str = C.fiobj_str_new(name.ptr, name.len);
|
// const new_fiobj_str = fio.fiobj_str_new(name.ptr, name.len);
|
||||||
// C.fiobj_free(new_fiobj_str);
|
// fio.fiobj_free(new_fiobj_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn setStatusNumeric(self: *const Self, status: usize) void {
|
pub fn setStatusNumeric(self: *const Self, status: usize) void {
|
||||||
|
@ -104,9 +102,9 @@ pub const SimpleRequest = struct {
|
||||||
|
|
||||||
pub fn nextParam(self: *const Self) ?HttpParam {
|
pub fn nextParam(self: *const Self) ?HttpParam {
|
||||||
if (self.h.*.params == 0) return null;
|
if (self.h.*.params == 0) return null;
|
||||||
var key: C.FIOBJ = undefined;
|
var key: fio.FIOBJ = undefined;
|
||||||
const value = C.fiobj_hash_pop(self.h.*.params, &key);
|
const value = fio.fiobj_hash_pop(self.h.*.params, &key);
|
||||||
if (value == C.FIOBJ_INVALID) {
|
if (value == fio.FIOBJ_INVALID) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return HttpParam{
|
return HttpParam{
|
||||||
|
@ -116,7 +114,7 @@ pub const SimpleRequest = struct {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const HttpRequestFn = *const fn (r: [*c]C.http_s) callconv(.C) void;
|
pub const HttpRequestFn = *const fn (r: [*c]fio.http_s) callconv(.C) void;
|
||||||
pub const SimpleHttpRequestFn = *const fn (SimpleRequest) void;
|
pub const SimpleHttpRequestFn = *const fn (SimpleRequest) void;
|
||||||
|
|
||||||
pub const SimpleHttpListenerSettings = struct {
|
pub const SimpleHttpListenerSettings = struct {
|
||||||
|
@ -143,7 +141,7 @@ pub const SimpleHttpListener = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// we could make it dynamic by passing a SimpleHttpListener via udata
|
// we could make it dynamic by passing a SimpleHttpListener via udata
|
||||||
pub fn theOneAndOnlyRequestCallBack(r: [*c]C.http_s) callconv(.C) void {
|
pub fn theOneAndOnlyRequestCallBack(r: [*c]fio.http_s) callconv(.C) void {
|
||||||
if (the_one_and_only_listener) |l| {
|
if (the_one_and_only_listener) |l| {
|
||||||
var req: SimpleRequest = .{
|
var req: SimpleRequest = .{
|
||||||
.path = util.fio2str(r.*.path),
|
.path = util.fio2str(r.*.path),
|
||||||
|
@ -165,7 +163,7 @@ pub const SimpleHttpListener = struct {
|
||||||
pfolder = pf.ptr;
|
pfolder = pf.ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
var x: C.http_settings_s = .{
|
var x: fio.http_settings_s = .{
|
||||||
.on_request = if (self.settings.on_request) |_| Self.theOneAndOnlyRequestCallBack else null,
|
.on_request = if (self.settings.on_request) |_| Self.theOneAndOnlyRequestCallBack else null,
|
||||||
.on_upgrade = null,
|
.on_upgrade = null,
|
||||||
.on_response = null,
|
.on_response = null,
|
||||||
|
@ -198,7 +196,7 @@ pub const SimpleHttpListener = struct {
|
||||||
// const result = try bufPrint(buf, fmt ++ "\x00", args);
|
// const result = try bufPrint(buf, fmt ++ "\x00", args);
|
||||||
// return result[0 .. result.len - 1 :0];
|
// return result[0 .. result.len - 1 :0];
|
||||||
// }
|
// }
|
||||||
if (C.http_listen(printed_port.ptr, self.settings.interface, x) == -1) {
|
if (fio.http_listen(printed_port.ptr, self.settings.interface, x) == -1) {
|
||||||
return error.ListenError;
|
return error.ListenError;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -207,7 +205,7 @@ pub const SimpleHttpListener = struct {
|
||||||
// the SimpleHttpRequestFn will check if this is null and not process
|
// the SimpleHttpRequestFn will check if this is null and not process
|
||||||
// the request if it isn't set. hence, if started under full load, the
|
// the request if it isn't set. hence, if started under full load, the
|
||||||
// first request(s) might not be serviced, as long as it takes from
|
// first request(s) might not be serviced, as long as it takes from
|
||||||
// C.http_listen() to here
|
// fio.http_listen() to here
|
||||||
Self.the_one_and_only_listener = self;
|
Self.the_one_and_only_listener = self;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -216,10 +214,10 @@ pub const SimpleHttpListener = struct {
|
||||||
// lower level listening
|
// lower level listening
|
||||||
//
|
//
|
||||||
pub const ListenSettings = struct {
|
pub const ListenSettings = struct {
|
||||||
on_request: ?*const fn ([*c]C.http_s) callconv(.C) void = null,
|
on_request: ?*const fn ([*c]fio.http_s) callconv(.C) void = null,
|
||||||
on_upgrade: ?*const fn ([*c]C.http_s, [*c]u8, usize) callconv(.C) void = null,
|
on_upgrade: ?*const fn ([*c]fio.http_s, [*c]u8, usize) callconv(.C) void = null,
|
||||||
on_response: ?*const fn ([*c]C.http_s) callconv(.C) void = null,
|
on_response: ?*const fn ([*c]fio.http_s) callconv(.C) void = null,
|
||||||
on_finish: ?*const fn ([*c]C.struct_http_settings_s) callconv(.C) void = null,
|
on_finish: ?*const fn ([*c]fio.struct_http_settings_s) callconv(.C) void = null,
|
||||||
public_folder: ?[]const u8 = null,
|
public_folder: ?[]const u8 = null,
|
||||||
max_header_size: usize = 32 * 1024,
|
max_header_size: usize = 32 * 1024,
|
||||||
max_body_size: usize = 50 * 1024 * 1024,
|
max_body_size: usize = 50 * 1024 * 1024,
|
||||||
|
@ -242,7 +240,7 @@ pub fn listen(port: [*c]const u8, interface: [*c]const u8, settings: ListenSetti
|
||||||
pfolder_len = pf.len;
|
pfolder_len = pf.len;
|
||||||
pfolder = pf.ptr;
|
pfolder = pf.ptr;
|
||||||
}
|
}
|
||||||
var x: C.http_settings_s = .{
|
var x: fio.http_settings_s = .{
|
||||||
.on_request = settings.on_request,
|
.on_request = settings.on_request,
|
||||||
.on_upgrade = settings.on_upgrade,
|
.on_upgrade = settings.on_upgrade,
|
||||||
.on_response = settings.on_response,
|
.on_response = settings.on_response,
|
||||||
|
@ -268,14 +266,14 @@ pub fn listen(port: [*c]const u8, interface: [*c]const u8, settings: ListenSetti
|
||||||
// std.debug.print("X\n", .{});
|
// std.debug.print("X\n", .{});
|
||||||
std.time.sleep(500 * 1000 * 1000);
|
std.time.sleep(500 * 1000 * 1000);
|
||||||
|
|
||||||
if (C.http_listen(port, interface, x) == -1) {
|
if (fio.http_listen(port, interface, x) == -1) {
|
||||||
return error.ListenError;
|
return error.ListenError;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// lower level sendBody
|
// lower level sendBody
|
||||||
pub fn sendBody(request: [*c]C.http_s, body: []const u8) void {
|
pub fn sendBody(request: [*c]fio.http_s, body: []const u8) void {
|
||||||
_ = C.http_send_body(request, @intToPtr(
|
_ = fio.http_send_body(request, @intToPtr(
|
||||||
*anyopaque,
|
*anyopaque,
|
||||||
@ptrToInt(body.ptr),
|
@ptrToInt(body.ptr),
|
||||||
), body.len);
|
), body.len);
|
||||||
|
|
Loading…
Add table
Reference in a new issue