Summary description for ByteVector.
Exemplo n.º 1
0
 internal void Add(ByteVector data)
 {
     Add(data.Data, 0, data.Size);
 }
		int AnalyzeReply(ByteVector reply, out string reason)
		{
			if(0 == reply.Size)
				throw new SocketException(SockErrors.WSAECONNREFUSED);
				//throw new ProxyErrorException("Web proxy close the connection.");

			//reply should be in following form:
			//"HTTP/x.x xxx reason(\r)?\n"
			string replyStr = Encoding.ASCII.GetString(reply.Data, 0, reply.Size);
			Match m = _replyRegEx.Match(replyStr);

			if((reply.Size < 14) || (m.Groups.Count != 4))
				throw new ProtocolViolationException("Web proxy reply is incorrect.");

			int code = int.Parse(m.Groups["code"].Value);
			reason = m.Groups["reason"].Value;

			return code;
		}
		ByteVector ReadReply()
		{
			ByteVector reply = new ByteVector();
			byte[] buf = new byte[512];
			while(true)
			{
				int num = Receive(buf);
				if(0 == num)
					break;

				reply.Add(buf, 0, num);

				//handle the end of reply
				int afterEndPos = FindReplyEnd(reply.Data, reply.Size);
				if(afterEndPos > 0)
				{
					if(afterEndPos < num) //read after reply finished?
					{
						//put data back into the buffer for further
						//processing in receive functions
						PutBufferData(buf, afterEndPos, num - afterEndPos);
						reply.CutTail(num - afterEndPos);
					}
					break;
				}

				if(reply.Size > _maxReplySize)
					throw new ProtocolViolationException("Web proxy reply exceed maximum length.");
			}
			return reply;
		}
Exemplo n.º 4
0
        override internal void Connect(string hostName, int hostPort)
        {
            CheckDisposed();

            SetProgress(true);
            try
            {
                if (null == hostName)
                {
                    throw new ArgumentNullException("hostName", "The value cannot be null.");
                }

                if (hostPort < IPEndPoint.MinPort || hostPort > IPEndPoint.MaxPort)
                {
                    throw new ArgumentOutOfRangeException("hostPort", "Value, specified for the port is out of the valid range.");
                }

                //------------------------------------
                // Get end point for the proxy server
                IPHostEntry proxyEntry = GetHostByName(_proxyServer);
                if (null == proxyEntry)
                {
                    throw new SocketException(SockErrors.WSAHOST_NOT_FOUND);
                }
                //throw new HostNotFoundException("Unable to resolve proxy name.");

                IPEndPoint proxyEndPoint;
                if (_proxyServer.Equals("127.0.0.1"))
                {
                    proxyEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), _proxyPort);
                }
                else
                {
                    proxyEndPoint = ConstructEndPoint(proxyEntry, _proxyPort);
                }

                //------------------------------------------
                // Connect to proxy server
                _socket.Connect(proxyEndPoint);

                bool useCredentials = PreAuthenticate;
                while (true)
                {
                    //------------------------------------------
                    // Send CONNECT command
                    byte[] cmd = GetConnectCmd(hostName, hostPort, useCredentials);
                    NStream.Write(cmd, 0, cmd.Length);

                    //-----------------------------------------
                    // Read the reply
                    ByteVector reply = ReadReply();

                    //------------------------------------------
                    // Analyze reply
                    string reason = null;
                    int    code   = AnalyzeReply(reply, out reason);

                    //------------------------------------------
                    //is good return code?
                    if (code >= 200 && code <= 299)
                    {
                        return;
                    }

                    //------------------------------------------
                    //If Proxy Authentication Required
                    //but we do not issued it, then try again
                    if ((407 == code) &&
                        !useCredentials &&
                        (_proxyUser != null))
                    {
                        useCredentials = true;
                        continue;
                    }

                    //string msg = string.Format("Connection refused by web proxy: {0} ({1}).", reason, code);
                    //throw new ProxyErrorException(msg);
                    throw new SocketException(SockErrors.WSAECONNREFUSED);
                }
            }
            finally
            {
                SetProgress(false);
            }
        }