示例#1
0
文件: Request.php 项目: garethwi/yin
 /**
  * @inheritDoc
  */
 public function getParsedBody()
 {
     if ($this->serverRequest->getBody()->getContents() && empty($this->serverRequest->getParsedBody())) {
         $this->serverRequest = $this->serverRequest->withParsedBody(json_decode($this->serverRequest->getBody(), true));
     }
     return $this->serverRequest->getParsedBody();
 }
 public function modify(ServerRequestInterface $request) : ServerRequestInterface
 {
     if (false === strpos($request->getHeaderLine('Content-Type'), 'application/json')) {
         return $request;
     }
     // TODO: Implement broken json handling
     return $request->withParsedBody(json_decode($request->getBody()->getContents(), $this->asAssociativeArray));
 }
 public function modify(ServerRequestInterface $request) : ServerRequestInterface
 {
     if (false === strpos($request->getHeaderLine('Content-Type'), 'application/x-www-form-urlencoded')) {
         return $request;
     }
     parse_str($request->getBody()->getContents(), $parsedBody);
     return $request->withParsedBody($parsedBody);
 }
示例#4
0
文件: Util.php 项目: tonis-io/oauth2
 /**
  * Converts a PSR-7 request into an OAuth2 request.
  *
  * @param ServerRequestInterface $psrRequest
  * @return Request
  */
 public static function convertRequestFromPsr7(ServerRequestInterface $psrRequest)
 {
     $headers = [];
     foreach ($psrRequest->getHeaders() as $header => $value) {
         $headers[$header] = implode(';', $value);
     }
     return new Request($psrRequest->getQueryParams(), is_array($psrRequest->getParsedBody()) ? $psrRequest->getParsedBody() : [], $psrRequest->getAttributes(), $psrRequest->getCookieParams(), self::getFiles($psrRequest->getUploadedFiles()), $psrRequest->getServerParams(), $psrRequest->getBody()->__toString(), $headers);
 }
示例#5
0
 /**
  * {@inheritdoc}
  */
 public function __invoke(Request $request, Response $response, callable $out = null)
 {
     if (str_contains($request->getHeaderLine('content-type'), 'json')) {
         $input = json_decode($request->getBody(), true);
         $request = $request->withParsedBody($input ?: []);
     }
     return $out ? $out($request, $response) : $response;
 }
 /**
  * @return string
  */
 private function getBody()
 {
     if (isset($this->body)) {
         return $this->body;
     }
     $this->body = (string) $this->request->getBody();
     return $this->body;
 }
 protected function parseJsonBody(ServerRequestInterface $request)
 {
     $bodyJson = @json_decode($request->getBody()->__toString(), true);
     if (json_last_error() != JSON_ERROR_NONE) {
         throw new \Exception(json_last_error_msg());
     }
     return $bodyJson;
 }
 /**
  * {@inheritdoc}
  */
 public function createRequest(ServerRequestInterface $psrRequest)
 {
     $parsedBody = $psrRequest->getParsedBody();
     $parsedBody = is_array($parsedBody) ? $parsedBody : array();
     $request = new Request($psrRequest->getQueryParams(), $parsedBody, $psrRequest->getAttributes(), $psrRequest->getCookieParams(), $this->getFiles($psrRequest->getUploadedFiles()), $psrRequest->getServerParams(), $psrRequest->getBody()->__toString());
     $request->headers->replace($psrRequest->getHeaders());
     return $request;
 }
示例#9
0
 /**
  * @param  ServerRequestInterface $request
  */
 public function send(ServerRequestInterface $request)
 {
     $this->currentUri = $request->getUri();
     $this->setBase($this->currentUri);
     if (empty($this->currentUri->getHost())) {
         $this->currentUri = new Uri($this->getBase() . $this->currentUri);
     }
     $curl = curl_init();
     $options = [CURLOPT_URL => (string) $this->currentUri, CURLOPT_FOLLOWLOCATION => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => $request->getMethod(), CURLOPT_USERAGENT => Loader::USER_AGENT, CURLOPT_HEADER => true, CURLOPT_HTTPHEADER => $this->getConvertedHeaders($request->getHeaders())];
     if ($request->getBody()->getSize()) {
         $options[CURLOPT_POSTFIELDS] = (string) $request->getBody();
     }
     curl_setopt_array($curl, $options);
     $response = curl_exec($curl);
     $this->currentUri = new Uri(curl_getinfo($curl, CURLINFO_EFFECTIVE_URL));
     return \GuzzleHttp\Psr7\parse_response($response);
 }
