commit: 89bf8376b90cc33d52264f92b8a456792b91654d
parent c684d77ff8b9032df8dbd0886853b449e7360dcf
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Thu, 18 Feb 2021 21:17:40 +0100
ap-fetch.sh: Replace with ap-fetch.pl
Diffstat:
2 files changed, 51 insertions(+), 22 deletions(-)
diff --git a/ap-fetch.pl b/ap-fetch.pl
@@ -0,0 +1,51 @@
+#!/usr/bin/env perl
+# AP-Client: CLI-based client / toolbox for ActivityPub
+# Copyright © 2020-2021 AP-Client Authors <https://hacktivis.me/git/ap-client/>
+# SPDX-License-Identifier: BSD-3-Clause
+use strict;
+use utf8;
+no warnings; # Wide Character…
+
+use Getopt::Std;
+
+use LWP::UserAgent;
+
+my %options=();
+my $ua = LWP::UserAgent->new;
+
+
+getopts("rj", \%options);
+
+if($#ARGV != 0) {
+ print "usage: ap-fetch.pl [-r|-j] <url>\n";
+ print " -r Raw output\n";
+ print " -j Pipe into jq(1)\n";
+ print "Otherwise it pipes into ap-represent.pl.\n";
+ exit 1;
+}
+$ua->agent("AP-Client fetch <https://hacktivis.me/git/ap-client/>");
+my $req = HTTP::Request->new(GET => $ARGV[0]);
+$req->header('Accept' => 'application/activity+json');
+
+my $res = $ua->request($req);
+
+if($res->is_success) {
+ my $content_type = $res->header("Content-Type");
+ my $content_match = /application\/([^+]*+)?json(; .*)?/;
+
+ if($content_type =~ $content_match) {
+ if(defined $options{r}) {
+ print $res->content;
+ } elsif(defined $options{j}) {
+ open(my $pipe_out, '|-', 'jq .') or die "Couldn't open a pipe into jq: $!";
+ print $pipe_out $res->content;
+ } else {
+ open(my $pipe_out, '|-', 'ap-represent.pl') or die "Couldn't open a pipe into ap-represent.pl: $!";
+ print $pipe_out $res->content;
+ }
+ } else {
+ print "Got $content_type instead of $content_match";
+ }
+} else {
+ print "Got ", $res->status_line, " instead of 2xx\n";
+}
diff --git a/ap-fetch.sh b/ap-fetch.sh
@@ -1,22 +0,0 @@
-#!/bin/sh
-# AP-Client: CLI-based client / toolbox for ActivityPub
-# Copyright © 2020 AP-Client Authors <https://hacktivis.me/git/ap-client/>
-# SPDX-License-Identifier: BSD-3-Clause
-accept_header='Accept: application/ld+json,application/activity+json,application/json'
-
-die() {
- echo $@ >&2
- exit 1
-}
-
-if [ -z "$@" ]
-then
- die "Usage: ${0} [curl options] <url>"
-fi
-
-if curl -sSL -H "${accept_header}" -I $@ | tee /dev/stderr | grep -q -i -e '^Content-Type: application/.*json'
-then
- curl -sSL -H "${accept_header}" $@
-else
- die "Doesn’t have “Content-Type: application/.*json” in the headers"
-fi