public function dispatchLoopShutdown() { $log = $this->_getLogger(); $coreLog = $this->_getLogCoreAccess(); $asyncLog = $this->_getLogAsyncAccess(); // Try to obtain the logged in user $id = Zend_Auth::getInstance()->getIdentity(); $msg = '[' . (isset($id['username']) ? $id['username'] : '******') . '] '; $msg .= $this->_method . ' ' . $this->_path; // Calculate the time expend serving the request $time = ceil((microtime(true) - $this->_time) * 1000); $msg .= ' (' . $time . 'ms)'; // Check if the request was redirecto to the error controller $error = $this->_request->getParam('error_handler'); if (NULL !== $error) { $ex = $error->exception; $msg .= ' [' . $ex->getCode() . '] ' . $ex->getMessage(); $log->err($msg); if ($coreLog) { $coreLog->err($msg); } if ($asyncLog) { $asyncLog->err($msg); } } else { $log->info($msg); } // Print debug information about the request if ($this->_request instanceof Zend_Controller_Request_Http) { $query = $this->_request->getQuery(); if (!empty($query)) { $log->info(">>> Request (Query): " . http_build_query($query)); } $headers = array(); if (!function_exists('apache_request_headers')) { foreach ($_SERVER as $key => $value) { if (substr($key, 0, 5) == "HTTP_") { $name = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($key, 5))))); $headers[] = "{$name}: {$value}"; } } } else { foreach (getallheaders() as $name => $value) { $headers[] = "{$name}: {$value}"; } } $log->debug(">>> Headers:\n" . implode("\n", $headers)); $body = $this->_request->getRawBody(); if (!empty($body)) { $log->debug(">>> Request (Body):\n" . $body); } $resp = Zend_Controller_Front::getInstance()->getResponse(); $headers = array(); foreach ($resp->getHeaders() as $header) { $headers[] = $header['name'] . ": " . $header['value']; } $log->debug("<<< Headers:\n" . implode("\n", $headers)); $log->debug("<<< Response:\n" . $resp->getBody()); } }
/** * PHP only parses the body into $_POST if its a POST request * this parses the reqest body in accordance with RFC2616 spec regardless of the HTTP method */ private function handleRequestBody(Zend_Controller_Request_Abstract $request) { $header = strtolower($request->getHeader('Content-Type')); // cleanup the charset part $header = current(explode(';', $header)); // detect request body content type foreach ($this->requestTypes as $contentType) { if ($header == $contentType) { break; } } // extract the raw body $rawBody = $request->getRawBody(); // treat these two separately because of the way PHP treats POST if (in_array($contentType, array('multipart/form-data', 'application/x-www-form-urlencoded'))) { // PHP takes care of everything for us in this case lets just modify the $_FILES array if ($request->isPost() && $contentType == 'multipart/form-data') { // if there are files, lets modify the array to match what we've done below foreach ($_FILES as &$file) { $data = file_get_contents($file['tmp_name']); $file['content'] = base64_encode($data); } // reset the array pointer unset($file); } else { switch ($contentType) { case 'application/x-www-form-urlencoded': parse_str($rawBody, $_POST); break; // this is wher the magic happens // creates the $_FILES array for none POST requests // this is wher the magic happens // creates the $_FILES array for none POST requests case 'multipart/form-data': // extract the boundary parse_str(end(explode(';', $request->getHeader('Content-Type')))); if (isset($boundary)) { // get rid of the boundary at the edges if (preg_match(sprintf('/--%s(.+)--%s--/s', $boundary, $boundary), $rawBody, $regs)) { // split into chuncks $chunks = explode('--' . $boundary, trim($regs[1])); foreach ($chunks as $chunk) { // parse each chunk if (preg_match('/Content-Disposition: form-data; name="(?P<name>.+?)"(?:; filename="(?P<filename>.+?)")?(?P<headers>(?:\\r|\\n)+?.+?(?:\\r|\\n)+?)?(?P<data>.+)/si', $chunk, $regs)) { // dedect a file upload if (!empty($regs['filename'])) { // put aside for further analysis $data = $regs['data']; $headers = $this->parseHeaders($regs['headers']); // set our params variable $_FILES[$regs['name']] = array('name' => $regs['filename'], 'type' => $headers['Content-Type'], 'size' => mb_strlen($data), 'content' => base64_encode($data)); // otherwise its a regular key=value combination } else { $_POST[$regs['name']] = trim($regs['data']); } } } } } break; } } $request->setParams($_POST + $_FILES); } elseif (!empty($rawBody)) { // seems like we are dealing with an encoded request try { switch ($contentType) { case 'text/javascript': case 'application/json': case 'application/javascript': $_POST = (array) Zend_Json::decode($rawBody, Zend_Json::TYPE_OBJECT); break; case 'text/xml': case 'application/xml': $json = @Zend_Json::fromXml($rawBody); $_POST = (array) Zend_Json::decode($json, Zend_Json::TYPE_OBJECT)->request; break; case 'text/php': case 'application/x-httpd-php': case 'application/x-httpd-php-source': $_POST = (array) unserialize($rawBody); break; default: $_POST = (array) $rawBody; break; } $request->setParams($_POST); } catch (Exception $e) { $request->dispatchError(REST_Response::BAD_REQUEST, 'Invalid Payload Format'); return; } } }