+#ifdef __APPLE__
+#define LIB_EXT "dylib"
+#else
+#define LIB_EXT "so"
+#endif
+
+static struct ustream_ssl_ctx *ssl_ctx;
+static const struct ustream_ssl_ops *ssl_ops;
+static int quiet = false;
+static bool verify = true;
+static const char *output_file;
+static int output_fd = -1;
+static int error_ret;
+
+static int open_output_file(const char *path, bool create)
+{
+ const char *str;
+ char *filename;
+ int len;
+ int flags = O_WRONLY;
+
+ if (create)
+ flags |= O_CREAT;
+
+ if (output_file) {
+ if (!strcmp(output_file, "-"))
+ return STDOUT_FILENO;
+
+ return open(output_file, flags, 0644);
+ }
+
+ /* Don't automatically overwrite files if the name is derived from the URL */
+ if (create)
+ flags |= O_EXCL;
+
+ len = strcspn(path, ";&");
+ while (len > 0 && path[len - 1] == '/')
+ len--;
+
+ for (str = path + len - 1; str >= path; str--) {
+ if (*str == '/')
+ break;
+ }
+
+ str++;
+ len -= str - path;
+
+ if (len > 0) {
+ filename = alloca(len + 1);
+ strncpy(filename, str, len);
+ filename[len] = 0;
+ } else {
+ filename = "index.html";
+ }
+
+ return open(filename, flags, 0644);
+}
+
+static void request_done(struct uclient *cl)
+{
+ if (output_fd >= 0) {
+ close(output_fd);
+ output_fd = -1;
+ }
+ uclient_disconnect(cl);
+ uloop_end();
+}
+