PuSH 0.4: No outgoing 'sync' verifications. Feed renewal script. No auto-renewal.

Among other things (such as permanent subscriptions), Pubsubhubbub 0.4
removed the "sync" verification method. This means that any incoming
PuSH subscription requests that follow the 0.4 spec won't really
_require_that we handle it as a background process, but if we were to
try direct verification of the subscription - and fail - there's no way
we could  pick up the ball again. So _essentially_ we require background
processing with retries.

This means we must implement something like the "poorman cron" or
similar, so background processing can be handled
on-demand/on-site-visit. This is how Friendica, Drupal etc. handles it
and is necessary for environments where we can't run separate queue
daemons.

When the poorman-cron-ish thing is implemented, auto-renewal will work
for all users.

PuSH 0.4 spec:
    https://pubsubhubbub.googlecode.com/git/pubsubhubbub-core-0.4.html
More on PuSH 0.4 release (incl. breaking changes):
    https://groups.google.com/forum/#!msg/pubsubhubbub/7RPlYMds4RI/2mIHQTdV3aoJ
This commit is contained in:
Mikael Nordfeldth 2013-11-02 20:02:28 +01:00
parent 2cecfe8f82
commit ab4113168f
5 changed files with 86 additions and 50 deletions

View File

@ -82,9 +82,8 @@ class PushCallbackAction extends Action
$mode = $this->arg('hub_mode');
$topic = $this->arg('hub_topic');
$challenge = $this->arg('hub_challenge');
$lease_seconds = $this->arg('hub_lease_seconds');
$verify_token = $this->arg('hub_verify_token');
common_log(LOG_INFO, __METHOD__ . ": sub verification mode: $mode topic: $topic challenge: $challenge lease_seconds: $lease_seconds verify_token: $verify_token");
$lease_seconds = $this->arg('hub_lease_seconds'); // Must be >0 for PuSH 0.4!
common_log(LOG_INFO, __METHOD__ . ": sub verification mode: $mode topic: $topic challenge: $challenge lease_seconds: $lease_seconds");
if ($mode != 'subscribe' && $mode != 'unsubscribe') {
// TRANS: Client exception. %s is an invalid value for hub.mode.
@ -92,16 +91,11 @@ class PushCallbackAction extends Action
}
$feedsub = FeedSub::getKV('uri', $topic);
if (!$feedsub) {
if (!$feedsub instanceof FeedSub) {
// TRANS: Client exception. %s is an invalid feed name.
throw new ClientException(sprintf(_m('Bad hub.topic feed "%s".'),$topic), 404);
}
if ($feedsub->verify_token !== $verify_token) {
// TRANS: Client exception. %1$s the invalid token, %2$s is the topic for which the invalid token was given.
throw new ClientException(sprintf(_m('Bad hub.verify_token %1$s for %2$s.'),$token,$topic), 404);
}
if ($mode == 'subscribe') {
// We may get re-sub requests legitimately.
if ($feedsub->sub_state != 'subscribe' && $feedsub->sub_state != 'active') {

View File

@ -89,20 +89,12 @@ class PushHubAction extends Action
throw new ClientException(sprintf(_m('Unsupported hub.topic %s this hub only serves local user and group Atom feeds.'),$topic));
}
$verify = $this->arg('hub.verify'); // @fixme may be multiple
if ($verify != 'sync' && $verify != 'async') {
// TRANS: Client exception. %s is sync or async.
throw new ClientException(sprintf(_m('Invalid hub.verify "%s". It must be sync or async.'),$verify));
}
$lease = $this->arg('hub.lease_seconds', null);
if ($mode == 'subscribe' && $lease != '' && !preg_match('/^\d+$/', $lease)) {
// TRANS: Client exception. %s is the invalid lease value.
throw new ClientException(sprintf(_m('Invalid hub.lease "%s". It must be empty or positive integer.'),$lease));
}
$token = $this->arg('hub.verify_token', null);
$secret = $this->arg('hub.secret', null);
if ($secret != '' && strlen($secret) >= 200) {
// TRANS: Client exception. %s is the invalid hub secret.
@ -110,7 +102,7 @@ class PushHubAction extends Action
}
$sub = HubSub::getByHashkey($topic, $callback);
if (!$sub) {
if (!$sub instanceof HubSub) {
// Creating a new one!
$sub = new HubSub();
$sub->topic = $topic;
@ -125,16 +117,14 @@ class PushHubAction extends Action
}
}
if (!common_config('queue', 'enabled')) {
// Won't be able to background it.
$verify = 'sync';
}
if ($verify == 'async') {
$sub->scheduleVerify($mode, $token);
header('HTTP/1.1 202 Accepted');
} else {
$verify = $this->arg('hub.verify'); // TODO: deprecated
$token = $this->arg('hub.verify_token', null); // TODO: deprecated
if ($verify == 'sync') { // pre-0.4 PuSH
$sub->verify($mode, $token);
header('HTTP/1.1 204 No Content');
} else { // If $verify is not "sync", we might be using PuSH 0.4
$sub->scheduleVerify($mode, $token); // If we were certain it's PuSH 0.4, token could be removed
header('HTTP/1.1 202 Accepted');
}
}

View File

@ -30,8 +30,6 @@ if (!defined('STATUSNET')) {
PuSH subscription flow:
$profile->subscribe()
generate random verification token
save to verify_token
sends a sub request to the hub...
main/push/callback
@ -69,7 +67,6 @@ class FeedSub extends Managed_DataObject
// PuSH subscription data
public $huburi;
public $secret;
public $verify_token;
public $sub_state; // subscribe, active, unsubscribe, inactive
public $sub_start;
public $sub_end;
@ -85,7 +82,6 @@ class FeedSub extends Managed_DataObject
'id' => array('type' => 'serial', 'not null' => true, 'description' => 'FeedSub local unique id'),
'uri' => array('type' => 'varchar', 'not null' => true, 'length' => 255, 'description' => 'FeedSub uri'),
'huburi' => array('type' => 'text', 'description' => 'FeedSub hub-uri'),
'verify_token' => array('type' => 'text', 'description' => 'FeedSub verify-token'),
'secret' => array('type' => 'text', 'description' => 'FeedSub stored secret'),
'sub_state' => array('type' => 'enum("subscribe","active","unsubscribe","inactive")', 'not null' => true, 'description' => 'subscription state'),
'sub_start' => array('type' => 'datetime', 'description' => 'subscription start'),
@ -168,10 +164,10 @@ class FeedSub extends Managed_DataObject
* @return bool true on success, false on failure
* @throws ServerException if feed state is not valid
*/
public function subscribe($mode='subscribe')
public function subscribe()
{
if ($this->sub_state && $this->sub_state != 'inactive') {
common_log(LOG_WARNING, "Attempting to (re)start PuSH subscription to $this->uri in unexpected state $this->sub_state");
common_log(LOG_WARNING, "Attempting to (re)start PuSH subscription to {$this->uri} in unexpected state {$this->sub_state}");
}
if (empty($this->huburi)) {
if (common_config('feedsub', 'fallback_hub')) {
@ -202,7 +198,7 @@ class FeedSub extends Managed_DataObject
*/
public function unsubscribe() {
if ($this->sub_state != 'active') {
common_log(LOG_WARNING, "Attempting to (re)end PuSH subscription to $this->uri in unexpected state $this->sub_state");
common_log(LOG_WARNING, "Attempting to (re)end PuSH subscription to {$this->uri} in unexpected state {$this->sub_state}");
}
if (empty($this->huburi)) {
if (common_config('feedsub', 'fallback_hub')) {
@ -248,10 +244,29 @@ class FeedSub extends Managed_DataObject
}
}
static public function renewalCheck()
{
$fs = new FeedSub();
// the "" empty string check is because we historically haven't saved unsubscribed feeds as NULL
$fs->whereAdd('sub_end IS NOT NULL AND sub_end!="" AND sub_end < NOW() - INTERVAL 1 day');
if ($fs->find() === false) {
throw new NoResultException($fs);
}
return $fs;
}
public function renew()
{
$this->subscribe();
}
/**
* @return boolean true on successful sub/unsub, false on failure
*/
protected function doSubscribe($mode)
{
$this->query('BEGIN');
$orig = clone($this);
$this->verify_token = common_random_hexstr(16);
if ($mode == 'subscribe') {
$this->secret = common_random_hexstr(32);
}
@ -264,8 +279,7 @@ class FeedSub extends Managed_DataObject
$headers = array('Content-Type: application/x-www-form-urlencoded');
$post = array('hub.mode' => $mode,
'hub.callback' => $callback,
'hub.verify' => 'sync',
'hub.verify_token' => $this->verify_token,
'hub.verify' => 'async', // TODO: deprecated, remove when noone uses PuSH <0.4
'hub.secret' => $this->secret,
'hub.topic' => $this->uri);
$client = new HTTPClient();
@ -286,30 +300,26 @@ class FeedSub extends Managed_DataObject
$response = $client->post($hub, $headers, $post);
$status = $response->getStatus();
if ($status == 202) {
$this->query('COMMIT');
common_log(LOG_INFO, __METHOD__ . ': sub req ok, awaiting verification callback');
return true;
} else if ($status == 204) {
common_log(LOG_INFO, __METHOD__ . ': sub req ok and verified');
return true;
} else if ($status >= 200 && $status < 300) {
common_log(LOG_ERR, __METHOD__ . ": sub req returned unexpected HTTP $status: " . $response->getBody());
return false;
} else {
common_log(LOG_ERR, __METHOD__ . ": sub req failed with HTTP $status: " . $response->getBody());
return false;
}
$this->query('ROLLBACK');
} catch (Exception $e) {
$this->query('ROLLBACK');
// wtf!
common_log(LOG_ERR, __METHOD__ . ": error \"{$e->getMessage()}\" hitting hub $this->huburi subscribing to $this->uri");
$orig = clone($this);
$this->verify_token = '';
$this->sub_state = 'inactive';
$this->update($orig);
unset($orig);
return false;
}
return false;
}
/**
@ -318,7 +328,7 @@ class FeedSub extends Managed_DataObject
*
* @param int $lease_seconds provided hub.lease_seconds parameter, if given
*/
public function confirmSubscribe($lease_seconds=0)
public function confirmSubscribe($lease_seconds)
{
$original = clone($this);
@ -327,7 +337,7 @@ class FeedSub extends Managed_DataObject
if ($lease_seconds > 0) {
$this->sub_end = common_sql_date(time() + $lease_seconds);
} else {
$this->sub_end = null;
$this->sub_end = null; // Backwards compatibility to StatusNet (PuSH <0.4 supported permanent subs)
}
$this->modified = common_sql_now();
@ -343,7 +353,6 @@ class FeedSub extends Managed_DataObject
$original = clone($this);
// @fixme these should all be null, but DB_DataObject doesn't save null values...?????
$this->verify_token = '';
$this->secret = '';
$this->sub_state = '';
$this->sub_start = '';

View File

@ -114,7 +114,7 @@ class HubSub extends Managed_DataObject
}
$data = array('sub' => clone($this),
'mode' => $mode,
'token' => $token,
'token' => $token, // let's put it in there if remote uses PuSH <0.4
'retries' => $retries);
$qm = QueueManager::get();
$qm->enqueue($data, 'hubconf');
@ -139,8 +139,8 @@ class HubSub extends Managed_DataObject
if ($mode == 'subscribe') {
$params['hub.lease_seconds'] = $this->lease;
}
if ($token !== null) {
$params['hub.verify_token'] = $token;
if ($token !== null) { // TODO: deprecated in PuSH 0.4
$params['hub.verify_token'] = $token; // let's put it in there if remote uses PuSH <0.4
}
// Any existing query string parameters must be preserved

View File

@ -0,0 +1,43 @@
#!/usr/bin/env php
<?php
/*
* StatusNet - a distributed open-source microblogging tool
* Copyright (C) 2010, StatusNet, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
$helptext = <<<END_OF_HELP
resub-feed.php
Resubscribe to any soon expiring feeds. Default time to expiration is 1 day.
END_OF_HELP;
require_once INSTALLDIR.'/scripts/commandline.inc';
try {
$sub = FeedSub::renewalCheck();
} catch (NoResultException $e) {
echo 'There were no expiring feeds.';
exit;
}
while ($sub->fetch()) {
echo "Renewing feed subscription\n\tExp.: {$sub->sub_end}\n\tFeed: {$sub->uri}\n\tHub: {$sub->huburi}\n";
$sub->renew();
}
echo "Done!";