Skip to content

Commit

Permalink
first init.
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 committed Sep 16, 2023
0 parents commit f9906f2
Show file tree
Hide file tree
Showing 8 changed files with 528 additions and 0 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: CI

on:
workflow_dispatch:
pull_request:
paths:
- '**.zig'
- '**.yml'
push:
branches:
- main
paths:
- '**.zig'
- '**.yml'

jobs:
test:
timeout-minutes: 10
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v2
with:
version: master
- name: Run example
run: zig build run-basic
- name: Install deps
run: |
sudo apt update && sudo apt install -y valgrind libcurl4-openssl-dev
- name: Memory leak detect
run: |
zig build -Dcpu=baseline --verbose
TEST_BINARY=./zig-out/bin/basic
valgrind --leak-check=full --tool=memcheck \
--show-leak-kinds=all --error-exitcode=1 ${TEST_BINARY}
cross-compile:
timeout-minutes: 10
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
targets: [x86_64-windows, x86_64-linux, x86_64-macos, aarch64-macos]
steps:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v1
with:
version: master
- name: Install
run: |
sudo apt update && sudo apt install -y libcurl4-openssl-dev
- name: Build demo
run: |
zig build -Dtarget=${{ matrix.targets }}
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Created by https://www.toptal.com/developers/gitignore/api/zig
# Edit at https://www.toptal.com/developers/gitignore?templates=zig

### zig ###
# Zig programming language

zig-cache/
zig-out/
build/
build-*/
docgen_tmp/

# End of https://www.toptal.com/developers/gitignore/api/zig
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) Jiacai Liu <dev@liujiacai.net>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
60 changes: 60 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#+TITLE: Zig-curl
#+DATE: 2023-09-16T23:16:15+0800
#+LASTMOD: 2023-09-16T23:52:27+0800
#+OPTIONS: toc:nil num:nil
#+STARTUP: content

[[https://github.com/jiacai2050/zig-curl/actions/workflows/CI.yml][https://github.com/jiacai2050/zig-curl/actions/workflows/CI.yml/badge.svg]]

[[https://curl.haxx.se/libcurl/][libcurl]] bindings for Zig.

* Installation
First add this package to =build.zig.zon= of your project like this:
#+begin_src zig
.{
.dependencies = .{
.curl = .{
.url = "https://github.com/jiacai2050/zig-curl/archive/${COMMIT}.tar.gz",
.hash = "xxx",
},
},
}
#+end_src
Then in your =build.zig=, access the module like this:
#+begin_src zig
const curl = b.dependency("curl", .{
.target = target,
.optimize = optimize,
});

exe.addModule("curl", sqlite.module("curl"));
// Note: since this package doesn't bundle static libcurl,
// so users need to link to system-wide libcurl.
exe.linkSystemLibrary("libcurl");
#+end_src

* Usage
#+begin_src zig
const Easy = @import("curl").Easy;

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

const easy = try Easy.init(allocator);
defer easy.deinit();

const resp = try easy.get("http://httpbin.org/anything");
defer resp.deinit();

std.debug.print("Status code: {d}\nBody: {s}\n", .{
resp.status_code,
resp.body.items,
});
}
#+end_src
More usage can refer to [[file:examples/basic.zig][basic.zig]].
* Roadmap
- [ ] Currently only easy API is supported, support [[https://curl.se/libcurl/c/libcurl-multi.html][multi API]].
- By default, this package will attempt to dynamically link to the system-wide libcurl and the system-wide SSL library.
40 changes: 40 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const std = @import("std");
const Build = std.Build;
const Module = Build.Module;
const LazyPath = Build.LazyPath;

const MODULE_NAME = "curl";

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const module = b.addModule(MODULE_NAME, .{
.source_file = .{ .path = "src/main.zig" },
});

try addExample(b, "basic", module);

const main_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});

const run_main_tests = b.addRunArtifact(main_tests);
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&run_main_tests.step);
}

fn addExample(b: *std.Build, comptime name: []const u8, curl_module: *Module) !void {
const exe = b.addExecutable(.{
.name = name,
.root_source_file = LazyPath.relative("examples/" ++ name ++ ".zig"),
});

b.installArtifact(exe);
exe.addModule(MODULE_NAME, curl_module);
exe.linkSystemLibrary("libcurl");

const run_step = b.step("run-" ++ name, std.fmt.comptimePrint("Run {s} example", .{name}));
run_step.dependOn(&b.addRunArtifact(exe).step);
}
47 changes: 47 additions & 0 deletions examples/basic.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const std = @import("std");
const mem = std.mem;
const Allocator = mem.Allocator;
const Easy = @import("curl").Easy;

fn get(easy: Easy) !void {
const resp = try easy.get("http://httpbin.org/anything");
defer resp.deinit();

std.debug.print("Status code: {d}\nBody: {s}\n", .{
resp.status_code,
resp.body.items,
});
const date_header = try resp.get_header("date");
if (date_header) |h| {
std.debug.print("date header: {s}\n", .{h.get()});
} else {
std.debug.print("date header not found\n", .{});
}
}

fn post(easy: Easy) !void {
var payload = std.io.fixedBufferStream(
\\\ {"name": "John", "age": 15}
);
const resp = try easy.post("http://httpbin.org/anything", "application/json", payload.reader());
defer resp.deinit();

std.debug.print("Status code: {d}\nBody: {s}\n", .{
resp.status_code,
resp.body.items,
});
}

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

const easy = try Easy.init(allocator);
defer easy.deinit();

std.debug.print("-----------GET demo\n", .{});
try get(easy);
std.debug.print("-----------POST demo\n", .{});
try post(easy);
}
Loading

0 comments on commit f9906f2

Please sign in to comment.