private static Uri ParseUri(string path, thinCLIProtocol tcli) { // The server sometimes sends us a relative path (e.g. for VM import) // and sometimes an absolute URI (https://host/path). Construct the absolute URI // based on what we're given. The same hack exists in the server code... // See CA-10942. if (path.StartsWith("/")) { string[] bits = path.Split('?'); UriBuilder uriBuilder = new UriBuilder(); uriBuilder.Scheme = "https"; uriBuilder.Host = tcli.conf.hostname; uriBuilder.Port = tcli.conf.port; uriBuilder.Path = bits[0]; if (bits.Length > 1) { uriBuilder.Query = bits[1]; } return(uriBuilder.Uri); } else { return(new Uri(path)); } }
public static void CheckUploadFiles(String filename, thinCLIProtocol tCLIprotocol) { if (!(tCLIprotocol.EnteredParamValues.Any(filename.StartsWith))) { throw new Exception(string.Format("The file '{0}' to upload is not the same as the one entered at the command line.", filename)); } }
public static void version_handshake(Stream stream, thinCLIProtocol tCLIprotocol) { /* Check for the initial magic string */ byte[] magic = Types.unmarshal_n(stream, (uint)tCLIprotocol.magic_string.Length); for (int i = 0; i < tCLIprotocol.magic_string.Length; i++) { if (magic[i] != tCLIprotocol.magic_string[i]) { tCLIprotocol.dGlobalError("Failed to find a server on " + tCLIprotocol.conf.hostname + ":" + tCLIprotocol.conf.port); tCLIprotocol.dGlobalUsage(); tCLIprotocol.dExit(1); } } /* Read the remote version numbers */ int remote_major = Types.unmarshal_int(stream); int remote_minor = Types.unmarshal_int(stream); tCLIprotocol.dGlobalDebug("Remote host has version " + remote_major + "." + remote_minor, tCLIprotocol); tCLIprotocol.dGlobalDebug("Client has version " + tCLIprotocol.major + "." + tCLIprotocol.minor, tCLIprotocol); if (tCLIprotocol.major != remote_major) { tCLIprotocol.dGlobalError("Protocol version mismatch talking to server on " + tCLIprotocol.conf.hostname + ":" + tCLIprotocol.conf.port); tCLIprotocol.dGlobalUsage(); tCLIprotocol.dExit(1); } /* Tell the server our version numbers */ for (int i = 0; i < tCLIprotocol.magic_string.Length; i++) { stream.WriteByte((byte)tCLIprotocol.magic_string[i]); } Types.marshal_int(stream, tCLIprotocol.major); Types.marshal_int(stream, tCLIprotocol.minor); }
public static Stream doRPC(String method, Uri uri, thinCLIProtocol tCLIprotocol, params string[] headers) { Stream http = Transport.connect(tCLIprotocol, uri.Host, uri.Port); String startLine = string.Format("{0} {1} HTTP/1.0", method, uri.PathAndQuery); writeLine(http, startLine); foreach (string h in headers) { writeLine(http, h); } writeLine(http, ""); String response = readLine(http); int code = getResultCode(response); switch (code) { case 200: break; case 302: string url = ""; while (true) { response = readLine(http); if (response.StartsWith("Location: ")) { url = response.Substring(10); } if (response.Equals("\r\n") || response.Equals("")) { break; } } Uri redirect = new Uri(url.Trim()); tCLIprotocol.conf.hostname = redirect.Host; http.Close(); return(doRPC(method, redirect, tCLIprotocol, headers)); default: tCLIprotocol.dGlobalError(string.Format("Received error code {0} from the server doing an HTTP {1}", code, method)); http.Close(); return(null); } while (true) { response = readLine(http); if (response.Equals("\r\n") || response.Equals("")) { break; } } // Stream should be positioned after the headers return(http); }
/// <param name="addr">The target URI, including scheme, hostname and path.</param> public static Stream doRPC(String method, Uri uri, thinCLIProtocol tCLIprotocol) { Stream http = Transport.connect(tCLIprotocol, uri.Host, uri.Port); String header = method + " " + uri.PathAndQuery + " HTTP/1.0\r\n\r\n"; writeLine(http, header); String response = readLine(http); int code = getResultCode(response); switch (code) { case 200: break; case 302: string url = ""; while (true) { response = readLine(http); if (response.StartsWith("Location: ")) { url = response.Substring(10); } if (response.Equals("\r\n") || response.Equals("")) { break; } } Uri redirect = new Uri(url.Trim()); tCLIprotocol.conf.hostname = redirect.Host; http.Close(); return(doRPC(method, redirect, tCLIprotocol)); default: tCLIprotocol.dGlobalError("Received an error message from the server doing an HTTP " + method + " " + uri.PathAndQuery + " : " + response); http.Close(); return(null); } while (true) { response = readLine(http); if (response.Equals("\r\n") || response.Equals("")) { break; } } // Stream should be positioned after the headers return(http); }
public static void performCommand(string Body, thinCLIProtocol tCLIprotocol) { string body = Body; body += "username="******"\n"; body += "password="******"\n"; if (body.Length != 0) { body = body.Substring(0, body.Length - 1); // strip last "\n" } string header = "POST /cli HTTP/1.0\r\n"; string content_length = "content-length: " + Encoding.UTF8.GetBytes(body).Length + "\r\n"; string tosend = header + content_length + "\r\n" + body; try { Stream stream = Transport.connect(tCLIprotocol, tCLIprotocol.conf.hostname, tCLIprotocol.conf.port); if (stream == null) { // The SSL functions already tell us what happened tCLIprotocol.dExit(1); } byte[] message = Encoding.UTF8.GetBytes(tosend); stream.Write(message, 0, message.Length); stream.Flush(); Messages.version_handshake(stream, tCLIprotocol); interpreter(stream, tCLIprotocol); } catch (SocketException) { tCLIprotocol.dGlobalError("Connection to " + tCLIprotocol.conf.hostname + ":" + tCLIprotocol.conf.port + " refused."); tCLIprotocol.dExit(1); } catch (Exception e) { if (tCLIprotocol.conf.debug) { throw e; } tCLIprotocol.dGlobalError("Caught exception: " + e.Message); tCLIprotocol.dExit(1); } }
public static Stream connect(thinCLIProtocol tCLIprotocol, String hostname, int port) { if (port != 443) { TcpClient client = new TcpClient(hostname, port); Stream stream = client.GetStream(); return(stream); } else { TcpClient client = new TcpClient(hostname, port); // Create an SSL stream that will close the client's stream. SslStream sslStream = new SslStream( client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null ); try { sslStream.AuthenticateAsClient("", null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12, true); } catch (AuthenticationException e) { if (tCLIprotocol.conf.debug) { throw e; } tCLIprotocol.dGlobalError("Authentication failed - closing the connection."); client.Close(); return(null); } catch (Exception e) { if (tCLIprotocol.conf.debug) { throw e; } tCLIprotocol.dGlobalError("Exception during SSL auth - closing the connection."); client.Close(); return(null); } return(sslStream); } }
public static void load(Stream stream, string filename, thinCLIProtocol tCLIprotocol) { try { using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) { FileInfo fi = new FileInfo(filename); // Immediately report our success in opening the file marshal_response(stream, tag.OK); // The server doesn't like multiple chunks but this is fine for // Zurich/Geneva imports Types.marshal_int(stream, 4 + 4 + 4); marshal_tag(stream, tag.Blob); marshal_tag(stream, tag.Chunk); Types.marshal_int32(stream, (uint)fi.Length); byte[] block = new byte[tCLIprotocol.conf.block_size]; while (true) { int n = fs.Read(block, 0, block.Length); if (n == 0) { Types.marshal_int(stream, 4 + 4); marshal_tag(stream, tag.Blob); marshal_tag(stream, tag.End); break; } stream.Write(block, 0, n); tCLIprotocol.dProgress(n); } } } catch (DirectoryNotFoundException) { marshal_response(stream, tag.Failed); } catch (FileNotFoundException) { marshal_response(stream, tag.Failed); } }
public static void http_get(Stream stream, string filename, Uri uri, thinCLIProtocol tCLIprotocol) { try { if (File.Exists(filename)) { throw new Exception(string.Format("The file '{0}' already exists", filename)); } using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write)) { using (Stream http = HTTP.doRPC("GET", uri, tCLIprotocol)) { if (http == null) { tCLIprotocol.dGlobalError("Server rejected request"); marshal_response(stream, tag.Failed); return; } byte[] block = new byte[tCLIprotocol.conf.block_size]; while (true) { int n = http.Read(block, 0, block.Length); if (n == 0) { break; } fs.Write(block, 0, n); } marshal_response(stream, tag.OK); } } } catch (Exception e) { tCLIprotocol.dGlobalError("Received exception: " + e.Message); tCLIprotocol.dGlobalError("Unable to write output file: " + filename); marshal_response(stream, tag.Failed); } }
public static void http_put(Stream stream, string filename, Uri uri, thinCLIProtocol tCLIprotocol) { try { using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) { using (Stream http = HTTP.doRPC("PUT", uri, tCLIprotocol, string.Format("Content-Length: {0}", fs.Length))) { if (http == null) { marshal_response(stream, tag.Failed); return; } byte[] block = new byte[tCLIprotocol.conf.block_size]; while (true) { int n = fs.Read(block, 0, block.Length); if (n == 0) { break; } http.Write(block, 0, n); } marshal_response(stream, tag.OK); } } } catch (FileNotFoundException) { tCLIprotocol.dGlobalError("File not found"); marshal_response(stream, tag.Failed); } catch (Exception e) { tCLIprotocol.dGlobalError(string.Format("Received exception: {0}", e.Message)); marshal_response(stream, tag.Failed); } }
public static void CheckPermitFiles(String filename, thinCLIProtocol tCLIprotocol, bool includeCurrentDir = false) { string fullpath = ""; try { fullpath = Path.GetFullPath(filename); } catch (Exception ex) { throw new Exception(string.Format("Failed to retrieve full path of file '{0}', '{1}'", filename, ex.Message)); } if (includeCurrentDir) { tCLIprotocol.EnteredParamValues.Add(Directory.GetCurrentDirectory()); } foreach (string value in tCLIprotocol.EnteredParamValues) { try { String valueFullPath = Path.GetFullPath(value); if (fullpath.StartsWith(valueFullPath)) { tCLIprotocol.dGlobalDebug("Passed permit files check", tCLIprotocol); return; } } catch { } } throw new Exception(string.Format("The file with name '{0}' is not present at the command line.", filename)); }
public static void protocol_failure(string msg, tag t, thinCLIProtocol tCLIprotocol) { tCLIprotocol.dGlobalError("Protocol failure: Reading " + msg + ": unexpected tag: " + t); tCLIprotocol.dExit(1); }
public static void http_put(Stream stream, string filename, Uri uri, thinCLIProtocol tCLIprotocol) { try { using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) { using (Stream http = HTTP.doRPC("PUT", uri, tCLIprotocol)) { if (http == null) { marshal_response(stream, tag.Failed); return; } byte[] block = new byte[tCLIprotocol.conf.block_size]; while (true) { int n = fs.Read(block, 0, block.Length); if (n == 0) break; http.Write(block, 0, n); } marshal_response(stream, tag.OK); } } } catch (FileNotFoundException) { tCLIprotocol.dGlobalError("File not found"); marshal_response(stream, tag.Failed); } catch (Exception e) { tCLIprotocol.dGlobalError(string.Format("Received exception: {0}", e.Message)); marshal_response(stream, tag.Failed); } }
public static void Main(string[] args) { thinCLIProtocol tCLIprotocol = new thinCLIProtocol(new delegateGlobalError(Global.error), new delegateGlobalUsage(Global.usage), new delegateGlobalDebug(Global.debug), new delegateConsoleWrite(delegate(string s) { Console.Write(s); }), new delegateConsoleWriteLine(delegate(string s) { Console.WriteLine(s); }), new delegateConsoleReadLine(delegate { return Console.ReadLine(); }), new delegateExit(delegate(int i) { Environment.Exit(i); }), new delegateProgress(delegate(int i) {}), new Config()); String body = ""; Char[] eqsep = {'='}; for(int i=0; i<args.Length; i++) { String s = args[i]; try { if (s.StartsWith("server")){ tCLIprotocol.conf.hostname = (s.Split(eqsep))[1]; } else if (s.Equals("-s")) { tCLIprotocol.conf.hostname = args[++i]; } else if (s.Equals("-u")){ tCLIprotocol.conf.username = args[++i]; } else if (s.Equals("-pw")){ tCLIprotocol.conf.password = args[++i]; } else if (s.Equals("-p")){ tCLIprotocol.conf.port = Int32.Parse(args[++i]); } else if (s.Equals("--nossl")){ tCLIprotocol.conf.nossl = true; } else if (s.Equals("-debug")){ tCLIprotocol.conf.debug = true; } else if (s.Equals("-version")){ Console.WriteLine("ThinCLI protocol: " + tCLIprotocol.major + "." + tCLIprotocol.minor); Environment.Exit(0); } else body += s + "\n"; } catch { Global.error("Failed to parse command-line arguments"); Global.usage(); Environment.Exit(1); } } if (tCLIprotocol.conf.hostname.Equals("")) { Global.error("No hostname was specified."); Global.usage(); Environment.Exit(1); } Messages.performCommand(body, tCLIprotocol); }
/// <param name="addr">The target URI, including scheme, hostname and path.</param> public static Stream doRPC(String method, Uri uri, thinCLIProtocol tCLIprotocol) { Stream http = Transport.connect(tCLIprotocol, uri.Host, uri.Port); String header = method + " " + uri.PathAndQuery + " HTTP/1.0\r\n\r\n"; writeLine(http, header); String response = readLine(http); int code = getResultCode(response); switch (code) { case 200: break; case 302: string url = ""; while (true) { response = readLine(http); if (response.StartsWith("Location: ")) url = response.Substring(10); if (response.Equals("\r\n") || response.Equals("")) break; } Uri redirect = new Uri(url.Trim()); tCLIprotocol.conf.hostname = redirect.Host; http.Close(); return doRPC(method, redirect, tCLIprotocol); default: tCLIprotocol.dGlobalError("Received an error message from the server doing an HTTP " + method + " " + uri.PathAndQuery + " : " + response); http.Close(); return null; } while (true) { response = readLine(http); if (response.Equals("\r\n") || response.Equals("")) break; } // Stream should be positioned after the headers return http; }
private static Uri ParseUri(string path, thinCLIProtocol tcli) { // The server sometimes sends us a relative path (e.g. for VM import) // and sometimes an absolute URI (https://host/path). Construct the absolute URI // based on what we're given. The same hack exists in the server code... // See CA-10942. if (path.StartsWith("/")) { string[] bits = path.Split('?'); UriBuilder uriBuilder = new UriBuilder(); uriBuilder.Scheme = "https"; uriBuilder.Host = tcli.conf.hostname; uriBuilder.Port = tcli.conf.port; uriBuilder.Path = bits[0]; if (bits.Length > 1) uriBuilder.Query = bits[1]; return uriBuilder.Uri; } else { return new Uri(path); } }
public static void interpreter(Stream stream, thinCLIProtocol tCLIprotocol) { string filename = ""; string path = ""; string msg = ""; while (!tCLIprotocol.dropOut) { Types.unmarshal_int32(stream); // total message length (unused here) Messages.tag t = Messages.unmarshal_tag(stream); switch (t) { case Messages.tag.Command: t = Messages.unmarshal_tag(stream); switch (t) { case Messages.tag.Print: msg = Types.unmarshal_string(stream); tCLIprotocol.dGlobalDebug("Read: Print: " + msg, tCLIprotocol); tCLIprotocol.dConsoleWriteLine(msg); break; case Messages.tag.PrintStderr: msg = Types.unmarshal_string(stream); tCLIprotocol.dGlobalDebug("Read: PrintStderr: " + msg, tCLIprotocol); tCLIprotocol.dConsoleWriteLine(msg); break; case Messages.tag.Debug: msg = Types.unmarshal_string(stream); tCLIprotocol.dGlobalDebug("Read: Debug: " + msg, tCLIprotocol); tCLIprotocol.dConsoleWriteLine(msg); break; case Messages.tag.Exit: int code = Types.unmarshal_int(stream); tCLIprotocol.dGlobalDebug("Read: Command Exit " + code, tCLIprotocol); tCLIprotocol.dExit(code); break; case Messages.tag.Error: tCLIprotocol.dGlobalDebug("Read: Command Error", tCLIprotocol); string err_code = Types.unmarshal_string(stream); tCLIprotocol.dConsoleWriteLine("Error code: " + err_code); tCLIprotocol.dConsoleWrite("Error params: "); int length = Types.unmarshal_int(stream); for (int i = 0; i < length; i++) { string param = Types.unmarshal_string(stream); tCLIprotocol.dConsoleWrite(param); if (i != (length - 1)) tCLIprotocol.dConsoleWrite(", "); } tCLIprotocol.dConsoleWriteLine(""); break; case Messages.tag.Prompt: tCLIprotocol.dGlobalDebug("Read: Command Prompt", tCLIprotocol); string response = tCLIprotocol.dConsoleReadLine(); tCLIprotocol.dConsoleWriteLine("Read "+response); /* NB, 4+4+4 here for the blob, chunk and string length, put in by the marshal_string function. A franken-marshal. */ Types.marshal_int(stream, 4 + 4 + 4); // length Messages.marshal_tag(stream, Messages.tag.Blob); Messages.marshal_tag(stream, Messages.tag.Chunk); Types.marshal_string(stream, response); Types.marshal_int(stream, 4 + 4); // length Messages.marshal_tag(stream, Messages.tag.Blob); Messages.marshal_tag(stream, Messages.tag.End); break; case Messages.tag.Load: filename = Types.unmarshal_string(stream); tCLIprotocol.dGlobalDebug("Read: Load " + filename, tCLIprotocol); Messages.load(stream, filename, tCLIprotocol); break; case Messages.tag.HttpPut: filename = Types.unmarshal_string(stream); path = Types.unmarshal_string(stream); Uri uri = ParseUri(path, tCLIprotocol); tCLIprotocol.dGlobalDebug("Read: HttpPut " + filename + " -> " + uri, tCLIprotocol); Messages.http_put(stream, filename, uri, tCLIprotocol); break; case Messages.tag.HttpGet: filename = Types.unmarshal_string(stream); path = Types.unmarshal_string(stream); uri = ParseUri(path, tCLIprotocol); tCLIprotocol.dGlobalDebug("Read: HttpGet " + filename + " -> " + uri, tCLIprotocol); Messages.http_get(stream, filename, uri, tCLIprotocol); break; default: Messages.protocol_failure("Command", t, tCLIprotocol); break; } break; default: Messages.protocol_failure("Message", t, tCLIprotocol); break; } } }
public static void performCommand(string Body, thinCLIProtocol tCLIprotocol) { string body = Body; body += "username="******"\n"; body += "password="******"\n"; if (body.Length != 0) { body = body.Substring(0, body.Length - 1); // strip last "\n" } string header = "POST /cli HTTP/1.0\r\n"; string content_length = "content-length: " + Encoding.UTF8.GetBytes(body).Length + "\r\n"; string tosend = header + content_length + "\r\n" + body; try { Stream stream = Transport.connect(tCLIprotocol, tCLIprotocol.conf.hostname, tCLIprotocol.conf.port); if (stream == null) { // The SSL functions already tell us what happened tCLIprotocol.dExit(1); } byte[] message = Encoding.UTF8.GetBytes(tosend); stream.Write(message, 0, message.Length); stream.Flush(); Messages.version_handshake(stream, tCLIprotocol); interpreter(stream, tCLIprotocol); } catch (SocketException) { tCLIprotocol.dGlobalError("Connection to " + tCLIprotocol.conf.hostname + ":" + tCLIprotocol.conf.port + " refused."); tCLIprotocol.dExit(1); } catch (Exception e) { if (tCLIprotocol.conf.debug) throw e; tCLIprotocol.dGlobalError("Caught exception: " + e.Message); tCLIprotocol.dExit(1); } }
public static void interpreter(Stream stream, thinCLIProtocol tCLIprotocol) { string filename = ""; string path = ""; string msg = ""; while (!tCLIprotocol.dropOut) { Types.unmarshal_int32(stream); // total message length (unused here) Messages.tag t = Messages.unmarshal_tag(stream); switch (t) { case Messages.tag.Command: t = Messages.unmarshal_tag(stream); switch (t) { case Messages.tag.Print: msg = Types.unmarshal_string(stream); tCLIprotocol.dGlobalDebug("Read: Print: " + msg, tCLIprotocol); tCLIprotocol.dConsoleWriteLine(msg); break; case Messages.tag.PrintStderr: msg = Types.unmarshal_string(stream); tCLIprotocol.dGlobalDebug("Read: PrintStderr: " + msg, tCLIprotocol); tCLIprotocol.dConsoleWriteLine(msg); break; case Messages.tag.Debug: msg = Types.unmarshal_string(stream); tCLIprotocol.dGlobalDebug("Read: Debug: " + msg, tCLIprotocol); tCLIprotocol.dConsoleWriteLine(msg); break; case Messages.tag.Exit: int code = Types.unmarshal_int(stream); tCLIprotocol.dGlobalDebug("Read: Command Exit " + code, tCLIprotocol); tCLIprotocol.dExit(code); break; case Messages.tag.Error: tCLIprotocol.dGlobalDebug("Read: Command Error", tCLIprotocol); string err_code = Types.unmarshal_string(stream); tCLIprotocol.dConsoleWriteLine("Error code: " + err_code); tCLIprotocol.dConsoleWrite("Error params: "); int length = Types.unmarshal_int(stream); for (int i = 0; i < length; i++) { string param = Types.unmarshal_string(stream); tCLIprotocol.dConsoleWrite(param); if (i != (length - 1)) { tCLIprotocol.dConsoleWrite(", "); } } tCLIprotocol.dConsoleWriteLine(""); break; case Messages.tag.Prompt: tCLIprotocol.dGlobalDebug("Read: Command Prompt", tCLIprotocol); string response = tCLIprotocol.dConsoleReadLine(); tCLIprotocol.dConsoleWriteLine("Read " + response); /* NB, 4+4+4 here for the blob, chunk and string length, put in by the marshal_string * function. A franken-marshal. */ Types.marshal_int(stream, 4 + 4 + 4); // length Messages.marshal_tag(stream, Messages.tag.Blob); Messages.marshal_tag(stream, Messages.tag.Chunk); Types.marshal_string(stream, response); Types.marshal_int(stream, 4 + 4); // length Messages.marshal_tag(stream, Messages.tag.Blob); Messages.marshal_tag(stream, Messages.tag.End); break; case Messages.tag.Load: filename = Types.unmarshal_string(stream); CheckPermitFiles(filename, tCLIprotocol); tCLIprotocol.dGlobalDebug("Read: Load " + filename, tCLIprotocol); Messages.load(stream, filename, tCLIprotocol); break; case Messages.tag.HttpPut: filename = Types.unmarshal_string(stream); CheckPermitFiles(filename, tCLIprotocol); path = Types.unmarshal_string(stream); Uri uri = ParseUri(path, tCLIprotocol); tCLIprotocol.dGlobalDebug("Read: HttpPut " + filename + " -> " + uri, tCLIprotocol); Messages.http_put(stream, filename, uri, tCLIprotocol); break; case Messages.tag.HttpGet: filename = Types.unmarshal_string(stream); CheckPermitFiles(filename, tCLIprotocol, true); path = Types.unmarshal_string(stream); uri = ParseUri(path, tCLIprotocol); tCLIprotocol.dGlobalDebug("Read: HttpGet " + filename + " -> " + uri, tCLIprotocol); Messages.http_get(stream, filename, uri, tCLIprotocol); break; default: Messages.protocol_failure("Command", t, tCLIprotocol); break; } break; default: Messages.protocol_failure("Message", t, tCLIprotocol); break; } } }
public static Stream connect(thinCLIProtocol tCLIprotocol, String hostname, int port) { if (port != 443){ TcpClient client = new TcpClient(hostname, port); Stream stream = client.GetStream(); return stream; } else { TcpClient client = new TcpClient(hostname, port); // Create an SSL stream that will close the client's stream. SslStream sslStream = new SslStream( client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null ); try { sslStream.AuthenticateAsClient("", null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12, true); } catch (AuthenticationException e){ if (tCLIprotocol.conf.debug) throw e; tCLIprotocol.dGlobalError("Authentication failed - closing the connection."); client.Close(); return null; } catch (Exception e) { if (tCLIprotocol.conf.debug) throw e; tCLIprotocol.dGlobalError("Exception during SSL auth - closing the connection."); client.Close(); return null; } return sslStream; } }
public static void http_get(Stream stream, string filename, Uri uri, thinCLIProtocol tCLIprotocol) { try { if (File.Exists(filename)) throw new Exception(string.Format("The file '{0}' already exists", filename)); using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write)) { using (Stream http = HTTP.doRPC("GET", uri, tCLIprotocol)) { if (http == null) { tCLIprotocol.dGlobalError("Server rejected request"); marshal_response(stream, tag.Failed); return; } byte[] block = new byte[tCLIprotocol.conf.block_size]; while (true) { int n = http.Read(block, 0, block.Length); if (n == 0) break; fs.Write(block, 0, n); } marshal_response(stream, tag.OK); } } } catch (Exception e) { tCLIprotocol.dGlobalError("Received exception: " + e.Message); tCLIprotocol.dGlobalError("Unable to write output file: " + filename); marshal_response(stream, tag.Failed); } }