Пример #1
0
		protected override void DoRequestNotHandled(ContextRFC AContext, HttpRequestInfo ARequestInfo, HttpResponseInfo AResponseInfo) {
			bool LHandled = false;
			if (mOnRequestNotHandled != null) {
				mOnRequestNotHandled(AContext, ARequestInfo, AResponseInfo, ref LHandled);
			}
			if (!LHandled) {
				AResponseInfo.ContentText = "<html><body><h3>The server didn't handle your request.</h3></body></html>";
			}
		}
Пример #2
0
        private void ProcessBannedRequest(HttpRequestInfo request, HttpResponseInfo response)
        {
            string page = File.ReadAllText(
                string.Format(@"{0}\BannedSitePage.htm", 
                new FileInfo(this.GetType().Assembly.Location).Directory));

            response.ResponseNo = 200;
            response.ContentText = page;                        
        }
Пример #3
0
		protected override void DoCommand(ContextRFC context, HttpRequestInfo request, HttpResponseInfo response, ref bool handled) {
			switch (request.CommandType) {
				case HttpCommandEnum.Get: {
						if (mOnCommandGet != null) {
							mOnCommandGet(context, request, response, ref handled);
						}
						break;
					}
				default: {
						if (mOnCommandOther != null) {
							mOnCommandOther(context, request, response, ref handled);
						}
						break;
					}
			}
		}
Пример #4
0
		protected override void DoCommand(ContextRFC context, HttpRequestInfo request, HttpResponseInfo response, ref bool handled) {
            // first raise events is user want to handle request by event
            switch (request.CommandType)
            {
                case HttpCommandEnum.Connect:
                    if (OnCommandConnect != null) OnCommandConnect(context, request, response, ref handled);
                    break;
                case HttpCommandEnum.Delete:
                    if (OnCommandDelete != null) OnCommandDelete(context, request, response, ref handled);
                    break;
                case HttpCommandEnum.Get:
                    if (OnCommandGet != null) OnCommandGet(context, request, response, ref handled);
                    break;
                case HttpCommandEnum.Head:
                    if (OnCommandHead != null) OnCommandHead(context, request, response, ref handled);
                    break;
                case HttpCommandEnum.Options:
                    if (OnCommandOptions != null) OnCommandOptions(context, request, response, ref handled);
                    break;
                case HttpCommandEnum.Post:
                    if (OnCommandPost != null) OnCommandPost(context, request, response, ref handled);
                    break;
                case HttpCommandEnum.Put:
                    if (OnCommandPut != null) OnCommandPut(context, request, response, ref handled);
                    break;
                case HttpCommandEnum.Trace:
                    if (OnCommandTrace != null) OnCommandTrace(context, request, response, ref handled);
                    break;
                default:
                    if (OnCommandOther != null) OnCommandOther(context, request, response, ref handled);
                    break;
            }
            // if request handled from an event handler skip request handlers
            if (handled) return;

            // try to handle request by request handlers collection
            RequestHandlersLock.AcquireReaderLock(-1);
			try {
				for (int i = 0; i < mRequestHandlers.Count && !handled; i++) {
                    mRequestHandlers[i].HandleCommand(this, request, response, ref handled);
				}
			} finally {
				RequestHandlersLock.ReleaseReaderLock();
			}
		}
Пример #5
0
        public void HandleCommand(HttpServer server, HttpRequestInfo request, HttpResponseInfo response, ref bool handled)
        {
            if (request.CommandType != HttpCommandEnum.Get) return;

            Logger.Log("Command: {0}",request.RawHttpCommand);

            handled = true;
            try
            {

                if (IsBannedUri(request.RequestURI))
                {
                    ProcessBannedRequest(request, response);
                }
                else
                    ProcessRequest(request, response);
            }
            catch (Exception ex)
            {
                Logger.Log("Error: {0} on command {1}", ex.Message, request.RequestURI);
            }

        }
Пример #6
0
        protected override void DoCommand(ContextRFC context, HttpRequestInfo request, HttpResponseInfo response, ref bool handled)
        {
            switch (request.CommandType)
            {
            case HttpCommandEnum.Get: {
                if (mOnCommandGet != null)
                {
                    mOnCommandGet(context, request, response, ref handled);
                }
                break;
            }

            default: {
                if (mOnCommandOther != null)
                {
                    mOnCommandOther(context, request, response, ref handled);
                }
                break;
            }
            }
        }
