Ejemplo n.º 1
0
        private void Process(HttpListenerContext context)
        {
            if (context.Request.IsWebSocketRequest)
            {
                SocketClient.CreateSocketThread(context);
//				SocketClient client = new SocketClient ();
//				client.Execute (context);
                return;
            }
            if (context.Request.HttpMethod == "POST")
            {
                CobaClient client = _createClient();
                client.ExecutePost(context);
                return;
            }
            //	Console.WriteLine ("client : " + context.Request.RemoteEndPoint.ToString ());
            string filename = context.Request.Url.AbsolutePath;

            if (filename.Equals("/get.folder"))
            {
                CobaClient client = _createClient();
                client.Execute(context, filename);
                return;
            }

            if (filename.Equals("/mkdir"))
            {
                CobaClient client = _createClient();
                client.CreateFolder(context);
                return;
            }
            if (filename.Equals("/mouse"))
            {
                CobaClient client = _createClient();
                client.ExecuteMouse(context);
                return;
            }
            if (filename.Length >= 2 && filename[0] == '/' && filename[1] == '~')
            {
                CobaClient client = _createClient();
                client.Send(context, filename);
                return;
            }
            string url = context.Request.Url.ToString();

            //		Console.WriteLine(filename + " url:" + url);
            filename = filename.Substring(1);

            if (filename.IndexOf(".php") != -1)
            {
                PHP php = new PHP();
                php.Execute(context, filename);
                return;
            }

            if (string.IsNullOrEmpty(filename))
            {
                foreach (string indexFile in _indexFiles)
                {
                    if (File.Exists(Path.Combine(_rootDirectory, indexFile)))
                    {
                        filename = indexFile;
                        break;
                    }
                }
            }

            filename = Path.Combine(_rootDirectory, filename);

            if (File.Exists(filename))
            {
                //Console.WriteLine ("File :" + filename);
                try
                {
                    Stream input = new FileStream(filename, FileMode.Open,
                                                  FileAccess.Read,
                                                  FileShare.Read);

                    //Adding permanent http response headers
                    string mime;
                    context.Response.StatusCode      = (int)HttpStatusCode.OK;
                    context.Response.ContentType     = _mimeTypeMappings.TryGetValue(Path.GetExtension(filename), out mime) ? mime : "application/octet-stream";
                    context.Response.ContentLength64 = input.Length;
                    context.Response.AddHeader("Date", DateTime.Now.ToString("r"));
                    context.Response.AddHeader("Last-Modified", System.IO.File.GetLastWriteTime(filename).ToString("r"));

                    byte[] buffer = new byte[1024 * 64];
                    int    nbytes;
                    while ((nbytes = input.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        context.Response.OutputStream.Write(buffer, 0, nbytes);
                    }
                    input.Close();
                    context.Response.OutputStream.Flush();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("exception: " + ex.ToString());
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                }
            }
            else
            {
                context.Response.StatusCode = (int)HttpStatusCode.NotFound;
            }

            context.Response.OutputStream.Close();
        }
Ejemplo n.º 2
0
        private void Process(HttpListenerContext context)
        {
            if (context.Request.IsWebSocketRequest) {
                SocketClient.CreateSocketThread (context);
            //				SocketClient client = new SocketClient ();
            //				client.Execute (context);
                return;
            }
            if (context.Request.HttpMethod == "POST") {
                CobaClient client = _createClient ();
                client.ExecutePost (context);
                return;
            }
            //	Console.WriteLine ("client : " + context.Request.RemoteEndPoint.ToString ());
            string filename = context.Request.Url.AbsolutePath;
            if (filename.Equals ("/get.folder")) {
                CobaClient client = _createClient ();
                client.Execute (context, filename);
                return;
            }

            if (filename.Equals ("/mkdir")) {
                CobaClient client =  _createClient ();
                client.CreateFolder (context);
                return;
            }
            if (filename.Equals ("/mouse")) {
                CobaClient client = _createClient ();
                client.ExecuteMouse (context);
                return;
            }
            if (filename.Length >= 2 && filename[0] == '/' && filename[1] == '~') {
                CobaClient client =  _createClient ();
                client.Send(context, filename);
                return;
            }
            string url = context.Request.Url.ToString();
            //		Console.WriteLine(filename + " url:" + url);
            filename = filename.Substring(1);

            if (filename.IndexOf (".php") != -1) {
                PHP php = new PHP ();
                php.Execute (context, filename);
                return;
            }

            if (string.IsNullOrEmpty(filename))
            {
                foreach (string indexFile in _indexFiles)
                {
                    if (File.Exists(Path.Combine(_rootDirectory, indexFile)))
                    {
                        filename = indexFile;
                        break;
                    }
                }
            }

            filename = Path.Combine(_rootDirectory, filename);

            if (File.Exists(filename))
            {
                //Console.WriteLine ("File :" + filename);
                try
                {
                    Stream input = new FileStream(filename, FileMode.Open ,
                        FileAccess.Read,
                        FileShare.Read);

                    //Adding permanent http response headers
                    string mime;
                    context.Response.StatusCode = (int)HttpStatusCode.OK;
                    context.Response.ContentType = _mimeTypeMappings.TryGetValue(Path.GetExtension(filename), out mime) ? mime : "application/octet-stream";
                    context.Response.ContentLength64 = input.Length;
                    context.Response.AddHeader("Date", DateTime.Now.ToString("r"));
                    context.Response.AddHeader("Last-Modified", System.IO.File.GetLastWriteTime(filename).ToString("r"));

                    byte[] buffer = new byte[1024 * 64];
                    int nbytes;
                    while ((nbytes = input.Read(buffer, 0, buffer.Length)) > 0)
                        context.Response.OutputStream.Write(buffer, 0, nbytes);
                    input.Close();
                    context.Response.OutputStream.Flush();

                }
                catch (Exception ex)
                {
                    Console.WriteLine ("exception: " + ex.ToString ());
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                }

            }
            else
            {
                context.Response.StatusCode = (int)HttpStatusCode.NotFound;
            }

            context.Response.OutputStream.Close();
        }