public function __call($event, $args) { if ($this->state) { // call state method JAXLLogger::debug(sprintf("calling state handler '%s' for incoming event '%s'", is_array($this->state) ? $this->state[1] : $this->state, $event)); if (is_callable($this->state)) { $call = $this->state; } else { $call = method_exists($this, $this->state) ? array(&$this, $this->state) : $this->state; } $r = call_user_func($call, $event, $args); // 4 cases of possible return value if (is_callable($r)) { $this->state = $r; } elseif (is_array($r) && count($r) == 2) { list($this->state, $ret) = $r; } elseif (is_array($r) && count($r) == 1) { $this->state = $r[0]; } elseif (is_string($r)) { $this->state = $r; } else { $this->handle_invalid_state($r); } JAXLLogger::debug("current state '" . (is_array($this->state) ? $this->state[1] : $this->state) . "'"); // return case if (!is_callable($r) && is_array($r) && count($r) == 2) { return $ret; } } else { JAXLLogger::debug("invalid state found, nothing called for event " . $event . ""); } }
public function logged_out($stanza) { if ($stanza->name == "error" && $stanza->ns == XMPP::NS_XMPP) { $reason = $stanza->children[0]->name; $this->jaxl->handle_auth_failure($reason); $this->jaxl->send_end_stream(); return array("logged_out", 0); } else { JAXLLogger::debug("uncatched stanza received in logged_out"); } }
public function dispatch($request) { foreach ($this->rules as $rule) { //JAXLLogger::debug("matching $request->path with pattern $rule->pattern"); if (($matches = $rule->match($request->path, $request->method)) !== false) { JAXLLogger::debug("matching rule found, dispatching"); $params = array($request); // TODO: a bad way to restrict on 'pk', fix me for generalization if (isset($matches['pk'])) { $params[] = $matches['pk']; } call_user_func_array($rule->cb, $params); return true; } } return false; }
public function on_client_write_ready($client) { $client_id = (int) $client; JAXLLogger::debug("client#{$client_id} is write ready"); try { // send in chunks $total = $this->clients[$client_id]['obuffer']; $written = @fwrite($client, substr($total, 0, $this->send_chunk_size)); if ($written === false) { // fwrite failed JAXLLogger::warning("====> fwrite failed"); $this->clients[$client_id]['obuffer'] = $total; } elseif ($written == strlen($total) || $written == $this->send_chunk_size) { // full chunk written //JAXLLogger::debug("full chunk written"); $this->clients[$client_id]['obuffer'] = substr($total, $this->send_chunk_size); } else { // partial chunk written //JAXLLogger::debug("partial chunk $written written"); $this->clients[$client_id]['obuffer'] = substr($total, $written); } // if no more stuff to write, remove write handler if (strlen($this->clients[$client_id]['obuffer']) === 0) { $this->del_write_cb($client_id); // if scheduled for close and not closed do it and clean up if ($this->clients[$client_id]['close'] && !$this->clients[$client_id]['closed']) { if (is_resource($client)) { fclose($client); } $this->clients[$client_id]['closed'] = true; unset($this->clients[$client_id]); JAXLLogger::debug("closed client#" . $client_id); } } } catch (JAXLException $e) { JAXLLogger::debug("====> got fwrite exception"); } }
public function wait_for_content_body($event, $data) { if ($event == 'process') { if ($data[0] == '--' . $this->boundary) { JAXLLogger::debug("start of new multipart/form-data detected"); return array('wait_for_content_disposition', true); } elseif ($data[0] == '--' . $this->boundary . '--') { JAXLLogger::debug("end of multipart form data detected"); return array('wait_for_empty_line', true); } else { $this->form_data[$this->index]['body'] .= $data[0]; return array('wait_for_content_body', true); } } else { JAXLLogger::warning("invalid {$event} rcvd"); return array('wait_for_content_body', false); } }
public static function shutdown_handler() { try { JAXLLogger::debug("got shutdown handler"); if (null !== ($error = error_get_last())) { throw new JAXLException($error['message'], $error['type'], $error['file'], $error['line']); } } catch (Exception $e) { JAXLLogger::debug("shutdown handler catched with exception " . json_encode($e)); } }
public function on_request($sock, $raw) { JAXLLogger::debug("on_request for client#{$sock}"); $request = $this->requests[$sock]; // 'wait_for_body' state is reached when ever // application calls recv_body() method // on received $request object if ($request->state() == 'wait_for_body') { $request->body($raw); } else { // break on crlf $lines = explode(HTTP_CRLF, $raw); // parse request line if ($request->state() == 'wait_for_request_line') { list($method, $resource, $version) = explode(" ", $lines[0]); $request->line($method, $resource, $version); unset($lines[0]); JAXLLogger::info($request->ip . " " . $request->method . " " . $request->resource . " " . $request->version); } // parse headers foreach ($lines as $line) { $line_parts = explode(":", $line); if (count($line_parts) > 1) { if (strlen($line_parts[0]) > 0) { $k = $line_parts[0]; unset($line_parts[0]); $v = implode(":", $line_parts); $request->set_header($k, $v); } } elseif (strlen(trim($line_parts[0])) == 0) { $request->empty_line(); } else { // if exploded line array size is 1 // and there is something in $line_parts[0] // must be request body $request->body($line); } } } // if request has reached 'headers_received' state? if ($request->state() == 'headers_received') { // dispatch to any matching rule found JAXLLogger::debug("delegating to dispatcher for further routing"); $dispatched = $this->dispatcher->dispatch($request); // if no dispatch rule matched call generic callback if (!$dispatched && $this->cb) { JAXLLogger::debug("no dispatch rule matched, sending to generic callback"); call_user_func($this->cb, $request); } elseif (!$dispatched) { // elseif not dispatched and not generic callbacked // send 404 not_found // TODO: send 404 if no callback is registered for this request JAXLLogger::debug("dropping request since no matching dispatch rule or generic callback was specified"); $request->not_found('404 Not Found'); } } else { // if state is not 'headers_received' // reactivate client socket for read event $this->server->read($sock); } }
public function disconnect() { JAXLLogger::debug("disconnecting"); }
public function on_response($raw) { JAXLLogger::debug($raw); }
public function handle_auth_mechs($stanza, $mechanisms) { if ($this->ev->exists('on_stream_features')) { return $this->ev->emit('on_stream_features', array($stanza)); } // extract available mechanisms $mechs = array(); if ($mechanisms) { foreach ($mechanisms->children as $mechanism) { $mechs[$mechanism->text] = true; } } $pref_auth = $this->cfg['auth_type']; // check if preferred auth type exists in available mechanisms if (isset($mechs[$pref_auth]) && $mechs[$pref_auth]) { JAXLLogger::debug("pref_auth " . $pref_auth . " exists"); } else { JAXLLogger::debug("pref_auth " . $pref_auth . " doesn't exists"); JAXLLogger::error("preferred auth type not supported, trying {$pref_auth}"); } $this->send_auth_pkt($pref_auth, isset($this->jid) ? $this->jid->to_string() : null, $this->pass); if ($pref_auth == 'CRAM-MD5') { return "wait_for_cram_md5_response"; } elseif ($pref_auth == 'SCRAM-SHA-1') { return "wait_for_scram_sha1_response"; } }
public function on_write_ready($fd) { //JAXLLogger::debug("on write ready called"); $total = strlen($this->obuffer); $bytes = @fwrite($fd, $this->obuffer); $this->send_bytes += $bytes; JAXLLogger::debug("sent " . $bytes . "/" . $this->send_bytes . " of data"); JAXLLogger::debug(substr($this->obuffer, 0, $bytes)); $this->obuffer = substr($this->obuffer, $bytes, $total - $bytes); // unwatch for write if obuffer is empty if (strlen($this->obuffer) === 0) { JAXLLoop::unwatch($fd, array('write' => true)); $this->writing = false; } //JAXLLogger::debug("current obuffer size: ".strlen($this->obuffer).""); }
public function logged_out($event, $args) { switch ($event) { case "end_cb": $this->trans->disconnect(); return "disconnected"; break; case "end_stream": return "disconnected"; break; case "disconnect": $this->trans->disconnect(); return "disconnected"; break; case "connect": return $this->do_connect($args); break; default: // exit for any other event in logged_out state JAXLLogger::debug("uncatched {$event}"); return $this->handle_other($event, $args); //return array("logged_out", 0); break; } }
protected function send_response($code, array $headers = array(), $body = null) { // send out response line $this->send_line($code); // set content length of body exists and is not already set if ($body && !isset($headers['Content-Length'])) { $headers['Content-Length'] = strlen($body); } // send out headers $this->send_headers($code, $headers); // send body // prefixed with an empty line JAXLLogger::debug("sending out HTTP_CRLF prefixed body"); if ($body) { $this->send_body(HTTPServer::HTTP_CRLF . $body); } }
public static function run() { if (!self::$is_running) { self::$is_running = true; self::$clock = new JAXLClock(); while (self::$active_read_fds + self::$active_write_fds > 0) { self::select(); } JAXLLogger::debug("no more active fd's to select"); self::$is_running = false; } }
public function __destruct() { if (is_resource($this->fd)) { fclose($this->fd); } if (file_exists($this->get_pipe_file_path())) { unlink($this->get_pipe_file_path()); } JAXLLogger::debug("unlinking pipe file"); }