X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=plugins%2FTwitterBridge%2Fjsonstreamreader.php;h=f6572c9eefafbae4aafd993de2bdfbf528967c00;hb=e0e7cb7c5376a7adfdcf8e0724aedfae3de471ef;hp=ad8e2626ad15d48450ecbfe048968a1d7925ea06;hpb=3b304fc0efd0bb4d75b964fe5585703d113d9c99;p=quix0rs-gnu-social.git diff --git a/plugins/TwitterBridge/jsonstreamreader.php b/plugins/TwitterBridge/jsonstreamreader.php index ad8e2626ad..f6572c9eef 100644 --- a/plugins/TwitterBridge/jsonstreamreader.php +++ b/plugins/TwitterBridge/jsonstreamreader.php @@ -49,7 +49,9 @@ abstract class JsonStreamReader /** * Starts asynchronous connect operation... * - * @param $url + * @fixme Can we do the open-socket fully async to? (need write select infrastructure) + * + * @param string $url */ public function connect($url) { @@ -93,21 +95,34 @@ abstract class JsonStreamReader $this->state = 'waiting'; } + /** + * Send some fun data off to the server. + * + * @param string $buffer + */ function send($buffer) { - echo "Writing...\n"; - var_dump($buffer); fwrite($this->socket, $buffer); } + /** + * Read next packet of data from the socket. + * + * @return string + */ function read() { - echo "Reading...\n"; $buffer = fread($this->socket, 65536); - var_dump($buffer); return $buffer; } + /** + * Build HTTP request headers. + * + * @param string $host + * @param string $path + * @return string + */ protected function httpOpen($host, $path) { $lines = array( @@ -159,66 +174,92 @@ abstract class JsonStreamReader /** * Take a chunk of input over the horn and go go go! :D + * * @param string $buffer */ - function handleInput($socket) + public function handleInput($socket) { if ($this->socket !== $socket) { throw new Exception('Got input from unexpected socket!'); } - $buffer = $this->read(); + try { + $buffer = $this->read(); + $lines = explode(self::CRLF, $buffer); + foreach ($lines as $line) { + $this->handleLine($line); + } + } catch (Exception $e) { + common_log(LOG_ERR, "$this->id aborting connection due to error: " . $e->getMessage()); + fclose($this->socket); + throw $e; + } + } + + protected function handleLine($line) + { switch ($this->state) { case 'waiting': - $this->handleInputWaiting($buffer); + $this->handleLineWaiting($line); break; case 'headers': - $this->handleInputHeaders($buffer); + $this->handleLineHeaders($line); break; case 'active': - $this->handleInputActive($buffer); + $this->handleLineActive($line); break; default: - throw new Exception('Invalid state in handleInput: ' . $this->state); + throw new Exception('Invalid state in handleLine: ' . $this->state); } } - function handleInputWaiting($buffer) + /** + * + * @param $line + */ + protected function handleLineWaiting($line) { - common_log(LOG_DEBUG, "$this->id Does this happen? " . $buffer); + $bits = explode(' ', $line, 3); + if (count($bits) != 3) { + throw new Exception("Invalid HTTP response line: $line"); + } + + list($http, $status, $text) = $bits; + if (substr($http, 0, 5) != 'HTTP/') { + throw new Exception("Invalid HTTP response line chunk '$http': $line"); + } + if ($status != '200') { + throw new Exception("Bad HTTP response code $status: $line"); + } + common_log(LOG_DEBUG, "$this->id $line"); $this->state = 'headers'; - $this->handleInputHeaders($buffer); } - function handleInputHeaders($buffer) + protected function handleLineHeaders($line) { - $lines = explode(self::CRLF, $buffer); - foreach ($lines as $line) { - if ($line == '') { - $this->state = 'active'; - common_log(LOG_DEBUG, "$this->id connection is active!"); - } else { - common_log(LOG_DEBUG, "$this->id read HTTP header: $line"); - $this->responseHeaders[] = $line; - } + if ($line == '') { + $this->state = 'active'; + common_log(LOG_DEBUG, "$this->id connection is active!"); + } else { + common_log(LOG_DEBUG, "$this->id read HTTP header: $line"); + $this->responseHeaders[] = $line; } } - function handleInputActive($buffer) + protected function handleLineActive($line) { - // One JSON object on each line... - // Will we always deliver on packet boundaries? - $lines = explode("\n", $buffer); - foreach ($lines as $line) { - $data = json_decode($line, true); - if ($data) { - $this->handleJson($data); - } else { - common_log(LOG_ERR, "$this->id received bogus JSON data: " . $line); - } + if ($line == "") { + // Server sends empty lines as keepalive. + return; + } + $data = json_decode($line); + if ($data) { + $this->handleJson($data); + } else { + common_log(LOG_ERR, "$this->id received bogus JSON data: " . var_export($line, true)); } } - abstract function handleJson(array $data); + abstract protected function handleJson(stdClass $data); }