http server
This commit is contained in:
@@ -15,15 +15,9 @@ main :: () -> s32 {
|
||||
opt : s32 = 1;
|
||||
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, @opt, 4);
|
||||
|
||||
addr := SockAddr.{
|
||||
sin_len = 16,
|
||||
sin_family = 2,
|
||||
sin_port = htons(PORT),
|
||||
sin_addr = 0,
|
||||
sin_zero = 0
|
||||
};
|
||||
addr := SockAddr.{ sin_len = 16, sin_family = 2, sin_port = htons(PORT) };
|
||||
|
||||
if bind(fd, @addr, 16) < 0 {
|
||||
if bind(fd, addr, 16) < 0 {
|
||||
print("error: bind()\n");
|
||||
return 1;
|
||||
}
|
||||
@@ -40,8 +34,8 @@ main :: () -> s32 {
|
||||
if client < 0 { continue; }
|
||||
|
||||
// Read request
|
||||
buf := alloc(4096);
|
||||
read(client, buf, 4096);
|
||||
buf : [4096]u8 = ---;
|
||||
read(client, buf, buf.len);
|
||||
|
||||
// Send response
|
||||
body := "<html><body><h1>Hello from sx!</h1></body></html>";
|
||||
|
||||
@@ -9,8 +9,8 @@ setsockopt :: (fd: s32, level: s32, optname: s32, optval: *s32, optlen: u32) ->
|
||||
bind :: (fd: s32, addr: *SockAddr, addrlen: u32) -> s32 #foreign libc;
|
||||
listen :: (fd: s32, backlog: s32) -> s32 #foreign libc;
|
||||
accept :: (fd: s32, addr: *SockAddr, addrlen: *u32) -> s32 #foreign libc;
|
||||
read :: (fd: s32, buf: [:0]u8, count: s64) -> s64 #foreign libc;
|
||||
write :: (fd: s32, buf: [:0]u8, count: s64) -> s64 #foreign libc;
|
||||
read :: (fd: s32, buf: [*]u8, count: s64) -> s64 #foreign libc;
|
||||
write :: (fd: s32, buf: [*]u8, count: s64) -> s64 #foreign libc;
|
||||
close :: (fd: s32) -> s32 #foreign libc;
|
||||
|
||||
// Constants (macOS)
|
||||
@@ -24,8 +24,8 @@ SockAddr :: struct {
|
||||
sin_len: u8;
|
||||
sin_family: u8;
|
||||
sin_port: u16;
|
||||
sin_addr: u32;
|
||||
sin_zero: u64;
|
||||
sin_addr: u32 = 0;
|
||||
sin_zero: u64 = 0;
|
||||
}
|
||||
|
||||
htons :: (port: s64) -> u16 {
|
||||
|
||||
@@ -4,9 +4,9 @@ sqrt :: (x: $T) -> T #builtin;
|
||||
sin :: (x: $T) -> T #builtin;
|
||||
cos :: (x: $T) -> T #builtin;
|
||||
size_of :: ($T: Type) -> s64 #builtin;
|
||||
alloc :: (size: s64) -> string #builtin;
|
||||
malloc :: (size: s64) -> *void #builtin;
|
||||
memcpy :: (dst: *void, src: *void, size: s64) -> *void #builtin;
|
||||
memset :: (dst: *void, val: s64, size: s64) -> void #builtin;
|
||||
free :: (ptr: *void) -> void #builtin;
|
||||
type_of :: (val: $T) -> Type #builtin;
|
||||
type_name :: ($T: Type) -> string #builtin;
|
||||
@@ -18,6 +18,20 @@ field_value_int :: ($T: Type, idx: s64) -> s64 #builtin;
|
||||
field_index :: ($T: Type, val: T) -> s64 #builtin;
|
||||
string :: []u8 #builtin;
|
||||
|
||||
CString :: union {
|
||||
s: string;
|
||||
struct { ptr: *void; len: s64; };
|
||||
}
|
||||
|
||||
cstring :: (size: s64) -> string {
|
||||
raw := malloc(size + 1);
|
||||
memset(raw, 0, size + 1);
|
||||
rs : CString = ---;
|
||||
rs.ptr = raw;
|
||||
rs.len = size;
|
||||
rs.s;
|
||||
}
|
||||
|
||||
int_to_string :: (n: s64) -> string {
|
||||
if n == 0 { return "0"; }
|
||||
neg := n < 0;
|
||||
@@ -26,7 +40,7 @@ int_to_string :: (n: s64) -> string {
|
||||
len := 0;
|
||||
while tmp > 0 { len += 1; tmp = tmp / 10; }
|
||||
total := if neg then len + 1 else len;
|
||||
buf := alloc(total);
|
||||
buf := cstring(total);
|
||||
i := total - 1;
|
||||
while v > 0 {
|
||||
buf[i] = (v % 10) + 48;
|
||||
@@ -53,18 +67,17 @@ float_to_string :: (f: f64) -> string {
|
||||
fl := fstr.len;
|
||||
prefix := if neg then 1 else 0;
|
||||
total := prefix + il + 1 + 6;
|
||||
buf := alloc(total);
|
||||
buf := cstring(total);
|
||||
pos := 0;
|
||||
if neg { buf[0] = 45; pos = 1; }
|
||||
i := 0;
|
||||
while i < il { buf[pos] = istr[i]; pos += 1; i += 1; }
|
||||
memcpy(@buf[pos], istr.ptr, il);
|
||||
pos = pos + il;
|
||||
buf[pos] = 46;
|
||||
pos += 1;
|
||||
pad := 6 - fl;
|
||||
j := 0;
|
||||
while j < pad { buf[pos] = 48; pos += 1; j += 1; }
|
||||
k := 0;
|
||||
while k < fl { buf[pos] = fstr[k]; pos += 1; k += 1; }
|
||||
memset(@buf[pos], 48, pad);
|
||||
pos = pos + pad;
|
||||
memcpy(@buf[pos], fstr.ptr, fl);
|
||||
buf;
|
||||
}
|
||||
|
||||
@@ -84,7 +97,7 @@ int_to_hex_string :: (n: s64) -> string {
|
||||
g3 := r3 % 65536;
|
||||
if g3 < 0 { g3 = g3 + 65536; }
|
||||
|
||||
buf := alloc(16);
|
||||
buf := cstring(16);
|
||||
// Group 3: digits 0-3 (bits 48-63)
|
||||
i := 3;
|
||||
v := g3;
|
||||
@@ -133,21 +146,15 @@ int_to_hex_string :: (n: s64) -> string {
|
||||
concat :: (a: string, b: string) -> string {
|
||||
al := a.len;
|
||||
bl := b.len;
|
||||
buf := alloc(al + bl);
|
||||
i := 0;
|
||||
while i < al { buf[i] = a[i]; i += 1; }
|
||||
j := 0;
|
||||
while j < bl { buf[al + j] = b[j]; j += 1; }
|
||||
buf := cstring(al + bl);
|
||||
memcpy(buf.ptr, a.ptr, al);
|
||||
memcpy(@buf[al], b.ptr, bl);
|
||||
buf;
|
||||
}
|
||||
|
||||
substr :: (s: string, start: s64, len: s64) -> string {
|
||||
buf := alloc(len);
|
||||
i := 0;
|
||||
while i < len {
|
||||
buf[i] = s[start + i];
|
||||
i += 1;
|
||||
}
|
||||
buf := cstring(len);
|
||||
memcpy(buf.ptr, @s[start], len);
|
||||
buf;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user