Skip to content

Commit 1e27477

Browse files
author
Ranjan Dasgupta
authored
Merge pull request #783 from Besik13/feature/skip_port
Makes port additions during the handshake are configurable
2 parents f12b693 + 9a315e3 commit 1e27477

6 files changed

Lines changed: 66 additions & 40 deletions

File tree

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/MqttConnectOptions.java

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public class MqttConnectOptions {
7474
private int mqttVersion = MQTT_VERSION_DEFAULT;
7575
private boolean automaticReconnect = false;
7676
private int maxReconnectDelay = 128000;
77+
private boolean skipPortDuringHandshake = false;
7778
private Map<String, String> customWebSocketHeaders = null;
7879

7980
// Client Operation Parameters
@@ -100,7 +101,7 @@ public MqttConnectOptions() {
100101

101102
/**
102103
* Returns the password to use for the connection.
103-
*
104+
*
104105
* @return the password to use for the connection.
105106
*/
106107
public char[] getPassword() {
@@ -109,7 +110,7 @@ public char[] getPassword() {
109110

110111
/**
111112
* Sets the password to use for the connection.
112-
*
113+
*
113114
* @param password
114115
* A Char Array of the password
115116
*/
@@ -119,7 +120,7 @@ public void setPassword(char[] password) {
119120

120121
/**
121122
* Returns the user name to use for the connection.
122-
*
123+
*
123124
* @return the user name to use for the connection.
124125
*/
125126
public String getUserName() {
@@ -128,7 +129,7 @@ public String getUserName() {
128129

129130
/**
130131
* Sets the user name to use for the connection.
131-
*
132+
*
132133
* @param userName
133134
* The Username as a String
134135
*/
@@ -138,7 +139,7 @@ public void setUserName(String userName) {
138139

139140
/**
140141
* Get the maximum time (in millis) to wait between reconnects
141-
*
142+
*
142143
* @return Get the maximum time (in millis) to wait between reconnects
143144
*/
144145
public int getMaxReconnectDelay() {
@@ -147,7 +148,7 @@ public int getMaxReconnectDelay() {
147148

148149
/**
149150
* Set the maximum time to wait between reconnects
150-
*
151+
*
151152
* @param maxReconnectDelay
152153
* the duration (in millis)
153154
*/
@@ -207,7 +208,7 @@ private void validateWill(String dest, Object payload) {
207208

208209
/**
209210
* Sets up the will information, based on the supplied parameters.
210-
*
211+
*
211212
* @param topic
212213
* the topic to send the LWT message to
213214
* @param msg
@@ -228,7 +229,7 @@ protected void setWill(String topic, MqttMessage msg, int qos, boolean retained)
228229

229230
/**
230231
* Returns the "keep alive" interval.
231-
*
232+
*
232233
* @see #setKeepAliveInterval(int)
233234
* @return the keep alive interval.
234235
*/
@@ -238,7 +239,7 @@ public int getKeepAliveInterval() {
238239

239240
/**
240241
* Returns the MQTT version.
241-
*
242+
*
242243
* @see #setMqttVersion(int)
243244
* @return the MQTT version.
244245
*/
@@ -274,7 +275,7 @@ public void setKeepAliveInterval(int keepAliveInterval) throws IllegalArgumentEx
274275
/**
275276
* Returns the "max inflight". The max inflight limits to how many messages we
276277
* can send without receiving acknowledgments.
277-
*
278+
*
278279
* @see #setMaxInflight(int)
279280
* @return the max inflight
280281
*/
@@ -288,7 +289,7 @@ public int getMaxInflight() {
288289
* <p>
289290
* The default value is 10
290291
* </p>
291-
*
292+
*
292293
* @param maxInflight
293294
* the number of maxInfligt messages
294295
*/
@@ -301,7 +302,7 @@ public void setMaxInflight(int maxInflight) {
301302

302303
/**
303304
* Returns the connection timeout value.
304-
*
305+
*
305306
* @see #setConnectionTimeout(int)
306307
* @return the connection timeout value.
307308
*/
@@ -315,7 +316,7 @@ public int getConnectionTimeout() {
315316
* the MQTT server to be established. The default timeout is 30 seconds. A value
316317
* of 0 disables timeout processing meaning the client will wait until the
317318
* network connection is made successfully or fails.
318-
*
319+
*
319320
* @param connectionTimeout
320321
* the timeout value, measured in seconds. It must be &gt;0;
321322
*/
@@ -329,7 +330,7 @@ public void setConnectionTimeout(int connectionTimeout) {
329330
/**
330331
* Returns the socket factory that will be used when connecting, or
331332
* <code>null</code> if one has not been set.
332-
*
333+
*
333334
* @return The Socket Factory
334335
*/
335336
public SocketFactory getSocketFactory() {
@@ -341,7 +342,7 @@ public SocketFactory getSocketFactory() {
341342
* apply its own policies around the creation of network sockets. If using an
342343
* SSL connection, an <code>SSLSocketFactory</code> can be used to supply
343344
* application-specific security settings.
344-
*
345+
*
345346
* @param socketFactory
346347
* the factory to use.
347348
*/
@@ -351,7 +352,7 @@ public void setSocketFactory(SocketFactory socketFactory) {
351352

352353
/**
353354
* Returns the topic to be used for last will and testament (LWT).
354-
*
355+
*
355356
* @return the MqttTopic to use, or <code>null</code> if LWT is not set.
356357
* @see #setWill(MqttTopic, byte[], int, boolean)
357358
*/
@@ -363,7 +364,7 @@ public String getWillDestination() {
363364
* Returns the message to be sent as last will and testament (LWT). The returned
364365
* object is "read only". Calling any "setter" methods on the returned object
365366
* will result in an <code>IllegalStateException</code> being thrown.
366-
*
367+
*
367368
* @return the message to use, or <code>null</code> if LWT is not set.
368369
*/
369370
public MqttMessage getWillMessage() {
@@ -372,7 +373,7 @@ public MqttMessage getWillMessage() {
372373

373374
/**
374375
* Returns the SSL properties for the connection.
375-
*
376+
*
376377
* @return the properties for the SSL connection
377378
*/
378379
public Properties getSSLProperties() {
@@ -448,7 +449,7 @@ public Properties getSSLProperties() {
448449
* object instead of using the default algorithm available in the platform.
449450
* Example values: "PKIX" or "IBMJ9X509".</dd>
450451
* </dl>
451-
*
452+
*
452453
* @param props
453454
* The SSL {@link Properties}
454455
*/
@@ -466,7 +467,7 @@ public void setHttpsHostnameVerificationEnabled(boolean httpsHostnameVerificatio
466467

467468
/**
468469
* Returns the HostnameVerifier for the SSL connection.
469-
*
470+
*
470471
* @return the HostnameVerifier for the SSL connection
471472
*/
472473
public HostnameVerifier getSSLHostnameVerifier() {
@@ -480,7 +481,7 @@ public HostnameVerifier getSSLHostnameVerifier() {
480481
* <p>
481482
* There is no default HostnameVerifier
482483
* </p>
483-
*
484+
*
484485
* @param hostnameVerifier
485486
* the {@link HostnameVerifier}
486487
*/
@@ -491,7 +492,7 @@ public void setSSLHostnameVerifier(HostnameVerifier hostnameVerifier) {
491492
/**
492493
* Returns whether the client and server should remember state for the client
493494
* across reconnects.
494-
*
495+
*
495496
* @return the clean session flag
496497
*/
497498
public boolean isCleanSession() {
@@ -518,7 +519,7 @@ public boolean isCleanSession() {
518519
* <li>The server will treat a subscription as non-durable
519520
* </ul>
520521
* </ul>
521-
*
522+
*
522523
* @param cleanSession
523524
* Set to True to enable cleanSession
524525
*/
@@ -528,7 +529,7 @@ public void setCleanSession(boolean cleanSession) {
528529

529530
/**
530531
* Return a list of serverURIs the client may connect to
531-
*
532+
*
532533
* @return the serverURIs or null if not set
533534
*/
534535
public String[] getServerURIs() {
@@ -581,7 +582,7 @@ public String[] getServerURIs() {
581582
* </p>
582583
* </li>
583584
* </ol>
584-
*
585+
*
585586
* @param serverURIs
586587
* to be used by the client
587588
*/
@@ -616,7 +617,7 @@ public void setMqttVersion(int mqttVersion) throws IllegalArgumentException {
616617
/**
617618
* Returns whether the client will automatically attempt to reconnect to the
618619
* server if the connection is lost
619-
*
620+
*
620621
* @return the automatic reconnection flag.
621622
*/
622623
public boolean isAutomaticReconnect() {
@@ -635,14 +636,14 @@ public boolean isAutomaticReconnect() {
635636
* double until it is at 2 minutes at which point the delay will stay at 2
636637
* minutes.</li>
637638
* </ul>
638-
*
639+
*
639640
* @param automaticReconnect
640641
* If set to True, Automatic Reconnect will be enabled
641642
*/
642643
public void setAutomaticReconnect(boolean automaticReconnect) {
643644
this.automaticReconnect = automaticReconnect;
644645
}
645-
646+
646647
public int getExecutorServiceTimeout() {
647648
return executorServiceTimeout;
648649
}
@@ -651,13 +652,31 @@ public int getExecutorServiceTimeout() {
651652
* Set the time in seconds that the executor service should wait when
652653
* terminating before forcefully terminating. It is not recommended to change
653654
* this value unless you are absolutely sure that you need to.
654-
*
655+
*
655656
* @param executorServiceTimeout the time in seconds to wait when shutting down.Ï
656657
*/
657658
public void setExecutorServiceTimeout(int executorServiceTimeout) {
658659
this.executorServiceTimeout = executorServiceTimeout;
659660
}
660661

662+
/**
663+
* Returns whether to skip a port during a handshake
664+
*
665+
* @return skipPortDuringHandshake
666+
*/
667+
public boolean isSkipPortDuringHandshake() {
668+
return skipPortDuringHandshake;
669+
}
670+
671+
/**
672+
* Sets a flag that indicates whether to add a port to the host during a handshake
673+
*
674+
* @param skip if set to True, the port will not be added
675+
*/
676+
public void setSkipPortDuringHandshake(boolean skip) {
677+
this.skipPortDuringHandshake = skip;
678+
}
679+
661680
/**
662681
* @return The Debug Properties
663682
*/
@@ -680,6 +699,7 @@ public Properties getDebug() {
680699
} else {
681700
p.put("SSLProperties", getSSLProperties());
682701
}
702+
p.put("SkipPortDuringHandshake", isSkipPortDuringHandshake());
683703
return p;
684704
}
685705

@@ -700,5 +720,5 @@ public Map<String, String> getCustomWebSocketHeaders() {
700720
public String toString() {
701721
return Debug.dumpProperties(getDebug(), "Connection options");
702722
}
703-
723+
704724
}

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/websocket/WebSocketHandshake.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,23 @@ public class WebSocketHandshake {
5050
private static final String HTTP_HEADER_CONNECTION_VALUE = "upgrade";
5151
private static final String HTTP_HEADER_SEC_WEBSOCKET_PROTOCOL = "sec-websocket-protocol";
5252

53+
private final boolean skipPortDuringHandshake;
54+
5355
InputStream input;
5456
OutputStream output;
5557
String uri;
5658
String host;
5759
int port;
5860
Map<String, String> customWebSocketHeaders;
5961

60-
public WebSocketHandshake(InputStream input, OutputStream output, String uri, String host, int port, Map<String, String> customWebSocketHeaders){
62+
public WebSocketHandshake(InputStream input, OutputStream output, String uri, String host, int port, Map<String, String> customWebSocketHeaders, boolean skipPortDuringHandshake){
6163
this.input = input;
6264
this.output = output;
6365
this.uri = uri;
6466
this.host = host;
6567
this.port = port;
6668
this.customWebSocketHeaders = customWebSocketHeaders;
69+
this.skipPortDuringHandshake = skipPortDuringHandshake;
6770
}
6871

6972

@@ -99,7 +102,7 @@ private void sendHandshakeRequest(String key) throws IOException{
99102

100103
PrintWriter pw = new PrintWriter(output);
101104
pw.print("GET " + path + " HTTP/1.1" + LINE_SEPARATOR);
102-
if (port != 80) {
105+
if (port != 80 && !skipPortDuringHandshake) {
103106
pw.print("Host: " + host + ":" + port + LINE_SEPARATOR);
104107
}
105108
else {

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/websocket/WebSocketNetworkModule.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class WebSocketNetworkModule extends TCPNetworkModule {
4141
private Map<String, String> customWebsocketHeaders;
4242
private PipedInputStream pipedInputStream;
4343
private WebSocketReceiver webSocketReceiver;
44+
private final boolean skipPortDuringHandshake;
4445
ByteBuffer recievedPayload;
4546

4647
/**
@@ -49,21 +50,21 @@ public class WebSocketNetworkModule extends TCPNetworkModule {
4950
* Frame before passing it through to the real socket.
5051
*/
5152
private ByteArrayOutputStream outputStream = new ExtendedByteArrayOutputStream(this);
52-
53-
public WebSocketNetworkModule(SocketFactory factory, String uri, String host, int port, String resourceContext, Map<String, String> customWebsocketHeaders){
53+
54+
public WebSocketNetworkModule(SocketFactory factory, String uri, String host, int port, String resourceContext, Map<String, String> customWebsocketHeaders, boolean skipPortDuringHandshake){
5455
super(factory, host, port, resourceContext);
5556
this.uri = uri;
5657
this.host = host;
5758
this.port = port;
5859
this.customWebsocketHeaders = customWebsocketHeaders;
5960
this.pipedInputStream = new PipedInputStream();
60-
61+
this.skipPortDuringHandshake = skipPortDuringHandshake;
6162
log.setResourceName(resourceContext);
6263
}
6364

6465
public void start() throws IOException, MqttException {
6566
super.start();
66-
WebSocketHandshake handshake = new WebSocketHandshake(getSocketInputStream(), getSocketOutputStream(), uri, host, port, customWebsocketHeaders);
67+
WebSocketHandshake handshake = new WebSocketHandshake(getSocketInputStream(), getSocketOutputStream(), uri, host, port, customWebsocketHeaders, skipPortDuringHandshake);
6768
handshake.execute();
6869
this.webSocketReceiver = new WebSocketReceiver(getSocketInputStream(), pipedInputStream);
6970
webSocketReceiver.start("webSocketReceiver");

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/websocket/WebSocketNetworkModuleFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public NetworkModule createNetworkModule(URI brokerUri, MqttConnectOptions optio
5252
throw ExceptionHelper.createMqttException(MqttException.REASON_CODE_SOCKET_FACTORY_MISMATCH);
5353
}
5454
WebSocketNetworkModule netModule = new WebSocketNetworkModule(factory, brokerUri.toString(), host, port,
55-
clientId, options.getCustomWebSocketHeaders());
55+
clientId, options.getCustomWebSocketHeaders(), options.isSkipPortDuringHandshake());
5656
netModule.setConnectTimeout(options.getConnectionTimeout());
5757
return netModule;
5858
}

0 commit comments

Comments
 (0)