|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* soluble-japha / PHPJavaBridge driver client. |
|
7
|
|
|
* |
|
8
|
|
|
* Refactored version of phpjababridge's Java.inc file compatible |
|
9
|
|
|
* with php java bridge 6.2 |
|
10
|
|
|
* |
|
11
|
|
|
* |
|
12
|
|
|
* @credits http://php-java-bridge.sourceforge.net/pjb/ |
|
13
|
|
|
* |
|
14
|
|
|
* @see http://github.com/belgattitude/soluble-japha |
|
15
|
|
|
* |
|
16
|
|
|
* @author Jost Boekemeier |
|
17
|
|
|
* @author Vanvelthem Sébastien (refactoring and fixes from original implementation) |
|
18
|
|
|
* @license MIT |
|
19
|
|
|
* |
|
20
|
|
|
* The MIT License (MIT) |
|
21
|
|
|
* Copyright (c) 2014-2017 Jost Boekemeier |
|
22
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
23
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
24
|
|
|
* in the Software without restriction, including without limitation the rights |
|
25
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
26
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
27
|
|
|
* furnished to do so, subject to the following conditions: |
|
28
|
|
|
* |
|
29
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
30
|
|
|
* all copies or substantial portions of the Software. |
|
31
|
|
|
* |
|
32
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
33
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
34
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
35
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
36
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
37
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
38
|
|
|
* THE SOFTWARE. |
|
39
|
|
|
*/ |
|
40
|
|
|
|
|
41
|
|
|
namespace Soluble\Japha\Bridge\Driver\Pjb62; |
|
42
|
|
|
|
|
43
|
|
|
use Soluble\Japha\Bridge\Exception\ConnectionException; |
|
44
|
|
|
use Soluble\Japha\Bridge\Socket\StreamSocket; |
|
45
|
|
|
|
|
46
|
|
|
class SimpleHttpHandler extends SocketHandler |
|
47
|
|
|
{ |
|
48
|
|
|
public $headers; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var array |
|
52
|
|
|
*/ |
|
53
|
|
|
public $cookies; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
public $context; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
public $ssl; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var int |
|
67
|
|
|
*/ |
|
68
|
|
|
public $port; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var string |
|
72
|
|
|
*/ |
|
73
|
|
|
public $host; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var array |
|
77
|
|
|
*/ |
|
78
|
|
|
protected $cachedValues = []; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @var string |
|
82
|
|
|
*/ |
|
83
|
|
|
protected $java_servlet; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @var int |
|
87
|
|
|
*/ |
|
88
|
|
|
protected $java_recv_size; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @var int |
|
92
|
|
|
*/ |
|
93
|
|
|
protected $java_send_size; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @throws Exception\IllegalStateException on channel creation error |
|
97
|
|
|
* |
|
98
|
|
|
* @param Protocol $protocol |
|
99
|
|
|
* @param string $ssl |
|
100
|
|
|
* @param string $host |
|
101
|
|
|
* @param int $port |
|
102
|
|
|
* @param string $java_servlet |
|
103
|
|
|
* @param int $java_recv_size |
|
104
|
|
|
* @param int $java_send_size |
|
105
|
|
|
*/ |
|
106
|
30 |
|
public function __construct(Protocol $protocol, $ssl, $host, $port, $java_servlet, $java_recv_size, $java_send_size) |
|
107
|
|
|
{ |
|
108
|
30 |
|
$this->cookies = []; |
|
109
|
30 |
|
$this->protocol = $protocol; |
|
110
|
30 |
|
$this->ssl = $ssl; |
|
111
|
30 |
|
$this->host = $host; |
|
112
|
30 |
|
$this->port = $port; |
|
113
|
30 |
|
$this->java_servlet = $java_servlet; |
|
114
|
|
|
|
|
115
|
30 |
|
$this->java_send_size = $java_send_size; |
|
116
|
30 |
|
$this->java_recv_size = $java_recv_size; |
|
117
|
|
|
|
|
118
|
30 |
|
$this->cachedValues = [ |
|
119
|
|
|
'getContext' => null |
|
120
|
|
|
]; |
|
121
|
30 |
|
$this->createChannel(); |
|
122
|
30 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @throws Exception\IllegalStateException on channel creation error |
|
126
|
|
|
* @throws Exception\BrokenConnectionException if all goes wrong |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function createChannel() |
|
129
|
|
|
{ |
|
130
|
|
|
$channelName = Pjb62Driver::getJavaBridgeHeader('X_JAVABRIDGE_REDIRECT', $_SERVER); |
|
131
|
|
|
$context = Pjb62Driver::getJavaBridgeHeader('X_JAVABRIDGE_CONTEXT', $_SERVER); |
|
132
|
|
|
$len = strlen($context); |
|
133
|
|
|
$len0 = PjbProxyClient::getInstance()->getCompatibilityOption($this->protocol->client); |
|
134
|
|
|
$len1 = chr($len & 0xFF); |
|
135
|
|
|
$len >>= 8; |
|
136
|
|
|
$len2 = chr($len & 0xFF); |
|
137
|
|
|
$this->channel = $this->getChannel($channelName); |
|
138
|
|
|
$this->protocol->setSocketHandler(new SocketHandler($this->protocol, $this->channel)); |
|
139
|
|
|
$this->protocol->write("\177${len0}${len1}${len2}${context}"); |
|
140
|
|
|
$this->context = sprintf("X_JAVABRIDGE_CONTEXT: %s\r\n", $context); |
|
141
|
|
|
$this->protocol->handler = $this->protocol->getSocketHandler(); |
|
142
|
|
|
|
|
143
|
|
|
if ($this->protocol->client->sendBuffer !== null) { |
|
144
|
|
|
$ret = $this->protocol->handler->write($this->protocol->client->sendBuffer); |
|
145
|
|
|
if ($ret === null) { |
|
146
|
|
|
$this->protocol->handler->shutdownBrokenConnection('Broken local connection handle'); |
|
147
|
|
|
} |
|
148
|
|
|
$this->protocol->client->sendBuffer = null; |
|
149
|
|
|
$this->protocol->handler->read(1); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function getContextFromCgiEnvironment(): string |
|
154
|
|
|
{ |
|
155
|
|
|
return Pjb62Driver::getJavaBridgeHeader('X_JAVABRIDGE_CONTEXT', $_SERVER); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @return string |
|
160
|
|
|
*/ |
|
161
|
28 |
|
public function getContext() |
|
162
|
|
|
{ |
|
163
|
28 |
|
if (!array_key_exists('getContext', $this->cachedValues)) { |
|
164
|
|
|
$ctx = $this->getContextFromCgiEnvironment(); |
|
165
|
|
|
if ($ctx) { |
|
166
|
|
|
$this->cachedValues['getContext'] = sprintf('X_JAVABRIDGE_CONTEXT: %s', $ctx); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
28 |
|
return $this->cachedValues['getContext']; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
28 |
|
public function getWebAppInternal() |
|
174
|
|
|
{ |
|
175
|
28 |
|
$context = $this->protocol->webContext; |
|
176
|
28 |
|
if (isset($context)) { |
|
177
|
|
|
return $context; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
28 |
|
return ($this->java_servlet == 'User' && |
|
181
|
28 |
|
array_key_exists('PHP_SELF', $_SERVER) && |
|
182
|
28 |
|
array_key_exists('HTTP_HOST', $_SERVER)) ? $_SERVER['PHP_SELF'].'javabridge' : null; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
28 |
|
public function getWebApp(): string |
|
186
|
|
|
{ |
|
187
|
28 |
|
$context = $this->getWebAppInternal(); |
|
188
|
28 |
|
if (null === $context) { |
|
189
|
28 |
|
$context = $this->java_servlet; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
28 |
|
return $context; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @throws Exception\BrokenConnectionException |
|
197
|
|
|
*/ |
|
198
|
|
|
public function write(string $data): ?int |
|
199
|
|
|
{ |
|
200
|
|
|
return $this->protocol->getSocketHandler()->write($data); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function doSetCookie($key, $val, $path) |
|
204
|
|
|
{ |
|
205
|
|
|
$path = trim($path); |
|
206
|
|
|
$webapp = $this->getWebAppInternal(); |
|
207
|
|
|
if (!$webapp) { |
|
208
|
|
|
$path = '/'; |
|
209
|
|
|
} |
|
210
|
|
|
setcookie($key, $val, 0, $path); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @throws Exception\BrokenConnectionException |
|
215
|
|
|
*/ |
|
216
|
|
|
public function read(int $size): string |
|
217
|
|
|
{ |
|
218
|
|
|
return $this->protocol->getSocketHandler()->read($size); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @param string $channelName generally the tcp port on which to connect |
|
223
|
|
|
* |
|
224
|
|
|
* @return SocketChannelP |
|
225
|
|
|
* |
|
226
|
|
|
* @throws Exception\ConnectException |
|
227
|
|
|
*/ |
|
228
|
9 |
|
public function getChannel(string $channelName): SocketChannelP |
|
229
|
|
|
{ |
|
230
|
9 |
|
$persistent = $this->protocol->client->getParam(Client::PARAM_USE_PERSISTENT_CONNECTION); |
|
231
|
|
|
try { |
|
232
|
9 |
|
$streamSocket = new StreamSocket( |
|
233
|
9 |
|
$this->ssl === 'ssl://' ? StreamSocket::TRANSPORT_SSL : StreamSocket::TRANSPORT_TCP, |
|
234
|
9 |
|
$this->host.':'.$channelName, |
|
235
|
9 |
|
null, |
|
236
|
9 |
|
[], |
|
237
|
9 |
|
$persistent |
|
238
|
|
|
); |
|
239
|
9 |
|
$socket = $streamSocket->getSocket(); |
|
240
|
|
|
} catch (\Throwable $e) { |
|
241
|
|
|
$logger = $this->protocol->getClient()->getLogger(); |
|
242
|
|
|
$logger->critical(sprintf( |
|
243
|
|
|
'[soluble-japha] %s (%s)', |
|
244
|
|
|
$e->getMessage(), |
|
245
|
|
|
__METHOD__ |
|
246
|
|
|
)); |
|
247
|
|
|
throw new ConnectionException($e->getMessage(), $e->getCode()); |
|
248
|
|
|
} |
|
249
|
9 |
|
stream_set_timeout($socket, -1); |
|
250
|
|
|
|
|
251
|
9 |
|
return new SocketChannelP($socket, $this->host, $this->java_recv_size, $this->java_send_size); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
17 |
|
public function keepAlive(): void |
|
255
|
|
|
{ |
|
256
|
17 |
|
parent::keepAlive(); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
public function redirect(): void |
|
260
|
|
|
{ |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|