예제 #1
0
    private static void processAccepted (Socket socket, EndPointListener listener)
    {
      HttpConnection conn = null;
      try {
        conn = new HttpConnection (socket, listener);
        lock (listener._unregisteredSync)
          listener._unregistered[conn] = conn;

        conn.BeginReadRequest ();
      }
      catch {
        if (conn != null) {
          conn.Close (true);
          return;
        }

        socket.Close ();
      }
    }
예제 #2
0
		/// <summary>
		/// Connects to the server specified by the endpoint, sends the request and waits for a response.
		/// </summary>
		/// <param name="ep">The address:port of the server</param>
		/// <param name="request">The request to send</param>
		/// <param name="exception">Any exception that is throw or encountered during the request/response session with the server</param>
		/// <param name="verbose">A flag that indicates the connection's verbosity level, true for more messages</param>
		/// <returns></returns>
		public static HttpResponse GetResponse(
			IPEndPoint ep,
			HttpRequest request, 
			out Exception exception, 
			bool verbose,
			EventHandler<HttpMessageProgressEventArgs> onSendProgress,
			EventHandler<HttpMessageProgressEventArgs> onRecvProgress,
			object stateObject)
		{
			#region Params Validation

			if (ep == null)
				throw new ArgumentNullException("ep");

			if (request == null)
				throw new ArgumentNullException("request");

			exception = null;

			#endregion

			// define a connection for this request/response session
			HttpConnection connection = null;

			try
			{
				// create a connection to the remote end point
				connection = new HttpConnection(ep, false, verbose, HttpOptions.SendTimeout, HttpOptions.RecvTimeout);
				
				// return a response from the server
				return HttpRequest.GetResponse(connection, request, out exception, onSendProgress, onRecvProgress, stateObject);
			}
            catch (ThreadAbortException)
            {
                
            }
			catch(Exception ex)
			{
                //Debug.WriteLine(ex);
				exception = ex;
			}
			finally
			{
				// always try and close the connect up afterwards
				if (connection != null)
					connection.Close();
			}
			return null;
		}
예제 #3
0
        public void HttpConnection_Receive_Fail()
        {
            EnhancedSocket sockListen = null;
            EnhancedSocket sockAccept = null;
            HttpConnection con;
            HttpRequest    request;
            HttpResponse   response;
            TimeSpan       orgTimeout;
            IAsyncResult   ar;

            orgTimeout = HttpStack.TimeoutSweepInterval;
            HttpStack.TimeoutSweepInterval = TimeSpan.FromMilliseconds(250);

            sockListen = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sockListen.Bind(new IPEndPoint(IPAddress.Any, ServerPort));
            sockListen.Listen(10);

            try
            {
                ar = sockListen.BeginAccept(null, null);

                con = new HttpConnection(HttpOption.None);
                con.Connect("http://localhost:" + ServerPort.ToString());

                sockAccept = sockListen.EndAccept(ar);

                request = new HttpRequest("GET", "/foo.htm", null);
                request["Content-Length"] = request.Content.Size.ToString();

                ar = con.BeginQuery(request, DateTime.MaxValue, null, null);
                Thread.Sleep(100);

                sockAccept.Close();
                sockAccept = null;

                try
                {
                    response = con.EndQuery(ar);
                    Assert.Fail();
                }
                catch
                {
                }

                Assert.IsTrue(con.IsClosed);
                con.Close();
            }
            finally
            {
                HttpStack.TimeoutSweepInterval = orgTimeout;

                if (sockListen != null)
                {
                    sockListen.Close();
                }

                if (sockAccept != null)
                {
                    sockAccept.Close();
                }
            }
        }
예제 #4
0
    private static void onAccept (object sender, EventArgs e)
    {
      var args = (SocketAsyncEventArgs) e;
      var listener = (EndPointListener) args.UserToken;
      Socket accepted = null;
      if (args.SocketError == SocketError.Success) {
        accepted = args.AcceptSocket;
        args.AcceptSocket = null;
      }

      try {
        listener._socket.AcceptAsync (args);
      }
      catch {
        if (accepted != null)
          accepted.Close ();

        return;
      }

      if (accepted == null)
        return;

      HttpConnection conn = null;
      try {
        conn = new HttpConnection (accepted, listener);
        lock (((ICollection) listener._unregistered).SyncRoot)
          listener._unregistered [conn] = conn;

        conn.BeginReadRequest ();
      }
      catch {
        if (conn != null) {
          conn.Close (true);
          return;
        }

        accepted.Close ();
      }
    }
예제 #5
0
        private HttpResponse Navigate(HttpRequest request, HttpBehavior httpBehavior)
        {
            bool         ContinueRedirect = true;
            HttpResponse response         = null;

            HttpConnectionFactory connFactory = new HttpConnectionFactory();
            HttpConnection        connection  = connFactory.GetConnnection(request.Uri, this.m_proxy);

            connection.Timeout = Timeout;

            HttpBehavior.RedirectStep rs = null;
            string redirectUri           = null;
            int    responseCode          = 0;
            int    redirectCounter       = 0;

            try {
                while (ContinueRedirect)
                {
                    try {
                        response     = SendRequestAndGetResponse(connection, request);
                        redirectUri  = response.Location;
                        responseCode = response.ResponseCode;

                        // response code 100 means that we need to wait for another response
                        // and receive the response from the socket again on the same connection
                        if (responseCode == 100)
                        {
                            response     = GetResponse(connection);
                            redirectUri  = response.Location;
                            responseCode = response.ResponseCode;
                        }

                        if (httpBehavior != null)
                        {
                            rs = httpBehavior.GetNextStep();
                            rs.Compare(responseCode, redirectUri);
                            ContinueRedirect = !httpBehavior.IsEmpty();
                        }
                        else
                        {
                            ContinueRedirect = (redirectCounter < this.m_maxRedirects && (responseCode == 301 || responseCode == 302));
                            redirectCounter++;
                        }

                        if (ContinueRedirect)
                        {
                            request = new HttpGet(new Uri(redirectUri));
                            // make sure the connection is still open and redirect url is from the same host
                            connection         = connFactory.GetConnnection(request.Uri, this.m_proxy, connection);
                            connection.Timeout = Timeout;
                        }
                    } catch (Exception ex) {
                        int i = 0;
                        throw ex;
                    }
                }
            } finally {
                connection.Close();
            }
            return(response);
        }