Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set User Agent for Jupyter websocket connections #21910

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/repl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ doctest = false
alacritty_terminal.workspace = true
anyhow.workspace = true
async-dispatcher.workspace = true
async-tungstenite = { workspace = true, features = ["async-std", "async-tls"] }
base64.workspace = true
client.workspace = true
collections.workspace = true
Expand Down
35 changes: 32 additions & 3 deletions crates/repl/src/kernels/remote_kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ use gpui::{Task, View, WindowContext};
use http_client::{AsyncBody, HttpClient, Request};
use jupyter_protocol::{ExecutionState, JupyterKernelspec, JupyterMessage, KernelInfoReply};

use async_tungstenite::{
async_std::connect_async,
tungstenite::{client::IntoClientRequest, http::HeaderValue},
};

use futures::StreamExt;
use smol::io::AsyncReadExt as _;

Expand All @@ -11,8 +16,8 @@ use crate::Session;
use super::RunningKernel;
use anyhow::Result;
use jupyter_websocket_client::{
JupyterWebSocketReader, JupyterWebSocketWriter, KernelLaunchRequest, KernelSpecsResponse,
RemoteServer,
JupyterWebSocket, JupyterWebSocketReader, JupyterWebSocketWriter, KernelLaunchRequest,
KernelSpecsResponse, RemoteServer,
};
use std::{fmt::Debug, sync::Arc};

Expand Down Expand Up @@ -151,7 +156,31 @@ impl RemoteRunningKernel {
)
.await?;

let (kernel_socket, _response) = remote_server.connect_to_kernel(&kernel_id).await?;
let ws_url = format!(
"{}/api/kernels/{}/channels?token={}",
remote_server.base_url.replace("http", "ws"),
kernel_id,
remote_server.token
);

let mut req: Request<()> = ws_url.into_client_request()?;
let headers = req.headers_mut();

headers.insert(
"User-Agent",
HeaderValue::from_str(&format!(
"Zed/{} ({}; {})",
"repl",
std::env::consts::OS,
std::env::consts::ARCH
))?,
);

let response = connect_async(req).await;

let (ws_stream, _response) = response?;

let kernel_socket = JupyterWebSocket { inner: ws_stream };

let (mut w, mut r): (JupyterWebSocketWriter, JupyterWebSocketReader) =
kernel_socket.split();
Expand Down
Loading