/** * Set up an HTTPClient with auth for our resource. * * @param string $method * @return HTTPClient */ private function httpClient($method = 'GET') { $client = new HTTPClient($this->url); $client->setMethod($method); $client->setAuth($this->user, $this->pass); return $client; }
protected function doSubscribe($mode) { $orig = clone $this; $this->verify_token = common_good_rand(16); if ($mode == 'subscribe') { $this->secret = common_good_rand(32); } $this->sub_state = $mode; $this->update($orig); unset($orig); try { $callback = common_local_url('pushcallback', array('feed' => $this->id)); $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.secret' => $this->secret, 'hub.topic' => $this->uri); $client = new HTTPClient(); if ($this->huburi) { $hub = $this->huburi; } else { if (common_config('feedsub', 'fallback_hub')) { $hub = common_config('feedsub', 'fallback_hub'); if (common_config('feedsub', 'hub_user')) { $u = common_config('feedsub', 'hub_user'); $p = common_config('feedsub', 'hub_pass'); $client->setAuth($u, $p); } } else { throw new FeedSubException('WTF?'); } } $response = $client->post($hub, $headers, $post); $status = $response->getStatus(); if ($status == 202) { 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; } } } } catch (Exception $e) { // 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; } }
/** * Setting to subscribe means it is _waiting_ to become active. This * cannot be done in a transaction because there is a chance that the * remote script we're calling (as in the case of PuSHpress) performs * the lookup _while_ we're POSTing data, which means the transaction * never completes (PushcallbackAction gets an 'inactive' state). * * @return boolean true when everything is ok (throws Exception on fail) * @throws Exception on failure, can be HTTPClient's or our own. */ protected function doSubscribe($mode) { $orig = clone $this; if ($mode == 'subscribe') { $this->secret = common_random_hexstr(32); } $this->sub_state = $mode; $this->update($orig); unset($orig); try { $callback = common_local_url('pushcallback', array('feed' => $this->id)); $headers = array('Content-Type: application/x-www-form-urlencoded'); $post = array('hub.mode' => $mode, 'hub.callback' => $callback, 'hub.verify' => 'async', 'hub.verify_token' => 'Deprecated-since-PuSH-0.4', 'hub.secret' => $this->secret, 'hub.topic' => $this->getUri()); $client = new HTTPClient(); if ($this->huburi) { $hub = $this->huburi; } else { if (common_config('feedsub', 'fallback_hub')) { $hub = common_config('feedsub', 'fallback_hub'); if (common_config('feedsub', 'hub_user')) { $u = common_config('feedsub', 'hub_user'); $p = common_config('feedsub', 'hub_pass'); $client->setAuth($u, $p); } } else { throw new FeedSubException('Server could not find a usable PuSH hub.'); } } $response = $client->post($hub, $headers, $post); $status = $response->getStatus(); // PuSH specificed response status code if ($status == 202) { common_log(LOG_INFO, __METHOD__ . ': sub req ok, awaiting verification callback'); return; } else { if ($status >= 200 && $status < 300) { common_log(LOG_ERR, __METHOD__ . ": sub req returned unexpected HTTP {$status}: " . $response->getBody()); } else { common_log(LOG_ERR, __METHOD__ . ": sub req failed with HTTP {$status}: " . $response->getBody()); } } } catch (Exception $e) { common_log(LOG_ERR, __METHOD__ . ": error \"{$e->getMessage()}\" hitting hub {$this->huburi} subscribing to {$this->getUri()}"); // Reset the subscription state. $orig = clone $this; $this->sub_state = 'inactive'; $this->update($orig); // Throw the Exception again. throw $e; } throw new ServerException("{$mode} request failed."); }
function postToCollection($url, $activity) { $client = new HTTPClient($url); $client->setMethod('POST'); $client->setAuth($this->username, $this->password); $client->setHeader('Content-Type', 'application/atom+xml;type=entry'); $client->setBody($activity->asString(true, true, true)); $response = $client->send(); $status = $response->getStatus(); $reason = $response->getReasonPhrase(); if ($status >= 200 && $status < 300) { return true; } else { if ($status >= 400 && $status < 500) { // TRANS: Client exception thrown when post to collection fails with a 400 status. // TRANS: %1$s is a URL, %2$s is the status, %s$s is the fail reason. throw new ClientException(sprintf(_m('URLSTATUSREASON', '%1$s %2$s %3$s'), $url, $status, $reason)); } else { if ($status >= 500 && $status < 600) { // TRANS: Server exception thrown when post to collection fails with a 500 status. // TRANS: %1$s is a URL, %2$s is the status, %s$s is the fail reason. throw new ServerException(sprintf(_m('URLSTATUSREASON', '%1$s %2$s %3$s'), $url, $status, $reason)); } else { // That's unexpected. // TRANS: Exception thrown when post to collection fails with a status that is not handled. // TRANS: %1$s is a URL, %2$s is the status, %s$s is the fail reason. throw new Exception(sprintf(_m('URLSTATUSREASON', '%1$s %2$s %3$s'), $url, $status, $reason)); } } } }