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

Update Azure Cloud Shell for their new URI format #16247

Merged
merged 9 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions .github/actions/spelling/allow/allow.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
aci
admins
allcolors
Apc
Expand All @@ -8,6 +9,7 @@ breadcrumbs
bsd
calt
ccmp
ccon
changelog
clickable
clig
Expand Down Expand Up @@ -91,6 +93,7 @@ reserialize
reserializes
rlig
runtimes
servicebus
shcha
slnt
Sos
Expand All @@ -117,6 +120,7 @@ vsdevcmd
walkthrough
walkthroughs
We'd
westus
wildcards
XBox
YBox
Expand Down
33 changes: 29 additions & 4 deletions src/cascadia/TerminalConnection/AzureConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation

switch (bufferType)
{
case WINHTTP_WEB_SOCKET_BINARY_MESSAGE_BUFFER_TYPE:
Copy link
Contributor Author

@PankajBhojwani PankajBhojwani Oct 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bufferType they return seems to have changed to BINARY_MESSAGE_BUFFER_TYPE, just adding that case here seems to work fine, not sure if this has other implications though

case WINHTTP_WEB_SOCKET_UTF8_FRAGMENT_BUFFER_TYPE:
case WINHTTP_WEB_SOCKET_UTF8_MESSAGE_BUFFER_TYPE:
{
Expand Down Expand Up @@ -797,7 +798,7 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
// - an optional HTTP method (defaults to POST if content is present, GET otherwise)
// Return value:
// - the response from the server as a json value
WDJ::JsonObject AzureConnection::_SendRequestReturningJson(std::wstring_view uri, const WWH::IHttpContent& content, WWH::HttpMethod method)
WDJ::JsonObject AzureConnection::_SendRequestReturningJson(std::wstring_view uri, const WWH::IHttpContent& content, WWH::HttpMethod method, const Windows::Foundation::Uri referer)
{
if (!method)
{
Expand All @@ -810,6 +811,11 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
auto headers{ request.Headers() };
headers.Accept().TryParseAdd(L"application/json");

if (referer)
{
headers.Referer(referer);
}

const auto response{ _httpClient.SendRequestAsync(request).get() };
const auto string{ response.Content().ReadAsStringAsync().get() };
const auto jsonResult{ WDJ::JsonObject::Parse(string) };
Expand Down Expand Up @@ -974,17 +980,36 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
auto uri{ fmt::format(L"{}terminals?cols={}&rows={}&version=2019-01-01&shell={}", _cloudShellUri, _initialCols, _initialRows, shellType) };

WWH::HttpStringContent content{
L"",
L"{}",
WSS::UnicodeEncoding::Utf8,
// LOAD-BEARING. the API returns "'content-type' should be 'application/json' or 'multipart/form-data'"
L"application/json"
};

const auto terminalResponse = _SendRequestReturningJson(uri, content);
const auto terminalResponse = _SendRequestReturningJson(uri, content, WWH::HttpMethod::Post(), Windows::Foundation::Uri(_cloudShellUri));
_terminalID = terminalResponse.GetNamedString(L"id");

auto socketUri = terminalResponse.GetNamedString(L"socketUri");
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved

// we have to do some post-handling to get the proper socket endpoint
// at this point, socketUri is of the form: wss://ccon-prod-westus-aci-03.servicebus.windows.net/cc-AAAA-AAAAAAAA//aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
// we need it to become: wss://ccon-prod-westus-aci-03.servicebus.windows.net/$hc/cc-AAAA-AAAAAAAA/terminals/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
// (the "aaaaaa..." part at the end is just the _terminalID, that we already have stored)
std::wstring wSocketUri{ socketUri };
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved

// get the substring up until the ".net"
const auto dotNetPos = wSocketUri.find(L".net") + 4;
const auto wSocketUriBody = wSocketUri.substr(0, dotNetPos);

// get the portion between the ".net" and the "//" (this is the cc-AAAA-AAAAAAAA part)
const auto lastDoubleSlashPos = wSocketUri.find_last_of(L"//");
const auto wSocketUriMiddle = wSocketUri.substr(dotNetPos, lastDoubleSlashPos - (dotNetPos));

// piece together the final uri, adding in the "$hc" and "terminals" where needed
auto finalSocketUri{ fmt::format(L"{}/$hc{}terminals/{}", wSocketUriBody, wSocketUriMiddle, _terminalID) };
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved

// Return the uri
return terminalResponse.GetNamedString(L"socketUri");
return winrt::hstring(finalSocketUri);
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
}

// Method description:
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalConnection/AzureConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation

void _WriteStringWithNewline(const std::wstring_view str);
void _WriteCaughtExceptionRecord();
winrt::Windows::Data::Json::JsonObject _SendRequestReturningJson(std::wstring_view uri, const winrt::Windows::Web::Http::IHttpContent& content = nullptr, winrt::Windows::Web::Http::HttpMethod method = nullptr);
winrt::Windows::Data::Json::JsonObject _SendRequestReturningJson(std::wstring_view uri, const winrt::Windows::Web::Http::IHttpContent& content = nullptr, winrt::Windows::Web::Http::HttpMethod method = nullptr, const winrt::Windows::Foundation::Uri referer = nullptr);
void _setAccessToken(std::wstring_view accessToken);
winrt::Windows::Data::Json::JsonObject _GetDeviceCode();
winrt::Windows::Data::Json::JsonObject _WaitForUser(const winrt::hstring& deviceCode, int pollInterval, int expiresIn);
Expand Down
Loading