1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 15:14:08 +00:00

fix breaking changes as of 14-4-24

This commit is contained in:
Andreas Stocker 2024-04-14 18:19:18 +02:00
parent 0f8903c06e
commit 44af23827e
9 changed files with 62 additions and 68 deletions

View file

@ -5,17 +5,19 @@ const zap = @import("zap");
fn makeRequest(a: std.mem.Allocator, url: []const u8) !void {
const uri = try std.Uri.parse(url);
var h = std.http.Headers{ .allocator = a };
defer h.deinit();
var http_client: std.http.Client = .{ .allocator = a };
defer http_client.deinit();
var req = try http_client.open(.GET, uri, h, .{});
var server_header_buffer: [2048]u8 = undefined;
var req = try http_client.open(.GET, uri, .{
.server_header_buffer = &server_header_buffer,
.extra_headers = &.{
.{ .name = "cookie", .value = "ZIG_ZAP=awesome" },
},
});
defer req.deinit();
try req.headers.append("cookie", "ZIG_ZAP=awesome");
try req.send(.{});
try req.send();
try req.wait();
}

View file

@ -54,7 +54,7 @@ fn userIdFromPath(self: *Self, path: []const u8) ?usize {
}
fn getUser(e: *zap.Endpoint, r: zap.Request) void {
const self = @fieldParentPtr(Self, "ep", e);
const self: *Self = @fieldParentPtr("ep", e);
if (r.path) |path| {
// /users
if (path.len == e.settings.path.len) {
@ -81,7 +81,7 @@ fn listUsers(self: *Self, r: zap.Request) void {
}
fn postUser(e: *zap.Endpoint, r: zap.Request) void {
const self = @fieldParentPtr(Self, "ep", e);
const self: *Self = @fieldParentPtr("ep", e);
if (r.body) |body| {
const maybe_user: ?std.json.Parsed(User) = std.json.parseFromSlice(User, self.alloc, body, .{}) catch null;
if (maybe_user) |u| {
@ -100,7 +100,7 @@ fn postUser(e: *zap.Endpoint, r: zap.Request) void {
}
fn putUser(e: *zap.Endpoint, r: zap.Request) void {
const self = @fieldParentPtr(Self, "ep", e);
const self: *Self = @fieldParentPtr("ep", e);
if (r.path) |path| {
if (self.userIdFromPath(path)) |id| {
if (self._users.get(id)) |_| {
@ -126,7 +126,7 @@ fn putUser(e: *zap.Endpoint, r: zap.Request) void {
}
fn deleteUser(e: *zap.Endpoint, r: zap.Request) void {
const self = @fieldParentPtr(Self, "ep", e);
const self: *Self = @fieldParentPtr("ep", e);
if (r.path) |path| {
if (self.userIdFromPath(path)) |id| {
var jsonbuf: [128]u8 = undefined;

View file

@ -5,16 +5,16 @@ const zap = @import("zap");
fn makeRequest(a: std.mem.Allocator, url: []const u8) !void {
const uri = try std.Uri.parse(url);
var h = std.http.Headers{ .allocator = a };
defer h.deinit();
var http_client: std.http.Client = .{ .allocator = a };
defer http_client.deinit();
var req = try http_client.open(.GET, uri, h, .{});
var server_header_buffer: [2048]u8 = undefined;
var req = try http_client.open(.GET, uri, .{
.server_header_buffer = &server_header_buffer,
});
defer req.deinit();
try req.send(.{});
try req.send();
try req.wait();
}

View file

@ -72,7 +72,7 @@ const UserMiddleWare = struct {
pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool {
// this is how we would get our self pointer
const self = @fieldParentPtr(Self, "handler", handler);
const self: *Self = @fieldParentPtr("handler", handler);
_ = self;
// do our work: fill in the user field of the context
@ -115,7 +115,7 @@ const SessionMiddleWare = struct {
// note that the first parameter is of type *Handler, not *Self !!!
pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool {
// this is how we would get our self pointer
const self = @fieldParentPtr(Self, "handler", handler);
const self: *Self = @fieldParentPtr("handler", handler);
_ = self;
context.session = Session{
@ -151,7 +151,7 @@ const HtmlMiddleWare = struct {
pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool {
// this is how we would get our self pointer
const self = @fieldParentPtr(Self, "handler", handler);
const self: *Self = @fieldParentPtr("handler", handler);
_ = self;
std.debug.print("\n\nHtmlMiddleware: handling request with context: {any}\n\n", .{context});

View file

@ -60,7 +60,7 @@ const UserMiddleWare = struct {
pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool {
// this is how we would get our self pointer
const self = @fieldParentPtr(Self, "handler", handler);
const self: *Self = @fieldParentPtr("handler", handler);
_ = self;
// do our work: fill in the user field of the context
@ -105,7 +105,7 @@ const SessionMiddleWare = struct {
// note that the first parameter is of type *Handler, not *Self !!!
pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool {
// this is how we would get our self pointer
const self = @fieldParentPtr(Self, "handler", handler);
const self: *Self = @fieldParentPtr("handler", handler);
_ = self;
context.session = Session{
@ -155,7 +155,7 @@ const HtmlEndpoint = struct {
}
pub fn get(ep: *zap.Endpoint, r: zap.Request) void {
const self = @fieldParentPtr(Self, "ep", ep);
const self: *Self = @fieldParentPtr("ep", ep);
_ = self;
var buf: [1024]u8 = undefined;

View file

@ -112,7 +112,7 @@ pub fn Authenticating(comptime Authenticator: type) type {
/// GET: here, the auth_endpoint will be passed in as endpoint.
/// Authenticates GET requests using the Authenticator.
pub fn get(e: *Endpoint, r: zap.Request) void {
const authEp: *Self = @fieldParentPtr(Self, "auth_endpoint", e);
const authEp: *Self = @fieldParentPtr("auth_endpoint", e);
switch (authEp.authenticator.authenticateRequest(&r)) {
.AuthFailed => {
if (e.settings.unauthorized) |unauthorized| {
@ -132,7 +132,7 @@ pub fn Authenticating(comptime Authenticator: type) type {
/// POST: here, the auth_endpoint will be passed in as endpoint.
/// Authenticates POST requests using the Authenticator.
pub fn post(e: *Endpoint, r: zap.Request) void {
const authEp: *Self = @fieldParentPtr(Self, "auth_endpoint", e);
const authEp: *Self = @fieldParentPtr("auth_endpoint", e);
switch (authEp.authenticator.authenticateRequest(&r)) {
.AuthFailed => {
if (e.settings.unauthorized) |unauthorized| {
@ -152,7 +152,7 @@ pub fn Authenticating(comptime Authenticator: type) type {
/// PUT: here, the auth_endpoint will be passed in as endpoint.
/// Authenticates PUT requests using the Authenticator.
pub fn put(e: *Endpoint, r: zap.Request) void {
const authEp: *Self = @fieldParentPtr(Self, "auth_endpoint", e);
const authEp: *Self = @fieldParentPtr("auth_endpoint", e);
switch (authEp.authenticator.authenticateRequest(&r)) {
.AuthFailed => {
if (e.settings.unauthorized) |unauthorized| {
@ -172,7 +172,7 @@ pub fn Authenticating(comptime Authenticator: type) type {
/// DELETE: here, the auth_endpoint will be passed in as endpoint.
/// Authenticates DELETE requests using the Authenticator.
pub fn delete(e: *Endpoint, r: zap.Request) void {
const authEp: *Self = @fieldParentPtr(Self, "auth_endpoint", e);
const authEp: *Self = @fieldParentPtr("auth_endpoint", e);
switch (authEp.authenticator.authenticateRequest(&r)) {
.AuthFailed => {
if (e.settings.unauthorized) |unauthorized| {
@ -192,7 +192,7 @@ pub fn Authenticating(comptime Authenticator: type) type {
/// PATCH: here, the auth_endpoint will be passed in as endpoint.
/// Authenticates PATCH requests using the Authenticator.
pub fn patch(e: *Endpoint, r: zap.Request) void {
const authEp: *Self = @fieldParentPtr(Self, "auth_endpoint", e);
const authEp: *Self = @fieldParentPtr("auth_endpoint", e);
switch (authEp.authenticator.authenticateRequest(&r)) {
.AuthFailed => {
if (e.settings.unauthorized) |unauthorized| {
@ -212,7 +212,7 @@ pub fn Authenticating(comptime Authenticator: type) type {
/// OPTIONS: here, the auth_endpoint will be passed in as endpoint.
/// Authenticates OPTIONS requests using the Authenticator.
pub fn options(e: *Endpoint, r: zap.Request) void {
const authEp: *Self = @fieldParentPtr(Self, "auth_endpoint", e);
const authEp: *Self = @fieldParentPtr("auth_endpoint", e);
switch (authEp.authenticator.authenticateRequest(&r)) {
.AuthFailed => {
if (e.settings.unauthorized) |unauthorized| {

View file

@ -82,7 +82,7 @@ pub fn EndpointHandler(comptime HandlerType: anytype, comptime ContextType: anyt
/// If `breakOnFinish` is `true`, the handler will stop handing requests down the chain if
/// the endpoint processed the request.
pub fn onRequest(handler: *HandlerType, r: zap.Request, context: *ContextType) bool {
var self = @fieldParentPtr(Self, "handler", handler);
var self: *Self = @fieldParentPtr("handler", handler);
r.setUserContext(context);
self.endpoint.onRequest(r);

View file

@ -28,7 +28,7 @@ fn usage() void {
\\ instructions
;
std.debug.print("{s}", .{message});
std.os.exit(1);
std.posix.exit(1);
}
var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){};
@ -150,24 +150,26 @@ fn sendToDiscordPart(allocator: std.mem.Allocator, url: []const u8, message_json
// url
const uri = try std.Uri.parse(url);
// http headers
var h = std.http.Headers{ .allocator = allocator };
defer h.deinit();
try h.append("accept", "*/*");
try h.append("Content-Type", "application/json");
// client
var http_client: std.http.Client = .{ .allocator = allocator };
defer http_client.deinit();
var server_header_buffer: [2048]u8 = undefined;
// request
var req = try http_client.open(.POST, uri, h, .{});
var req = try http_client.open(.POST, uri, .{
.server_header_buffer = &server_header_buffer,
.extra_headers = &.{
.{ .name = "accept", .value = "*/*" },
.{ .name = "Content-Type", .value = "application/json" },
},
});
defer req.deinit();
req.transfer_encoding = .chunked;
// connect, send request
try req.send(.{});
try req.send();
// send POST payload
try req.writer().writeAll(message_json);
@ -336,7 +338,7 @@ fn command_announce(allocator: std.mem.Allocator, tag: []const u8) !void {
defer allocator.free(url);
sendToDiscord(allocator, url, announcement) catch |err| {
std.debug.print("HTTP ERROR: {any}\n", .{err});
std.os.exit(1);
std.posix.exit(1);
};
}

View file

@ -1,43 +1,33 @@
const std = @import("std");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{
.thread_safe = true,
}){};
const allocator = gpa.allocator();
var server = std.http.Server.init(.{
.reuse_address = true,
});
defer server.deinit();
// var gpa = std.heap.GeneralPurposeAllocator(.{
// .thread_safe = true,
// }){};
// const allocator = gpa.allocator();
const address = try std.net.Address.parseIp("127.0.0.1", 3000);
try server.listen(address);
var http_server = try address.listen(.{
.reuse_address = true,
});
const max_header_size = 8192;
var read_buffer: [2048]u8 = undefined;
// const max_header_size = 8192;
while (true) {
var res = try server.accept(.{
.allocator = allocator,
.header_strategy = .{ .dynamic = max_header_size },
});
// const start_time = std.time.nanoTimestamp();
defer res.deinit();
defer _ = res.reset();
try res.wait();
const connection = try http_server.accept();
defer connection.stream.close();
var server = std.http.Server.init(connection, &read_buffer);
var request = try server.receiveHead();
const server_body: []const u8 = "HI FROM ZIG STD!\n";
res.transfer_encoding = .{ .content_length = server_body.len };
try res.headers.append("content-type", "text/plain");
try res.headers.append("connection", "close");
try res.send();
var buf: [128]u8 = undefined;
_ = try res.readAll(&buf);
_ = try res.writer().writeAll(server_body);
try res.finish();
// const end_time = std.time.nanoTimestamp();
// const diff = end_time - start_time;
// std.debug.print("{d}\n", .{diff});
try request.respond(server_body, .{
.extra_headers = &.{
.{ .name = "content_type", .value = "text/plain" },
.{ .name = "connection", .value = "close" },
},
});
}
}