示例#10
0
 /**
  * @inheritDoc
  */
 public function getCredentials(ServerRequestInterface $request)
 {
     $json = json_decode($request->getBody()->getContents(), true);
     if (empty($json[$this->identifier]) || empty($json[$this->password])) {
         return null;
     }
     return new Credentials($json[$this->identifier], $json[$this->password]);
 }
示例#11
0
 /**
  * Parses request bodies based on content type
  *
  * @param  Request  $request
  * @param  Response $response
  * @param  callable $next
  * @return Response
  */
 public function __invoke(Request $request, Response $response, callable $next)
 {
     $mime = strtolower($request->getHeaderLine('Content-Type'));
     if ($this->isApplicableMimeType($mime) && !$request->getParsedBody()) {
         $parsed = $this->getParsedBody((string) $request->getBody());
         $request = $request->withParsedBody($parsed);
     }
     return $next($request, $response);
 }
 /**
  * Convert a PSR-7 ServerRequest to a Zend\Http server-side request.
  *
  * @param ServerRequestInterface $psr7Request
  * @param bool $shallow Whether or not to convert without body/file
  *     parameters; defaults to false, meaning a fully populated request
  *     is returned.
  * @return Zend\Request
  */
 public static function toZend(ServerRequestInterface $psr7Request, $shallow = false)
 {
     if ($shallow) {
         return new Zend\Request($psr7Request->getMethod(), $psr7Request->getUri(), $psr7Request->getHeaders(), $psr7Request->getCookieParams(), $psr7Request->getQueryParams(), [], [], $psr7Request->getServerParams());
     }
     $zendRequest = new Zend\Request($psr7Request->getMethod(), $psr7Request->getUri(), $psr7Request->getHeaders(), $psr7Request->getCookieParams(), $psr7Request->getQueryParams(), $psr7Request->getParsedBody() ?: [], self::convertUploadedFiles($psr7Request->getUploadedFiles()), $psr7Request->getServerParams());
     $zendRequest->setContent($psr7Request->getBody());
     return $zendRequest;
 }
 public function enrichRequestWithParsedBody(ServerRequestInterface $request)
 {
     if ($request->hasHeader(HeaderName::CONTENT_TYPE) && $request->getHeaderLine(HeaderName::CONTENT_TYPE) === 'application/json') {
         $parsedBody = $this->serializer->deserialize($request->getBody()->__toString(), 'array', 'json');
         return $request->withParsedBody($parsedBody);
     } else {
         return $request->withParsedBody([]);
     }
 }
示例#14
0
 public static function createRequest(PsrServerRequestInterface $psrRequest)
 {
     $request = new Request(new Uri($psrRequest->getUri()), $psrRequest->getMethod(), $psrRequest->getHeaders(), $psrRequest->getBody());
     $attributes = $psrRequest->getAttributes();
     foreach ($attributes as $name => $value) {
         $request->setAttribute($name, $value);
     }
     return $request;
 }
 /**
  * {@inheritDoc}
  */
 public function parse(ServerRequestInterface $request)
 {
     $rawBody = (string) $request->getBody();
     $parsedBody = json_decode($rawBody, true);
     if (json_last_error() !== JSON_ERROR_NONE) {
         throw new MalformedRequestBodyException('Error when parsing JSON request body: ' . json_last_error_msg());
     }
     return $request->withAttribute('rawBody', $rawBody)->withParsedBody($parsedBody);
 }
示例#16
0
 /**
  * {@inheritdoc}
  */
 public function __invoke(Request $request, Response $response, callable $out = null)
 {
     if (str_contains($request->getHeaderLine('content-type'), 'json')) {
         $input = json_decode($request->getBody(), true);
         foreach ($input as $name => $value) {
             $request = $request->withAttribute($name, $value);
         }
     }
     return $out ? $out($request, $response) : $response;
 }
示例#17
0
 protected function getValues(ServerRequestInterface $request)
 {
     $contentType = $request->getHeader('Content-type');
     if (count($contentType) > 0 && strpos($contentType[0], 'application/json') !== false) {
         $value = json_decode($request->getBody()->getContents(), true);
     } else {
         $value = $request->getParsedBody();
     }
     return $value;
 }
