From 0c9db2abafee75c91dd3d0a0b3003e9090b98613 Mon Sep 17 00:00:00 2001
From: Sheng <webmaster0115@gmail.com>
Date: Wed, 10 Oct 2018 22:30:37 +0800
Subject: [PATCH] Set default port 22 on server side

---
 tests/test_app.py        |  8 --------
 webssh/handler.py        | 12 ++++++++----
 webssh/static/js/main.js |  8 ++------
 3 files changed, 10 insertions(+), 18 deletions(-)

diff --git a/tests/test_app.py b/tests/test_app.py
index 85c99bf..427b50d 100644
--- a/tests/test_app.py
+++ b/tests/test_app.py
@@ -92,10 +92,6 @@ class TestAppBasic(AsyncHTTPTestCase):
         response = self.sync_post(body)
         self.assert_response(b'Missing argument hostname', response)
 
-        body = 'hostname=127.0.0.1&username=admin&password&_xsrf=yummy'
-        response = self.sync_post(body)
-        self.assert_response(b'Missing argument port', response)
-
         body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy'
         response = self.sync_post(body)
         self.assert_response(b'Missing argument username', response)
@@ -104,10 +100,6 @@ class TestAppBasic(AsyncHTTPTestCase):
         response = self.sync_post(body)
         self.assert_response(b'Missing value hostname', response)
 
-        body = 'hostname=127.0.0.1&port=&username=&password&_xsrf=yummy'
-        response = self.sync_post(body)
-        self.assert_response(b'Missing value port', response)
-
         body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy'
         response = self.sync_post(body)
         self.assert_response(b'Missing value username', response)
diff --git a/webssh/handler.py b/webssh/handler.py
index 879527c..56775d1 100644
--- a/webssh/handler.py
+++ b/webssh/handler.py
@@ -30,6 +30,7 @@ except ImportError:
 
 DELAY = 3
 KEY_MAX_SIZE = 16384
+DEFAULT_PORT = 22
 
 
 class InvalidValueError(Exception):
@@ -154,11 +155,14 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
         return value
 
     def get_port(self):
-        value = self.get_value('port')
+        value = self.get_argument('port', u'')
+        if not value:
+            return DEFAULT_PORT
+
         port = to_int(value)
-        if port and is_valid_port(port):
-            return port
-        raise InvalidValueError('Invalid port: {}'.format(value))
+        if port is None or not is_valid_port(port):
+            raise InvalidValueError('Invalid port: {}'.format(value))
+        return port
 
     def lookup_hostname(self, hostname, port):
         key = hostname if port == 22 else '[{}]:{}'.format(hostname, port)
diff --git a/webssh/static/js/main.js b/webssh/static/js/main.js
index cb83713..02b1a78 100644
--- a/webssh/static/js/main.js
+++ b/webssh/static/js/main.js
@@ -387,11 +387,7 @@ jQuery(function($){
       attr = attrs[i];
       val = data.get(attr);
       if (typeof val === 'string') {
-        val = val.trim();
-        if (attr === 'port' && val === '') {
-          val = 22;
-        }
-        data.set(attr, val);
+        data.set(attr, val.trim());
       }
     }
   }
@@ -410,7 +406,7 @@ jQuery(function($){
     if (!hostname) {
       msg = 'Need value hostname';
     } else if (!port) {
-      msg = 'Need value port';
+      msg = '';
     } else if (!username) {
       msg = 'Need value username';
     } else if (!hostname_tester.test(hostname)) {