-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from xflow-systems/zig-0.12.0
WebSockets: fix write function by replacing fio.str2fio with util.str2fio
- Loading branch information
Showing
7 changed files
with
45 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }, | ||
}, | ||
}); | ||
} | ||
} |