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);
}
}