Zig standard library (std.http.Server) Zig
Zig standard library (std.http.Server) ਲਈ Zig ਸਨਿੱਪਟ, ਜੋ /pixel.gif ਦੀਆਂ ਬੇਨਤੀਆਂ ਦਾ ਜਵਾਬ ਖਾਲੀ GIF ਨਾਲ ਦਿੰਦਾ ਹੈ; GIF ਸ਼ੁਰੂਆਤ ’ਤੇ ਇੱਕ ਵਾਰ ਡੀਕੋਡ ਹੁੰਦੀ ਹੈ।
Zig standard library (std.http.Server) Zig
// Zig 0.16: std.http.Server on the std.Io interface (juicy main supplies `io`).
const std = @import("std");
const Io = std.Io;
const gif = [_]u8{ 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00, 0x80, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x21, 0xf9, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44, 0x01, 0x00, 0x3b };
pub fn main(init: std.process.Init) !void {
const io = init.io;
const address = try Io.net.IpAddress.parse("0.0.0.0", 8080);
var server = try address.listen(io, .{ .reuse_address = true });
defer server.deinit(io);
// One task per connection.
var group: Io.Group = .init;
defer group.cancel(io);
while (true) {
const stream = try server.accept(io);
group.async(io, handle, .{ io, stream });
}
}
fn handle(io: Io, stream: Io.net.Stream) void {
defer stream.close(io);
var recv_buffer: [4096]u8 = undefined;
var send_buffer: [4096]u8 = undefined;
var reader = stream.reader(io, &recv_buffer);
var writer = stream.writer(io, &send_buffer);
var http = std.http.Server.init(&reader.interface, &writer.interface);
while (http.reader.state == .ready) {
var request = http.receiveHead() catch return;
const is_pixel = request.head.method == .GET and
std.mem.eql(u8, request.head.target, "/pixel.gif");
const sent = if (is_pixel)
request.respond(&gif, .{ .extra_headers = &.{
.{ .name = "content-type", .value = "image/gif" },
.{ .name = "cache-control", .value = "no-store" },
} })
else
request.respond("Not Found", .{ .status = .not_found });
sent catch return;
}
}
ਅਸੀਂ ਇਹ ਸਨਿੱਪਟ ਚਲਾਇਆ ਅਤੇ ਇਸ ਵੱਲੋਂ ਵਾਪਸ ਕੀਤੀਆਂ ਬਾਈਟਾਂ ਦੀ ਤੁਲਨਾ ਕੀਤੀ।
ਜਾਂਚੋ
curl -sI http://localhost:8080/pixel.gif