ActiveConnection GetClient(string host, int port, bool useSsl) { ActiveConnection connection = null; #if USE_KEEPALIVE var kill = new List <ActiveConnection> (); lock (connectionPool) { foreach (var i in connectionPool) { if (i.host == host && i.port == port) { if (i.Connected) { connection = i; } else { kill.Add(i); } break; } } foreach (var i in kill) { connectionPool.Remove(i); } if (connection != null) { connectionPool.Remove(connection); } } #endif if (connection == null) { connection = new ActiveConnection() { host = host, port = port }; connection.client = new TcpClient(); connection.client.Connect(uri.Host, uri.Port); if (useSsl) { #if USE_SSL connection.stream = new SslStream(connection.client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate)); var ssl = connection.stream as SslStream; ssl.AuthenticateAsClient(uri.Host); #endif } else { connection.stream = connection.client.GetStream(); } } return(connection); }
ActiveConnection GetClient(string url, int port, bool useSsl) { var key = string.Format("{0}:{1}", url, port); ActiveConnection connection = null; List <ActiveConnection> pool = null; if (!activeConnections.TryGetValue(key, out pool)) { pool = activeConnections [key] = new List <ActiveConnection> (); } while (pool.Count > 0) { connection = pool [0]; pool.RemoveAt(0); if (connection.client.Connected) { break; } connection = null; } if (connection == null) { connection = new ActiveConnection() { key = key }; connection.client = new TcpClient(); connection.client.Connect(uri.Host, uri.Port); if (useSsl) { connection.stream = new SslStream(connection.client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate)); var ssl = connection.stream as SslStream; ssl.AuthenticateAsClient(uri.Host); } else { connection.stream = connection.client.GetStream(); } } return(connection); }
public void Send() { if (sent) { throw new InvalidOperationException("Request has already completed."); } sent = true; isDone = false; state = RequestState.Waiting; #if USE_GZIP if (acceptGzip) { headers.Set("Accept-Encoding", "gzip"); } #endif if (timeout > 0) { SimpleWWW.Instance.StartCoroutine(Timeout()); } ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object t) { try { var retry = 0; while (++retry < maximumRedirects) { if (useCache) { string etag = ""; if (etags.TryGetValue(uri.AbsoluteUri, out etag)) { headers.Set("If-None-Match", etag); } } var hostHeader = uri.Host; if (uri.Port != 80 && uri.Port != 443) { hostHeader += ":" + uri.Port.ToString(); } headers.Set("Host", hostHeader); #if USE_COOKIES if (enableCookies && uri != null) { try { var c = cookies.GetCookieHeader(uri); if (c != null && c.Length > 0) { headers.Set("Cookie", c); } } catch (NullReferenceException) { //Some cookies make the .NET cookie class barf. MEGH again. } catch (IndexOutOfRangeException) { //Another weird exception that comes through from the cookie class. } } #endif ActiveConnection connection = null; while (true) { try { //pull a connection from the pool (a new one is created if needed) connection = GetClient(uri.Host, uri.Port, uri.Scheme.ToLower() == "https"); } catch (Exception e) { Debug.Log(e); exception = e; response = null; break; } try { WriteToStream(connection.stream); } catch (IOException e) { Debug.Log(e); exception = new IOException("Server closed the connection:" + e.ToString()); response = null; break; } response = new Response(this); state = RequestState.Reading; try { response.ReadFromStream(connection.stream); } catch (IOException e) { Debug.Log(e); exception = new IOException("Server closed the connection:" + e.ToString()); response = null; break; } catch (HTTPException) { continue; } break; } if (response != null) { #if USE_COOKIES if (enableCookies) { foreach (var i in response.headers.GetAll("Set-Cookie")) { try { cookies.SetCookies(uri, i); } catch (System.Net.CookieException) { //Some cookies make the .NET cookie class barf. MEGH. } } } #endif switch (response.status) { case 101: upgradedConnection = connection; retry = maximumRedirects; break; case 304: retry = maximumRedirects; break; case 307: case 302: case 301: uri = new Uri(response.headers.Get("Location")); if (OnRedirect != null) { OnRedirect(uri); retry = maximumRedirects; } break; default: retry = maximumRedirects; break; } //close the connection back if not upgraded. if (upgradedConnection == null) { lock (connectionPool) { var close = response.headers.Get("Connection").ToLower() == "close"; if (!close) { connectionPool.Add(connection); } else { connection.stream.Close(); } } } } } if (useCache && response != null) { string etag = response.headers.Get("etag"); if (etag.Length > 0) { etags [uri.AbsoluteUri] = etag; SaveEtags(); } } } catch (Exception e) { Debug.Log(e); exception = e; response = null; } state = RequestState.Done; isDone = true; })); }
public void Send () { if (sent) { throw new InvalidOperationException ("Request has already completed."); } sent = true; isDone = false; state = RequestState.Waiting; #if USE_GZIP if (acceptGzip) { headers.Set ("Accept-Encoding", "gzip"); } #endif if (timeout > 0) SimpleWWW.Instance.StartCoroutine (Timeout ()); ThreadPool.QueueUserWorkItem (new WaitCallback (delegate(object t) { try { var retry = 0; while (++retry < maximumRedirects) { if (useCache) { string etag = ""; if (etags.TryGetValue (uri.AbsoluteUri, out etag)) { headers.Set ("If-None-Match", etag); } } var hostHeader = uri.Host; if (uri.Port != 80 && uri.Port != 443) { hostHeader += ":" + uri.Port.ToString (); } headers.Set ("Host", hostHeader); #if USE_COOKIES if (enableCookies && uri != null) { try { var c = cookies.GetCookieHeader (uri); if (c != null && c.Length > 0) { headers.Set ("Cookie", c); } } catch (NullReferenceException) { //Some cookies make the .NET cookie class barf. MEGH again. } catch (IndexOutOfRangeException) { //Another weird exception that comes through from the cookie class. } } #endif ActiveConnection connection = null; while (true) { try { //pull a connection from the pool (a new one is created if needed) connection = GetClient (uri.Host, uri.Port, uri.Scheme.ToLower () == "https"); } catch (Exception e) { Debug.Log (e); exception = e; response = null; break; } try { WriteToStream (connection.stream); } catch (IOException e) { Debug.Log (e); exception = new IOException ("Server closed the connection:" + e.ToString ()); response = null; break; } response = new Response (this); state = RequestState.Reading; try { response.ReadFromStream (connection.stream); } catch (IOException e) { Debug.Log (e); exception = new IOException ("Server closed the connection:" + e.ToString ()); response = null; break; } catch (HTTPException) { continue; } break; } if (response != null) { #if USE_COOKIES if (enableCookies) { foreach (var i in response.headers.GetAll("Set-Cookie")) { try { cookies.SetCookies (uri, i); } catch (System.Net.CookieException) { //Some cookies make the .NET cookie class barf. MEGH. } } } #endif switch (response.status) { case 101: upgradedConnection = connection; retry = maximumRedirects; break; case 304: retry = maximumRedirects; break; case 307: case 302: case 301: uri = new Uri (response.headers.Get ("Location")); if (OnRedirect != null) { OnRedirect (uri); retry = maximumRedirects; } break; default: retry = maximumRedirects; break; } //close the connection back if not upgraded. if (upgradedConnection == null) { lock (connectionPool) { var close = response.headers.Get ("Connection").ToLower () == "close"; if (!close) { connectionPool.Add (connection); } else { connection.stream.Close (); } } } } } if (useCache && response != null) { string etag = response.headers.Get ("etag"); if (etag.Length > 0) { etags [uri.AbsoluteUri] = etag; SaveEtags (); } } } catch (Exception e) { Debug.Log (e); exception = e; response = null; } state = RequestState.Done; isDone = true; })); }
ActiveConnection GetClient (string host, int port, bool useSsl) { ActiveConnection connection = null; #if USE_KEEPALIVE var kill = new List<ActiveConnection> (); lock (connectionPool) { foreach (var i in connectionPool) { if (i.host == host && i.port == port) { if (i.Connected) { connection = i; } else { kill.Add (i); } break; } } foreach (var i in kill) { connectionPool.Remove (i); } if (connection != null) { connectionPool.Remove (connection); } } #endif if (connection == null) { connection = new ActiveConnection () { host = host, port = port }; connection.client = new TcpClient (); connection.client.Connect (uri.Host, uri.Port); if (useSsl) { #if USE_SSL connection.stream = new SslStream (connection.client.GetStream (), false, new RemoteCertificateValidationCallback (ValidateServerCertificate)); var ssl = connection.stream as SslStream; ssl.AuthenticateAsClient (uri.Host); #endif } else { connection.stream = connection.client.GetStream (); } } return connection; }
ActiveConnection GetClient(string url, int port, bool useSsl) { var key = string.Format ("{0}:{1}", url, port); ActiveConnection connection = null; List<ActiveConnection> pool = null; if (!activeConnections.TryGetValue (key, out pool)) { pool = activeConnections [key] = new List<ActiveConnection> (); } while (pool.Count > 0) { connection = pool [0]; pool.RemoveAt (0); if (connection.client.Connected) break; connection = null; } if (connection == null) { connection = new ActiveConnection () { key = key }; connection.client = new TcpClient (); connection.client.Connect (uri.Host, uri.Port); if (useSsl) { connection.stream = new SslStream (connection.client.GetStream (), false, new RemoteCertificateValidationCallback (ValidateServerCertificate)); var ssl = connection.stream as SslStream; ssl.AuthenticateAsClient (uri.Host); } else { connection.stream = connection.client.GetStream (); } } return connection; }
public void Send() { isDone = false; state = RequestState.Waiting; if (acceptGzip) SetHeader ("Accept-Encoding", "gzip"); ThreadPool.QueueUserWorkItem (new WaitCallback (delegate(object t) { try { var retry = 0; while (++retry < maximumRedirects) { if (useCache) { string etag = ""; if (etags.TryGetValue (uri.AbsoluteUri, out etag)) { SetHeader ("If-None-Match", etag); } } var hostHeader = uri.Host; if (uri.Port != 80 && uri.Port != 443) hostHeader += ":" + uri.Port.ToString (); SetHeader ("Host", hostHeader); ActiveConnection connection; try { //pull a connection from the pool (a new one is created if needed) connection = GetClient (uri.Host, uri.Port, uri.Scheme.ToLower () == "https"); } catch (Exception e) { exception = e; response = null; break; } try { WriteToStream (connection.stream); } catch (IOException e) { exception = new IOException ("Server closed the connection:" + e.ToString ()); response = null; break; } response = new Response (this); state = RequestState.Reading; try { response.ReadFromStream (connection.stream); } catch (IOException e) { exception = new IOException ("Server closed the connection:" + e.ToString ()); response = null; break; } switch (response.status) { case 101: upgradedConnection = connection; retry = maximumRedirects; break; case 304: retry = maximumRedirects; break; case 307: case 302: case 301: uri = new Uri (response.GetHeader ("Location")); if (OnRedirect != null) { OnRedirect (uri); retry = maximumRedirects; } break; default: retry = maximumRedirects; break; } //place the connection back into the pool if not upgraded. if(upgradedConnection == null) { if (response.GetHeader ("Connection").ToLower () != "keep-alive") { connection.stream.Close (); } activeConnections [connection.key].Add (connection); } } if (useCache) { string etag = response.GetHeader ("etag"); if (etag.Length > 0) { etags [uri.AbsoluteUri] = etag; SaveEtags (); } } } catch (Exception e) { exception = e; response = null; } state = RequestState.Done; isDone = true; })); }
public void Send() { isDone = false; state = RequestState.Waiting; if (acceptGzip) { SetHeader("Accept-Encoding", "gzip"); } ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object t) { try { var retry = 0; while (++retry < maximumRedirects) { if (useCache) { string etag = ""; if (etags.TryGetValue(uri.AbsoluteUri, out etag)) { SetHeader("If-None-Match", etag); } } var hostHeader = uri.Host; if (uri.Port != 80 && uri.Port != 443) { hostHeader += ":" + uri.Port.ToString(); } SetHeader("Host", hostHeader); ActiveConnection connection; try { //pull a connection from the pool (a new one is created if needed) connection = GetClient(uri.Host, uri.Port, uri.Scheme.ToLower() == "https"); } catch (Exception e) { exception = e; response = null; break; } try { WriteToStream(connection.stream); } catch (IOException e) { exception = new IOException("Server closed the connection:" + e.ToString()); response = null; break; } response = new Response(this); state = RequestState.Reading; try { response.ReadFromStream(connection.stream); } catch (IOException e) { exception = new IOException("Server closed the connection:" + e.ToString()); response = null; break; } switch (response.status) { case 101: upgradedConnection = connection; retry = maximumRedirects; break; case 304: retry = maximumRedirects; break; case 307: case 302: case 301: uri = new Uri(response.GetHeader("Location")); if (OnRedirect != null) { OnRedirect(uri); retry = maximumRedirects; } break; default: retry = maximumRedirects; break; } //place the connection back into the pool if not upgraded. if (upgradedConnection == null) { if (response.GetHeader("Connection").ToLower() != "keep-alive") { connection.stream.Close(); } activeConnections [connection.key].Add(connection); } } if (useCache) { string etag = response.GetHeader("etag"); if (etag.Length > 0) { etags [uri.AbsoluteUri] = etag; SaveEtags(); } } } catch (Exception e) { exception = e; response = null; } state = RequestState.Done; isDone = true; })); }