Skip to content

Commit d1c34f4

Browse files
authored
Merge pull request #314 from jasongrout/termsocket
Add TermSocket back from nbclassic
2 parents b5f3fdd + 5499476 commit d1c34f4

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
## [1.0] - 2020-9-18
1111

12-
## Added.
12+
### Added.
1313

1414
* Added a basic, styled `login.html` template. ([220](https://github.com/jupyter/jupyter_server/pull/220), [295](https://github.com/jupyter/jupyter_server/pull/295))
1515
* Added new extension manager API for handling server extensions. ([248](https://github.com/jupyter/jupyter_server/pull/248), [265](https://github.com/jupyter/jupyter_server/pull/265), [275](https://github.com/jupyter/jupyter_server/pull/275), [303](https://github.com/jupyter/jupyter_server/pull/303))
1616
* The favicon and Jupyter logo are now available under jupyter_server's static namespace. ([284](https://github.com/jupyter/jupyter_server/pull/284))
1717

18-
## Changed.
18+
### Changed.
1919

2020
* `load_jupyter_server_extension` should be renamed to `_load_jupyter_server_extension` in server extensions. Server now throws a warning when the old name is used. ([213](https://github.com/jupyter/jupyter_server/pull/213))
2121
* Docs for server extensions now recommend using `authenticated` decorator for handlers. ([219](https://github.com/jupyter/jupyter_server/pull/219))
@@ -25,11 +25,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525
* Dropped support for Python 3.5. ([296](https://github.com/jupyter/jupyter_server/pull/296))
2626
* Made the `config_dir_name` trait configurable in `ConfigManager`. ([297](https://github.com/jupyter/jupyter_server/pull/297))
2727

28-
## Removed for now removed features.
28+
### Removed for now removed features.
2929

3030
* Removed ipykernel as a dependency of jupyter_server. ([255](https://github.com/jupyter/jupyter_server/pull/255))
3131

32-
## Fixed for any bug fixes.
32+
### Fixed for any bug fixes.
3333
* Prevent a re-definition of prometheus metrics if `notebook` package already imports them. ([#210](https://github.com/jupyter/jupyter_server/pull/210))
3434
* Fixed `terminals` REST API unit tests that weren't shutting down properly. ([221](https://github.com/jupyter/jupyter_server/pull/221))
3535
* Fixed jupyter_server on Windows for Python < 3.7. Added patch to handle subprocess cleanup. ([240](https://github.com/jupyter/jupyter_server/pull/240))

jupyter_server/terminal/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from tornado.log import app_log
1212
from jupyter_server.utils import url_path_join as ujoin
1313
from . import api_handlers
14+
from .handlers import TermSocket
1415

1516

1617
def initialize(webapp, root_dir, connection_url, settings):
@@ -33,6 +34,8 @@ def initialize(webapp, root_dir, connection_url, settings):
3334
terminal_manager.log = app_log
3435
base_url = webapp.settings['base_url']
3536
handlers = [
37+
(ujoin(base_url, r"/terminals/websocket/(\w+)"), TermSocket,
38+
{'term_manager': terminal_manager}),
3639
(ujoin(base_url, r"/api/terminals"), api_handlers.TerminalRootHandler),
3740
(ujoin(base_url, r"/api/terminals/(\w+)"), api_handlers.TerminalHandler),
3841
]

jupyter_server/terminal/handlers.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#encoding: utf-8
2+
"""Tornado handlers for the terminal emulator."""
3+
4+
# Copyright (c) Jupyter Development Team.
5+
# Distributed under the terms of the Modified BSD License.
6+
7+
from tornado import web
8+
import terminado
9+
from jupyter_server._tz import utcnow
10+
from ..base.handlers import JupyterHandler
11+
from ..base.zmqhandlers import WebSocketMixin
12+
13+
14+
class TermSocket(WebSocketMixin, JupyterHandler, terminado.TermSocket):
15+
16+
def origin_check(self):
17+
"""Terminado adds redundant origin_check
18+
Tornado already calls check_origin, so don't do anything here.
19+
"""
20+
return True
21+
22+
def get(self, *args, **kwargs):
23+
if not self.get_current_user():
24+
raise web.HTTPError(403)
25+
return super(TermSocket, self).get(*args, **kwargs)
26+
27+
def on_message(self, message):
28+
super(TermSocket, self).on_message(message)
29+
self.application.settings['terminal_last_activity'] = utcnow()
30+
31+
def write_message(self, message, binary=False):
32+
super(TermSocket, self).write_message(message, binary=binary)
33+
self.application.settings['terminal_last_activity'] = utcnow()

tests/test_terminal.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,37 @@ async def test_terminal_create_with_kwargs(fetch, ws_fetch, terminal_path, kill_
7070

7171
assert data['name'] == term_name
7272
await kill_all()
73+
74+
75+
async def test_terminal_create_with_cwd(fetch, ws_fetch, terminal_path):
76+
resp = await fetch(
77+
'api', 'terminals',
78+
method='POST',
79+
body=json.dumps({'cwd': str(terminal_path)}),
80+
allow_nonstandard_methods=True,
81+
)
82+
83+
data = json.loads(resp.body.decode())
84+
term_name = data['name']
85+
86+
ws = await ws_fetch(
87+
'terminals', 'websocket', term_name
88+
)
89+
90+
ws.write_message(json.dumps(['stdin', 'pwd\r\n']))
91+
92+
message_stdout = ''
93+
while True:
94+
try:
95+
message = await asyncio.wait_for(ws.read_message(), timeout=1.0)
96+
except asyncio.TimeoutError:
97+
break
98+
99+
message = json.loads(message)
100+
101+
if message[0] == 'stdout':
102+
message_stdout += message[1]
103+
104+
ws.close()
105+
106+
assert str(terminal_path) in message_stdout

0 commit comments

Comments
 (0)