Пример #7
0
        protected override void DoRequestNotHandled(ContextRFC AContext, HttpRequestInfo ARequestInfo, HttpResponseInfo AResponseInfo)
        {
            bool LHandled = false;

            if (mOnRequestNotHandled != null)
            {
                mOnRequestNotHandled(AContext, ARequestInfo, AResponseInfo, ref LHandled);
            }
            if (!LHandled)
            {
                AResponseInfo.ContentText = "<html><body><h3>The server didn't handle your request.</h3></body></html>";
            }
        }
Пример #8
0
        private void ProcessRequest(HttpRequestInfo request, HttpResponseInfo response)
        {
            //lock (_syncRoot)
            //{
                HttpClient client = new HttpClient();
                try
                {

                    client.Host = request.Host;
                    client.Port = 80;
                    client.Connect();
                    try
                    {
                        client.Request.Accept = request.Accept;
                        client.Request.AcceptCharset = request.AcceptCharSet;
                        client.Request.AcceptEncoding = request.AcceptEncoding;
                        client.Request.AcceptLanguage = request.AcceptLanguage;
                       // client.Request.Authorization = request.Authentication;
                        client.Request.BasicAuthentication = request.BasicAuthentication;
                        client.Request.CacheControl = request.CacheControl;
                        client.Request.Connection = request.Connection;
                        client.Request.ContentEncoding = request.ContentEncoding;
                        client.Request.ContentLanguage = request.ContentLanguage;
                        client.Request.ContentLength = request.ContentLength;
                        //client.Request.ContentRangeEnd = request.ContentRangeEnd;
                        //client.Request.ContentRangeStart = request.ContentRangeStart;
                        client.Request.ContentType = request.ContentType;
                        //client.Request.ContentVersion = request.ContentVersion;
                        //client.Request.CustomHeaders = request.CustomHeaders;
                        client.Request.Date = request.Date;
                        client.Request.Expires = request.Expires;
                        client.Request.From = request.From;
                        client.Request.Host = request.Host;
                        client.Request.LastModified = request.LastModified;
                        client.Request.Method = HttpCommandEnum.Get;
                        client.Request.Password = request.Password;
                        client.Request.Pragma = request.Pragma;
                        client.Request.ProxyConnection = request.ProxyConnection;

                        //client.Request.RawHeaders.Clear();
                        //foreach (string header in request.RawHeaders)
                        //{
                        //    client.Request.RawHeaders.Add(header);
                        //}

                        client.Request.Referer = request.Referer;
                        client.Request.UserAgent = request.UserAgent;
                        client.Request.UserName = request.UserName;

                        MemoryStream mem = new MemoryStream();
                        client.Get(request.RequestURI, mem);

                        response.CacheControl = client.Response.CacheControl;
                        response.Connection = client.Response.Connection;
                        response.ContentEncoding = client.Response.ContentEncoding;
                        response.ContentLanguage = client.Response.ContentLanguage;
                        response.ContentLength = client.Response.ContentLength;
                        //response.ContentRangeEnd = client.Response.ContentRangeEnd;
                        //response.ContentRangeStart = client.Response.ContentRangeEnd;
                        response.ContentStream = client.Response.ContentStream;
                        response.ContentType = client.Response.ContentType;
                        //response.ContentVersion = client.Response.ContentVersion;
                        //response.CustomHeaders = client.Response.CustomHeaders;
                        response.Date = client.Response.Date;
                        response.Expires = client.Response.Expires;

                        response.LastModified = client.Response.LastModified;
                        response.Location = client.Response.Location;
                        response.Pragma = client.Response.Pragma;

                        request.RawHeaders.Clear();
                        foreach (string header in client.Response.RawHeaders)
                        {
                            request.RawHeaders.Add(header);
                        }

                        response.ResponseNo = client.Response.ResponseCode;
                        response.ResponseText = client.Response.ResponseText;
                        response.Server = client.Response.Server;
                        response.WWWAuthenticate = client.Response.WWWAuthenticate;
                    }

                    finally { client.Disconnect(); }
                }
                catch (Exception ex)
                {
                    response.ResponseNo = 500;
                    response.ContentText = string.Format("Exception: {0}", ex.Message);
                }
            //}
        }
