From 2d9d010b76891326cea83e663029310bb4684cd9 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Thu, 6 Jan 2022 15:22:50 -0800 Subject: [PATCH] post->put and putCurl Crowdstrike strikes again. Not sure what is even the issue this time... --- src/clipboard.zig | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/clipboard.zig b/src/clipboard.zig index bcbeb07..92689eb 100644 --- a/src/clipboard.zig +++ b/src/clipboard.zig @@ -68,7 +68,7 @@ pub fn clipboardChanged(self: *Self, contents: []const u8) !void { return; }; defer aa.free(encrypted); - post(aa, encrypted) catch |e| { + put(aa, encrypted) catch |e| { std.log.err("error posting clipboard contents {}", .{e}); if (@errorReturnTrace()) |trace| { std.debug.dumpStackTrace(trace.*); @@ -172,6 +172,32 @@ fn getCurl(allocator: std.mem.Allocator, curl_path: []const u8) ![]const u8 { } } +fn putCurl(allocator: std.mem.Allocator, curl_path: []const u8, data: []const u8) !void { + std.log.debug("curl path: {s}", .{curl_path}); + const result = try std.ChildProcess.exec(.{ + .allocator = allocator, + .argv = &[_][]const u8{ + curl_path, // TODO: use Comspec + "-s", + "-X", + "PUT", + "--data-binary", + data, + clipboard_url, + }, + }); + + try std.io.getStdErr().writer().writeAll(result.stderr); + switch (result.term) { + .Exited => |code| if (code == 0) { + return; + } else return error.NonZeroExit, + .Signal => return error.FailedWithSignal, + .Stopped => return error.WasStopped, + .Unknown => return error.Failed, + } +} + fn execLinux(allocator: std.mem.Allocator, cmd: []const u8) !std.ChildProcess.ExecResult { return std.ChildProcess.exec(.{ .allocator = allocator, @@ -196,7 +222,10 @@ fn execLinux(allocator: std.mem.Allocator, cmd: []const u8) !std.ChildProcess.Ex // }); // } -fn post(allocator: std.mem.Allocator, data: []const u8) !void { +fn put(allocator: std.mem.Allocator, data: []const u8) !void { + if (config.curl) |curl| + return putCurl(allocator, curl, data); + // TODO: Windows // var cert_reader = std.io.fixedBufferStream( // @embedFile("/etc/ssl/certs/ca-certificates.crt"),