示例#18
0
 public function __construct(ServerRequestInterface $request)
 {
     $this->request = $request;
     if ($request->getParsedBody()) {
         $this->data = json_decode(json_encode($request->getParsedBody()), true);
     } else {
         $this->data = json_decode($request->getBody(), true);
     }
     $this->validateSchema();
 }
示例#19
0
 public function __invoke(Request $request, Response $response, callable $next)
 {
     $type = $request->getHeader('Content-Type');
     $method = $request->getMethod();
     if ('GET' != $method && !empty($type) && 'application/json' == strtolower($type[0])) {
         $body = (string) $request->getBody();
         $request = $request->withParsedBody(json_decode($body));
     }
     return $next($request, $response);
 }
示例#20
0
 /**
  * @param Request $request
  * @return Request
  */
 protected function parseFromUrlEncoded(Request $request)
 {
     $rawBody = (string) $request->getBody();
     if (empty($rawBody)) {
         return $request;
     }
     $parsedBody = [];
     parse_str($rawBody, $parsedBody);
     return $request->withParsedBody($parsedBody);
 }
示例#21
0
 /**
  * @inheritDoc
  */
 public function parse(ServerRequestInterface $request) : ServerRequestInterface
 {
     if (false === strpos($request->getHeaderLine('Content-Type'), 'application/json')) {
         return $request;
     }
     $body = $request->getBody()->getContents();
     $parsedBody = json_decode($body, true);
     // TODO: Implement broken json handling
     return $request->withParsedBody($parsedBody);
 }
示例#22
0
 /**
  * @inheritDoc
  */
 public function getParsedBody()
 {
     if (empty($this->serverRequest->getParsedBody()) === false) {
         return $this->serverRequest->getParsedBody();
     }
     $content = $this->serverRequest->getBody()->__toString();
     if ($content) {
         $this->serverRequest = $this->serverRequest->withParsedBody(json_decode($content, true));
     }
     return $this->serverRequest->getParsedBody();
 }
示例#23
0
 /**
  * Handle the payload.
  *
  * @param ServerRequestInterface $request
  *
  * @return ServerRequestInterface
  */
 protected function handlePayload(ServerRequestInterface $request)
 {
     if ($request->getParsedBody() || !in_array($request->getMethod(), ['POST', 'PUT', 'DELETE'], true)) {
         return $request;
     }
     $contentType = trim($request->getHeaderLine('Content-Type'));
     //json
     if (stripos($contentType, 'application/json') === 0) {
         return $request->withParsedBody($this->parseJson($request->getBody()))->withBody(Middleware::createStream());
     }
     //urlencoded
     if (stripos($contentType, 'application/x-www-form-urlencoded') === 0) {
         return $request->withParsedBody($this->parseUrlEncoded($request->getBody()))->withBody(Middleware::createStream());
     }
     //csv
     if (stripos($contentType, 'text/csv') === 0) {
         return $request->withParsedBody($this->parseCsv($request->getBody()))->withBody(Middleware::createStream());
     }
     return $request;
 }
示例#24
0
 /**
  * @param \Psr\Http\Message\ServerRequestInterface $request
  * @param \Slim\Http\Response                      $response
  *
  * @return \Slim\Http\Response
  * @throws \livetyping\hermitage\app\exceptions\BadRequestException
  */
 public function __invoke(Request $request, Response $response) : Response
 {
     $mime = (string) current($request->getHeader('Content-Type'));
     $binary = (string) $request->getBody();
     if (empty($mime) || empty($binary) || !in_array($mime, Util::supportedMimeTypes())) {
         throw new BadRequestException('Invalid mime-type or body.');
     }
     $command = new StoreImageCommand($mime, $binary);
     $this->bus->handle($command);
     return $response->withStatus(201)->withJson(['filename' => $command->getPath()]);
 }
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
 {
     $tokenHeader = $request->getHeader('X-Auth-Token');
     if (count($tokenHeader) > 0) {
         $tokenHeaderValue = $tokenHeader[0];
     } else {
         $tokenHeaderValue = null;
     }
     $data = array('requestMethod' => $request->getMethod(), 'requestUri' => $request->getRequestTarget(), 'queryParams' => $request->getQueryParams(), 'formParams' => $request->getParsedBody(), 'rawBody' => (string) $request->getBody(), 'headers' => $request->getHeaders(), 'X-Auth-Token' => $tokenHeaderValue);
     return new JsonResponse($data);
 }
 /** {@inheritdoc} */
 public function execute(ServerHttpRequest $httpRequest) : HttpResponse
 {
     // Only works on requests with non-empty JSON bodies
     if ($httpRequest->getHeaderLine('Content-Type') !== 'application/vnd.api+json' || $httpRequest->getBody()->getSize() === 0) {
         return $this->application->execute($httpRequest);
     }
     $parsedBody = json_decode($httpRequest->getBody()->getContents(), true);
     if (json_last_error() !== JSON_ERROR_NONE) {
         return new JsonApiErrorResponse([['title' => 'Invalid JSON, couldn\'t decode.', 'status' => '400']], 400);
     }
     return $this->application->execute($httpRequest->withParsedBody($parsedBody));
 }
