mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 15:14:08 +00:00
added errors
This commit is contained in:
parent
0ec7fa4a91
commit
615d90b8f4
10 changed files with 51 additions and 39 deletions
|
@ -219,7 +219,7 @@ fn on_request(r: zap.SimpleRequest) void {
|
|||
if (r.query) |the_query| {
|
||||
std.debug.print("QUERY: {s}\n", .{the_query});
|
||||
}
|
||||
_ = r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>");
|
||||
r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>") catch return;
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
|
|
|
@ -55,7 +55,7 @@ fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
|||
if (userIdFromPath(path)) |id| {
|
||||
if (users.get(id)) |user| {
|
||||
if (zap.stringifyBuf(&jsonbuf, user, .{})) |json| {
|
||||
_ = r.sendJson(json);
|
||||
r.sendJson(json) catch return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -66,8 +66,8 @@ fn listUsers(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
|||
_ = e;
|
||||
|
||||
if (users.toJSON()) |json| {
|
||||
_ = r.sendJson(json);
|
||||
alloc.free(json);
|
||||
defer alloc.free(json);
|
||||
r.sendJson(json) catch return;
|
||||
} else |err| {
|
||||
std.debug.print("LIST error: {}\n", .{err});
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ fn postUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
|||
if (users.addByName(u.first_name, u.last_name)) |id| {
|
||||
var jsonbuf: [128]u8 = undefined;
|
||||
if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| {
|
||||
_ = r.sendJson(json);
|
||||
r.sendJson(json) catch return;
|
||||
}
|
||||
} else |err| {
|
||||
std.debug.print("ADDING error: {}\n", .{err});
|
||||
|
@ -106,11 +106,11 @@ fn putUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
|||
var jsonbuf: [128]u8 = undefined;
|
||||
if (users.update(id, u.first_name, u.last_name)) {
|
||||
if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| {
|
||||
_ = r.sendJson(json);
|
||||
r.sendJson(json) catch return;
|
||||
}
|
||||
} else {
|
||||
if (zap.stringifyBuf(&jsonbuf, .{ .status = "ERROR", .id = id }, .{})) |json| {
|
||||
_ = r.sendJson(json);
|
||||
r.sendJson(json) catch return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -127,11 +127,11 @@ fn deleteUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
|||
var jsonbuf: [128]u8 = undefined;
|
||||
if (users.delete(id)) {
|
||||
if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| {
|
||||
_ = r.sendJson(json);
|
||||
r.sendJson(json) catch return;
|
||||
}
|
||||
} else {
|
||||
if (zap.stringifyBuf(&jsonbuf, .{ .status = "ERROR", .id = id }, .{})) |json| {
|
||||
_ = r.sendJson(json);
|
||||
r.sendJson(json) catch return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,11 +9,11 @@ fn on_request_verbose(r: zap.SimpleRequest) void {
|
|||
if (r.query) |the_query| {
|
||||
std.debug.print("QUERY: {s}\n", .{the_query});
|
||||
}
|
||||
_ = r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>");
|
||||
r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>") catch return;
|
||||
}
|
||||
|
||||
fn on_request_minimal(r: zap.SimpleRequest) void {
|
||||
_ = r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>");
|
||||
r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>") catch return;
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
|
|
|
@ -13,8 +13,8 @@ fn on_request(r: zap.SimpleRequest) void {
|
|||
std.debug.print(">> BODY: {s}\n", .{the_body});
|
||||
}
|
||||
|
||||
r.setContentTypeFromPath();
|
||||
_ = r.sendBody(
|
||||
r.setContentTypeFromPath() catch return;
|
||||
r.sendBody(
|
||||
\\ <html><body>
|
||||
\\ <h1>Hello from ZAP!!!</h1>
|
||||
\\ <form action="/" method="post">
|
||||
|
@ -25,7 +25,7 @@ fn on_request(r: zap.SimpleRequest) void {
|
|||
\\ <button>Send</button>
|
||||
\\ </form>
|
||||
\\ </body></html>
|
||||
);
|
||||
) catch return;
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
|
|
|
@ -26,8 +26,8 @@ fn on_request(r: zap.SimpleRequest) void {
|
|||
json_to_send = "null";
|
||||
}
|
||||
std.debug.print("<< json: {s}\n", .{json_to_send});
|
||||
r.setContentType(.JSON);
|
||||
_ = r.sendBody(json_to_send);
|
||||
r.setContentType(.JSON) catch return;
|
||||
r.sendBody(json_to_send) catch return;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -25,11 +25,11 @@ fn on_request(r: zap.SimpleRequest) void {
|
|||
},
|
||||
});
|
||||
defer ret.deinit();
|
||||
r.setContentType(.TEXT);
|
||||
r.setContentType(.TEXT) catch return;
|
||||
if (ret.str()) |s| {
|
||||
_ = r.sendBody(s);
|
||||
r.sendBody(s) catch return;
|
||||
} else {
|
||||
_ = r.sendBody("<html><body><h1>MustacheBuild() failed!</h1></body></html>");
|
||||
r.sendBody("<html><body><h1>MustacheBuild() failed!</h1></body></html>") catch return;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,18 +10,18 @@ fn dispatch_routes(r: zap.SimpleRequest) void {
|
|||
}
|
||||
}
|
||||
// or default: present menu
|
||||
_ = r.sendBody(
|
||||
r.sendBody(
|
||||
\\ <html>
|
||||
\\ <body>
|
||||
\\ <p><a href="/static">static</a></p>
|
||||
\\ <p><a href="/dynamic">dynamic</a></p>
|
||||
\\ </body>
|
||||
\\ </html>
|
||||
);
|
||||
) catch return;
|
||||
}
|
||||
|
||||
fn static_site(r: zap.SimpleRequest) void {
|
||||
_ = r.sendBody("<html><body><h1>Hello from STATIC ZAP!</h1></body></html>");
|
||||
r.sendBody("<html><body><h1>Hello from STATIC ZAP!</h1></body></html>") catch return;
|
||||
}
|
||||
|
||||
var dynamic_counter: i32 = 0;
|
||||
|
@ -33,7 +33,7 @@ fn dynamic_site(r: zap.SimpleRequest) void {
|
|||
"<html><body><h1>Hello # {d} from DYNAMIC ZAP!!!</h1></body></html>",
|
||||
.{dynamic_counter},
|
||||
) catch "ERROR";
|
||||
_ = r.sendBody(filled_buf);
|
||||
r.sendBody(filled_buf) catch return;
|
||||
}
|
||||
|
||||
fn setup_routes(a: std.mem.Allocator) !void {
|
||||
|
|
|
@ -3,7 +3,7 @@ const zap = @import("zap");
|
|||
|
||||
fn on_request(r: zap.SimpleRequest) void {
|
||||
r.setStatus(.not_found);
|
||||
_ = r.sendBody("<html><body><h1>404 - File not found</h1></body></html>");
|
||||
r.sendBody("<html><body><h1>404 - File not found</h1></body></html>") catch return;
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
pub const FIOBJ = usize;
|
||||
pub extern fn is_invalid(o: FIOBJ) c_int;
|
||||
pub const fio_url_s = extern struct {
|
||||
scheme: fio_str_info_s,
|
||||
user: fio_str_info_s,
|
||||
|
|
41
src/zap.zig
41
src/zap.zig
|
@ -43,6 +43,12 @@ pub const ListenError = error{
|
|||
ListenError,
|
||||
};
|
||||
|
||||
pub const HttpError = error{
|
||||
HttpSendBody,
|
||||
HttpSetContentType,
|
||||
HttpSetHeader,
|
||||
};
|
||||
|
||||
pub const HttpParam = struct {
|
||||
key: []const u8,
|
||||
value: []const u8,
|
||||
|
@ -63,25 +69,25 @@ pub const SimpleRequest = struct {
|
|||
|
||||
const Self = @This();
|
||||
|
||||
pub fn sendBody(self: *const Self, body: []const u8) c_int {
|
||||
pub fn sendBody(self: *const Self, body: []const u8) HttpError!void {
|
||||
const ret = fio.http_send_body(self.h, @intToPtr(
|
||||
*anyopaque,
|
||||
@ptrToInt(body.ptr),
|
||||
), body.len);
|
||||
debug("SimpleRequest.sendBody(): ret = {}\n", .{ret});
|
||||
return ret;
|
||||
if (ret == -1) return error.HttpSendBody;
|
||||
}
|
||||
|
||||
pub fn sendJson(self: *const Self, json: []const u8) !c_int {
|
||||
pub fn sendJson(self: *const Self, json: []const u8) HttpError!void {
|
||||
if (self.setContentType(.JSON)) {
|
||||
return fio.http_send_body(self.h, @intToPtr(
|
||||
if (fio.http_send_body(self.h, @intToPtr(
|
||||
*anyopaque,
|
||||
@ptrToInt(json.ptr),
|
||||
), json.len);
|
||||
} else return error{ERR_CONTENT_TYPE};
|
||||
), json.len) != 0) return error.HttpSendBody;
|
||||
} else |err| return err;
|
||||
}
|
||||
|
||||
pub fn setContentType(self: *const Self, c: ContentType) bool {
|
||||
pub fn setContentType(self: *const Self, c: ContentType) HttpError!void {
|
||||
const s = switch (c) {
|
||||
.TEXT => "text/plain",
|
||||
.JSON => "application/json",
|
||||
|
@ -96,25 +102,28 @@ pub const SimpleRequest = struct {
|
|||
self: *const Self,
|
||||
c: ContentType,
|
||||
logger: *const Log,
|
||||
) void {
|
||||
) HttpError!void {
|
||||
const s = switch (c) {
|
||||
.TEXT => "text/plain",
|
||||
.JSON => "application/json",
|
||||
else => "text/html",
|
||||
};
|
||||
logger.log("setting content-type to {s}\n", .{s});
|
||||
self.setHeader("content-type", s);
|
||||
return self.setHeader("content-type", s);
|
||||
}
|
||||
|
||||
pub fn setContentTypeFromPath(self: *const Self) void {
|
||||
_ = fio.fiobj_hash_set(
|
||||
pub fn setContentTypeFromPath(self: *const Self) !void {
|
||||
const t = fio.http_mimetype_find2(self.h.*.path);
|
||||
if (fio.is_invalid(t) == 1) return error.HttpSetContentType;
|
||||
const ret = fio.fiobj_hash_set(
|
||||
self.h.*.private_data.out_headers,
|
||||
fio.HTTP_HEADER_CONTENT_TYPE,
|
||||
fio.http_mimetype_find2(self.h.*.path),
|
||||
t,
|
||||
);
|
||||
if (ret == -1) return error.HttpSetContentType;
|
||||
}
|
||||
|
||||
pub fn setHeader(self: *const Self, name: []const u8, value: []const u8) bool {
|
||||
pub fn setHeader(self: *const Self, name: []const u8, value: []const u8) HttpError!void {
|
||||
const hname: fio.fio_str_info_s = .{
|
||||
.data = util.toCharPtr(name),
|
||||
.len = name.len,
|
||||
|
@ -135,7 +144,8 @@ pub const SimpleRequest = struct {
|
|||
// const new_fiobj_str = fio.fiobj_str_new(name.ptr, name.len);
|
||||
// fio.fiobj_free(new_fiobj_str);
|
||||
|
||||
return ret == 0;
|
||||
if (ret == 0) return;
|
||||
return error.HttpSetHeader;
|
||||
}
|
||||
|
||||
pub fn setStatusNumeric(self: *const Self, status: usize) void {
|
||||
|
@ -319,10 +329,11 @@ pub fn listen(port: [*c]const u8, interface: [*c]const u8, settings: ListenSetti
|
|||
}
|
||||
|
||||
// lower level sendBody
|
||||
pub fn sendBody(request: [*c]fio.http_s, body: []const u8) void {
|
||||
pub fn sendBody(request: [*c]fio.http_s, body: []const u8) HttpError!void {
|
||||
const ret = fio.http_send_body(request, @intToPtr(
|
||||
*anyopaque,
|
||||
@ptrToInt(body.ptr),
|
||||
), body.len);
|
||||
debug("sendBody(): ret = {}\n", .{ret});
|
||||
if (ret != -1) return error.HttpSendBody;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue