Skip to content

Commit

Permalink
Merge pull request jupyter-server#314 from jasongrout/termsocket
Browse files Browse the repository at this point in the history
Add TermSocket back from nbclassic
  • Loading branch information
jasongrout authored Sep 29, 2020
2 parents 46040ad + f38d1f5 commit db4c373
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
3 changes: 3 additions & 0 deletions jupyter_server/terminal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from tornado.log import app_log
from jupyter_server.utils import url_path_join as ujoin
from . import api_handlers
from .handlers import TermSocket


def initialize(webapp, root_dir, connection_url, settings):
Expand All @@ -33,6 +34,8 @@ def initialize(webapp, root_dir, connection_url, settings):
terminal_manager.log = app_log
base_url = webapp.settings['base_url']
handlers = [
(ujoin(base_url, r"/terminals/websocket/(\w+)"), TermSocket,
{'term_manager': terminal_manager}),
(ujoin(base_url, r"/api/terminals"), api_handlers.TerminalRootHandler),
(ujoin(base_url, r"/api/terminals/(\w+)"), api_handlers.TerminalHandler),
]
Expand Down
33 changes: 33 additions & 0 deletions jupyter_server/terminal/handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#encoding: utf-8
"""Tornado handlers for the terminal emulator."""

# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from tornado import web
import terminado
from jupyter_server._tz import utcnow
from ..base.handlers import JupyterHandler
from ..base.zmqhandlers import WebSocketMixin


class TermSocket(WebSocketMixin, JupyterHandler, terminado.TermSocket):

def origin_check(self):
"""Terminado adds redundant origin_check
Tornado already calls check_origin, so don't do anything here.
"""
return True

def get(self, *args, **kwargs):
if not self.get_current_user():
raise web.HTTPError(403)
return super(TermSocket, self).get(*args, **kwargs)

def on_message(self, message):
super(TermSocket, self).on_message(message)
self.application.settings['terminal_last_activity'] = utcnow()

def write_message(self, message, binary=False):
super(TermSocket, self).write_message(message, binary=binary)
self.application.settings['terminal_last_activity'] = utcnow()
34 changes: 34 additions & 0 deletions tests/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,37 @@ async def test_terminal_create_with_kwargs(fetch, ws_fetch, terminal_path, kill_

assert data['name'] == term_name
await kill_all()


async def test_terminal_create_with_cwd(fetch, ws_fetch, terminal_path):
resp = await fetch(
'api', 'terminals',
method='POST',
body=json.dumps({'cwd': str(terminal_path)}),
allow_nonstandard_methods=True,
)

data = json.loads(resp.body.decode())
term_name = data['name']

ws = await ws_fetch(
'terminals', 'websocket', term_name
)

ws.write_message(json.dumps(['stdin', 'pwd\r\n']))

message_stdout = ''
while True:
try:
message = await asyncio.wait_for(ws.read_message(), timeout=1.0)
except asyncio.TimeoutError:
break

message = json.loads(message)

if message[0] == 'stdout':
message_stdout += message[1]

ws.close()

assert str(terminal_path) in message_stdout

0 comments on commit db4c373

Please sign in to comment.