Автор: Tim Wagner (tw@appserver.io)
Наследование: implements AppserverIo\Psr\HttpMessage\PartInterface
Пример #1
0
 /**
  * Creates a new servlet part instance with the data from the HTTP part.
  *
  * @param \AppserverIo\Psr\HttpMessage\PartInterface $httpPart The HTTP part we want to copy
  *
  * @return \AppserverIo\Appserver\ServletEngine\Http\Part The initialized servlet part
  */
 public static function fromHttpRequest(PartInterface $httpPart)
 {
     // create a temporary filename
     $httpPart->write($tmpFilename = tempnam(ini_get('upload_tmp_dir'), 'tmp_'));
     // initialize the servlet part instance
     $servletPart = new Part();
     $servletPart->setName($httpPart->getName());
     $servletPart->setFilename($httpPart->getFilename());
     $servletPart->setTmpFilename($tmpFilename);
     // return the servlet part instance
     return $servletPart;
 }
Пример #2
0
 /**
  * Initializes the servlet request with the data from the injected HTTP request instance.
  *
  * @return void
  */
 public function init()
 {
     // reset the servlet request
     $httpRequest = $this->getHttpRequest();
     // initialize the parts
     foreach ($httpRequest->getParts() as $part) {
         $this->addPart(Part::fromHttpRequest($part));
     }
     // set the body content if we can find one
     if ($httpRequest->getHeader(HttpProtocol::HEADER_CONTENT_LENGTH) > 0) {
         $this->setBodyStream($httpRequest->getBodyContent());
     }
     // copy server variables to members
     $this->setServerName($this->getServerVar(ServerVars::SERVER_NAME));
     $this->setQueryString($this->getServerVar(ServerVars::QUERY_STRING));
     $this->setRequestUri($this->getServerVar(ServerVars::X_REQUEST_URI));
     $this->setDocumentRoot($this->getServerVar(ServerVars::DOCUMENT_ROOT));
     $this->setRequestUrl($this->getServerVar(ServerVars::HTTP_HOST) . $this->getServerVar(ServerVars::X_REQUEST_URI));
 }
Пример #3
0
 /**
  * Process servlet request.
  *
  * @param \AppserverIo\Psr\HttpMessage\RequestInterface          $request        A request object
  * @param \AppserverIo\Psr\HttpMessage\ResponseInterface         $response       A response object
  * @param \AppserverIo\Server\Interfaces\RequestContextInterface $requestContext A requests context instance
  * @param int                                                    $hook           The current hook to process logic for
  *
  * @return bool
  * @throws \AppserverIo\Server\Exceptions\ModuleException
  */
 public function process(RequestInterface $request, ResponseInterface $response, RequestContextInterface $requestContext, $hook)
 {
     try {
         // if false hook is coming do nothing
         if (ModuleHooks::REQUEST_POST !== $hook) {
             return;
         }
         // check if we are the handler that has to process this request
         if ($requestContext->getServerVar(ServerVars::SERVER_HANDLER) !== $this->getModuleName()) {
             return;
         }
         // initialize servlet session, request + response
         $servletRequest = new Request();
         $servletRequest->injectHttpRequest($request);
         $servletRequest->injectServerVars($requestContext->getServerVars());
         // initialize the parts
         foreach ($request->getParts() as $part) {
             $servletRequest->addPart(Part::fromHttpRequest($part));
         }
         // set the body content if we can find one
         if ($request->getHeader(HttpProtocol::HEADER_CONTENT_LENGTH) > 0) {
             $servletRequest->setBodyStream($request->getBodyContent());
         }
         // prepare the servlet request
         $this->prepareServletRequest($servletRequest);
         // initialize the servlet response with the Http response values
         $servletResponse = new Response();
         $servletRequest->injectResponse($servletResponse);
         // load the application associated with this request
         $application = $this->findRequestedApplication($requestContext);
         // prepare and set the applications context path
         $servletRequest->setContextPath($contextPath = '/' . $application->getName());
         $servletRequest->setServletPath(str_replace($contextPath, '', $servletRequest->getServletPath()));
         // prepare the base modifier which allows our apps to provide a base URL
         $webappsDir = $this->getServerContext()->getServerConfig()->getDocumentRoot();
         $relativeRequestPath = strstr($servletRequest->getServerVar(ServerVars::DOCUMENT_ROOT), $webappsDir);
         $proposedBaseModifier = str_replace($webappsDir, '', $relativeRequestPath);
         if (strpos($proposedBaseModifier, $contextPath) === 0) {
             $servletRequest->setBaseModifier('');
         } else {
             $servletRequest->setBaseModifier($contextPath);
         }
         // initialize the request handler instance
         $dispatched = false;
         $applicationName = $application->getName();
         while ($dispatched === false) {
             if ($this->requestHandlers[$applicationName][$i = rand(0, 9)]->isWaiting()) {
                 $this->requestHandlers[$applicationName][$i]->handleRequest($servletRequest, $servletResponse);
                 $dispatched = true;
                 break;
             }
         }
         // copy the values from the servlet response back to the HTTP response
         $response->setStatusCode($servletResponse->getStatusCode());
         $response->setStatusReasonPhrase($servletResponse->getStatusReasonPhrase());
         $response->setVersion($servletResponse->getVersion());
         $response->setState($servletResponse->getState());
         // append the content to the body stream
         $response->appendBodyStream($servletResponse->getBodyStream());
         // transform the servlet headers back into HTTP headers
         $headers = array();
         foreach ($servletResponse->getHeaders() as $name => $header) {
             $headers[$name] = $header;
         }
         // set the headers as array (because we don't know if we have to use the append flag)
         $response->setHeaders($headers);
         // copy the servlet response cookies back to the HTTP response
         foreach ($servletResponse->getCookies() as $cookie) {
             $response->addCookie(unserialize($cookie));
         }
         // set response state to be dispatched after this without calling other modules process
         $response->setState(HttpResponseStates::DISPATCH);
     } catch (ModuleException $me) {
         throw $me;
     } catch (\Exception $e) {
         throw new ModuleException($e, 500);
     }
 }