实战:Zig 编写高性能 Web 服务(1)

1.1 认识 std.http

std.http 是 Zig 标准库中用于处理 HTTP 相关操作的类库。以我学习新的编程语言的经历来看,编写web程序是最常见的技术场景,所以熟练掌握 HTTP server/client 服务相关的编程知识是比较重要的。

std.http 主要包含以下API:

  • Client: HTTP client implementation.
  • Server: HTTP server implementation.
  • protocol:headers parse methods.
  • Connection: Connection type (keep_alive, close)
  • ContentEncoding: Content encoding options (compress, deflate, gzip and zstd)
  • Field: Common type for name and value
  • Headers: HTTP headers
  • Method: HTTP methods such as GET and POST
  • Status: HTTP status codes (not_found = 404, teapot = 418, etc.)
  • TransferEncoding: Form of encoding used to transfer the body (chunked)
  • Version: Currently HTTP/1.0 and HTTP/1.1

1.2 编写一个HTTP client程序

先创建一个开工项目:

bash 复制代码
$ mkdir -p httpz
$ cd httpz
$ zig init

$ ls -ls
total 20
4 -rw-r--r-- 1 xiaods xiaods 3879 Jun  3 11:53 build.zig
4 -rw-r--r-- 1 xiaods xiaods 3080 Jun  3 11:50 build.zig.zon
4 drwxr-xr-x 2 xiaods xiaods 4096 Jun  3 13:33 src
4 drwxr-xr-x 6 xiaods xiaods 4096 Jun  3 11:51 zig-cache
4 drwxr-xr-x 4 xiaods xiaods 4096 Jun  3 11:51 zig-out

编辑 src/main.zig,我们将使用 std.heap.GeneralPurposeAllocator,这是一个安全的分配器,可以防止双重释放(double-free)、使用后释放(use-after-free),并且能够检测内存泄漏。

cpp 复制代码
const std = @import("std");
const print = std.debug.print;
const http = std.http;

    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

下一步,为了发送一个请求,我们需要几样东西:

  • client.open 函数
  • 一个从URL解析而来的 std.Uri

下面是我们如何将这些参数组合在一起的方法:

cpp 复制代码
    const uri = try std.Uri.parse("http://httpbin.org/headers");
    const buf = try allocator.alloc(u8, 1024 * 1024 * 4);
    defer allocator.free(buf);
    var req = try client.open(.GET, uri, .{
        .server_header_buffer = buf,
    });
    defer req.deinit();

为了真正的发送请求,需要通过send,finish,wait来完成:

cpp 复制代码
 try req.send();
 try req.finish();
 try req.wait();

打印返回的服务器headers 信息:

cpp 复制代码
var iter = req.response.iterateHeaders();
    while (iter.next()) |header| {
        std.debug.print("Name:{s}, Value:{s}\n", .{ header.name, header.value });
    }

    try std.testing.expectEqual(req.response.status, .ok);

打印返回的服务端内容:

cpp 复制代码
    var rdr = req.reader();
    const body = try rdr.readAllAlloc(allocator, 1024 * 1024 * 4);
    defer allocator.free(body);

    print("Body:\n{s}\n", .{body});

把上面的代码所有内容放在一起,并打印出响应内容:

cpp 复制代码
const std = @import("std");
const print = std.debug.print;
const http = std.http;

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    var client = http.Client{ .allocator = allocator };
    defer client.deinit();

    const uri = try std.Uri.parse("http://httpbin.org/headers");
    const buf = try allocator.alloc(u8, 1024 * 1024 * 4);
    defer allocator.free(buf);
    var req = try client.open(.GET, uri, .{
        .server_header_buffer = buf,
    });
    defer req.deinit();

    try req.send();
    try req.finish();
    try req.wait();

    var iter = req.response.iterateHeaders();
    while (iter.next()) |header| {
        std.debug.print("Name:{s}, Value:{s}\n", .{ header.name, header.value });
    }

    try std.testing.expectEqual(req.response.status, .ok);

    var rdr = req.reader();
    const body = try rdr.readAllAlloc(allocator, 1024 * 1024 * 4);
    defer allocator.free(body);

    print("Body:\n{s}\n", .{body});
}

跑一下:

bash 复制代码
 $ zig build run
