Skip to content

Commit 5ef072a

Browse files
committed
discover webmention endpoints in the Link HTTP response header
also don't verify SSL certs. python-requests's verification is overly aggressive. fixes #7. thanks again for the find, @jeena!
1 parent db5d47f commit 5ef072a

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

webmentiontools/send.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
# -*- coding: utf-8 -*-
33

44
import urlparse
5+
import re
56
import requests
67
from bs4 import BeautifulSoup
78

89
class WebmentionSend():
910

11+
LINK_HEADER_RE = re.compile(
12+
r'''<([^>]+)>; rel=["'](http://)?webmention(\.org/?)?["']''')
13+
1014
def __init__(self, source, target):
1115
self.source_url = source
1216
self.target_url = target
@@ -21,7 +25,7 @@ def send(self, **kwargs):
2125
return self._notifyReceiver()
2226

2327
def _discoverEndpoint(self):
24-
r = requests.get(self.target_url, **self.requests_kwargs)
28+
r = requests.get(self.target_url, verify=False, **self.requests_kwargs)
2529
if r.status_code != 200:
2630
self.error = {
2731
'code':'BAD_TARGET_URL',
@@ -30,6 +34,18 @@ def _discoverEndpoint(self):
3034
'http_status': r.status_code,
3135
}
3236
return False
37+
38+
# look in the headers
39+
# XXX: it looks like requests doesn't handle multiple headers with the
40+
# same name, e.g. 'Link'. from skimming the code, it looks like the last
41+
# one wins. ugh. :/
42+
for link in r.headers.get('link', '').split(','):
43+
match = self.LINK_HEADER_RE.search(link)
44+
if match:
45+
self.receiver_endpoint = match.group(1)
46+
return True
47+
48+
# look in the content
3349
html = r.text
3450
soup = BeautifulSoup(html)
3551
tag = soup.find('link', attrs={'rel': 'webmention'})
@@ -51,7 +67,7 @@ def _discoverEndpoint(self):
5167
def _notifyReceiver(self):
5268
payload = {'source': self.source_url, 'target': self.target_url}
5369
headers = {'Accept': '*/*'}
54-
r = requests.post(self.receiver_endpoint, data=payload, **self.requests_kwargs)
70+
r = requests.post(self.receiver_endpoint, verify=False, data=payload, **self.requests_kwargs)
5571

5672
request_str = 'POST %s (with source=%s, target=%s)' % (self.receiver_endpoint, self.source_url, self.target_url)
5773
if r.status_code / 100 != 2:

0 commit comments

Comments
 (0)