22# -*- coding: utf-8 -*-
33
44import urlparse
5+ import re
56import requests
67from bs4 import BeautifulSoup
78
89class 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