-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFromApiGateway.class.php
More file actions
executable file
·119 lines (103 loc) · 3.39 KB
/
Copy pathFromApiGateway.class.php
File metadata and controls
executable file
·119 lines (103 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php namespace com\amazon\aws\lambda;
use io\streams\MemoryInputStream;
use lang\IllegalArgumentException;
use web\io\{Input, Parts};
/**
* Input from Amazon AWS API Gateway version 2.0
*
* @test com.amazon.aws.lambda.unittest.FromApiGatewayTest
* @test com.amazon.aws.lambda.unittest.InvocationEventsTest
* @see https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
*/
class FromApiGateway implements Input {
private $event, $input;
/**
* Creates a new instance from a given event
*
* @param [:var] $event
* @throws lang.IllegalArgumentException for missing or unhandled versions
*/
public function __construct($event) {
if (!isset($event['version'])) {
throw new IllegalArgumentException('Cannot handle API gateway without version');
} else if ('2.0' !== $event['version']) {
throw new IllegalArgumentException('Cannot handle API gateway version '.$event['version']);
}
// Handle event body
if (!isset($event['body'])) {
$this->input= '';
} else if ($event['isBase64Encoded']) {
$this->input= base64_decode($event['body']);
} else {
$this->input= $event['body'];
}
$this->event= $event;
}
/** @return com.amazon.aws.lambda.RequestContext */
public function context() { return new RequestContext($this->event['requestContext']); }
/** @return string */
public function version() {
sscanf($this->event['requestContext']['http']['protocol'] ?? 'HTTP/1.1', "HTTP/%[^\r]", $version);
return $version;
}
/** @return string */
public function scheme() { return $this->event['headers']['x-forwarded-proto'] ?? 'http'; }
/** @return string */
public function method() { return $this->event['requestContext']['http']['method'] ?? 'GET'; }
/** @return string */
public function resource() {
return $this->event['rawQueryString']
? $this->event['rawPath'].'?'.$this->event['rawQueryString']
: $this->event['rawPath']
;
}
/** @return iterable */
public function headers() {
yield 'remote-addr' => $this->event['requestContext']['http']['sourceIp'] ?? '127.0.0.1';
yield from $this->event['headers'] ?? [];
if (isset($this->event['headers']['cookie']) || empty($this->event['cookies'])) return;
yield 'cookie' => implode('; ', $this->event['cookies']);
}
/** @return ?io.streams.InputStream */
public function incoming() {
return isset($this->event['body']) ? new MemoryInputStream($this->input) : null;
}
/** @return ?string */
public function readLine() {
if ('' === $this->input) return null;
$p= strpos($this->input, "\n");
if (false === $p) {
$line= $this->input;
$this->input= '';
} else {
$line= substr($this->input, 0, $p);
$this->input= substr($this->input, $p + 1);
}
return $line;
}
/**
* Reads a given number of bytes
*
* @param int $length Pass -1 to read all
* @return string
*/
public function read($length= -1) {
if (-1 === $length) {
$chunk= $this->input;
$this->input= '';
} else {
$chunk= substr($this->input, 0, $length);
$this->input= substr($this->input, $length);
}
return $chunk;
}
/**
* Returns parts from a multipart/form-data request
*
* @param string $boundary
* @return iterable
*/
public function parts($boundary) {
return new Parts($this->incoming(), $boundary);
}
}