Skip to content

Commit

Permalink
disable parse response header
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 committed Sep 16, 2023
1 parent a91a066 commit d43a7e1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Install deps
run: |
sudo apt update && sudo apt install -y valgrind libcurl4-openssl-dev
- name: Run example
- name: Run examples
run: zig build run-basic
- name: Memory leak detect
run: |
Expand All @@ -50,6 +50,6 @@ jobs:
- uses: goto-bus-stop/setup-zig@v1
with:
version: master
- name: Build demo
- name: Run examples
run: |
zig build run-basic
13 changes: 7 additions & 6 deletions examples/basic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ fn get(easy: Easy) !void {
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", .{});
}

// 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 {
Expand Down
64 changes: 33 additions & 31 deletions src/easy.zig
Original file line number Diff line number Diff line change
Expand Up @@ -127,36 +127,38 @@ pub const Response = struct {
self.body.deinit();
}

pub const Header = struct {
c_header: *c.struct_curl_header,
name: []const u8,

/// Get gets the first value associated with the given key.
/// Applications need to copy the data if it wants to keep it around.
pub fn get(self: @This()) []const u8 {
return mem.sliceTo(self.c_header.value, 0);
}
};

pub fn get_header(self: @This(), name: []const u8) !?Header {
const c_name = try fmt.allocPrintZ(self.allocator, "{s}", .{name});
defer self.allocator.free(c_name);

var header: ?*c.struct_curl_header = null;
// https://curl.se/libcurl/c/curl_easy_header.html
const code = c.curl_easy_header(self.handle, name.ptr, 0, c.CURLH_HEADER, -1, &header);

// https://curl.se/libcurl/c/libcurl-errors.html
return switch (code) {
c.CURLHE_OK => .{
.c_header = header.?,
.name = name,
},
c.CURLHE_MISSING => null,
c.CURLHE_BADINDEX => error.BadIndex,
else => error.HeaderOther,
};
}
// Parse response header using `curl_easy_header` require libcurl >= 7.84.0.
// Find other solution to parse.
// pub const Header = struct {
// c_header: *c.struct_curl_header,
// name: []const u8,

// /// Get gets the first value associated with the given key.
// /// Applications need to copy the data if it wants to keep it around.
// pub fn get(self: @This()) []const u8 {
// return mem.sliceTo(self.c_header.value, 0);
// }
// };

// pub fn get_header(self: @This(), name: []const u8) !?Header {
// const c_name = try fmt.allocPrintZ(self.allocator, "{s}", .{name});
// defer self.allocator.free(c_name);

// var header: ?*c.struct_curl_header = null;
// // https://curl.se/libcurl/c/curl_easy_header.html
// const code = c.curl_easy_header(self.handle, name.ptr, 0, c.CURLH_HEADER, -1, &header);

// // https://curl.se/libcurl/c/libcurl-errors.html
// return switch (code) {
// c.CURLHE_OK => .{
// .c_header = header.?,
// .name = name,
// },
// c.CURLHE_MISSING => null,
// c.CURLHE_BADINDEX => error.BadIndex,
// else => error.HeaderOther,
// };
// }
};

pub fn init(allocator: mem.Allocator) !Self {
Expand All @@ -165,7 +167,7 @@ pub fn init(allocator: mem.Allocator) !Self {
return error.Init;
}

return Self{
return .{
.allocator = allocator,
.handle = handle.?,
};
Expand Down

0 comments on commit d43a7e1

Please sign in to comment.