Пример #9
0
        protected override void DoExecute(object sender, ContextRunEventArgs <int, ReplyRFC, ContextRFC> eventArgs)
        {
            HttpRequestInfo  LRequestInfo;
            HttpResponseInfo LResponseInfo;
            bool             ContinueProcessing = true;
            string           S           = "";
            string           sCmd        = "";
            string           sVersion    = "";
            string           sRequestURI = "";



            URI    LURI;
            bool   LCloseConnection = !KeepAlive;
            string LRawHttpCommand  = "";

            try
            {
                try
                {
                    do
                    {
                        LRawHttpCommand = eventArgs.Context.TcpConnection.Socket.ReadLn();

                        string[] commandParts = LRawHttpCommand.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                        switch (commandParts.Length)
                        {
                        case 2:
                            sCmd        = commandParts[0].ToUpper();
                            sRequestURI = "/";
                            sVersion    = commandParts[1];
                            break;

                        case 3:
                            sCmd        = commandParts[0].ToUpper();
                            sRequestURI = commandParts[1];
                            sVersion    = commandParts[2];
                            break;

                        default:
                            throw new HTTPErrorParsingCommandException(ResourceStrings.ErrorParsingCommand);
                        }

                        // TODO: Check for 1.0 only at this point
                        using (LRequestInfo = new HttpRequestInfo(LRawHttpCommand, eventArgs.Context.TcpConnection.Socket.EndPoint, sCmd, eventArgs.Context))
                        {
                            LRequestInfo.RawHttpCommand = LRawHttpCommand;
                            LRequestInfo.RawHeaders.Clear();
                            eventArgs.Context.TcpConnection.Socket.MaxCapturedLines = MaximumHeaderLineCount;
                            eventArgs.Context.TcpConnection.Socket.Capture(LRequestInfo.RawHeaders, String.Empty);
                            LRequestInfo.ProcessHeaders();

                            // TODO: This is the area that needs fixed. Ive hacked it for now
                            // Get data can exists with POSTs, but can POST data exist with GETs?
                            // If only the first, the solution is easy. If both - need more
                            // investigation.

                            LRequestInfo.PostStream = null;
                            Stream TempStream = LRequestInfo.__PostStream;
                            CreatePostStream(eventArgs.Context, out TempStream);
                            LRequestInfo.__PostStream = TempStream;
                            if (LRequestInfo.PostStream == null)
                            {
                                LRequestInfo.__PostStream = new MemoryStream();
                            }
                            if (LRequestInfo.ContentLength > 0)
                            {
                                eventArgs.Context.TcpConnection.Socket.ReadStream(LRequestInfo.PostStream, LRequestInfo.ContentLength);
                            }
                            else
                            {
                                if (sCmd == "POST")
                                {
                                    if (!LRequestInfo.HasContentLength)
                                    {
                                        eventArgs.Context.TcpConnection.Socket.ReadStream(LRequestInfo.PostStream, -1, true);
                                    }
                                }
                            }
                            LRequestInfo.__Command           = sCmd;
                            LRequestInfo.PostStream.Position = 0;
                            LRequestInfo.__FormParams        = new StreamReader(LRequestInfo.PostStream).ReadToEnd();
                            LRequestInfo.__UnparsedParams    = LRequestInfo.FormParams;



                            LRequestInfo.__QueryParams = sRequestURI.IndexOf("?") >= 0 ?
                                                         sRequestURI.Substring(sRequestURI.IndexOf("?") + 1) : string.Empty;

                            //              LRequestInfo.__RemoteIP = ((new IPEndPoint(. eventArgs.Context.TcpConnection.Socket.EndPoint).Address;
                            //              LRequestInfo.__RemotePort = ((IPEndPoint)eventArgs.Context.TcpConnection.Socket.EndPoint).Port;
                            if (LRequestInfo.QueryParams.Length > 0)
                            {
                                if (LRequestInfo.UnparsedParams.Length == 0)
                                {
                                    LRequestInfo.__UnparsedParams = LRequestInfo.QueryParams;
                                }
                                else
                                {
                                    LRequestInfo.__UnparsedParams += "&" + LRequestInfo.QueryParams;
                                }
                            }
                            if (ParseParams)
                            {
                                if (LRequestInfo.ContentType.ToLower() == "application/x-www-form-urlencoded")
                                {
                                    LRequestInfo.DecodeAndSetParams(LRequestInfo.UnparsedParams);
                                }
                                else
                                {
                                    LRequestInfo.DecodeAndSetParams(LRequestInfo.QueryParams);
                                }
                            }
                            ReadCookiesFromRequestHeader(LRequestInfo);
                            LRequestInfo.__Version  = sVersion;
                            LRequestInfo.RequestURI = sRequestURI;
                            if (sRequestURI == "*")
                            {
                                LRequestInfo.__Document = "*";
                            }
                            else
                            {
                                LURI = new URI(sRequestURI);
                                LRequestInfo.__Document = URI.URLDecode(LURI.Document);
                                if (LURI.Host.Length > 0 &&
                                    LRequestInfo.Host.Length == 0)
                                {
                                    LRequestInfo.__Host = LURI.Host;
                                }
                            }
                            S = LRequestInfo.RawHeaders.Values("Authorization");
                            LRequestInfo.__AuthExists = S.Length > 0;
                            if (LRequestInfo.AuthExists)
                            {
                                string AuthMethod = Global.Fetch(ref S, " ");
                                if (AuthenticationRegistry.FindAuthenticationMethod(AuthMethod) != null)
                                {
                                    using (AuthenticationBase ab = AuthenticationRegistry.GetAuthenticationMethodHandler(AuthMethod))
                                    {
                                        ab.DecodeAuthParams(S);
                                        LRequestInfo.__AuthUserName = ab.UserName;
                                        LRequestInfo.__AuthPassword = ab.Password;
                                    }
                                }
                                else
                                {
                                    throw new HTTPUnsupportedAuthorisationSchemeException(ResourceStrings.UnsupportedAuthorizationScheme);
                                }
                            }

                            using (LResponseInfo = new HttpResponseInfo(eventArgs.Context.TcpConnection))
                            {
                                try
                                {
                                    LResponseInfo.CloseConnection = !(_KeepAlive && LRequestInfo.Connection.Equals("Keep-alive", StringComparison.InvariantCultureIgnoreCase));
                                    GetSessionFromCookie(eventArgs.Context, LRequestInfo, LResponseInfo, out ContinueProcessing);
                                    if (ServerSoftware.Trim().Length > 0)
                                    {
                                        LResponseInfo.ServerSoftware = ServerSoftware;
                                    }
                                    try
                                    {
                                        if (ContinueProcessing)
                                        {
                                            HandleCommand(eventArgs.Context, LRequestInfo, LResponseInfo);
                                        }
                                    }
                                    catch (SocketException)
                                    {
                                        throw;
                                    }
                                    catch (Exception E)
                                    {
                                        LResponseInfo.ResponseNo  = 500;
                                        LResponseInfo.ContentText = E.Message;
                                        LResponseInfo.ContentType = "text/plain";
                                    }
                                    if (!LResponseInfo.HeaderHasBeenWritten)
                                    {
                                        LResponseInfo.WriteHeader();
                                    }
                                    if (LResponseInfo.ContentText.Length > 0 ||
                                        LResponseInfo.ContentStream != null)
                                    {
                                        LResponseInfo.WriteContent();
                                    }
                                }
                                finally
                                {
                                    LCloseConnection = LResponseInfo.CloseConnection;
                                }
                            }
                        }
                    }while (!LCloseConnection);
                }
                catch (SocketException E)
                {
                    if (E.ErrorCode != 10054) // WSAECONNRESET
                    {
                        throw;
                    }
                }
                catch (ClosedSocketException)
                {
                    eventArgs.Context.TcpConnection.Disconnect();
                }
                catch (Exception)
                {
                    throw;
                }
            }
            finally
            {
                eventArgs.Context.TcpConnection.Disconnect(false);
            }
            eventArgs.ReturnValue = false;
        }
Пример #10
0
 protected abstract void DoCommand(ContextRFC context, HttpRequestInfo request, HttpResponseInfo response, ref bool handled);
Пример #11
0
 protected abstract void DoRequestNotHandled(ContextRFC AContext, HttpRequestInfo ARequestInfo, HttpResponseInfo AResponseInfo);
Пример #12
0
 public HttpSession CreateSession(ContextRFC AContext, HttpRequestInfo AHttpRequestInfo, HttpResponseInfo AHttpResponseInfo)
 {
     if (SessionState)
     {
         HttpSession TempResult = null;
         DoOnCreateSession(AContext, out TempResult);
         if (TempResult == null)
         {
             TempResult = _SessionList.CreateUniqueSession(AHttpRequestInfo.RemoteIP);
         }
         else
         {
             _SessionList.Add(TempResult);
         }
         ServerCookie TempCookie = AHttpResponseInfo.Cookies.Add();
         TempCookie.CookieName = Http.SessionIdCookie;
         TempCookie.Value      = TempResult.SessionId;
         TempCookie.Path       = "/";
         TempCookie.MaxAge     = -1;
         AHttpRequestInfo.SetSession(TempResult);
         AHttpResponseInfo.SetSession(TempResult);
         return(TempResult);
     }
     return(null);
 }
Пример #13
0
        protected HttpSession GetSessionFromCookie(ContextRFC AContext, HttpRequestInfo AHttpRequestInfo, HttpResponseInfo AHttpResponseInfo, out bool OContinueProcessing)
        {
            OContinueProcessing = true;
            HttpSession TempResult = null;

            if (SessionState)
            {
                int    CurrentCookieIndex = AHttpRequestInfo.Cookies.GetCookieIndex(0, Http.SessionIdCookie);
                string SessionId          = "";
                while (TempResult == null &&
                       CurrentCookieIndex > -1)
                {
                    SessionId  = AHttpRequestInfo.Cookies[CurrentCookieIndex].Value;
                    TempResult = _SessionList.GetSession(SessionId, AHttpRequestInfo.RemoteIP);
                    if (TempResult == null)
                    {
                        DoInvalidSession(AContext, AHttpRequestInfo, AHttpResponseInfo, SessionId, out OContinueProcessing);
                    }
                    CurrentCookieIndex++;
                    CurrentCookieIndex = AHttpRequestInfo.Cookies.GetCookieIndex(CurrentCookieIndex, Http.SessionIdCookie);
                }
                if (_AutoStartSession &&
                    OContinueProcessing &&
                    TempResult == null)
                {
                    TempResult = CreateSession(AContext, AHttpRequestInfo, AHttpResponseInfo);
                }
            }
            AHttpRequestInfo.SetSession(TempResult);
            AHttpResponseInfo.SetSession(TempResult);
            return(TempResult);
        }
Пример #14
0
        private void HandleCommand(ContextRFC AContext, HttpRequestInfo ARequestInfo, HttpResponseInfo AResponseInfo)
        {
            bool LHandled = false;

            DoCommand(AContext, ARequestInfo, AResponseInfo, ref LHandled);
            if (!LHandled)
            {
                DoRequestNotHandled(AContext, ARequestInfo, AResponseInfo);
            }
        }
Пример #15
0
 protected virtual void DoInvalidSession(ContextRFC AContext, HttpRequestInfo ARequestInfo, HttpResponseInfo AResponseInfo, string AInvalidSessionId, out bool OContinueProcessing)
 {
     OContinueProcessing = false;
 }
Пример #16
0
        protected override void DoCommand(ContextRFC context, HttpRequestInfo request, HttpResponseInfo response, ref bool handled)
        {
            // first raise events is user want to handle request by event
            switch (request.CommandType)
            {
            case HttpCommandEnum.Connect:
                if (OnCommandConnect != null)
                {
                    OnCommandConnect(context, request, response, ref handled);
                }
                break;

            case HttpCommandEnum.Delete:
                if (OnCommandDelete != null)
                {
                    OnCommandDelete(context, request, response, ref handled);
                }
                break;

            case HttpCommandEnum.Get:
                if (OnCommandGet != null)
                {
                    OnCommandGet(context, request, response, ref handled);
                }
                break;

            case HttpCommandEnum.Head:
                if (OnCommandHead != null)
                {
                    OnCommandHead(context, request, response, ref handled);
                }
                break;

            case HttpCommandEnum.Options:
                if (OnCommandOptions != null)
                {
                    OnCommandOptions(context, request, response, ref handled);
                }
                break;

            case HttpCommandEnum.Post:
                if (OnCommandPost != null)
                {
                    OnCommandPost(context, request, response, ref handled);
                }
                break;

            case HttpCommandEnum.Put:
                if (OnCommandPut != null)
                {
                    OnCommandPut(context, request, response, ref handled);
                }
                break;

            case HttpCommandEnum.Trace:
                if (OnCommandTrace != null)
                {
                    OnCommandTrace(context, request, response, ref handled);
                }
                break;

            default:
                if (OnCommandOther != null)
                {
                    OnCommandOther(context, request, response, ref handled);
                }
                break;
            }
            // if request handled from an event handler skip request handlers
            if (handled)
            {
                return;
            }

            // try to handle request by request handlers collection
            RequestHandlersLock.AcquireReaderLock(-1);
            try {
                for (int i = 0; i < mRequestHandlers.Count && !handled; i++)
                {
                    mRequestHandlers[i].HandleCommand(this, request, response, ref handled);
                }
            } finally {
                RequestHandlersLock.ReleaseReaderLock();
            }
        }