| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875 |
- //
- // System.Net.WebConnection
- //
- // Authors:
- // Gonzalo Paniagua Javier ([email protected])
- //
- // (C) 2003 Ximian, Inc (http://www.ximian.com)
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System.IO;
- using System.Collections;
- using System.Net.Sockets;
- using System.Reflection;
- using System.Security.Cryptography.X509Certificates;
- using System.Text;
- using System.Threading;
- namespace System.Net
- {
- enum ReadState
- {
- None,
- Status,
- Headers,
- Content
- }
-
- class WebConnection
- {
- ServicePoint sPoint;
- Stream nstream;
- Socket socket;
- WebExceptionStatus status;
- WebConnectionGroup group;
- bool busy;
- WaitOrTimerCallback initConn;
- bool keepAlive;
- byte [] buffer;
- static AsyncCallback readDoneDelegate = new AsyncCallback (ReadDone);
- EventHandler abortHandler;
- ReadState readState;
- internal WebConnectionData Data;
- bool chunkedRead;
- ChunkStream chunkStream;
- AutoResetEvent goAhead;
- Queue queue;
- bool reused;
- int position;
- delegate void ReadAllHandler ();
- ReadAllHandler RAll;
- bool ssl;
- bool certsAvailable;
- static object classLock = new object ();
- static Type sslStream;
- static PropertyInfo piClient;
- static PropertyInfo piServer;
- public WebConnection (WebConnectionGroup group, ServicePoint sPoint)
- {
- this.group = group;
- this.sPoint = sPoint;
- buffer = new byte [4096];
- readState = ReadState.None;
- Data = new WebConnectionData ();
- initConn = new WaitOrTimerCallback (InitConnection);
- abortHandler = new EventHandler (Abort);
- goAhead = new AutoResetEvent (true);
- queue = group.Queue;
- }
- bool CanReuse ()
- {
- // The real condition is !(socket.Poll (0, SelectMode.SelectRead) || socket.Available != 0)
- // but if there's data pending to read (!) we won't reuse the socket.
- return (socket.Poll (0, SelectMode.SelectRead) == false);
- }
-
- void Connect ()
- {
- lock (this) {
- if (socket != null && socket.Connected && status == WebExceptionStatus.Success) {
- // Take the chunked stream to the expected state (State.None)
- if (CanReuse () && CompleteChunkedRead ()) {
- reused = true;
- return;
- }
- }
- reused = false;
- if (socket != null) {
- socket.Close();
- socket = null;
- }
- chunkStream = null;
- IPHostEntry hostEntry = sPoint.HostEntry;
- if (hostEntry == null) {
- status = sPoint.UsesProxy ? WebExceptionStatus.ProxyNameResolutionFailure :
- WebExceptionStatus.NameResolutionFailure;
- return;
- }
- foreach (IPAddress address in hostEntry.AddressList) {
- socket = new Socket (address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
- try {
- socket.Connect (new IPEndPoint(address, sPoint.Address.Port));
- status = WebExceptionStatus.Success;
- break;
- } catch (SocketException) {
- socket.Close();
- socket = null;
- status = WebExceptionStatus.ConnectFailure;
- }
- }
- }
- }
- static void EnsureSSLStreamAvailable ()
- {
- lock (classLock) {
- if (sslStream != null)
- return;
- // HttpsClientStream is an internal glue class in Mono.Security.dll
- sslStream = Type.GetType ("Mono.Security.Protocol.Tls.HttpsClientStream, " +
- Consts.AssemblyMono_Security, false);
- if (sslStream == null) {
- string msg = "Missing Mono.Security.dll assembly. " +
- "Support for SSL/TLS is unavailable.";
- throw new NotSupportedException (msg);
- }
- piClient = sslStream.GetProperty ("SelectedClientCertificate");
- piServer = sslStream.GetProperty ("ServerCertificate");
- }
- }
- bool CreateTunnel (HttpWebRequest request, Stream stream, out byte [] buffer)
- {
- StringBuilder sb = new StringBuilder ();
- sb.Append ("CONNECT ");
- sb.Append (request.Address.Host);
- sb.Append (':');
- sb.Append (request.Address.Port);
- sb.Append (" HTTP/");
- if (request.ServicePoint.ProtocolVersion == HttpVersion.Version11)
- sb.Append ("1.1");
- else
- sb.Append ("1.0");
- sb.Append ("\r\nHost: ");
- sb.Append (request.Address.Authority);
- if (request.Headers ["Proxy-Authorization"] != null) {
- sb.Append ("\r\nProxy-Authorization: ");
- sb.Append (request.Headers ["Proxy-Authorization"]);
- }
-
- sb.Append ("\r\n\r\n");
- byte [] connectBytes = Encoding.Default.GetBytes (sb.ToString ());
- stream.Write (connectBytes, 0, connectBytes.Length);
- return ReadHeaders (request, stream, out buffer);
- }
- bool ReadHeaders (HttpWebRequest request, Stream stream, out byte [] retBuffer)
- {
- retBuffer = null;
- byte [] buffer = new byte [256];
- MemoryStream ms = new MemoryStream ();
- bool gotStatus = false;
- while (true) {
- int n = stream.Read (buffer, 0, 256);
- if (n == 0) {
- HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadHeders");
- return false;
- }
-
- ms.Write (buffer, 0, n);
- int start = 0;
- string str = null;
- while (ReadLine (ms.GetBuffer (), ref start, (int) ms.Length, ref str)) {
- if (str == null) {
- if (ms.Length - start > 0) {
- retBuffer = new byte [ms.Length - start];
- Buffer.BlockCopy (ms.GetBuffer (), start, retBuffer, 0, retBuffer.Length);
- }
- return true;
- }
- if (gotStatus)
- continue;
- int spaceidx = str.IndexOf (' ');
- if (spaceidx == -1)
- throw new Exception ();
- int resultCode = Int32.Parse (str.Substring (spaceidx + 1, 3));
- if (resultCode != 200)
- throw new Exception ();
- gotStatus = true;
- }
- }
- }
- bool CreateStream (HttpWebRequest request)
- {
- try {
- NetworkStream serverStream = new NetworkStream (socket, false);
- if (request.Address.Scheme == Uri.UriSchemeHttps) {
- ssl = true;
- EnsureSSLStreamAvailable ();
- if (!reused || nstream == null || nstream.GetType () != sslStream) {
- byte [] buffer = null;
- if (sPoint.UseConnect) {
- bool ok = CreateTunnel (request, serverStream, out buffer);
- if (!ok)
- return false;
- }
- object[] args = new object [4] { serverStream,
- request.ClientCertificates,
- request, buffer};
- nstream = (Stream) Activator.CreateInstance (sslStream, args);
- }
- // we also need to set ServicePoint.Certificate
- // and ServicePoint.ClientCertificate but this can
- // only be done later (after handshake - which is
- // done only after a read operation).
- } else {
- ssl = false;
- nstream = serverStream;
- }
- } catch (Exception) {
- status = WebExceptionStatus.ConnectFailure;
- return false;
- }
- return true;
- }
-
- void HandleError (WebExceptionStatus st, Exception e, string where)
- {
- status = st;
- lock (this) {
- if (st == WebExceptionStatus.RequestCanceled)
- Data = new WebConnectionData ();
- }
- if (e == null) { // At least we now where it comes from
- try {
- throw new Exception (new System.Diagnostics.StackTrace ().ToString ());
- } catch (Exception e2) {
- e = e2;
- }
- }
- HttpWebRequest req = null;
- if (Data != null && Data.request != null)
- req = Data.request;
- Close (true);
- req.SetResponseError (st, e, where);
- }
-
- static void ReadDone (IAsyncResult result)
- {
- WebConnection cnc = (WebConnection) result.AsyncState;
- WebConnectionData data = cnc.Data;
- Stream ns = cnc.nstream;
- if (ns == null) {
- cnc.Close (true);
- return;
- }
- int nread = -1;
- try {
- nread = ns.EndRead (result);
- } catch (Exception e) {
- cnc.HandleError (WebExceptionStatus.ReceiveFailure, e, "ReadDone1");
- return;
- }
- if (nread == 0) {
- cnc.HandleError (WebExceptionStatus.ReceiveFailure, null, "ReadDone2");
- return;
- }
- if (nread < 0) {
- cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadDone3");
- return;
- }
- int pos = -1;
- nread += cnc.position;
- if (cnc.readState == ReadState.None) {
- Exception exc = null;
- try {
- pos = cnc.GetResponse (cnc.buffer, nread);
- } catch (Exception e) {
- exc = e;
- }
- if (exc != null) {
- cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, exc, "ReadDone4");
- return;
- }
- }
- if (cnc.readState != ReadState.Content) {
- int est = nread * 2;
- int max = (est < cnc.buffer.Length) ? cnc.buffer.Length : est;
- byte [] newBuffer = new byte [max];
- Buffer.BlockCopy (cnc.buffer, 0, newBuffer, 0, nread);
- cnc.buffer = newBuffer;
- cnc.position = nread;
- cnc.readState = ReadState.None;
- InitRead (cnc);
- return;
- }
- cnc.position = 0;
- WebConnectionStream stream = new WebConnectionStream (cnc);
- cnc.RAll = new ReadAllHandler (stream.ReadAll);
- string contentType = data.Headers ["Transfer-Encoding"];
- cnc.chunkedRead = (contentType != null && contentType.ToLower ().IndexOf ("chunked") != -1);
- if (!cnc.chunkedRead) {
- stream.ReadBuffer = cnc.buffer;
- stream.ReadBufferOffset = pos;
- stream.ReadBufferSize = nread;
- } else if (cnc.chunkStream == null) {
- try {
- cnc.chunkStream = new ChunkStream (cnc.buffer, pos, nread, data.Headers);
- } catch (Exception e) {
- cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, e, "ReadDone5");
- return;
- }
- } else {
- cnc.chunkStream.ResetBuffer ();
- try {
- cnc.chunkStream.Write (cnc.buffer, pos, nread);
- } catch (Exception e) {
- cnc.HandleError (WebExceptionStatus.ServerProtocolViolation, e, "ReadDone6");
- return;
- }
- }
- data.stream = stream;
-
- if (!ExpectContent (data.StatusCode))
- stream.ForceCompletion ();
- lock (cnc) {
- lock (cnc.queue) {
- if (cnc.queue.Count > 0) {
- stream.ReadAll ();
- } else {
- stream.CheckComplete ();
- }
- }
- }
-
- data.request.SetResponseData (data);
- }
- static bool ExpectContent (int statusCode)
- {
- return (statusCode >= 200 && statusCode != 204 && statusCode != 304);
- }
- internal void GetCertificates ()
- {
- // here the SSL negotiation have been done
- X509Certificate client = (X509Certificate) piClient.GetValue (nstream, null);
- X509Certificate server = (X509Certificate) piServer.GetValue (nstream, null);
- sPoint.SetCertificates (client, server);
- certsAvailable = (server != null);
- }
-
- static void InitRead (object state)
- {
- WebConnection cnc = (WebConnection) state;
- Stream ns = cnc.nstream;
- try {
- int size = cnc.buffer.Length - cnc.position;
- ns.BeginRead (cnc.buffer, cnc.position, size, readDoneDelegate, cnc);
- } catch (Exception e) {
- cnc.HandleError (WebExceptionStatus.ReceiveFailure, e, "InitRead");
- }
- }
-
- int GetResponse (byte [] buffer, int max)
- {
- int pos = 0;
- string line = null;
- bool lineok = false;
- bool isContinue = false;
- bool emptyFirstLine = false;
- do {
- if (readState == ReadState.None) {
- lineok = ReadLine (buffer, ref pos, max, ref line);
- if (!lineok)
- return -1;
- if (line == null) {
- emptyFirstLine = true;
- continue;
- }
- emptyFirstLine = false;
- readState = ReadState.Status;
- string [] parts = line.Split (' ');
- if (parts.Length < 2)
- return -1;
- if (String.Compare (parts [0], "HTTP/1.1", true) == 0) {
- Data.Version = HttpVersion.Version11;
- sPoint.SetVersion (HttpVersion.Version11);
- } else {
- Data.Version = HttpVersion.Version10;
- sPoint.SetVersion (HttpVersion.Version10);
- }
- Data.StatusCode = (int) UInt32.Parse (parts [1]);
- if (parts.Length >= 3)
- Data.StatusDescription = String.Join (" ", parts, 2, parts.Length - 2);
- else
- Data.StatusDescription = "";
- if (pos >= max)
- return pos;
- }
- emptyFirstLine = false;
- if (readState == ReadState.Status) {
- readState = ReadState.Headers;
- Data.Headers = new WebHeaderCollection ();
- ArrayList headers = new ArrayList ();
- bool finished = false;
- while (!finished) {
- if (ReadLine (buffer, ref pos, max, ref line) == false)
- break;
-
- if (line == null) {
- // Empty line: end of headers
- finished = true;
- continue;
- }
-
- if (line.Length > 0 && (line [0] == ' ' || line [0] == '\t')) {
- int count = headers.Count - 1;
- if (count < 0)
- break;
- string prev = (string) headers [count] + line;
- headers [count] = prev;
- } else {
- headers.Add (line);
- }
- }
- if (!finished)
- return -1;
- foreach (string s in headers)
- Data.Headers.SetInternal (s);
- if (Data.StatusCode == (int) HttpStatusCode.Continue) {
- sPoint.SendContinue = true;
- if (pos >= max)
- return pos;
- if (Data.request.ExpectContinue) {
- Data.request.DoContinueDelegate (Data.StatusCode, Data.Headers);
- // Prevent double calls when getting the
- // headers in several packets.
- Data.request.ExpectContinue = false;
- }
- readState = ReadState.None;
- isContinue = true;
- }
- else {
- readState = ReadState.Content;
- return pos;
- }
- }
- } while (emptyFirstLine || isContinue);
- return -1;
- }
-
- void InitConnection (object state, bool notUsed)
- {
- HttpWebRequest request = (HttpWebRequest) state;
- if (status == WebExceptionStatus.RequestCanceled) {
- busy = false;
- Data = new WebConnectionData ();
- goAhead.Set ();
- SendNext ();
- return;
- }
- keepAlive = request.KeepAlive;
- Data = new WebConnectionData ();
- Data.request = request;
- Connect ();
- if (status != WebExceptionStatus.Success) {
- request.SetWriteStreamError (status);
- Close (true);
- return;
- }
-
- if (!CreateStream (request)) {
- request.SetWriteStreamError (status);
- Close (true);
- return;
- }
- readState = ReadState.None;
- request.SetWriteStream (new WebConnectionStream (this, request));
- InitRead (this);
- }
-
- internal EventHandler SendRequest (HttpWebRequest request)
- {
- lock (this) {
- if (RAll != null) {
- RAll.BeginInvoke (null, null);
- RAll = null;
- }
- if (!busy) {
- busy = true;
- ThreadPool.RegisterWaitForSingleObject (goAhead, initConn,
- request, -1, true);
- } else {
- lock (queue) {
- queue.Enqueue (request);
- }
- }
- }
- return abortHandler;
- }
-
- void SendNext ()
- {
- lock (queue) {
- if (queue.Count > 0) {
- SendRequest ((HttpWebRequest) queue.Dequeue ());
- }
- }
- }
- internal void NextRead ()
- {
- lock (this) {
- busy = false;
- string header = (sPoint.UsesProxy) ? "Proxy-Connection" : "Connection";
- string cncHeader = (Data.Headers != null) ? Data.Headers [header] : null;
- bool keepAlive = (Data.Version == HttpVersion.Version11 && this.keepAlive);
- if (cncHeader != null) {
- cncHeader = cncHeader.ToLower ();
- keepAlive = (this.keepAlive && cncHeader.IndexOf ("keep-alive") != -1);
- }
- if ((socket != null && !socket.Connected) ||
- (!keepAlive || (cncHeader != null && cncHeader.IndexOf ("close") != -1))) {
- Close (false);
- }
- goAhead.Set ();
- lock (queue) {
- if (queue.Count > 0) {
- SendRequest ((HttpWebRequest) queue.Dequeue ());
- }
- }
- }
- }
-
- static bool ReadLine (byte [] buffer, ref int start, int max, ref string output)
- {
- bool foundCR = false;
- StringBuilder text = new StringBuilder ();
- int c = 0;
- while (start < max) {
- c = (int) buffer [start++];
- if (c == '\n') { // newline
- if ((text.Length > 0) && (text [text.Length - 1] == '\r'))
- text.Length--;
- foundCR = false;
- break;
- } else if (foundCR) {
- text.Length--;
- break;
- }
- if (c == '\r')
- foundCR = true;
-
- text.Append ((char) c);
- }
- if (c != '\n' && c != '\r')
- return false;
- if (text.Length == 0) {
- output = null;
- return (c == '\n' || c == '\r');
- }
- if (foundCR)
- text.Length--;
- output = text.ToString ();
- return true;
- }
- internal IAsyncResult BeginRead (byte [] buffer, int offset, int size, AsyncCallback cb, object state)
- {
- if (nstream == null)
- return null;
- IAsyncResult result = null;
- if (!chunkedRead || chunkStream.WantMore) {
- try {
- result = nstream.BeginRead (buffer, offset, size, cb, state);
- } catch (Exception) {
- HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked BeginRead");
- throw;
- }
- }
- if (chunkedRead) {
- WebAsyncResult wr = new WebAsyncResult (null, null, buffer, offset, size);
- wr.InnerAsyncResult = result;
- return wr;
- }
- return result;
- }
-
- internal int EndRead (IAsyncResult result)
- {
- if (nstream == null)
- return 0;
- if (chunkedRead) {
- WebAsyncResult wr = (WebAsyncResult) result;
- int nbytes = 0;
- if (wr.InnerAsyncResult != null)
- nbytes = nstream.EndRead (wr.InnerAsyncResult);
- bool done = (nbytes == 0);
- try {
- chunkStream.WriteAndReadBack (wr.Buffer, wr.Offset, wr.Size, ref nbytes);
- if (!done && nbytes == 0 && chunkStream.WantMore)
- nbytes = EnsureRead (wr.Buffer, wr.Offset, wr.Size);
- } catch (Exception e) {
- if (e is WebException)
- throw e;
- throw new WebException ("Invalid chunked data.", e,
- WebExceptionStatus.ServerProtocolViolation, null);
- }
- if ((done || nbytes == 0) && chunkStream.WantMore) {
- HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked EndRead");
- throw new WebException ("Read error", null, WebExceptionStatus.ReceiveFailure, null);
- }
- return nbytes;
- }
- return nstream.EndRead (result);
- }
- // To be called on chunkedRead when we can read no data from the ChunkStream yet
- int EnsureRead (byte [] buffer, int offset, int size)
- {
- byte [] morebytes = null;
- int nbytes = 0;
- while (nbytes == 0 && chunkStream.WantMore) {
- int localsize = chunkStream.ChunkLeft;
- if (localsize <= 0) // not read chunk size yet
- localsize = 1024;
- else if (localsize > 16384)
- localsize = 16384;
- if (morebytes == null || morebytes.Length < localsize)
- morebytes = new byte [localsize];
- int nread = nstream.Read (morebytes, 0, localsize);
- if (nread <= 0)
- return 0; // Error
- chunkStream.Write (morebytes, 0, nread);
- nbytes += chunkStream.Read (buffer, offset + nbytes, size - nbytes);
- }
- return nbytes;
- }
- bool CompleteChunkedRead()
- {
- if (!chunkedRead || chunkStream == null)
- return true;
- while (chunkStream.WantMore) {
- int nbytes = nstream.Read (buffer, 0, buffer.Length);
- if (nbytes <= 0)
- return false; // Socket was disconnected
- chunkStream.Write (buffer, 0, nbytes);
- }
- return true;
- }
- internal IAsyncResult BeginWrite (byte [] buffer, int offset, int size, AsyncCallback cb, object state)
- {
- IAsyncResult result = null;
- if (nstream == null)
- return null;
- try {
- result = nstream.BeginWrite (buffer, offset, size, cb, state);
- } catch (Exception) {
- status = WebExceptionStatus.SendFailure;
- throw;
- }
- return result;
- }
- internal void EndWrite (IAsyncResult result)
- {
- if (nstream != null)
- nstream.EndWrite (result);
- }
- internal int Read (byte [] buffer, int offset, int size)
- {
- if (nstream == null)
- return 0;
- int result = 0;
- try {
- bool done = false;
- if (!chunkedRead) {
- result = nstream.Read (buffer, offset, size);
- done = (result == 0);
- }
- if (chunkedRead) {
- try {
- chunkStream.WriteAndReadBack (buffer, offset, size, ref result);
- if (!done && result == 0 && chunkStream.WantMore)
- result = EnsureRead (buffer, offset, size);
- } catch (Exception e) {
- HandleError (WebExceptionStatus.ReceiveFailure, e, "chunked Read1");
- throw;
- }
- if ((done || result == 0) && chunkStream.WantMore) {
- HandleError (WebExceptionStatus.ReceiveFailure, null, "chunked Read2");
- throw new WebException ("Read error", null, WebExceptionStatus.ReceiveFailure, null);
- }
- }
- } catch (Exception e) {
- HandleError (WebExceptionStatus.ReceiveFailure, e, "Read");
- }
- return result;
- }
- internal void Write (byte [] buffer, int offset, int size)
- {
- if (nstream == null)
- return;
- try {
- nstream.Write (buffer, offset, size);
- // here SSL handshake should have been done
- if (ssl && !certsAvailable)
- GetCertificates ();
- } catch (Exception e) {
- if (e is WebException)
- throw e;
- HandleError (WebExceptionStatus.SendFailure, e, "Write");
- throw new WebException ("Not connected", e, WebExceptionStatus.SendFailure, null);
- }
- }
- void Close (bool sendNext)
- {
- lock (this) {
- busy = false;
- RAll = null;
- if (nstream != null) {
- try {
- nstream.Close ();
- } catch {}
- nstream = null;
- }
- if (socket != null) {
- try {
- socket.Close ();
- } catch {}
- socket = null;
- }
- goAhead.Set ();
- if (sendNext)
- SendNext ();
- }
- }
- void Abort (object sender, EventArgs args)
- {
- HandleError (WebExceptionStatus.RequestCanceled, null, "Abort");
- }
- internal bool Busy {
- get { lock (this) return busy; }
- }
-
- internal bool Connected {
- get {
- lock (this) {
- return (socket != null && socket.Connected);
- }
- }
- }
- }
- }
|