Exemplo n.º 1
0
 public function testGetRequestMethod()
 {
     $request = new Request('https://api.github.com/');
     $request->withMethod('POST');
     $response = $request->send();
     $this->assertEquals(404, $response->getStatusCode());
 }
Exemplo n.º 2
0
 public function __initialize(Request $request)
 {
     $this->request = $request;
     if (!$request->hasSession('manager')) {
         if ($request->isXmlHttpRequest()) {
             return new JsonResponse(['code' => 10086, 'msg' => 'Access denied.'], Response::HTTP_FORBIDDEN);
         }
         return $this->redirect($this->generateUrl($this->getParameters('admin-bundle.fail_url')));
     }
     $this->user = $request->getSession('manager');
     return true;
 }
Exemplo n.º 3
0
 /**
  * @param Request $request
  * @return array|JsonResponse|\FastD\Http\RedirectResponse
  */
 protected function verifyAccountAndPassword(Request $request)
 {
     $account = $request->request->hasGet('_username', null);
     $password = $request->request->hasGet('_password', null);
     if (empty($account) || empty($password)) {
         if ($request->isXmlHttpRequest()) {
             return new JsonResponse(['code' => 10086, 'msg' => 'Access denied.'], Response::HTTP_FORBIDDEN);
         }
         $url = $request->header->hasGet('REFERER', null);
         if (null === $url) {
             throw new \RuntimeException('Access denied.');
         }
         return $this->redirect($url);
     }
     return ['username' => $account, 'password' => $password];
 }
Exemplo n.º 4
0
 /**
  * ServerRequest constructor.
  *
  * @param array $get
  * @param array $post
  * @param array $files
  * @param array $cookie
  * @param array $server
  * @param SessionHandler|null $sessionHandler
  */
 public function __construct(array $get = [], array $post = [], array $files = [], array $cookie = [], array $server = [], SessionHandler $sessionHandler = null)
 {
     $this->query = new Bag($get);
     $this->body = new Bag($post);
     $this->server = new ServerBag($server);
     $this->cookie = new CookieBag($cookie);
     $this->file = new FileBag($files);
     $headers = [];
     array_walk($server, function ($value, $key) use(&$headers) {
         if (0 === strpos($key, 'HTTP_')) {
             $headers[$key] = $value;
         }
     });
     parent::__construct(sprintf('%s://%s', $this->server->getScheme(), $this->server->getHost() . $this->server->getBaseUrl() . $this->server->getPathInfo()), $headers, 'php://input');
     $this->withMethod($this->server->getMethod());
     unset($headers);
     try {
         $sessionKey = $this->getHeaderLine(Session::SESSION_KEY);
     } catch (InvalidArgumentException $e) {
         $sessionKey = null;
     }
     $this->session = Session::start($sessionKey, $sessionHandler);
     $this->withHeader(Session::SESSION_KEY, $this->session->getSessionId());
     $this->withCookieParams([Session::SESSION_KEY => $this->session->getSessionId()]);
 }
Exemplo n.º 5
0
 /**
  * Create one http request handle.
  *
  * @return Request|static
  */
 public static function createRequestHandle()
 {
     if (null === self::$requestFactory) {
         self::$requestFactory = new static($_GET, $_POST, $_FILES, $_COOKIE, $_SERVER);
         if (in_array(self::$requestFactory->server->get('REQUEST_METHOD'), array('PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD'))) {
             parse_str(self::$requestFactory->getContent(), $arguments);
             self::$requestFactory->request = new RequestAttribute($arguments);
         }
     }
     return self::$requestFactory;
 }
Exemplo n.º 6
0
 public function signOutAction(Request $request)
 {
     try {
         $redirectUrl = $this->generateUrl($this->getParameters('admin-bundle.logout_url'));
     } catch (\Exception $e) {
         if (!$request->request->has('redirect_url')) {
             throw new ServerInternalErrorException('redirect_url unconfiguration.');
         }
         $redirectUrl = $request->request->get('logout_url');
     }
     $request->clearSession('manager');
     if ($request->hasSession('manager')) {
         if ($request->isXmlHttpRequest()) {
             return new JsonResponse(['code' => 10088, 'msg' => 'Operation fail.'], Response::HTTP_BAD_REQUEST);
         }
         return $this->redirect($redirectUrl);
     }
     if ($request->isXmlHttpRequest()) {
         return new JsonResponse(['msg' => 'ok']);
     }
     return $this->redirect($redirectUrl);
 }
Exemplo n.º 7
0
<?php

/**
 * Created by PhpStorm.
 * User: janhuang
 * Date: 15/12/23
 * Time: 下午4:02
 * Github: https://www.github.com/janhuang
 * Coding: https://www.coding.net/janhuang
 * SegmentFault: http://segmentfault.com/u/janhuang
 * Blog: http://segmentfault.com/blog/janhuang
 * Gmail: bboyjanhuang@gmail.com
 * WebSite: http://www.janhuang.me
 */
include __DIR__ . '/../vendor/autoload.php';
$request = \FastD\Http\Request::createRequestHandle();
$response = $request->createRequest('http://localhost/me/fastd/library/http/examples/base.php')->ajax();
echo '<pre>';
print_r($response);
Exemplo n.º 8
0
 /**
  * @param Request $request
  * @return \FastD\Http\JsonResponse
  * @throws \Exception
  */
 public function uploadAction(Request $request)
 {
     if ($request->files->isEmpty()) {
         return $this->responseJson(['msg' => 'Empty files.'], Response::HTTP_BAD_REQUEST);
     }
     $uploadedFiles = $request->getUploader(['path' => $this->getParameters('media.uploaded.path'), 'exts' => $this->getParameters('media.uploaded.exts'), 'size' => $this->getParameters('media.uploaded.size')])->uploading();
     if (null == ($files = $uploadedFiles->getUploadFiles())) {
         return $this->responseJson(['msg' => 'Uploaded files error.']);
     }
     try {
         $remote = $this->getParameters('media.remote.url');
     } catch (\Exception $e) {
         $remote = null;
     }
     try {
         $repository = $this->getParameters('media.provider.repository');
         $connection = $this->getParameters('media.provider.connection');
         $mediaRepository = $this->getConnection($connection)->getRepository($repository);
     } catch (\Exception $e) {
         $mediaRepository = null;
     }
     foreach ($files as $file) {
         $relativePath = str_replace(dirname($this->get('kernel')->getRootPath()) . DIRECTORY_SEPARATOR . 'public/', '', $file->getRealPath());
         $thumb = $this->toThumbnail($relativePath, $this->getParameters('media.thumbnil.width'), $this->getParameters('media.thumbnil.height'));
         $id = $this->createNewImageRecord($mediaRepository, $file);
         $remoteUrl = $this->uploadRemote($request, $file, $remote);
     }
     return $this->responseJson(['id' => $id, 'url' => $this->asset($relativePath, $this->getParameters('media.resources_url')), 'thumb' => $this->asset($thumb, $this->getParameters('media.resources_url')), 'remote_url' => $remoteUrl]);
 }
Exemplo n.º 9
0
 /**
  * @return \FastD\Http\Request
  */
 public function createHttpRequestClient()
 {
     $request = Request::createRequestHandle();
     $this->container->set('kernel.request', $request);
     return $request;
 }