emptygif
மொழி: தமிழ்

அனைத்து மொழிகளும் ஃப்ரேம்வொர்க்குகளும்

Zig standard library (std.http.Server) Zig

/pixel.gif க்கு வரும் ரெக்வெஸ்ட்களுக்கு empty GIF மூலம் பதிலளிக்கும், Zig standard library (std.http.Server) க்கான Zig ஸ்னிப்பெட். GIF ஸ்டார்ட்அப்பில் ஒருமுறை மட்டுமே டீகோட் செய்யப்படுகிறது.

Zig standard library (std.http.Server) Zig

ஃபைல்
pixel.zig
ரன்
zig run -O ReleaseSafe pixel.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

அதிகாரப்பூர்வ சைட்: Zig standard library (std.http.Server)