]> gitweb.ps.run Git - zighttp/blobdiff - src/http.zig
make reading from socket a bit more reliable
[zighttp] / src / http.zig
index 2c47d1df0d8ec6d43afefd609395357341b3299c..c14f16dec2dbaab931ba2c952ad1a7054996132f 100644 (file)
@@ -59,11 +59,21 @@ pub const Server = struct {
                 errdefer posix.close(client_socket);
                 var event = linux.epoll_event{ .events = linux.EPOLL.IN, .data = .{ .fd = client_socket } };
                 try posix.epoll_ctl(self.efd, linux.EPOLL.CTL_ADD, client_socket, &event);
+                var addr: std.c.sockaddr = undefined;
+                var addr_size: std.c.socklen_t = @sizeOf(std.c.sockaddr);
+                _ = std.c.getpeername(client_socket, &addr, &addr_size);
+                std.debug.print("new connection from {}\n", .{addr});
             } else {
                 var closed = false;
                 var req = Request{ .fd = ready_socket };
 
-                const read = posix.read(ready_socket, buf) catch 0;
+                var read: usize = 0;
+                while (true) {
+                    const newly_read = posix.read(ready_socket, buf[read..]) catch 0;
+                    read += newly_read;
+                    if (newly_read == 0)
+                        break;
+                }
                 if (read == 0) {
                     closed = true;
                 } else {
@@ -109,6 +119,7 @@ pub const Request = struct {
     body: ?[]u8 = null,
 
     pub fn parse(self: *Request, buf: []u8) bool {
+        std.debug.print("buf: {s}\n", .{buf});
         var state: u8 = 0;
 
         var start: u32 = 0;
@@ -260,7 +271,7 @@ pub const Request = struct {
         }
     }
 
-    pub fn get_value(self: *Request, name: []const u8) ?[]const u8 {
+    pub fn get_value(self: Request, name: []const u8) ?[]const u8 {
         const body = self.body orelse return null;
         const name_index = std.mem.indexOf(u8, body, name) orelse return null;
         const eql_index = std.mem.indexOfScalarPos(u8, body, name_index, '=') orelse return null;