Name:Date, Value:Mon, 03 Jun 2024 08:24:19 GMT
Name:Content-Type, Value:application/json
Name:Content-Length, Value:202
Name:Connection, Value:keep-alive
Name:Server, Value:gunicorn/19.9.0
Name:Access-Control-Allow-Origin, Value:*
Name:Access-Control-Allow-Credentials, Value:true
Body:
{
  "headers": {
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbin.org",
    "User-Agent": "zig/0.12.0 (std.http)",
    "X-Amzn-Trace-Id": "Root=1-665d7db3-258c846d0fcca0912fadfa8b"
  }
}

成功了!我们成功地向服务器发送了一个GET请求并打印出了响应。

GET请求的例子我们看到了,那么如何发起POST请求呢?让我们继续拿例子说话。

准备好发送内容:

cpp 复制代码
const uri = try std.Uri.parse("http://httpbin.org/anything");

    const payload =
        \\ {
        \\  "name": "zig-learning",
        \\  "author": "xiaods"
        \\ }
    ;

发送POST 请求:

cpp 复制代码
var buf: [1024]u8 = undefined;
    var req = try client.open(.POST, uri, .{ .server_header_buffer = &buf });
    defer req.deinit();

    req.transfer_encoding = .{ .content_length = payload.len };
    try req.send();
    var wtr = req.writer();
    try wtr.writeAll(payload);
    try req.finish();
    try req.wait();

    try std.testing.expectEqual(req.response.status, .ok);

打印返回内容:

cpp 复制代码
var rdr = req.reader();
    const body = try rdr.readAllAlloc(allocator, 1024 * 1024 * 4);
    defer allocator.free(body);

    print("Body:\n{s}\n", .{body});

完整的Post代码如下:

cpp 复制代码
const std = @import("std");
const print = std.debug.print;
const http = std.http;

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    var client = http.Client{ .allocator = allocator };
    defer client.deinit();

    const uri = try std.Uri.parse("http://httpbin.org/anything");

    const payload =
        \\ {
        \\  "name": "zig-learning",
        \\  "author": "xiaods"
        \\ }
    ;

    var buf: [1024]u8 = undefined;
    var req = try client.open(.POST, uri, .{ .server_header_buffer = &buf });
    defer req.deinit();

    req.transfer_encoding = .{ .content_length = payload.len };
    try req.send();
    var wtr = req.writer();
    try wtr.writeAll(payload);
    try req.finish();
    try req.wait();

    try std.testing.expectEqual(req.response.status, .ok);

    var rdr = req.reader();
    const body = try rdr.readAllAlloc(allocator, 1024 * 1024 * 4);
    defer allocator.free(body);

    print("Body:\n{s}\n", .{body});
}

运行结果:

bash 复制代码
$ zig run src/http-post.zig 
Body:
{
  "args": {}, 
  "data": " {\n  \"name\": \"zig-learning\",\n  \"author\": \"xiaods\"\n }", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "52", 
    "Host": "httpbin.org", 
    "User-Agent": "zig/0.12.0 (std.http)", 
    "X-Amzn-Trace-Id": "Root=1-665d8114-01b0167844d8d101012e6d6a"
  }, 
  "json": {
    "author": "xiaods", 
    "name": "zig-learning"
  }, 
  "method": "POST", 
  "origin": "219.133.170.77", 
  "url": "http://httpbin.org/anything"
}

请消化消化以上代码,别着急,我们后面继续前行,编写web server

相关推荐
李白同学16 分钟前
【C语言】结构体内存对齐问题
c语言·开发语言
楼台的春风1 小时前
【MCU驱动开发概述】
c语言·驱动开发·单片机·嵌入式硬件·mcu·自动驾驶·嵌入式
黑子哥呢?2 小时前
安装Bash completion解决tab不能补全问题
开发语言·bash
青龙小码农2 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿2 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
彳卸风2 小时前
Unable to parse timestamp value: “20250220135445“, expected format is
开发语言
dorabighead3 小时前
JavaScript 高级程序设计 读书笔记(第三章)
开发语言·javascript·ecmascript
风与沙的较量丶3 小时前
Java中的局部变量和成员变量在内存中的位置
java·开发语言
水煮庄周鱼鱼4 小时前
C# 入门简介
开发语言·c#
编程星空4 小时前
css主题色修改后会多出一个css吗?css怎么定义变量?
开发语言·后端·rust