mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 23:24:09 +00:00
0.15.1-fix http_params example
This commit is contained in:
parent
b5227bccbb
commit
7672a5e099
2 changed files with 22 additions and 23 deletions
|
@ -17,19 +17,18 @@ pub const std_options: std.Options = .{
|
||||||
|
|
||||||
// We send ourselves a request
|
// We send ourselves a request
|
||||||
fn makeRequest(a: std.mem.Allocator, url: []const u8) !void {
|
fn makeRequest(a: std.mem.Allocator, url: []const u8) !void {
|
||||||
const uri = try std.Uri.parse(url);
|
|
||||||
|
|
||||||
var http_client: std.http.Client = .{ .allocator = a };
|
var http_client: std.http.Client = .{ .allocator = a };
|
||||||
defer http_client.deinit();
|
defer http_client.deinit();
|
||||||
|
|
||||||
var server_header_buffer: [2048]u8 = undefined;
|
const response = try http_client.fetch(.{
|
||||||
var req = try http_client.open(.GET, uri, .{
|
.location = .{ .url = url },
|
||||||
.server_header_buffer = &server_header_buffer,
|
.method = .GET,
|
||||||
|
.payload = null,
|
||||||
|
.keep_alive = false,
|
||||||
});
|
});
|
||||||
defer req.deinit();
|
if (response.status.class() != .success) {
|
||||||
|
std.debug.print("HTTP Error: {?s}", .{response.status.phrase()});
|
||||||
try req.send();
|
}
|
||||||
try req.wait();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn makeRequestThread(a: std.mem.Allocator, url: []const u8) !std.Thread {
|
fn makeRequestThread(a: std.mem.Allocator, url: []const u8) !std.Thread {
|
||||||
|
@ -95,7 +94,7 @@ pub fn main() !void {
|
||||||
std.debug.print("\n", .{});
|
std.debug.print("\n", .{});
|
||||||
|
|
||||||
// iterate over all params
|
// iterate over all params
|
||||||
const params = try r.parametersToOwnedList(alloc);
|
var params = try r.parametersToOwnedList(alloc);
|
||||||
defer params.deinit();
|
defer params.deinit();
|
||||||
for (params.items) |kv| {
|
for (params.items) |kv| {
|
||||||
std.log.info("Param `{s}` is {any}", .{ kv.key, kv.value });
|
std.log.info("Param `{s}` is {any}", .{ kv.key, kv.value });
|
||||||
|
|
|
@ -43,10 +43,10 @@ pub const HttpParamStrKVList = struct {
|
||||||
pub const HttpParamKVList = struct {
|
pub const HttpParamKVList = struct {
|
||||||
items: []HttpParamKV,
|
items: []HttpParamKV,
|
||||||
allocator: Allocator,
|
allocator: Allocator,
|
||||||
pub fn deinit(self: *const HttpParamKVList) void {
|
pub fn deinit(self: *HttpParamKVList) void {
|
||||||
for (self.items) |item| {
|
for (self.items) |*item| {
|
||||||
self.allocator.free(item.key);
|
self.allocator.free(item.key);
|
||||||
if (item.value) |v| {
|
if (item.value) |*v| {
|
||||||
v.free(self.allocator);
|
v.free(self.allocator);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -80,11 +80,11 @@ pub const HttpParam = union(HttpParamValueType) {
|
||||||
/// value will always be null
|
/// value will always be null
|
||||||
Array_Binfile: std.ArrayList(HttpParamBinaryFile),
|
Array_Binfile: std.ArrayList(HttpParamBinaryFile),
|
||||||
|
|
||||||
pub fn free(self: HttpParam, alloc: Allocator) void {
|
pub fn free(self: *HttpParam, alloc: Allocator) void {
|
||||||
switch (self) {
|
switch (self.*) {
|
||||||
.String => |s| alloc.free(s),
|
.String => |s| alloc.free(s),
|
||||||
.Array_Binfile => |a| {
|
.Array_Binfile => |*a| {
|
||||||
a.deinit();
|
a.deinit(alloc);
|
||||||
},
|
},
|
||||||
else => {
|
else => {
|
||||||
// nothing to free
|
// nothing to free
|
||||||
|
@ -190,7 +190,7 @@ fn parseBinfilesFrom(a: Allocator, o: fio.FIOBJ) !HttpParam {
|
||||||
|
|
||||||
if (fio.fiobj_ary_count(fn_ary) == len and fio.fiobj_ary_count(mt_ary) == len) {
|
if (fio.fiobj_ary_count(fn_ary) == len and fio.fiobj_ary_count(mt_ary) == len) {
|
||||||
var i: isize = 0;
|
var i: isize = 0;
|
||||||
var ret = std.ArrayList(HttpParamBinaryFile).init(a);
|
var ret = std.ArrayList(HttpParamBinaryFile).empty;
|
||||||
while (i < len) : (i += 1) {
|
while (i < len) : (i += 1) {
|
||||||
const file_data_obj = fio.fiobj_ary_entry(data, i);
|
const file_data_obj = fio.fiobj_ary_entry(data, i);
|
||||||
const file_name_obj = fio.fiobj_ary_entry(fn_ary, i);
|
const file_name_obj = fio.fiobj_ary_entry(fn_ary, i);
|
||||||
|
@ -215,7 +215,7 @@ fn parseBinfilesFrom(a: Allocator, o: fio.FIOBJ) !HttpParam {
|
||||||
const file_data = fio.fiobj_obj2cstr(file_data_obj);
|
const file_data = fio.fiobj_obj2cstr(file_data_obj);
|
||||||
const file_name = fio.fiobj_obj2cstr(file_name_obj);
|
const file_name = fio.fiobj_obj2cstr(file_name_obj);
|
||||||
const file_mimetype = fio.fiobj_obj2cstr(file_mimetype_obj);
|
const file_mimetype = fio.fiobj_obj2cstr(file_mimetype_obj);
|
||||||
try ret.append(.{
|
try ret.append(a, .{
|
||||||
.data = file_data.data[0..file_data.len],
|
.data = file_data.data[0..file_data.len],
|
||||||
.mimetype = file_mimetype.data[0..file_mimetype.len],
|
.mimetype = file_mimetype.data[0..file_mimetype.len],
|
||||||
.filename = file_name.data[0..file_name.len],
|
.filename = file_name.data[0..file_name.len],
|
||||||
|
@ -735,7 +735,7 @@ const CallbackContext_KV = struct {
|
||||||
const ctx: *CallbackContext_KV = @as(*CallbackContext_KV, @ptrCast(@alignCast(context_)));
|
const ctx: *CallbackContext_KV = @as(*CallbackContext_KV, @ptrCast(@alignCast(context_)));
|
||||||
// this is thread-safe, guaranteed by fio
|
// this is thread-safe, guaranteed by fio
|
||||||
const fiobj_key: fio.FIOBJ = fio.fiobj_hash_key_in_loop();
|
const fiobj_key: fio.FIOBJ = fio.fiobj_hash_key_in_loop();
|
||||||
ctx.params.append(.{
|
ctx.params.append(ctx.allocator, .{
|
||||||
.key = util.fio2strAlloc(ctx.allocator, fiobj_key) catch |err| {
|
.key = util.fio2strAlloc(ctx.allocator, fiobj_key) catch |err| {
|
||||||
ctx.last_error = err;
|
ctx.last_error = err;
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -764,7 +764,7 @@ const CallbackContext_StrKV = struct {
|
||||||
const ctx: *CallbackContext_StrKV = @as(*CallbackContext_StrKV, @ptrCast(@alignCast(context_)));
|
const ctx: *CallbackContext_StrKV = @as(*CallbackContext_StrKV, @ptrCast(@alignCast(context_)));
|
||||||
// this is thread-safe, guaranteed by fio
|
// this is thread-safe, guaranteed by fio
|
||||||
const fiobj_key: fio.FIOBJ = fio.fiobj_hash_key_in_loop();
|
const fiobj_key: fio.FIOBJ = fio.fiobj_hash_key_in_loop();
|
||||||
ctx.params.append(.{
|
ctx.params.append(ctx.allocator, .{
|
||||||
.key = util.fio2strAlloc(ctx.allocator, fiobj_key) catch |err| {
|
.key = util.fio2strAlloc(ctx.allocator, fiobj_key) catch |err| {
|
||||||
ctx.last_error = err;
|
ctx.last_error = err;
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -834,7 +834,7 @@ pub fn parametersToOwnedStrList(self: *const Request, a: Allocator) anyerror!Htt
|
||||||
if (howmany != self.getParamCount()) {
|
if (howmany != self.getParamCount()) {
|
||||||
return error.HttpIterParams;
|
return error.HttpIterParams;
|
||||||
}
|
}
|
||||||
return .{ .items = try params.toOwnedSlice(), .allocator = a };
|
return .{ .items = try params.toOwnedSlice(a), .allocator = a };
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the query / body parameters as key/value pairs
|
/// Returns the query / body parameters as key/value pairs
|
||||||
|
@ -859,7 +859,7 @@ pub fn parametersToOwnedList(self: *const Request, a: Allocator) !HttpParamKVLis
|
||||||
if (howmany != self.getParamCount()) {
|
if (howmany != self.getParamCount()) {
|
||||||
return error.HttpIterParams;
|
return error.HttpIterParams;
|
||||||
}
|
}
|
||||||
return .{ .items = try params.toOwnedSlice(), .allocator = a };
|
return .{ .items = try params.toOwnedSlice(a), .allocator = a };
|
||||||
}
|
}
|
||||||
|
|
||||||
/// get named parameter (parsed) as string
|
/// get named parameter (parsed) as string
|
||||||
|
|
Loading…
Add table
Reference in a new issue