in src/CloudEventFunctionWrapper.php [85:138]
public function execute(ServerRequestInterface $request): ResponseInterface
{
$body = (string) $request->getBody();
$eventType = $this->getEventType($request);
// We expect JSON if the content-type ends in "json" or if the event
// type is legacy or structured Cloud Event.
$shouldValidateJson = in_array($eventType, [
self::TYPE_LEGACY,
self::TYPE_STRUCTURED
]) || 'json' === substr($request->getHeaderLine('content-type'), -4);
if ($shouldValidateJson) {
$data = json_decode($body, true);
// Validate JSON, return 400 Bad Request on error
if (json_last_error() != JSON_ERROR_NONE) {
return new Response(400, [
self::FUNCTION_STATUS_HEADER => 'crash'
], sprintf(
'Could not parse CloudEvent: %s',
'' !== $body ? json_last_error_msg() : 'Missing cloudevent payload'
));
}
} else {
$data = $body;
}
switch ($this->getEventType($request)) {
case self::TYPE_LEGACY:
$mapper = new LegacyEventMapper();
$cloudevent = $mapper->fromJsonData($data, $request->getUri()->getPath());
break;
case self::TYPE_STRUCTURED:
$cloudevent = CloudEvent::fromArray($data);
break;
case self::TYPE_BINARY:
$cloudevent = $this->fromBinaryRequest($request, $data);
break;
default:
return new Response(400, [
self::FUNCTION_STATUS_HEADER => 'crash'
], 'invalid event type');
}
if ($this->marshalToCloudEventInterface) {
$cloudevent = new CloudEventSdkCompliant($cloudevent);
}
call_user_func($this->function, $cloudevent);
return new Response();
}