Skip to content

Commit fbf9544

Browse files
committed
Added the ability to not specify the host port during a web socket handshake
Signed-off-by: Yehor Beskhmelnytsyn <egor.besik@gmail.com>
1 parent d11139f commit fbf9544

6 files changed

Lines changed: 65 additions & 39 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 boolean automaticReconnect = false;
7575
private int maxReconnectDelay = 128000;
7676
private Properties customWebSocketHeaders = null;
77+
private boolean skipPortDuringHandshake = false;
7778

7879
// Client Operation Parameters
7980
private int executorServiceTimeout = 1; // How long to wait in seconds when terminating the executor service.
@@ -99,7 +100,7 @@ public MqttConnectOptions() {
99100

100101
/**
101102
* Returns the password to use for the connection.
102-
*
103+
*
103104
* @return the password to use for the connection.
104105
*/
105106
public char[] getPassword() {
@@ -108,7 +109,7 @@ public char[] getPassword() {
108109

109110
/**
110111
* Sets the password to use for the connection.
111-
*
112+
*
112113
* @param password
113114
* A Char Array of the password
114115
*/
@@ -118,7 +119,7 @@ public void setPassword(char[] password) {
118119

119120
/**
120121
* Returns the user name to use for the connection.
121-
*
122+
*
122123
* @return the user name to use for the connection.
123124
*/
124125
public String getUserName() {
@@ -127,7 +128,7 @@ public String getUserName() {
127128

128129
/**
129130
* Sets the user name to use for the connection.
130-
*
131+
*
131132
* @param userName
132133
* The Username as a String
133134
*/
@@ -137,7 +138,7 @@ public void setUserName(String userName) {
137138

138139
/**
139140
* Get the maximum time (in millis) to wait between reconnects
140-
*
141+
*
141142
* @return Get the maximum time (in millis) to wait between reconnects
142143
*/
143144
public int getMaxReconnectDelay() {
@@ -146,7 +147,7 @@ public int getMaxReconnectDelay() {
146147

147148
/**
148149
* Set the maximum time to wait between reconnects
149-
*
150+
*
150151
* @param maxReconnectDelay
151152
* the duration (in millis)
152153
*/
@@ -206,7 +207,7 @@ private void validateWill(String dest, Object payload) {
206207

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

228229
/**
229230
* Returns the "keep alive" interval.
230-
*
231+
*
231232
* @see #setKeepAliveInterval(int)
232233
* @return the keep alive interval.
233234
*/
@@ -237,7 +238,7 @@ public int getKeepAliveInterval() {
237238

238239
/**
239240
* Returns the MQTT version.
240-
*
241+
*
241242
* @see #setMqttVersion(int)
242243
* @return the MQTT version.
243244
*/
@@ -273,7 +274,7 @@ public void setKeepAliveInterval(int keepAliveInterval) throws IllegalArgumentEx
273274
/**
274275
* Returns the "max inflight". The max inflight limits to how many messages we
275276
* can send without receiving acknowledgments.
276-
*
277+
*
277278
* @see #setMaxInflight(int)
278279
* @return the max inflight
279280
*/
@@ -287,7 +288,7 @@ public int getMaxInflight() {
287288
* <p>
288289
* The default value is 10
289290
* </p>
290-
*
291+
*
291292
* @param maxInflight
292293
* the number of maxInfligt messages
293294
*/
@@ -300,7 +301,7 @@ public void setMaxInflight(int maxInflight) {
300301

301302
/**
302303
* Returns the connection timeout value.
303-
*
304+
*
304305
* @see #setConnectionTimeout(int)
305306
* @return the connection timeout value.
306307
*/
@@ -314,7 +315,7 @@ public int getConnectionTimeout() {
314315
* the MQTT server to be established. The default timeout is 30 seconds. A value
315316
* of 0 disables timeout processing meaning the client will wait until the
316317
* network connection is made successfully or fails.
317-
*
318+
*
318319
* @param connectionTimeout
319320
* the timeout value, measured in seconds. It must be &gt;0;
320321
*/
@@ -328,7 +329,7 @@ public void setConnectionTimeout(int connectionTimeout) {
328329
/**
329330
* Returns the socket factory that will be used when connecting, or
330331
* <code>null</code> if one has not been set.
331-
*
332+
*
332333
* @return The Socket Factory
333334
*/
334335
public SocketFactory getSocketFactory() {
@@ -340,7 +341,7 @@ public SocketFactory getSocketFactory() {
340341
* apply its own policies around the creation of network sockets. If using an
341342
* SSL connection, an <code>SSLSocketFactory</code> can be used to supply
342343
* application-specific security settings.
343-
*
344+
*
344345
* @param socketFactory
345346
* the factory to use.
346347
*/
@@ -350,7 +351,7 @@ public void setSocketFactory(SocketFactory socketFactory) {
350351

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

372373
/**
373374
* Returns the SSL properties for the connection.
374-
*
375+
*
375376
* @return the properties for the SSL connection
376377
*/
377378
public Properties getSSLProperties() {
@@ -447,7 +448,7 @@ public Properties getSSLProperties() {
447448
* object instead of using the default algorithm available in the platform.
448449
* Example values: "PKIX" or "IBMJ9X509".</dd>
449450
* </dl>
450-
*
451+
*
451452
* @param props
452453
* The SSL {@link Properties}
453454
*/
@@ -465,7 +466,7 @@ public void setHttpsHostnameVerificationEnabled(boolean httpsHostnameVerificatio
465466

466467
/**
467468
* Returns the HostnameVerifier for the SSL connection.
468-
*
469+
*
469470
* @return the HostnameVerifier for the SSL connection
470471
*/
471472
public HostnameVerifier getSSLHostnameVerifier() {
@@ -479,7 +480,7 @@ public HostnameVerifier getSSLHostnameVerifier() {
479480
* <p>
480481
* There is no default HostnameVerifier
481482
* </p>
482-
*
483+
*
483484
* @param hostnameVerifier
484485
* the {@link HostnameVerifier}
485486
*/
@@ -490,7 +491,7 @@ public void setSSLHostnameVerifier(HostnameVerifier hostnameVerifier) {
490491
/**
491492
* Returns whether the client and server should remember state for the client
492493
* across reconnects.
493-
*
494+
*
494495
* @return the clean session flag
495496
*/
496497
public boolean isCleanSession() {
@@ -517,7 +518,7 @@ public boolean isCleanSession() {
517518
* <li>The server will treat a subscription as non-durable
518519
* </ul>
519520
* </ul>
520-
*
521+
*
521522
* @param cleanSession
522523
* Set to True to enable cleanSession
523524
*/
@@ -527,7 +528,7 @@ public void setCleanSession(boolean cleanSession) {
527528

528529
/**
529530
* Return a list of serverURIs the client may connect to
530-
*
531+
*
531532
* @return the serverURIs or null if not set
532533
*/
533534
public String[] getServerURIs() {
@@ -580,7 +581,7 @@ public String[] getServerURIs() {
580581
* </p>
581582
* </li>
582583
* </ol>
583-
*
584+
*
584585
* @param serverURIs
585586
* to be used by the client
586587
*/
@@ -615,7 +616,7 @@ public void setMqttVersion(int mqttVersion) throws IllegalArgumentException {
615616
/**
616617
* Returns whether the client will automatically attempt to reconnect to the
617618
* server if the connection is lost
618-
*
619+
*
619620
* @return the automatic reconnection flag.
620621
*/
621622
public boolean isAutomaticReconnect() {
@@ -634,14 +635,14 @@ public boolean isAutomaticReconnect() {
634635
* double until it is at 2 minutes at which point the delay will stay at 2
635636
* minutes.</li>
636637
* </ul>
637-
*
638+
*
638639
* @param automaticReconnect
639640
* If set to True, Automatic Reconnect will be enabled
640641
*/
641642
public void setAutomaticReconnect(boolean automaticReconnect) {
642643
this.automaticReconnect = automaticReconnect;
643644
}
644-
645+
645646
public int getExecutorServiceTimeout() {
646647
return executorServiceTimeout;
647648
}
@@ -650,13 +651,31 @@ public int getExecutorServiceTimeout() {
650651
* Set the time in seconds that the executor service should wait when
651652
* terminating before forcefully terminating. It is not recommended to change
652653
* this value unless you are absolutely sure that you need to.
653-
*
654+
*
654655
* @param executorServiceTimeout the time in seconds to wait when shutting down.Ï
655656
*/
656657
public void setExecutorServiceTimeout(int executorServiceTimeout) {
657658
this.executorServiceTimeout = executorServiceTimeout;
658659
}
659660

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

@@ -700,5 +720,5 @@ public Properties 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
Properties customWebSocketHeaders;
5961

60-
public WebSocketHandshake(InputStream input, OutputStream output, String uri, String host, int port, Properties customWebSocketHeaders){
62+
public WebSocketHandshake(InputStream input, OutputStream output, String uri, String host, int port, Properties 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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class WebSocketNetworkModule extends TCPNetworkModule {
4141
private Properties customWebsocketHeaders;
4242
private PipedInputStream pipedInputStream;
4343
private WebSocketReceiver webSocketReceiver;
44+
private final boolean skipPortDuringHandshake;
4445
ByteBuffer recievedPayload;
4546

4647
/**
@@ -50,20 +51,20 @@ public class WebSocketNetworkModule extends TCPNetworkModule {
5051
*/
5152
private ByteArrayOutputStream outputStream = new ExtendedByteArrayOutputStream(this);
5253

53-
public WebSocketNetworkModule(SocketFactory factory, String uri, String host, int port, String resourceContext, Properties customWebsocketHeaders){
54+
public WebSocketNetworkModule(SocketFactory factory, String uri, String host, int port, String resourceContext, Properties 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)