Fixed further code smells as per code review.

Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
This commit is contained in:
Konstantina Chremmou 2022-05-18 14:08:00 +01:00
parent c4617240aa
commit 02f59946b9

View File

@ -177,9 +177,9 @@ namespace ThinCLI
} }
} }
public static class HTTP public static class Http
{ {
private static string readLine(Stream stream){ private static string ReadLine(Stream stream){
StringBuilder messageData = new StringBuilder(); StringBuilder messageData = new StringBuilder();
do { do {
int i = stream.ReadByte(); int i = stream.ReadByte();
@ -198,31 +198,31 @@ namespace ThinCLI
return messageData.ToString(); return messageData.ToString();
} }
private static void writeLine(Stream stream, string line){ private static void WriteLine(Stream stream, string line){
byte[] message = Encoding.UTF8.GetBytes($"{line}\r\n"); byte[] message = Encoding.UTF8.GetBytes($"{line}\r\n");
stream.Write(message, 0, message.Length); stream.Write(message, 0, message.Length);
stream.Flush(); stream.Flush();
} }
private static int getResultCode(string line){ private static int GetResultCode(string line){
string[] bits = line.Split(' '); string[] bits = line.Split(' ');
if (bits.Length < 2) return 0; if (bits.Length < 2) return 0;
return Int32.Parse(bits[1]); return Int32.Parse(bits[1]);
} }
public static Stream DoRPC(TcpClient client, string method, Uri uri, Config conf, params string[] headers) public static Stream DoRpc(TcpClient client, string method, Uri uri, Config conf, params string[] headers)
{ {
var transport = new Transport(conf); var transport = new Transport(conf);
Stream http = transport.Connect(client, uri.Port); Stream http = transport.Connect(client, uri.Port);
var startLine = $"{method} {uri.PathAndQuery} HTTP/1.0"; var startLine = $"{method} {uri.PathAndQuery} HTTP/1.0";
writeLine(http, startLine); WriteLine(http, startLine);
foreach (string h in headers) foreach (string h in headers)
writeLine(http, h); WriteLine(http, h);
writeLine(http, ""); WriteLine(http, "");
var response = readLine(http); var response = ReadLine(http);
int code = getResultCode(response); int code = GetResultCode(response);
switch (code) switch (code)
{ {
@ -233,7 +233,7 @@ namespace ThinCLI
string url = ""; string url = "";
while (true) while (true)
{ {
response = readLine(http); response = ReadLine(http);
if (response.StartsWith("Location: ")) if (response.StartsWith("Location: "))
url = response.Substring(10); url = response.Substring(10);
if (response.Equals("\r\n") || response.Equals("")) if (response.Equals("\r\n") || response.Equals(""))
@ -244,7 +244,7 @@ namespace ThinCLI
conf.Hostname = redirect.Host; conf.Hostname = redirect.Host;
http.Close(); http.Close();
http.Dispose(); http.Dispose();
return DoRPC(client, method, redirect, conf, headers); return DoRpc(client, method, redirect, conf, headers);
default: default:
Logger.Error($"Received error code {code} from the server doing an HTTP {method}"); Logger.Error($"Received error code {code} from the server doing an HTTP {method}");
@ -255,7 +255,7 @@ namespace ThinCLI
while (true) while (true)
{ {
response = readLine(http); response = ReadLine(http);
if (response.Equals("\r\n") || response.Equals("")) if (response.Equals("\r\n") || response.Equals(""))
break; break;
} }
@ -266,16 +266,17 @@ namespace ThinCLI
public static class Types public static class Types
{ {
public static uint unmarshal_int32(Stream stream){ public static uint UnMarshalUint(Stream stream)
{
uint a = (uint)stream.ReadByte(); uint a = (uint)stream.ReadByte();
uint b = (uint)stream.ReadByte(); uint b = (uint)stream.ReadByte();
uint c = (uint)stream.ReadByte(); uint c = (uint)stream.ReadByte();
uint d = (uint)stream.ReadByte(); uint d = (uint)stream.ReadByte();
//Console.WriteLine("a = " + a + " b = " + b + " c = " + c + " d = " + d);
return (a << 0) | (b << 8) | (c << 16) | (d << 24); return (a << 0) | (b << 8) | (c << 16) | (d << 24);
} }
public static void marshal_int32(Stream stream, uint x){ public static void MarshalUint(Stream stream, uint x)
{
uint mask = 0xff; uint mask = 0xff;
stream.WriteByte((byte)((x >> 0) & mask)); stream.WriteByte((byte)((x >> 0) & mask));
stream.WriteByte((byte)((x >> 8) & mask)); stream.WriteByte((byte)((x >> 8) & mask));
@ -283,36 +284,45 @@ namespace ThinCLI
stream.WriteByte((byte)((x >> 24) & mask)); stream.WriteByte((byte)((x >> 24) & mask));
} }
public static int unmarshal_int(Stream stream){ public static int UnMarshalInt(Stream stream)
return (int)unmarshal_int32(stream); {
return (int)UnMarshalUint(stream);
} }
public static void marshal_int(Stream stream, int x){ public static void MarshalInt(Stream stream, int x)
marshal_int32(stream, (uint)x); {
MarshalUint(stream, (uint)x);
} }
public static byte[] unmarshal_n(Stream stream, uint n){ public static byte[] UnMarshalN(Stream stream, uint n)
{
byte[] buffer = new byte[n]; byte[] buffer = new byte[n];
int toread = (int)n; int toRead = (int)n;
int offset = 0; int offset = 0;
while (toread > 0){
int nread = stream.Read(buffer, offset, toread); while (toRead > 0)
offset= nread; toread -= nread; {
int nRead = stream.Read(buffer, offset, toRead);
offset = nRead;
toRead -= nRead;
} }
return buffer; return buffer;
} }
public static string unmarshal_string(Stream stream){ public static string UnMarshalString(Stream stream)
uint length = unmarshal_int32(stream); {
byte[] buffer = unmarshal_n(stream, length); uint length = UnMarshalUint(stream);
byte[] buffer = UnMarshalN(stream, length);
Decoder decoder = Encoding.UTF8.GetDecoder(); Decoder decoder = Encoding.UTF8.GetDecoder();
char[] chars = new char[decoder.GetCharCount(buffer, 0, (int)length)]; char[] chars = new char[decoder.GetCharCount(buffer, 0, (int)length)];
decoder.GetChars(buffer, 0, (int)length, chars, 0); decoder.GetChars(buffer, 0, (int)length, chars, 0);
return new string(chars); return new string(chars);
} }
public static void marshal_string(Stream stream, string x){ public static void MarshalString(Stream stream, string x)
marshal_int(stream, x.Length); {
MarshalInt(stream, x.Length);
char[] c = x.ToCharArray(); char[] c = x.ToCharArray();
Encoder encoder = Encoding.UTF8.GetEncoder(); Encoder encoder = Encoding.UTF8.GetEncoder();
byte[] bytes = new byte[encoder.GetByteCount(c, 0, c.Length, true)]; byte[] bytes = new byte[encoder.GetByteCount(c, 0, c.Length, true)];
@ -333,24 +343,24 @@ namespace ThinCLI
private enum Tag private enum Tag
{ {
Print = 0, Load = 1, HttpGet = 12, HttpPut = 13, Prompt = 3, Exit = 4, Print = 0, Load = 1, HttpGet = 12, HttpPut = 13, Prompt = 3, Exit = 4,
Error = 14, OK = 5, Failed = 6, Chunk = 7, End = 8, Command = 9, Response = 10, Error = 14, Ok = 5, Failed = 6, Chunk = 7, End = 8, Command = 9, Response = 10,
Blob = 11, Debug = 15, PrintStderr = 16 Blob = 11, Debug = 15, PrintStderr = 16
} }
private static Tag UnmarshalTag(Stream stream) private static Tag UnMarshalTag(Stream stream)
{ {
int x = Types.unmarshal_int(stream); int x = Types.UnMarshalInt(stream);
return (Tag)x; return (Tag)x;
} }
private static void MarshalTag(Stream stream, Tag tag) private static void MarshalTag(Stream stream, Tag tag)
{ {
Types.marshal_int(stream, (int)tag); Types.MarshalInt(stream, (int)tag);
} }
private static void MarshalResponse(Stream stream, Tag t) private static void MarshalResponse(Stream stream, Tag t)
{ {
Types.marshal_int(stream, 4 + 4); Types.MarshalInt(stream, 4 + 4);
MarshalTag(stream, Tag.Response); MarshalTag(stream, Tag.Response);
MarshalTag(stream, t); MarshalTag(stream, t);
} }
@ -363,14 +373,14 @@ namespace ThinCLI
{ {
FileInfo fi = new FileInfo(filename); FileInfo fi = new FileInfo(filename);
// Immediately report our success in opening the file // Immediately report our success in opening the file
MarshalResponse(stream, Tag.OK); MarshalResponse(stream, Tag.Ok);
// The server doesn't like multiple chunks but this is fine for // The server doesn't like multiple chunks but this is fine for
// Zurich/Geneva imports // Zurich/Geneva imports
Types.marshal_int(stream, 4 + 4 + 4); Types.MarshalInt(stream, 4 + 4 + 4);
MarshalTag(stream, Tag.Blob); MarshalTag(stream, Tag.Blob);
MarshalTag(stream, Tag.Chunk); MarshalTag(stream, Tag.Chunk);
Types.marshal_int32(stream, (uint)fi.Length); Types.MarshalUint(stream, (uint)fi.Length);
byte[] block = new byte[conf.BlockSize]; byte[] block = new byte[conf.BlockSize];
while (true) while (true)
@ -378,7 +388,7 @@ namespace ThinCLI
int n = fs.Read(block, 0, block.Length); int n = fs.Read(block, 0, block.Length);
if (n == 0) if (n == 0)
{ {
Types.marshal_int(stream, 4 + 4); Types.MarshalInt(stream, 4 + 4);
MarshalTag(stream, Tag.Blob); MarshalTag(stream, Tag.Blob);
MarshalTag(stream, Tag.End); MarshalTag(stream, Tag.End);
break; break;
@ -404,7 +414,7 @@ namespace ThinCLI
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{ {
using (TcpClient client = new TcpClient(uri.Host, uri.Port)) using (TcpClient client = new TcpClient(uri.Host, uri.Port))
using (Stream http = HTTP.DoRPC(client, "PUT", uri, conf, $"Content-Length: {fs.Length}")) using (Stream http = Http.DoRpc(client, "PUT", uri, conf, $"Content-Length: {fs.Length}"))
{ {
if (http == null) if (http == null)
{ {
@ -418,7 +428,7 @@ namespace ThinCLI
if (n == 0) break; if (n == 0) break;
http.Write(block, 0, n); http.Write(block, 0, n);
} }
MarshalResponse(stream, Tag.OK); MarshalResponse(stream, Tag.Ok);
} }
} }
} }
@ -445,7 +455,7 @@ namespace ThinCLI
using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write)) using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
{ {
using (TcpClient client = new TcpClient(uri.Host, uri.Port)) using (TcpClient client = new TcpClient(uri.Host, uri.Port))
using (Stream http = HTTP.DoRPC(client, "GET", uri, conf)) using (Stream http = Http.DoRpc(client, "GET", uri, conf))
{ {
if (http == null) if (http == null)
{ {
@ -460,7 +470,7 @@ namespace ThinCLI
if (n == 0) break; if (n == 0) break;
fs.Write(block, 0, n); fs.Write(block, 0, n);
} }
MarshalResponse(stream, Tag.OK); MarshalResponse(stream, Tag.Ok);
} }
} }
} }
@ -475,7 +485,7 @@ namespace ThinCLI
private static void VersionHandshake(Stream stream, Config conf) private static void VersionHandshake(Stream stream, Config conf)
{ {
// Check for the initial prefix string // Check for the initial prefix string
byte[] magic = Types.unmarshal_n(stream, (uint)THIN_CLI_SERVER_PREFIX.Length); byte[] magic = Types.UnMarshalN(stream, (uint)THIN_CLI_SERVER_PREFIX.Length);
for (int i = 0; i < THIN_CLI_SERVER_PREFIX.Length; i++) for (int i = 0; i < THIN_CLI_SERVER_PREFIX.Length; i++)
{ {
@ -484,8 +494,8 @@ namespace ThinCLI
} }
// Read the remote version numbers // Read the remote version numbers
int remoteMajor = Types.unmarshal_int(stream); int remoteMajor = Types.UnMarshalInt(stream);
int remoteMinor = Types.unmarshal_int(stream); int remoteMinor = Types.UnMarshalInt(stream);
Logger.Debug($"Remote thin CLI protocol has version {remoteMajor}.{remoteMinor}", conf); Logger.Debug($"Remote thin CLI protocol has version {remoteMajor}.{remoteMinor}", conf);
Logger.Debug($"Client expects version {CLI_PROTOCOL_MAJOR}.{CLI_PROTOCOL_MINOR}", conf); Logger.Debug($"Client expects version {CLI_PROTOCOL_MAJOR}.{CLI_PROTOCOL_MINOR}", conf);
@ -497,8 +507,8 @@ namespace ThinCLI
foreach (var t in THIN_CLI_SERVER_PREFIX) foreach (var t in THIN_CLI_SERVER_PREFIX)
stream.WriteByte((byte)t); stream.WriteByte((byte)t);
Types.marshal_int(stream, CLI_PROTOCOL_MAJOR); Types.MarshalInt(stream, CLI_PROTOCOL_MAJOR);
Types.marshal_int(stream, CLI_PROTOCOL_MINOR); Types.MarshalInt(stream, CLI_PROTOCOL_MINOR);
} }
public static void PerformCommand(string command, Config conf) public static void PerformCommand(string command, Config conf)
@ -570,44 +580,44 @@ namespace ThinCLI
while (true) while (true)
{ {
Types.unmarshal_int32(stream); // total message length (unused here) Types.UnMarshalUint(stream); // total message length (unused here)
Tag t = UnmarshalTag(stream); Tag t = UnMarshalTag(stream);
switch (t) switch (t)
{ {
case Tag.Command: case Tag.Command:
t = UnmarshalTag(stream); t = UnMarshalTag(stream);
switch (t) switch (t)
{ {
case Tag.Print: case Tag.Print:
msg = Types.unmarshal_string(stream); msg = Types.UnMarshalString(stream);
Logger.Debug("Read: Print: ", conf); Logger.Debug("Read: Print: ", conf);
Logger.Info(msg); Logger.Info(msg);
break; break;
case Tag.PrintStderr: case Tag.PrintStderr:
msg = Types.unmarshal_string(stream); msg = Types.UnMarshalString(stream);
Logger.Debug("Read: PrintStderr: ", conf); Logger.Debug("Read: PrintStderr: ", conf);
Logger.Info(msg); Logger.Info(msg);
break; break;
case Tag.Debug: case Tag.Debug:
msg = Types.unmarshal_string(stream); msg = Types.UnMarshalString(stream);
Logger.Debug("Read: Debug: ", conf); Logger.Debug("Read: Debug: ", conf);
Logger.Info(msg); Logger.Info(msg);
break; break;
case Tag.Exit: case Tag.Exit:
int code = Types.unmarshal_int(stream); int code = Types.UnMarshalInt(stream);
Logger.Debug("Read: Command Exit " + code, conf); Logger.Debug("Read: Command Exit " + code, conf);
if (code == 0) if (code == 0)
return;//exit return;//exit
throw new ThinCliProtocolException($"Command Exit {code}", code); throw new ThinCliProtocolException($"Command Exit {code}", code);
case Tag.Error: case Tag.Error:
Logger.Debug("Read: Command Error", conf); Logger.Debug("Read: Command Error", conf);
string errCode = Types.unmarshal_string(stream); string errCode = Types.UnMarshalString(stream);
Logger.Info("Error code: " + errCode); Logger.Info("Error code: " + errCode);
var paramList = new List<string>(); var paramList = new List<string>();
int length = Types.unmarshal_int(stream); int length = Types.UnMarshalInt(stream);
for (int i = 0; i < length; i++) for (int i = 0; i < length; i++)
{ {
string param = Types.unmarshal_string(stream); string param = Types.UnMarshalString(stream);
paramList.Add(param); paramList.Add(param);
} }
Logger.Info("Error params: " + string.Join(", ", paramList)); Logger.Info("Error params: " + string.Join(", ", paramList));
@ -616,34 +626,34 @@ namespace ThinCLI
Logger.Debug("Read: Command Prompt", conf); Logger.Debug("Read: Command Prompt", conf);
string response = Console.ReadLine(); string response = Console.ReadLine();
Logger.Info("Read " + response); Logger.Info("Read " + response);
/* NB, 4+4+4 here for the blob, chunk and string length, put in by the marshal_string /* NB, 4+4+4 here for the blob, chunk and string length, put in by the MarshalString
function. A franken-marshal. */ function. A franken-marshal. */
Types.marshal_int(stream, 4 + 4 + 4); // length Types.MarshalInt(stream, 4 + 4 + 4); // length
MarshalTag(stream, Tag.Blob); MarshalTag(stream, Tag.Blob);
MarshalTag(stream, Tag.Chunk); MarshalTag(stream, Tag.Chunk);
Types.marshal_string(stream, response); Types.MarshalString(stream, response);
Types.marshal_int(stream, 4 + 4); // length Types.MarshalInt(stream, 4 + 4); // length
MarshalTag(stream, Tag.Blob); MarshalTag(stream, Tag.Blob);
MarshalTag(stream, Tag.End); MarshalTag(stream, Tag.End);
break; break;
case Tag.Load: case Tag.Load:
filename = Types.unmarshal_string(stream); filename = Types.UnMarshalString(stream);
CheckPermitFiles(filename, conf); CheckPermitFiles(filename, conf);
Logger.Debug("Read: Load " + filename, conf); Logger.Debug("Read: Load " + filename, conf);
Load(stream, filename, conf); Load(stream, filename, conf);
break; break;
case Tag.HttpPut: case Tag.HttpPut:
filename = Types.unmarshal_string(stream); filename = Types.UnMarshalString(stream);
CheckPermitFiles(filename, conf); CheckPermitFiles(filename, conf);
path = Types.unmarshal_string(stream); path = Types.UnMarshalString(stream);
Uri uri = ParseUri(path, conf); Uri uri = ParseUri(path, conf);
Logger.Debug("Read: HttpPut " + filename + " -> " + uri, conf); Logger.Debug("Read: HttpPut " + filename + " -> " + uri, conf);
HttpPut(stream, filename, uri, conf); HttpPut(stream, filename, uri, conf);
break; break;
case Tag.HttpGet: case Tag.HttpGet:
filename = Types.unmarshal_string(stream); filename = Types.UnMarshalString(stream);
CheckPermitFiles(filename, conf, true); CheckPermitFiles(filename, conf, true);
path = Types.unmarshal_string(stream); path = Types.UnMarshalString(stream);
uri = ParseUri(path, conf); uri = ParseUri(path, conf);
Logger.Debug("Read: HttpGet " + filename + " -> " + uri, conf); Logger.Debug("Read: HttpGet " + filename + " -> " + uri, conf);
HttpGet(stream, filename, uri, conf); HttpGet(stream, filename, uri, conf);