Exemplo n.º 1
0
        HttpRequestHandler BuildCapsHandler(string path)
        {
            HttpRequestSignature signature = new HttpRequestSignature();

            signature.Path = path;
            return(new HttpServer.HttpRequestHandler(signature, CapsCallback));
        }
Exemplo n.º 2
0
 public EventQueueServer(HttpServer server, string path)
 {
     HttpRequestSignature signature = new HttpRequestSignature();
     signature.Method = "post";
     signature.ContentType = String.Empty;
     signature.Path = path;
     HttpServer.HttpRequestCallback callback = new HttpServer.HttpRequestCallback(EventQueueHandler);
     HttpServer.HttpRequestHandler handler = new HttpServer.HttpRequestHandler(signature, callback);
     server.AddHandler(handler);
 }
Exemplo n.º 3
0
 void LoginWebpageGetHandler(HttpRequestSignature signature, ref HttpListenerContext context)
 {
     string pageContent = "<html><head><title>Simian</title></head><body><br/><h1>Welcome to Simian</h1></body></html>";
     byte[] pageData = Encoding.UTF8.GetBytes(pageContent);
     context.Response.OutputStream.Write(pageData, 0, pageData.Length);
     context.Response.Close();
 }
Exemplo n.º 4
0
        void LoginLLSDPostHandler(HttpRequestSignature signature, ref HttpListenerContext context)
        {
            string body = String.Empty;

            using (StreamReader reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding))
            {
                body = reader.ReadToEnd();
            }

            Console.WriteLine(body);
        }
Exemplo n.º 5
0
        public void AddHandler(string method, string contentType, string path, bool exactPath, bool sendResponseAfterCallback, HttpRequestCallback callback)
        {
            HttpRequestSignature signature = new HttpRequestSignature(method, contentType, path, exactPath);
            HttpRequestHandler handler = new HttpRequestHandler(signature, callback, sendResponseAfterCallback);

            lock (m_handlersWriteLock)
            {
                HttpRequestHandler[] newHandlers = new HttpRequestHandler[m_requestHandlers.Length + 1];

                for (int i = 0; i < m_requestHandlers.Length; i++)
                    newHandlers[i] = m_requestHandlers[i];
                newHandlers[m_requestHandlers.Length] = handler;

                m_requestHandlers = newHandlers;
            }
        }
