support -u/--url command line args to determine target
All checks were successful
AWS-Zig Build / build-zig-0.11.0-amd64-host (push) Successful in 2m0s

This commit is contained in:
Emil Lerch 2023-10-23 13:45:17 -07:00
parent 29687e1440
commit 05dd8b7e06
Signed by: lobo
GPG Key ID: A7B62D657EF764F8
2 changed files with 19 additions and 3 deletions

View File

@ -9,6 +9,7 @@ const Option = struct {
}; };
const target_option: Option = .{ .short = "t", .long = "target" }; const target_option: Option = .{ .short = "t", .long = "target" };
const url_option: Option = .{ .short = "u", .long = "url" };
const header_option: Option = .{ .short = "h", .long = "header" }; const header_option: Option = .{ .short = "h", .long = "header" };
/// Finds the "target" for this request. In a web request, this is the path /// Finds the "target" for this request. In a web request, this is the path
@ -32,15 +33,18 @@ fn findTargetWithoutContext(allocator: std.mem.Allocator) ![]const u8 {
var argIterator = try std.process.argsWithAllocator(allocator); var argIterator = try std.process.argsWithAllocator(allocator);
_ = argIterator.next(); _ = argIterator.next();
var is_target_option = false; var is_target_option = false;
var is_url_option = false;
while (argIterator.next()) |arg| { while (argIterator.next()) |arg| {
if (is_target_option) { if (is_target_option or is_url_option) {
if (std.mem.startsWith(u8, arg, "-") or if (std.mem.startsWith(u8, arg, "-") or
std.mem.startsWith(u8, arg, "--")) std.mem.startsWith(u8, arg, "--"))
{ {
// bad user input, but we're not returning errors here // bad user input, but we're not returning errors here
return "/"; return "/";
} }
return arg; if (is_target_option)
return arg;
return (try std.Uri.parse(arg)).path;
} }
if (std.mem.startsWith(u8, arg, "-" ++ target_option.short) or if (std.mem.startsWith(u8, arg, "-" ++ target_option.short) or
std.mem.startsWith(u8, arg, "--" ++ target_option.long)) std.mem.startsWith(u8, arg, "--" ++ target_option.long))
@ -48,9 +52,20 @@ fn findTargetWithoutContext(allocator: std.mem.Allocator) ![]const u8 {
// We'll search for --target=blah style first // We'll search for --target=blah style first
var split = std.mem.splitSequence(u8, arg, "="); var split = std.mem.splitSequence(u8, arg, "=");
_ = split.next(); _ = split.next();
if (split.next()) |s| return s; // found it const rest = split.rest();
if (split.next()) |_| return rest; // found it
is_target_option = true; is_target_option = true;
} }
if (std.mem.startsWith(u8, arg, "-" ++ url_option.short) or
std.mem.startsWith(u8, arg, "--" ++ url_option.long))
{
// We'll search for --target=blah style first
var split = std.mem.splitSequence(u8, arg, "=");
_ = split.next();
const rest = split.rest();
if (split.next()) |_| return (try std.Uri.parse(rest)).path; // found it
is_url_option = true;
}
} }
return "/"; return "/";
} }

View File

@ -12,6 +12,7 @@ pub const Response = struct {
status: std.http.Status = .ok, status: std.http.Status = .ok,
reason: ?[]const u8 = null, reason: ?[]const u8 = null,
request: struct { request: struct {
// TODO: We will likely end up needing method here at some point...
target: []const u8, target: []const u8,
headers: std.http.Headers, headers: std.http.Headers,
}, },