Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

[automated] Merge branch 'release/2.2' => 'master' #510

Merged
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
30 changes: 18 additions & 12 deletions src/Microsoft.HttpRepl/Commands/BaseHttpCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -116,8 +117,18 @@ protected override async Task ExecuteAsync(IShellState shellState, HttpState pro
bool deleteFile = false;
noBody = commandInput.Options[NoBodyOption].Count > 0;

if (!thisRequestHeaders.TryGetValue("content-type", out string contentType) && programState.Headers.TryGetValue("content-type", out IEnumerable<string> contentTypes))
{
contentType = contentTypes.FirstOrDefault();
}

if (!noBody)
{
if (string.IsNullOrEmpty(contentType))
{
contentType = "application/json";
}

if (commandInput.Options[BodyFileOption].Count > 0)
{
filePath = commandInput.Options[BodyFileOption][0].Text;
Expand All @@ -144,18 +155,7 @@ protected override async Task ExecuteAsync(IShellState shellState, HttpState pro
deleteFile = true;
filePath = Path.GetTempFileName();

if (!thisRequestHeaders.TryGetValue("content-type", out string contentType) && programState.Headers.TryGetValue("content-type", out IEnumerable<string> contentTypes))
{
contentType = contentTypes.FirstOrDefault();
}

if (contentType == null)
{
contentType = "application/json";
}

string exampleBody = programState.GetExampleBody(commandInput.Arguments.Count > 0 ? commandInput.Arguments[0].Text : string.Empty, contentType, Verb);
request.Headers.TryAddWithoutValidation("Content-Type", contentType);
string exampleBody = programState.GetExampleBody(commandInput.Arguments.Count > 0 ? commandInput.Arguments[0].Text : string.Empty, ref contentType, Verb);

if (!string.IsNullOrEmpty(exampleBody))
{
Expand All @@ -179,13 +179,19 @@ protected override async Task ExecuteAsync(IShellState shellState, HttpState pro
}
}

if (string.IsNullOrEmpty(contentType))
{
contentType = "application/json";
}

byte[] data = noBody
? new byte[0]
: string.IsNullOrEmpty(bodyContent)
? File.ReadAllBytes(filePath)
: Encoding.UTF8.GetBytes(bodyContent);

HttpContent content = new ByteArrayContent(data);
content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
request.Content = content;

if (deleteFile)
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.HttpRepl/Commands/HelpCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public void CoreGetHelp(IShellState shellState, ICommandDispatcher<HttpState, IC
shellState.ConsoleManager.WriteLine($"{"HEAD",navCommandColumn}{"Issues a HEAD request."}");
shellState.ConsoleManager.WriteLine($"{"OPTIONS",navCommandColumn}{"Issues an OPTIONS request."}");
shellState.ConsoleManager.WriteLine();
shellState.ConsoleManager.WriteLine($"{"set header",navCommandColumn}{"Sets or clears a header for all requests. e.g. `set header content-type:application/json`"}");
shellState.ConsoleManager.WriteLine($"{"set header",navCommandColumn}{"Sets or clears a header for all requests. e.g. `set header content-type application/json`"}");
shellState.ConsoleManager.WriteLine();

shellState.ConsoleManager.WriteLine();
Expand Down
17 changes: 16 additions & 1 deletion src/Microsoft.HttpRepl/Commands/SetBaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task ExecuteAsync(IShellState shellState, HttpState state, ICorePar
catch { }
}

if (state.BaseAddress == null || !Uri.TryCreate(state.BaseAddress, "/swagger/v1/swagger.json", out Uri result))
if (state.BaseAddress == null || !Uri.TryCreate(state.BaseAddress, "swagger.json", out Uri result))
{
state.SwaggerStructure = null;
}
Expand All @@ -64,6 +64,21 @@ public async Task ExecuteAsync(IShellState shellState, HttpState state, ICorePar
{
shellState.ConsoleManager.WriteLine("Using swagger metadata from " + result);
}
else
{
if (state.BaseAddress == null || !Uri.TryCreate(state.BaseAddress, "swagger/v1/swagger.json", out result))
{
state.SwaggerStructure = null;
}
else
{
await SetSwaggerCommand.CreateDirectoryStructureForSwaggerEndpointAsync(shellState, state, result, cancellationToken).ConfigureAwait(false);
if (state.SwaggerStructure != null)
{
shellState.ConsoleManager.WriteLine("Using swagger metadata from " + result);
}
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.HttpRepl/Commands/SetSwaggerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private static void FillDirectoryInfo(DirectoryStructure parent, EndpointMetadat
{
if (string.IsNullOrEmpty(parameterSetsByContentType.Key))
{
dirRequestInfo.SetFallbackRequestBody(method, GetBodyString(null, parameterSetsByContentType.Value));
dirRequestInfo.SetFallbackRequestBody(method, parameterSetsByContentType.Key, GetBodyString(null, parameterSetsByContentType.Value));
}

dirRequestInfo.SetRequestBody(method, parameterSetsByContentType.Key, GetBodyString(parameterSetsByContentType.Key, parameterSetsByContentType.Value));
Expand Down
11 changes: 9 additions & 2 deletions src/Microsoft.HttpRepl/DirectoryStructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ public class RequestInfo : IRequestInfo
private readonly HashSet<string> _methods = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, Dictionary<string, string>> _requestBodiesByMethodByContentType = new Dictionary<string, Dictionary<string, string>>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, string> _fallbackBodyStringsByMethod = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, string> _fallbackContentTypeStringsByMethod = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, IReadOnlyList<string>> _contentTypesByMethod = new Dictionary<string, IReadOnlyList<string>>(StringComparer.OrdinalIgnoreCase);

public IReadOnlyList<string> Methods => _methods.ToList();

public IReadOnlyDictionary<string, IReadOnlyList<string>> ContentTypesByMethod => _contentTypesByMethod;

public string GetRequestBodyForContentType(string contentType, string method)
public string GetRequestBodyForContentType(ref string contentType, string method)
{
if (_requestBodiesByMethodByContentType.TryGetValue(method, out Dictionary<string, string> bodiesByContentType)
&& bodiesByContentType.TryGetValue(contentType, out string body))
Expand All @@ -71,6 +72,11 @@ public string GetRequestBodyForContentType(string contentType, string method)

if (_fallbackBodyStringsByMethod.TryGetValue(method, out body))
{
if (_fallbackContentTypeStringsByMethod.TryGetValue(method, out string newContentType))
{
contentType = newContentType;
}

return body;
}

Expand Down Expand Up @@ -100,9 +106,10 @@ public void AddMethod(string method)
_methods.Add(method);
}

public void SetFallbackRequestBody(string method, string fallbackBodyString)
public void SetFallbackRequestBody(string method, string contentType, string fallbackBodyString)
{
_fallbackBodyStringsByMethod[method] = fallbackBodyString;
_fallbackContentTypeStringsByMethod[method] = contentType;
}
}
}
4 changes: 2 additions & 2 deletions src/Microsoft.HttpRepl/HttpState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ public bool SavePreferences()
}
}

public string GetExampleBody(string path, string contentType, string method)
public string GetExampleBody(string path, ref string contentType, string method)
{
Uri effectivePath = GetEffectivePath(path);
string rootRelativePath = effectivePath.LocalPath.Substring(BaseAddress.LocalPath.Length).TrimStart('/');
IDirectoryStructure structure = SwaggerStructure?.TraverseTo(rootRelativePath);
return structure?.RequestInfo?.GetRequestBodyForContentType(contentType, method);
return structure?.RequestInfo?.GetRequestBodyForContentType(ref contentType, method);
}

public IEnumerable<string> GetApplicableContentTypes(string method, string path)
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.HttpRepl/IRequestInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public interface IRequestInfo

IReadOnlyList<string> Methods { get; }

string GetRequestBodyForContentType(string contentType, string method);
string GetRequestBodyForContentType(ref string contentType, string method);
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.HttpRepl/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static async Task Main(string[] args)
shell.ShellState.ConsoleManager.AddBreakHandler(() => source.Cancel());
if (args.Length > 0)
{
if (string.Equals(args[0], "--help", StringComparison.OrdinalIgnoreCase))
if (string.Equals(args[0], "--help", StringComparison.OrdinalIgnoreCase) || string.Equals(args[0], "-h", StringComparison.OrdinalIgnoreCase))
{
shell.ShellState.ConsoleManager.WriteLine("Usage: dotnet httprepl [<BASE_ADDRESS>] [options]");
shell.ShellState.ConsoleManager.WriteLine();
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Repl/Commanding/DefaultCommandDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void AddCommand(ICommand<TProgramState, TParseResult> command)

public IParser Parser => _parser;

public IReadOnlyList<string> CollectSuggesetions(IShellState shellState)
public IReadOnlyList<string> CollectSuggestions(IShellState shellState)
{
string line = shellState.InputManager.GetCurrentBuffer();
TParseResult parseResult = _parser.Parse(line, shellState.ConsoleManager.CaretPosition);
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Repl/Commanding/ICommandDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface ICommandDispatcher
{
IParser Parser { get; }

IReadOnlyList<string> CollectSuggesetions(IShellState shellState);
IReadOnlyList<string> CollectSuggestions(IShellState shellState);

void OnReady(IShellState shellState);

Expand Down
42 changes: 24 additions & 18 deletions src/Microsoft.Repl/ConsoleHandling/ConsoleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,41 @@ public void MoveCaret(int positions)
return;
}

int bufferWidth = Console.BufferWidth;
int cursorTop = Console.CursorTop;
int cursorLeft = Console.CursorLeft;

while (positions < 0 && CaretPosition > 0)
{
if (-positions > Console.BufferWidth)
if (-positions > bufferWidth)
{
if (Console.CursorTop == 0)
if (cursorTop == 0)
{
Console.CursorLeft = 0;
cursorLeft = 0;
positions = 0;
}
else
{
positions += Console.BufferWidth;
--Console.CursorTop;
positions += bufferWidth;
--cursorTop;
}
}
else
{
int remaining = Console.CursorLeft + positions;
int remaining = cursorLeft + positions;

if (remaining >= 0)
{
Console.CursorLeft = remaining;
cursorLeft = remaining;
}
else if (Console.CursorTop == 0)
else if (cursorTop == 0)
{
Console.CursorLeft = 0;
cursorLeft = 0;
}
else
{
--Console.CursorTop;
Console.CursorLeft = Console.BufferWidth + remaining;
--cursorTop;
cursorLeft = bufferWidth + remaining;
}

positions = 0;
Expand All @@ -89,27 +93,29 @@ public void MoveCaret(int positions)

while (positions > 0)
{
if (positions > Console.BufferWidth)
if (positions > bufferWidth)
{
positions -= Console.BufferWidth;
++Console.CursorTop;
positions -= bufferWidth;
++cursorTop;
}
else
{
int spaceLeftOnLine = Console.BufferWidth - Console.CursorLeft - 1;
int spaceLeftOnLine = bufferWidth - cursorLeft - 1;
if (positions > spaceLeftOnLine)
{
++Console.CursorTop;
Console.CursorLeft = positions - spaceLeftOnLine - 1;
++cursorTop;
cursorLeft = positions - spaceLeftOnLine - 1;
}
else
{
Console.CursorLeft += positions;
cursorLeft += positions;
}

positions = 0;
}
}

Console.SetCursorPosition(cursorLeft, cursorTop);
}
}

Expand Down
24 changes: 16 additions & 8 deletions src/Microsoft.Repl/Input/InputManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,22 @@ public void ResetInput()

private void StashEchoState()
{
_ttyState = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX)
? GetTtyState()
: null;
string sttyFlags = null;
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX))
{
_ttyState = GetTtyState();
sttyFlags = "gfmt1:erase=08:werase=08 -echo";
}
//If it's any of the ubuntu variants on 18.x, stty tweaks are required
else if (System.Runtime.InteropServices.RuntimeInformation.OSDescription.IndexOf("buntu", StringComparison.OrdinalIgnoreCase) > -1)
{
_ttyState = GetTtyState();
sttyFlags = "erase 0x08 werase 0x08 -echo";
}

if (!string.IsNullOrEmpty(_ttyState))
if (!string.IsNullOrEmpty(sttyFlags))
{
//"gfmt1:cflag=4300:iflag=6b02:lflag=200005c7:oflag=3:discard=f:dsusp=19:eof=4:eol=ff:eol2=ff:erase=7f:intr=3:kill=15:lnext=16:min=1:quit=1c:reprint=12:start=11:status=14:stop=13:susp=1a:time=0:werase=17:ispeed=38400:ospeed=38400\n"
ProcessStartInfo psi = new ProcessStartInfo("stty", "gfmt1:erase=08:werase=08 -echo");
ProcessStartInfo psi = new ProcessStartInfo("stty", sttyFlags);
Process p = Process.Start(psi);
p?.WaitForExit();
}
Expand All @@ -133,7 +141,7 @@ private static string GetTtyState()
};
Process p = Process.Start(psi);
p?.WaitForExit();
string result = p?.StandardOutput.ReadToEnd();
string result = p?.StandardOutput.ReadToEnd().Trim();
return result;
}

Expand Down Expand Up @@ -331,7 +339,7 @@ public async Task StartAsync(IShellState state, CancellationToken cancellationTo
}

private void FlushInput(IShellState state, ref List<ConsoleKeyInfo> presses)
{
{
string str = new string(presses.Select(x => x.KeyChar).ToArray());

if (state.ConsoleManager.CaretPosition == _inputBuffer.Count)
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.Repl/Suggestions/SuggestionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void NextSuggestion(IShellState shellState)
else
{
_currentSuggestion = 0;
_suggestions = shellState.CommandDispatcher.CollectSuggesetions(shellState);
_suggestions = shellState.CommandDispatcher.CollectSuggestions(shellState);

if (_suggestions == null || _suggestions.Count == 0)
{
Expand Down Expand Up @@ -76,7 +76,7 @@ public void PreviousSuggestion(IShellState shellState)
}
else
{
_suggestions = shellState.CommandDispatcher.CollectSuggesetions(shellState);
_suggestions = shellState.CommandDispatcher.CollectSuggestions(shellState);
_currentSuggestion = _suggestions.Count - 1;

if (_suggestions == null || _suggestions.Count == 0)
Expand Down