From ae66c892be3543c98c532d168d96787e54958ad7 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Mon, 15 Feb 2021 10:43:56 -0700 Subject: [PATCH] Girocco/Notify.pm: add json_test_post function Rather than blindly accept just any old JSON POST url, the json_test_post function can be called at the time the url is first configured to verify that it can be POST'd to successfully. This is not unlike the behavior of that other well-known service that we are emulating except that the intent is to actually decline to configure the URL if anything other than a "successful" (aka 2xx) status code comes back. Signed-off-by: Kyle J. McKay --- Girocco/Notify.pm | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Girocco/Notify.pm b/Girocco/Notify.pm index 3c46fe9..43f937f 100644 --- a/Girocco/Notify.pm +++ b/Girocco/Notify.pm @@ -197,6 +197,8 @@ sub json { # print "$payload\n"; my $ua = LWP::UserAgent->new(agent => _jsonagent($proj)); + $ua->max_redirect(0); # no redirects allowed + $ua->max_size(32768); # there really shouldn't be hardly any response content at all $ua->timeout(5); my $ct = $proj->{jsontype}; defined($ct) && $ct eq 'application/json' or @@ -207,6 +209,42 @@ sub json { } +sub json_test_post { + my ($proj, $url) = @_; + defined($url) && $url ne "" or $url = $proj->{notifyjson}; + defined($url) && $url ne "" or return undef; + my $ct = $proj->{jsontype}; + defined($ct) && $ct eq 'application/json' or + $ct = 'application/x-www-form-urlencoded'; + my $hook = { + "active" => json_bool(1), + "config" => { + "content_type" => (($ct =~ /json/) ? "json" : "form"), + "url" => $url + }, + "events" => [ "push" ] + }; + my $payload = to_json { + "zen" => 'Why is a raven like a writing desk?', + "repository" => _jsonrepo($proj), + "hook" => $hook, + "sender" => { + "url" => _projurl($Girocco::Config::gitweburl,$proj->{name}), + "html_url" => _projurl($Girocco::Config::gitweburl,$proj->{name}), + "email" => $proj->{email} + } + }; + $payload = _jsonpayload($proj, $payload); + my $ua = LWP::UserAgent->new(agent => _jsonagent($proj)); + $ua->max_redirect(0); # no redirects allowed + $ua->max_size(32768); # there really shouldn't be hardly any response content at all + $ua->timeout(15); # Allow extra time on initial check + my @headers = ( Content_Type => $ct, Content_Length => length($payload) ); + my $r = $ua->post($url, @headers, _jsonsigheaders($proj, $payload), Content => $payload); + return $r->protocol !~ m{HTTP/0\.9}i && $r->is_success; # HTTP/0.9 is NOT okay for this test +} + + sub cia_commit { my ($cianame, $proj, $branch, $commit, $root) = @_; -- 2.11.4.GIT