-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMojo.pm
227 lines (171 loc) · 5.03 KB
/
Mojo.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package Web::Mention::Mojo;
use strict;
use warnings FATAL => 'all';
use Moose;
use MooseX::ClassAttribute;
use Mojo::UserAgent;
use Mojo::URL;
use Mojo::Log;
use Tuvix::TypeConstraints;
use Web::Microformats2::Parser;
use Try::Tiny;
use Mojo::Util qw\url_escape\;
use HTTP::Link;
extends 'Web::Mention';
# ¯\_(ツ)_/¯
*Mojo::URL::eq = sub {
return shift->to_abs->to_string eq
shift->to_abs->to_string;
};
*Mojo::URL::as_string = sub {
return shift->to_abs->to_string;
};
has 'error' => (
isa => 'Maybe[HashRef]',
is => 'rw'
);
has 'log' => (
isa => 'Mojo::Log',
is => 'rw',
default => sub {Mojo::Log->new()}
);
class_has '+ua' => (
isa => 'Mojo::UserAgent',
is => 'rw',
default => sub {Mojo::UserAgent->new}
);
has '+target' => (
isa => 'URL',
is => 'ro',
required => 1,
coerce => 1,
);
has '+source' => (
isa => 'URL',
is => 'ro',
required => 1,
coerce => 1,
);
has '+endpoint' => (
isa => 'Maybe[URL]',
is => 'ro',
required => 1,
coerce => 1,
);
has '+original_source' => (
isa => 'URL',
is => 'ro',
lazy_build => 1,
coerce => 1,
);
sub verify {
my $self = shift;
$self->is_tested(1);
my $response = $self->ua->get($self->source);
# Search for both plain and escaped ("percent-encoded") versions of the
# target URL in the source doc. We search for the latter to account for
# sites like Tumblr, who treat outgoing hyperlinks as weird internally-
# pointing links that pass external URLs as query-string parameters.
if (($response->result->body =~ $self->target)
|| ($response->result->body =~ url_escape($self->target))
) {
$self->time_verified(DateTime->now);
$self->source_html($response->result->body);
$self->_clear_mf2;
$self->_clear_content;
$self->_clear_author;
$self->log->debug(sprintf "Verified webmention. Source:[%s] Target:[%s] Endpoint:[%s]",
$self->source, $self->target, $self->endpoint);
return 1;
}
else {
return 0;
}
}
sub _build_source_mf2_document {
my $self = shift;
return unless $self->is_verified;
my $doc;
try {
my $parser = Web::Microformats2::Parser->new;
$doc = $parser->parse(
$self->source_html,
url_context => $self->source->to_string);
}
catch {
die "Error parsing source HTML: $_";
};
return $doc;
}
sub _build_endpoint {
my $self = shift;
my $endpoint;
my $source = $self->source;
my $target = $self->target;
# Is it in the Link HTTP header?
my $response = $self->ua->get($target);
my $headers = $response->res->can('headers')
? $response->res->headers
: undef;
if ($headers && $headers->can('link') && $headers->link) {
my @header_links = HTTP::Link->parse($headers->link);
foreach (@header_links) {
if ($_->{relation} eq 'webmention') {
$endpoint = $_->{iri};
}
}
}
# Is it in the HTML?
unless ($endpoint) {
$self->log->debug("No webmention link found in the Target headers of: " . $target);
if ($headers && $headers->content_type && $headers->content_type =~ m{^text/html\b}) {
my $dom = Mojo::DOM58->new($response->res->content->get_body_chunk);
my $nodes_ref = $dom->find(
'link[rel~="webmention"], a[rel~="webmention"]'
);
for my $node (@$nodes_ref) {
$endpoint = $node->attr('href');
last if defined $endpoint;
}
}
}
# TODO make sure that the URL is an absolute one
return defined $endpoint
? Mojo::URL->new($endpoint)->to_abs
: undef
}
sub send {
my $self = shift;
my $endpoint = $self->endpoint;
my $source = $self->source;
my $target = $self->target;
unless ($endpoint) {
return 0;
}
# Step three: send the webmention to the target!
my $response = $self->ua->post(
$self->endpoint => { 'Content-Type' => 'application/x-www-form-urlencoded' } => "source=$source&target=$target"
);
$self->error($response->res->error);
return !$response->res->error;
}
=pod
=encoding utf-8
=head1 NAME
Web::Mention::Mojo
=head1 DESCRIPTION
This is a subclass of Web::Mention by Jason McIntosh (http://jmac.org).
That's to make it work with L<Mojo::UserAgent> in stead of L<LWP::UserAgent>.
There've been a whole bottle of glue poured over it.
And some duct tape.
The functions which did not like the Mojo::UserAgent out of the box, like
_build_endpoint and _build_source_mf2_document and verify have been lifted over
from Web::Mention (c) Jason McIntosh and then modified to work, and then put in
this new box.
It's all to make tests run swiftly with L<Mojo::Test>. Not only of the sending end,
but also the recieving one. - all can be tested in one fell swoop.
So great.
¯\_(⊙_ʖ⊙)_/¯
It will do for now.
=cut
1;