|
| 1 | +using Renci.SshNet.Abstractions; |
| 2 | +using Renci.SshNet.Common; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Net; |
| 6 | +using System.Net.Sockets; |
| 7 | +using System.Text.RegularExpressions; |
| 8 | + |
| 9 | +namespace Renci.SshNet.Connection |
| 10 | +{ |
| 11 | + internal class HttpConnector : ConnectorBase |
| 12 | + { |
| 13 | + public override Socket Connect(IConnectionInfo connectionInfo) |
| 14 | + { |
| 15 | + var socket = SocketConnect(connectionInfo.ProxyHost, connectionInfo.ProxyPort, connectionInfo.Timeout); |
| 16 | + |
| 17 | + var httpResponseRe = new Regex(@"HTTP/(?<version>\d[.]\d) (?<statusCode>\d{3}) (?<reasonPhrase>.+)$"); |
| 18 | + var httpHeaderRe = new Regex(@"(?<fieldName>[^\[\]()<>@,;:\""/?={} \t]+):(?<fieldValue>.+)?"); |
| 19 | + |
| 20 | + SocketAbstraction.Send(socket, SshData.Ascii.GetBytes(string.Format("CONNECT {0}:{1} HTTP/1.0\r\n", connectionInfo.Host, connectionInfo.Port))); |
| 21 | + |
| 22 | + // Sent proxy authorization is specified |
| 23 | + if (!string.IsNullOrEmpty(connectionInfo.ProxyUsername)) |
| 24 | + { |
| 25 | + var authorization = string.Format("Proxy-Authorization: Basic {0}\r\n", |
| 26 | + Convert.ToBase64String(SshData.Ascii.GetBytes(string.Format("{0}:{1}", connectionInfo.ProxyUsername, connectionInfo.ProxyPassword)))); |
| 27 | + SocketAbstraction.Send(socket, SshData.Ascii.GetBytes(authorization)); |
| 28 | + } |
| 29 | + |
| 30 | + SocketAbstraction.Send(socket, SshData.Ascii.GetBytes("\r\n")); |
| 31 | + |
| 32 | + HttpStatusCode? statusCode = null; |
| 33 | + var contentLength = 0; |
| 34 | + |
| 35 | + while (true) |
| 36 | + { |
| 37 | + var response = SocketReadLine(socket, connectionInfo.Timeout); |
| 38 | + if (response == null) |
| 39 | + { |
| 40 | + // server shut down socket |
| 41 | + break; |
| 42 | + } |
| 43 | + |
| 44 | + if (statusCode == null) |
| 45 | + { |
| 46 | + var statusMatch = httpResponseRe.Match(response); |
| 47 | + if (statusMatch.Success) |
| 48 | + { |
| 49 | + var httpStatusCode = statusMatch.Result("${statusCode}"); |
| 50 | + statusCode = (HttpStatusCode)int.Parse(httpStatusCode); |
| 51 | + if (statusCode != HttpStatusCode.OK) |
| 52 | + { |
| 53 | + var reasonPhrase = statusMatch.Result("${reasonPhrase}"); |
| 54 | + throw new ProxyException(string.Format("HTTP: Status code {0}, \"{1}\"", httpStatusCode, |
| 55 | + reasonPhrase)); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + // continue on parsing message headers coming from the server |
| 63 | + var headerMatch = httpHeaderRe.Match(response); |
| 64 | + if (headerMatch.Success) |
| 65 | + { |
| 66 | + var fieldName = headerMatch.Result("${fieldName}"); |
| 67 | + if (fieldName.Equals("Content-Length", StringComparison.OrdinalIgnoreCase)) |
| 68 | + { |
| 69 | + contentLength = int.Parse(headerMatch.Result("${fieldValue}")); |
| 70 | + } |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + // check if we've reached the CRLF which separates request line and headers from the message body |
| 75 | + if (response.Length == 0) |
| 76 | + { |
| 77 | + // read response body if specified |
| 78 | + if (contentLength > 0) |
| 79 | + { |
| 80 | + var contentBody = new byte[contentLength]; |
| 81 | + SocketRead(socket, contentBody, 0, contentLength); |
| 82 | + } |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if (statusCode == null) |
| 88 | + { |
| 89 | + throw new ProxyException("HTTP response does not contain status line."); |
| 90 | + } |
| 91 | + |
| 92 | + return socket; |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Performs a blocking read on the socket until a line is read. |
| 97 | + /// </summary> |
| 98 | + /// <param name="socket">The <see cref="Socket"/> to read from.</param> |
| 99 | + /// <param name="timeout">A <see cref="TimeSpan"/> that represents the time to wait until a line is read.</param> |
| 100 | + /// <exception cref="SshOperationTimeoutException">The read has timed-out.</exception> |
| 101 | + /// <exception cref="SocketException">An error occurred when trying to access the socket.</exception> |
| 102 | + /// <returns> |
| 103 | + /// The line read from the socket, or <c>null</c> when the remote server has shutdown and all data has been received. |
| 104 | + /// </returns> |
| 105 | + private static string SocketReadLine(Socket socket, TimeSpan timeout) |
| 106 | + { |
| 107 | + var encoding = SshData.Ascii; |
| 108 | + var buffer = new List<byte>(); |
| 109 | + var data = new byte[1]; |
| 110 | + |
| 111 | + // read data one byte at a time to find end of line and leave any unhandled information in the buffer |
| 112 | + // to be processed by subsequent invocations |
| 113 | + do |
| 114 | + { |
| 115 | + var bytesRead = SocketAbstraction.Read(socket, data, 0, data.Length, timeout); |
| 116 | + if (bytesRead == 0) |
| 117 | + // the remote server shut down the socket |
| 118 | + break; |
| 119 | + |
| 120 | + var b = data[0]; |
| 121 | + |
| 122 | + if (b == Session.LineFeed && buffer.Count > 1 && buffer[buffer.Count - 1] == Session.CarriageReturn) |
| 123 | + { |
| 124 | + // Return line without CR |
| 125 | + return encoding.GetString(buffer.ToArray(), 0, buffer.Count - 1); |
| 126 | + } |
| 127 | + |
| 128 | + buffer.Add(b); |
| 129 | + } |
| 130 | + while (true); |
| 131 | + |
| 132 | + if (buffer.Count == 0) |
| 133 | + { |
| 134 | + return null; |
| 135 | + } |
| 136 | + |
| 137 | + return encoding.GetString(buffer.ToArray(), 0, buffer.Count); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments