@@ -118,42 +118,50 @@ def findEndpoint(html):
118118 return None
119119
120120
121- def discoverEndpoint (url , test_urls = True ):
121+ def discoverEndpoint (url , test_urls = True , debug = False ):
122122 """Discover any WebMention endpoint for a given URL.
123123
124124 :param link: URL to discover WebMention endpoint
125125 :param test_urls: optional flag to test URLs for validation
126- :rtype: tuple (status_code, URL)
126+ :param debug: if true, then include in the returned tuple
127+ a list of debug entries
128+ :rtype: tuple (status_code, URL, [debug])
127129 """
128130 if test_urls :
129131 URLValidator (message = 'invalid URL' )(url )
130132
131133 # status, webmention
132134 href = None
135+ d = []
133136 try :
134137 r = requests .get (url , verify = False )
135138 rc = r .status_code
139+ d .append ('is url [%s] retrievable? [%s]' % (url , rc ))
136140 if rc == requests .codes .ok :
137141 try :
138142 link = parse_link_header (r .headers ['link' ])
139143 href = link .get ('webmention' , '' ) or link .get ('http://webmention.org' , '' ) or link .get ('http://webmention.org/' , '' ) or link .get ('https://webmention.org' , '' ) or link .get ('https://webmention.org/' , '' )
140144
141145 # force searching in the HTML if not found
142146 if not href :
147+ d .append ('link header not found, forcing html scan' )
143148 raise AttributeError
144149 except (KeyError , AttributeError ):
145150 href = findEndpoint (r .text )
146151
147152 if href is not None :
148153 href = urljoin (url , href )
154+ d .append ('discovered href [%s]' % href )
149155 except (requests .exceptions .RequestException , requests .exceptions .ConnectionError ,
150156 requests .exceptions .HTTPError , requests .exceptions .URLRequired ,
151157 requests .exceptions .TooManyRedirects , requests .exceptions .Timeout ):
152158 rc = 500
153- return (rc , href )
154-
159+ if debug :
160+ return (rc , href , d )
161+ else :
162+ return (rc , href )
155163
156- def sendWebmention (sourceURL , targetURL , webmention = None , test_urls = True , vouchDomain = None ):
164+ def sendWebmention (sourceURL , targetURL , webmention = None , test_urls = True , vouchDomain = None , debug = False ):
157165 """Send to the :targetURL: a WebMention for the :sourceURL:
158166
159167 The WebMention will be discovered if not given in the :webmention:
@@ -163,6 +171,8 @@ def sendWebmention(sourceURL, targetURL, webmention=None, test_urls=True, vouchD
163171 :param targetURL: URL of mentioned post
164172 :param webmention: optional WebMention endpoint
165173 :param test_urls: optional flag to test URLs for validation
174+ :param debug: if true, then include in the returned tuple
175+ a list of debug entries
166176 :rtype: HTTPrequest object if WebMention endpoint was valid
167177 """
168178 if test_urls :
@@ -171,8 +181,9 @@ def sendWebmention(sourceURL, targetURL, webmention=None, test_urls=True, vouchD
171181 v (targetURL )
172182
173183 result = None
184+ d = []
174185 if webmention is None :
175- wStatus , wUrl = discoverEndpoint (targetURL )
186+ wStatus , wUrl = discoverEndpoint (targetURL , debug = False )
176187 else :
177188 wStatus = 200
178189 wUrl = webmention
@@ -186,14 +197,24 @@ def sendWebmention(sourceURL, targetURL, webmention=None, test_urls=True, vouchD
186197 if vouchDomain is not None :
187198 payload ['vouch' ] = vouchDomain
188199
189- print 'sending to' , wUrl , payload
200+ d . append ( 'sending to [%s] %s' % ( wUrl , payload ))
190201 try :
191202 result = requests .post (wUrl , data = payload )
203+ d .append ('POST returned %d' % result .status_code )
192204
193205 if result .status_code == 405 and len (result .history ) > 0 :
206+ d .append ('status code was 405, looking for redirect location' )
194207 o = result .history [- 1 ]
195208 if o .status_code == 301 and 'Location' in o .headers :
209+ d .append ('redirected to [%s]' % o .headers ['Location' ])
196210 result = requests .post (o .headers ['Location' ], data = payload )
211+ elif result .status_code not in (200 , 201 , 202 ):
212+ d .append ('status code was not 200, 201, 202' )
213+
197214 except :
215+ d .append ('exception during request post' )
198216 result = None
199- return result
217+ if debug :
218+ return result , d
219+ else :
220+ return result
0 commit comments