Example #1
0
		static void OnAccept (object sender, EventArgs e)
		{
			SocketAsyncEventArgs args = (SocketAsyncEventArgs) e;
			EndPointListener epl = (EndPointListener) args.UserToken;
			Socket accepted = null;
			if (args.SocketError == SocketError.Success) {
				accepted = args.AcceptSocket;
				args.AcceptSocket = null;
			}

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

			if (accepted == null)
				return;

			if (epl.secure && (epl.cert == null || epl.key == null)) {
				accepted.Close ();
				return;
			}
			HttpConnection conn = new HttpConnection (accepted, epl, epl.secure, epl.cert, epl.key);
			conn.BeginReadRequest ();
		}
		internal HttpListenerContext (HttpConnection cnc)
		{
			this.cnc = cnc;
			request = new HttpListenerRequest (this);
			response = new HttpListenerResponse (this);
		}
Example #3
0
		internal void AddConnection (HttpConnection cnc)
		{
			connections [cnc] = cnc;
		}
Example #4
0
		internal void RemoveConnection (HttpConnection cnc)
		{
			connections.Remove (cnc);
		}
Example #5
0
		internal void RemoveConnection (HttpConnection conn)
		{
			lock (unregistered) {
				unregistered.Remove (conn);
			}
		}
Example #6
0
		void Cleanup (bool close_existing)
		{
			lock (registry) {
				if (close_existing) {
					// Need to copy this since closing will call UnregisterContext
					ICollection keys = registry.Keys;
					var all = new HttpListenerContext [keys.Count];
					keys.CopyTo (all, 0);
					registry.Clear ();
					for (int i = all.Length - 1; i >= 0; i--)
						all [i].Connection.Close (true);
				}

				lock (connections.SyncRoot) {
					ICollection keys = connections.Keys;
					var conns = new HttpConnection [keys.Count];
					keys.CopyTo (conns, 0);
					connections.Clear ();
					for (int i = conns.Length - 1; i >= 0; i--)
						conns [i].Close (true);
				}
				lock (ctx_queue) {
					var ctxs = (HttpListenerContext []) ctx_queue.ToArray (typeof (HttpListenerContext));
					ctx_queue.Clear ();
					for (int i = ctxs.Length - 1; i >= 0; i--)
						ctxs [i].Connection.Close (true);
				}

				lock (wait_queue) {
					Exception exc = new ObjectDisposedException ("listener");
					foreach (ListenerAsyncResult ares in wait_queue) {
						ares.Complete (exc);
					}
					wait_queue.Clear ();
				}
			}
		}
Example #7
0
        private void Poll()
        {
            try {
                polling = true;

                while (polling) {
                    HttpListenerContext context;
                    try {
                        // The socket to run the service,
                        context = listener.GetContext();
                        // Make sure this ip address is allowed,
                        IPAddress ipAddress = null;
                        if (context.Request.RemoteEndPoint != null)
                            ipAddress = context.Request.RemoteEndPoint.Address;

                        Logger.Info("Connection opened with HTTP client " + ipAddress);

                        // Dispatch the connection to the thread pool,
                        HttpConnection c = new HttpConnection(this);
                        ThreadPool.QueueUserWorkItem(c.Work, context);
                    } catch(IOException e) {
                        Logger.Warning(e);
                    }
                }
            } catch(Exception e) {
                Logger.Error(e.Message);
            }
        }
Example #8
0
        private void Poll()
        {
            try {
                while (polling) {
                    HttpListenerContext context;
                    try {
                        // The socket to run the service,
                        context = listener.GetContext();
                        // Make sure this ip address is allowed,
                        IPAddress ipAddress = context.Request.RemoteEndPoint.Address;

                        Logger.Info("Connection opened with HTTP client " + ipAddress);

                        string ipAddressString = ipAddress.ToString();
                        bool authorized = context.Request.IsLocal || IsAddressAllowed(ipAddressString);

                        authorized = OnClientConnect(ipAddressString, authorized);

                        // Check it's allowed,)
                        if (authorized) {
                            // Dispatch the connection to the thread pool,
                            HttpConnection c = new HttpConnection(this);
                            ThreadPool.QueueUserWorkItem(c.Work, context);
                        } else {
                            context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                            context.Response.Close();
                            Logger.Error(String.Format("The client IP address {0} is not authorized", ipAddressString));
                        }
                    } catch (IOException e) {
                        Logger.Warning(e);
                    }
                }
            } catch(Exception e) {
                Logger.Error(e.Message);
            }
        }
 internal void Reset()
 {
     if (_timer != null)
     {
         _timer.Dispose();
         _timer = null;
     }
     _method = null;
     _path = null;
     _version = null;
     _mreParameters.Reset();
     _connection = null;
     _contentBuffer = new MemoryStream();
     _headers = null;
     _parameters = null;
     _jsonParameter = null;
     _uploadedFiles = null;
     _handlingThread = null;
     _response.Dispose();
     _response = null;
 }
 internal void StartRequest(long id,string[] words,HttpConnection connection,ref HttpParser parser)
 {
     _id = id;
     _method = words[0].ToUpper();
     _path = words[1];
     _version = words[2];
     _mreParameters = new ManualResetEvent(false);
     _connection = connection;
     _contentBuffer = new MemoryStream();
     _requestTimeout = _requestStart.AddMilliseconds(int.MaxValue);
     _headers = new HeaderCollection();
     _requestStart = DateTime.Now;
     _parser = parser;
     _response = new HttpResponse(this);
     parser.RequestHeaderLineRecieved = _RequestHeaderLineReceived;
     parser.RequestHeaderComplete = _RequestHeaderComplete;
     parser.RequestBodyBytesRecieved = _RequestBodyBytesReceived;
     parser.RequestComplete = _RequestComplete;
 }
Example #11
0
		static void OnAccept (IAsyncResult ares)
		{
			EndPointListener epl = (EndPointListener) ares.AsyncState;
			Socket accepted = null;
			try {
				if (epl.sock != null)
					accepted = epl.sock.EndAccept (ares);
			} catch {
				// Anything to do here?
			} finally {
				try {
					if (epl.sock != null)
						epl.sock.BeginAccept (OnAccept, epl);
				} catch {
					if (accepted != null) {
						try {
							accepted.Close ();
						} catch {}
						accepted = null;
					}
				} 
			}

			if (accepted == null)
				return;

			if (epl.secure && (epl.cert == null || epl.key == null)) {
				accepted.Close ();
				return;
			}
			HttpConnection conn = new HttpConnection (accepted, epl, epl.secure, epl.cert, epl.key);
			conn.BeginReadRequest ();
		}
Example #12
0
 public Catalog(HttpConnection con)
 {
     _connection = con;
 }
Example #13
0
		static void ProcessAccept (SocketAsyncEventArgs args) 
		{
			Socket accepted = null;
			if (args.SocketError == SocketError.Success)
				accepted = args.AcceptSocket;

			EndPointListener epl = (EndPointListener) args.UserToken;


			Accept (epl.sock, args, ref accepted);
			if (accepted == null)
				return;

			if (epl.secure && epl.cert == null) {
				accepted.Close ();
				return;
			}
			HttpConnection conn = new HttpConnection (accepted, epl, epl.secure, epl.cert);
			lock (epl.unregistered) {
				epl.unregistered [conn] = conn;
			}
			conn.BeginReadRequest ();
		}