GithubHelp home page GithubHelp logo

zig-tls12's Introduction

Zig-TLS12

HTTP client capable of TLS 1.2.

You may use HttpClient in the same way you use std.http.Client. The only difference of HttpClient from std.http.Client is that std.crypto.tls.Client which supports TLS 1.3 is replaced by a TLS 1.2 capable TlsClient.

Usage

Add to build.zig.zon:

.{
    .dependencies = .{
        // other dependencies...
        .tls12 = .{
            // Check releases for other versions
            .url = "https://github.com/melonedo/zig-tls12/archive/refs/heads/main.zip",
            // Let zon find out
            .hash = "12341234123412341234123412341234123412341234123412341234123412341234",
        },
    },
}

Add to build.zig:

// const exe = b.addExecutable(...);
const tls12 = b.dependency("tls12", .{
    .target = target,
    .optimize = optimize,
});
exe.addModule("tls12", tls12.module("zig-tls12"));

In zig program:

// Drop-in replacement of std.http.Client
const HttpClient = @import("tls12");

Example

This is tested againt zig version 0.12.0-dev.3674+a0de07760. Zig's HTTP interface has changed so it DOES NOT WORK on 0.11 and below.

const std = @import("std");
// With zon:    const HttpClient = @import("tls12");
// With stdlib: const HttpClient = std.http.Client;
const HttpClient = @import("HttpClient.zig");

pub fn main() !void {
    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();
    const allocator = arena.allocator();

    var client = HttpClient{ .allocator = allocator };
    defer client.deinit();
    try client.initDefaultProxies(allocator);

    const url = "https://httpbin.org";

    const uri = try std.Uri.parse(url);
    var server_header_buffer: [1024 * 1024]u8 = undefined;
    var req = try HttpClient.open(&client, .GET, uri, .{
        .server_header_buffer = &server_header_buffer,
        .redirect_behavior = @enumFromInt(10),
    });
    defer req.deinit();

    try req.send();
    try req.wait();
    const body = try req.reader().readAllAlloc(allocator, 16 * 1024 * 1024);
    defer allocator.free(body);

    std.debug.print("{s}: {s}\n", .{ url, body });
}

zig-tls12's People

Contributors

melonedo avatar cloudef avatar rofrol avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.