Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSockets: fix write function by replacing fio.str2fio with util.str2fio #80

Merged
merged 5 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub fn build(b: *std.Build) !void {
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const optimize = b.standardOptimizeOption(.{});


const use_openssl = b.option(bool, "openssl", "Use system-installed openssl for TLS support in zap") orelse blk: {
// Alternatively, use an os env var to determine whether to build openssl support
if (std.posix.getenv("ZAP_USE_OPENSSL")) |val| {
Expand Down
14 changes: 8 additions & 6 deletions examples/cookies/cookies.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
10 changes: 5 additions & 5 deletions examples/http_params/http_params.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
1 change: 1 addition & 0 deletions src/middleware.zig
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +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 {

const self: *Self = @fieldParentPtr("handler", handler);
r.setUserContext(context);
self.endpoint.onRequest(r);
Expand Down
2 changes: 1 addition & 1 deletion src/websockets.zig
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub fn Handler(comptime ContextType: type) type {
pub inline fn write(handle: WsHandle, message: []const u8, is_text: bool) WebSocketError!void {
if (fio.websocket_write(
handle,
fio.str2fio(message),
util.str2fio(message),
if (is_text) 1 else 0,
) != 0) {
return error.WriteError;
Expand Down
19 changes: 11 additions & 8 deletions tools/announceybot.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fn usage() void {
\\ instructions
;
std.debug.print("{s}", .{message});

std.process.exit(1);
}

Expand Down Expand Up @@ -150,24 +151,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);
Expand Down
46 changes: 18 additions & 28 deletions wrk/zigstd/main.zig
Original file line number Diff line number Diff line change
@@ -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 gpa = std.heap.GeneralPurposeAllocator(.{
// .thread_safe = true,
// }){};
// const allocator = gpa.allocator();

var server = std.http.Server.init(.{
const address = try std.net.Address.parseIp("127.0.0.1", 3000);
var http_server = try address.listen(.{
.reuse_address = true,
});
defer server.deinit();

const address = try std.net.Address.parseIp("127.0.0.1", 3000);
try server.listen(address);
var read_buffer: [2048]u8 = undefined;

const max_header_size = 8192;
// 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" },
},
});
}
}
Loading