示例#27
0
 /**
  * JSON middleware invokable class. It forces Content-Type on the response
  * to be application/json. If the request Content-Type is not json then
  * return the response with status 415 (Unsupported Media Type)
  *
  * @param  \Psr\Http\Message\ServerRequestInterface $request  PSR7 request
  * @param  \Psr\Http\Message\ResponseInterface      $response PSR7 response
  * @param  callable                                 $next     Next middleware
  *
  * @return \Psr\Http\Message\ResponseInterface
  */
 public function __invoke(Request $request, Response $response, $next)
 {
     $mediaType = $request->getMediaType();
     $method = strtolower($request->getMethod());
     if (in_array($method, ['post', 'put', 'patch']) && '' !== (string) $request->getBody()) {
         if (empty($mediaType) || $mediaType !== 'application/json') {
             return $response->withStatus(415);
         }
     }
     $response = $response->withHeader('Content-type', 'application/json');
     $response = $next($request, $response);
     return $response;
 }
 /**
  * Turns the JSON in the request object into an array
  */
 public function requestBodyJsonToArray(ServerRequestInterface $request) : array
 {
     $request->getBody()->rewind();
     $rawBodyContent = $request->getBody()->getContents();
     $urlDecodedContent = urldecode($rawBodyContent);
     $jsonDecodedContent = json_decode($urlDecodedContent, true);
     return $jsonDecodedContent ?: [];
 }
 /**
  * @return array
  */
 private function parseIncomingParams()
 {
     $parameters = [];
     $server = $this->request->getServerParams();
     $body = $this->request->getBody();
     $content_type = false;
     if (isset($server['CONTENT_TYPE'])) {
         $content_type = $server['CONTENT_TYPE'];
     }
     switch ($content_type) {
         case "application/json":
             $body_params = json_decode($body, true);
             if ($body_params) {
                 foreach ($body_params as $param_name => $param_value) {
                     $parameters[$param_name] = $param_value;
                 }
             }
             break;
         case "application/x-www-form-urlencoded":
             parse_str($body, $postvars);
             foreach ($postvars as $field => $value) {
                 $parameters[$field] = $value;
             }
             break;
     }
     return $parameters;
 }
示例#30
-1
文件: post.php 项目: rostmefpoter/bh
 public function execute(Request $req, Response $res, callable $next = null)
 {
     try {
         $name = urldecode($req->getAttribute('name'));
         $collection = $this->boot()->get('store')->getCollection($name);
         $contenttype = $req->getHeader('Content-Type');
         if (false !== strpos($contenttype[0], 'json')) {
             $body = json_decode($req->getBody(), true);
         } else {
             $body = $req->getParsedBody();
         }
         if (!$body) {
             throw new \RuntimeException('no request body');
         }
         $data = $collection->save($collection->item()->setData($body))->getData();
         if (isset($body['@labels'])) {
             foreach ($body['@labels'] as $label) {
                 $this->boot()->get('store')->setLabel($data['id'], $label['_label_name'], $label['_label_group'], true);
             }
         }
         if (isset($body['@properties'])) {
             foreach ($body['@properties'] as $property) {
                 $this->boot()->get('store')->setProperty($data['id'], $property['_property_name'], $property['_property_value'], $property['_property_idx'], $property['_property_type'], $property['_property_sys_type_id'], true);
             }
         }
         $data['@labels'] = $this->boot()->get('store')->getLabels($data['id']);
         $data['@properties'] = $this->boot()->get('store')->getProperties($data['id']);
         return $next($req, new JsonResponse(['status' => 'ok', 'data' => $data]));
     } catch (\Exception $ex) {
         return new JsonResponse(['status' => 'error', 'error' => $ex->getMessage()], 500);
     }
 }