Exemplo n.º 6
0
        private void RequestReceivedHandler(object sender, RequestEventArgs e)
        {
            IHttpClientContext context = (IHttpClientContext)sender;
            IHttpRequest request = e.Request;

            IHttpResponse response = request.CreateResponse(context);

            // Load cookies if they exist
            RequestCookies cookies = (request.Headers["cookie"] != null)
                ? new RequestCookies(request.Headers["cookie"])
                : new RequestCookies(String.Empty);
            request.SetCookies(cookies);

            // Create a request signature
            HttpRequestSignature signature = new HttpRequestSignature(request);

            // Look for a signature match in our handlers
            HttpRequestHandler foundHandler = null;

            for (int i = 0; i < m_requestHandlers.Length; i++)
            {
                HttpRequestHandler handler = m_requestHandlers[i];

                if (signature == handler.Signature)
                {
                    foundHandler = handler;
                    break;
                }
            }

            if (foundHandler != null)
                FireRequestCallback(context, request, response, foundHandler);
            else
                FireRequestCallback(context, request, response, m_notFoundHandler);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Add a request handler
 /// </summary>
 /// <param name="method">HTTP verb to match, or null to skip verb matching</param>
 /// <param name="contentType">Content-Type header to match, or null to skip Content-Type matching</param>
 /// <param name="path">Request URI path regular expression to match, or null to skip URI path matching</param>
 /// <param name="callback">Callback to fire when an incoming request matches the given pattern</param>
 public void AddHandler(string method, string contentType, string path, HttpRequestCallback callback)
 {
     HttpRequestSignature signature = new HttpRequestSignature();
     signature.Method = method;
     signature.ContentType = contentType;
     signature.Path = path;
     AddHandler(new HttpRequestHandler(signature, callback));
 }
Exemplo n.º 8
0
        void RequestHandler(IHttpClientContext client, IHttpRequest request)
        {
            HttpResponse response = new HttpResponse(client, request);

            // Create a request signature
            HttpRequestSignature signature = new HttpRequestSignature(request);

            // Look for a signature match in our handlers
            for (int i = 0; i < requestHandlers.Length; i++)
            {
                HttpRequestHandler handler = requestHandlers[i];

                if (handler.Signature != null && signature == handler.Signature)
                {
                    FireRequestCallback(client, request, response, handler.Callback);
                    return;
                }
            }

            // No registered handler matched this request's signature
            if (notFoundHandler != null)
            {
                FireRequestCallback(client, request, response, notFoundHandler);
            }
            else
            {
                // Send a default 404 response
                try
                {
                    response.Status = HttpStatusCode.NotFound;
                    response.Reason = String.Format("No request handler registered for Method=\"{0}\", Content-Type=\"{1}\", Path=\"{2}\"",
                        signature.Method, signature.ContentType, signature.Path);
                    string notFoundResponse = "<html><head><title>Page Not Found</title></head><body><h3>" + response.Reason + "</h3></body></html>";
                    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(notFoundResponse);
                    response.Body.Write(buffer, 0, buffer.Length);
                    response.Send();
                }
                catch (Exception) { }
            }
        }
 /// <summary>
 /// Add a request handler
 /// </summary>
 /// <param name="method">HTTP verb to match, or null to skip verb matching</param>
 /// <param name="contentType">Content-Type header to match, or null to skip Content-Type matching</param>
 /// <param name="path">Request URI path regular expression to match, or null to skip URI path matching</param>
 /// <param name="sendResponseAfterCallback">If true, the IHttpResponse will be sent to the client after
 /// the callback completes. Otherwise, the connection will be left open and the user is responsible for
 /// closing the connection later</param>
 /// <param name="callback">Callback to fire when an incoming request matches the given pattern</param>
 public void AddHandler(string method, string contentType, string path, bool sendResponseAfterCallback, HttpRequestCallback callback)
 {
     HttpRequestSignature signature = new HttpRequestSignature(method, contentType, path);
     AddHandler(new HttpRequestHandler(signature, callback, sendResponseAfterCallback));
 }
Exemplo n.º 10
0
 /// <summary>
 /// Add a request handler
 /// </summary>
 /// <param name="method">HTTP verb to match, or null to skip verb matching</param>
 /// <param name="contentType">Content-Type header to match, or null to skip Content-Type matching</param>
 /// <param name="path">Request URI path regular expression to match, or null to skip URI path matching</param>
 /// <param name="callback">Callback to fire when an incoming request matches the given pattern</param>
 /// <remarks>Using this overload, the response will automatically be sent when the callback completes</remarks>
 public void AddHandler(string method, string contentType, string path, HttpRequestCallback callback)
 {
     HttpRequestSignature signature = new HttpRequestSignature(method, contentType, path);
     AddHandler(new HttpRequestHandler(signature, callback, true));
 }
Exemplo n.º 11
0
        void InitHttpServer(int port, bool ssl)
        {
            HttpServer = new HttpServer(tcpPort, ssl);

            // Login webpage HEAD request, used to check if the login webpage is alive
            HttpRequestSignature signature = new HttpRequestSignature();
            signature.Method = "head";
            signature.ContentType = String.Empty;
            signature.Path = "/loginpage";
            HttpServer.HttpRequestCallback callback = new HttpServer.HttpRequestCallback(LoginWebpageHeadHandler);
            HttpServer.HttpRequestHandler handler = new HttpServer.HttpRequestHandler(signature, callback);
            HttpServer.AddHandler(handler);

            // Login webpage GET request, gets the login webpage data (purely aesthetic)
            signature.Method = "get";
            signature.ContentType = String.Empty;
            signature.Path = "/loginpage";
            callback = new HttpServer.HttpRequestCallback(LoginWebpageGetHandler);
            handler.Signature = signature;
            handler.Callback = callback;
            HttpServer.AddHandler(handler);

            // Client XML-RPC login
            signature.Method = "post";
            signature.ContentType = "text/xml";
            signature.Path = "/login";
            callback = new HttpServer.HttpRequestCallback(LoginXmlRpcPostHandler);
            handler.Signature = signature;
            handler.Callback = callback;
            HttpServer.AddHandler(handler);

            // Client LLSD login
            signature.Method = "post";
            signature.ContentType = "application/xml";
            signature.Path = "/login";
            callback = new HttpServer.HttpRequestCallback(LoginLLSDPostHandler);
            handler.Signature = signature;
            handler.Callback = callback;
            HttpServer.AddHandler(handler);

            HttpServer.Start();
        }
Exemplo n.º 12
0
 protected void EventQueueHandler(HttpRequestSignature signature, ref HttpListenerContext context)
 {
 }
Exemplo n.º 13
0
 HttpServer.HttpRequestHandler BuildCapsHandler(string path)
 {
     HttpRequestSignature signature = new HttpRequestSignature();
     signature.ContentType = "application/xml";
     signature.Path = path;
     return new HttpServer.HttpRequestHandler(signature, CapsCallback);
 }
Exemplo n.º 14
0
 void LoginWebpageHeadHandler(HttpRequestSignature signature, ref HttpListenerContext context)
 {
     context.Response.StatusCode = (int)HttpStatusCode.OK;
     context.Response.StatusDescription = "OK";
 }
Exemplo n.º 15
0
        void ProcessRequest(IHttpClientContext context, IHttpRequest request)
        {
            LogWriter.Write(this, LogPrio.Trace, "Processing request...");

            IHttpResponse response = request.CreateResponse(context);

            // Load cookies if they exist
            RequestCookies cookies = request.Headers["cookie"] != null
                                         ? new RequestCookies(request.Headers["cookie"])
                                         : new RequestCookies(String.Empty);
            request.SetCookies(cookies);

            // Create a request signature
            HttpRequestSignature signature = new HttpRequestSignature(request);

            // Look for a signature match in our handlers
            HttpRequestHandler foundHandler = null;

            bool doLock = !rwHandlersLock.IsReadLockHeld;
            if (doLock) rwHandlersLock.EnterReadLock();

            try
            {
                for (int i = 0; i < _requestHandlers.Length; i++)
                {
                    HttpRequestHandler handler = _requestHandlers[i];

                    if (signature == handler.Signature)
                    {
                        foundHandler = handler;
                        break;
                    }
                }
            }
            finally { if (doLock) rwHandlersLock.ExitReadLock(); }

            if (foundHandler != null)
                FireRequestCallback(context, request, response, foundHandler);
            else
                FireRequestCallback(context, request, response, _notFoundHandler);

            LogWriter.Write(this, LogPrio.Trace, "...done processing request.");
        }
Exemplo n.º 16
0
        void LoginXmlRpcPostHandler(HttpRequestSignature signature, ref HttpListenerContext context)
        {
            string
                firstName = String.Empty,
                lastName = String.Empty,
                password = String.Empty,
                start = String.Empty,
                version = String.Empty,
                channel = String.Empty;

            try
            {
                // Parse the incoming XML
                XmlReader reader = XmlReader.Create(context.Request.InputStream);

                reader.ReadStartElement("methodCall");
                {
                    string methodName = reader.ReadElementContentAsString("methodName", String.Empty);

                    if (methodName == "login_to_simulator")
                    {
                        reader.ReadStartElement("params");
                        reader.ReadStartElement("param");
                        reader.ReadStartElement("value");
                        reader.ReadStartElement("struct");
                        {
                            while (reader.Name == "member")
                            {
                                reader.ReadStartElement("member");
                                {
                                    string name = reader.ReadElementContentAsString("name", String.Empty);

                                    reader.ReadStartElement("value");
                                    {
                                        switch (name)
                                        {
                                            case "first":
                                                firstName = reader.ReadElementContentAsString("string", String.Empty);
                                                break;
                                            case "last":
                                                lastName = reader.ReadElementContentAsString("string", String.Empty);
                                                break;
                                            case "passwd":
                                                password = reader.ReadElementContentAsString("string", String.Empty);
                                                break;
                                            case "start":
                                                start = reader.ReadElementContentAsString("string", String.Empty);
                                                break;
                                            case "version":
                                                version = reader.ReadElementContentAsString("string", String.Empty);
                                                break;
                                            case "channel":
                                                channel = reader.ReadElementContentAsString("string", String.Empty);
                                                break;
                                            default:
                                                if (reader.Name == "string")
                                                    Console.WriteLine(String.Format("Ignore login xml value: name={0}, value={1}", name, reader.ReadInnerXml()));
                                                else
                                                    Console.WriteLine(String.Format("Unknown login xml: name={0}, value={1}", name, reader.ReadInnerXml()));
                                                break;
                                        }
                                    }
                                    reader.ReadEndElement();
                                }
                                reader.ReadEndElement();
                            }
                        }
                        reader.ReadEndElement();
                        reader.ReadEndElement();
                        reader.ReadEndElement();
                        reader.ReadEndElement();
                    }
                }
                reader.ReadEndElement();
                reader.Close();

                LoginResponseData responseData = HandleLogin(firstName, lastName, password, start, version, channel);
                if (responseData.Success)
                    responseData.InventorySkeleton = Inventory.CreateInventorySkeleton(responseData.AgentID);
                XmlWriter writer = XmlWriter.Create(context.Response.OutputStream);
                responseData.ToXmlRpc(writer);
                writer.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
Exemplo n.º 17
0
 HttpRequestHandler BuildCapsHandler(string path)
 {
     HttpRequestSignature signature = new HttpRequestSignature();
     signature.Path = path;
     return new HttpServer.HttpRequestHandler(signature, CapsCallback);
 }