using Microsoft.Win32; using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Net.Sockets; using System.Text; namespace Cryville.Common.Network.Http11 { public class Http11Client : IDisposable { private readonly string _directHost; protected string DirectHost { get { return _directHost; } } private readonly int _directPort; protected int DirectPort { get { return _directPort; } } readonly Uri _baseUri; readonly int origPort; protected const string Version = "HTTP/1.1"; protected TcpClient TcpClient { get; private set; } protected Stream RawTcpStream { get { return TcpClient.GetStream(); } } protected virtual Stream Stream { get { return TcpClient.GetStream(); } } protected virtual string WindowsProxyProtocolName { get { return "http"; } } private readonly bool _proxied = false; public Dictionary Headers { get; set; } public Http11Client(Uri baseUri, int port = 80) { _directHost = baseUri.Host; _directPort = port; _baseUri = baseUri; origPort = _baseUri.Port; Headers = new Dictionary(); _proxied = GetProxy(ref _directHost, ref _directPort); Shared.Logger.Log(0, "Network", "Connecting to {0}:{1}", DirectHost, DirectPort); TcpClient = new TcpClient(DirectHost, DirectPort); } public virtual void Connect() { if (_proxied) { Request(RawTcpStream, "CONNECT", string.Format(CultureInfo.InvariantCulture, "{0}:{1}", _baseUri.Host, origPort)); } } public virtual void Close() { TcpClient.Close(); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } public virtual void Dispose(bool disposing) { if (disposing) { Close(); } } public Http11Response Request(string method, Uri uri, string body = null, Encoding encoding = null) { string struri = GetUri(uri).PathAndQuery; return Request(Stream, method, struri, body, encoding); } Http11Response Request(Stream stream, string method, string uri, string body = null, Encoding encoding = null) { var headers = new Dictionary(StringComparer.OrdinalIgnoreCase); foreach (var h in Headers) headers.Add(h.Key, h.Value); headers["Host"] = _baseUri.Host; byte[] payload = null; if (body != null) { encoding ??= Encoding.UTF8; payload = encoding.GetBytes(body); headers.Add("Content-Encoding", encoding.EncodingName); headers.Add("Content-Length", payload.Length.ToString(CultureInfo.InvariantCulture)); } using (var writer = new StreamWriter(stream, Encoding.ASCII, 1024, true)) { writer.Write(method); writer.Write(' '); writer.Write(uri); writer.Write(' '); writer.Write(Version); writer.Write("\r\n"); foreach (var header in headers) { writer.Write(header.Key); writer.Write(':'); writer.Write(header.Value); writer.Write("\r\n"); } writer.Write("\r\n"); if (payload != null) writer.Write(payload); writer.Flush(); } var response = new Http11Response(stream); Shared.Logger.Log(0, "Network", "{0}", response); return response; } protected bool GetProxy(ref string host, ref int port) { // TODO use winhttp.dll if (Environment.OSVersion.Platform == PlatformID.Win32NT) { var reg = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings"); var proxyEnable = (int)reg.GetValue("ProxyEnable"); if (proxyEnable == 0) return false; var proxyStr = (string)reg.GetValue("ProxyServer"); if (!string.IsNullOrEmpty(proxyStr)) { string[] proxies = proxyStr.Split(';'); foreach (var p in proxies) { if (!p.Contains('=')) { string[] s = p.Split(':'); host = s[0]; port = int.Parse(s[1]); return true; } else if (p.StartsWith(WindowsProxyProtocolName + "=")) { string[] s = p.Split('=', ':'); host = s[1]; port = int.Parse(s[2]); return true; } } } } return false; } protected Uri GetUri(string path) { Uri address; if (_baseUri != null) { if (!Uri.TryCreate(_baseUri, path, out address)) { return new Uri(Path.GetFullPath(path)); } } else { if (!Uri.TryCreate(path, UriKind.Absolute, out address)) { return new Uri(Path.GetFullPath(path)); } } return GetUri(address); } protected Uri GetUri(Uri address) { if (address == null) { throw new ArgumentNullException("address"); } Uri uri = address; if (!address.IsAbsoluteUri && _baseUri != null && !Uri.TryCreate(_baseUri, address, out uri)) { return address; } return uri; } } }