예제 #1
0
        public override void SendUnknownResponseHeader(string name, string value)
        {
            WebTrace.WriteLine("SendUnknownResponseHeader (" + name + ", " + value + ")");
            if (String.Compare(name, "connection", true, CultureInfo.InvariantCulture) == 0)
            {
                sentConnection = true;
                if (value.ToLower().IndexOf("keep-alive") == -1)
                {
                    keepAlive = false;
                }
            }

            if (!sentConnection && !haveContentLength &&
                String.Compare(name, "Content-Length", true, CultureInfo.InvariantCulture) == 0)
            {
                haveContentLength = true;
                contentLength     = Int64.Parse(value);              // This should work, otherwise HttpResponse throws.
            }

            if (!headersSent)
            {
                responseHeaders.Append(name);
                responseHeaders.Append(": ");
                responseHeaders.Append(value);
                responseHeaders.Append("\r\n");
            }
        }
예제 #2
0
        public bool Start(bool bgThread)
        {
            if (started)
            {
                throw new InvalidOperationException("The server is already started.");
            }

            if (vpathToHost == null)
            {
                throw new InvalidOperationException("SetApplications must be called first.");
            }

            if (vpathToHost.Count == 0)
            {
                throw new InvalidOperationException("No applications defined or all of them are disabled");
            }

            listen_socket = webSource.CreateSocket();
            listen_socket.Listen(500);
            listen_socket.Blocking = false;
            runner = new Thread(new ThreadStart(RunServer));
            runner.IsBackground = bgThread;
            runner.Start();
            stop = false;
            WebTrace.WriteLine("Server started.");
            return(true);
        }
예제 #3
0
        public override void FlushResponse(bool finalFlush)
        {
            try
            {
                WebTrace.WriteLine("FlushResponse({0}), {1}", finalFlush, headersSent);
                if (!headersSent)
                {
                    responseHeaders.Insert(0, serverHeader);
                    responseHeaders.Insert(0, status);
                    responseHeaders.Append("\r\n");
                    byte [] b = Encoding.GetBytes(responseHeaders.ToString());
                    output.Write(b, 0, b.Length);
                    headersSent = true;
                }

                foreach (byte [] bytes in response)
                {
                    output.Write(bytes, 0, bytes.Length);
                }

                output.Flush();
                response.Clear();
                if (finalFlush)
                {
                    CloseConnection();
                }
            }
            catch (Exception e)
            {
                WebTrace.WriteLine(e.ToString());
            }
        }
예제 #4
0
 public override void SendUnknownResponseHeader(string name, string value)
 {
     WebTrace.WriteLine("SendUnknownResponseHeader (" + name + ", " + value + ")");
     if (!headersSent)
     {
         responseHeaders.AppendFormat("{0}: {1}\r\n", name, value);
     }
 }
예제 #5
0
 void RealStop()
 {
     runner.Abort();
     listen_socket.Close();
     UnloadAll();
     Thread.Sleep(1000);
     WebTrace.WriteLine("Server stopped.");
 }
예제 #6
0
 public override byte [] GetQueryStringRawBytes()
 {
     WebTrace.WriteLine("GetQueryStringRawBytes()");
     if (queryString == null)
     {
         return(null);
     }
     return(Encoding.GetBytes(queryString));
 }
예제 #7
0
 // blr
 public override int ReadEntityBody(byte [] buffer, int size)
 {
     WebTrace.WriteLine("ReadEntityBody()");
     if (buffer == null || size == 0)
     {
         return(0);
     }
     return(output.Read(buffer, 0, size));
 }
예제 #8
0
        public override string GetRawUrl()
        {
            WebTrace.WriteLine("GetRawUrl()");
            if (queryString != null && queryString.Length > 0)
            {
                return(path + "?" + queryString);
            }

            return(path);
        }
예제 #9
0
        public override int ReadEntityBody(byte [] buffer, int size)
        {
            WebTrace.WriteLine("ReadEntityBody()");
            if (size == 0)
            {
                return(0);
            }

            return(ReadInput(buffer, 0, size));
        }
예제 #10
0
        public void Stop()
        {
            if (!started)
            {
                throw new InvalidOperationException("The server is not started.");
            }

            stop = true;
//			listen_socket.Stop ();
            WebTrace.WriteLine("Server stopped.");
        }
예제 #11
0
        public override string GetKnownRequestHeader(int index)
        {
            if (headers == null)
            {
                return(null);
            }

            string headerName = HttpWorkerRequest.GetKnownRequestHeaderName(index);

            WebTrace.WriteLine(String.Format("GetKnownRequestHeader({0}) -> {1}", index, headerName));
            return(headers [headerName] as string);
        }
예제 #12
0
        public override void SendResponseFromMemory(byte [] data, int length)
        {
            WebTrace.WriteLine("SendResponseFromMemory ()");
            if (length <= 0)
            {
                return;
            }

            byte [] bytes = new byte [length];
            Array.Copy(data, 0, bytes, 0, length);
            response.Add(bytes);
        }
예제 #13
0
        public override string GetUriPath()
        {
            WebTrace.WriteLine("GetUriPath()");

            string result = path;

            if (pathInfo != null && pathInfo.Length > 0)
            {
                result += pathInfo;
            }

            return(result);
        }
예제 #14
0
 public override void CloseConnection()
 {
     WebTrace.WriteLine("CloseConnection()");
     if (requestBroker != null)
     {
         // We check for headersSent as broken user code might call
         // CloseConnection at an early stage.
         requestBroker.Close(requestId, (headersSent ? keepAlive : false));
         requestBroker = null;
         FreeMemoryStream(response);
         response = null;
     }
 }
예제 #15
0
        public void Start()
        {
            if (started)
            {
                throw new InvalidOperationException("The server is already started.");
            }

            listen_socket = new TcpListener(bindAddress);
            listen_socket.Start();
            runner = new Thread(new ThreadStart(RunServer));
            runner.Start();
            stop = false;
            WebTrace.WriteLine("Server started.");
        }
예제 #16
0
        public override void SendResponseFromMemory(byte [] data, int length)
        {
            WebTrace.WriteLine("SendResponseFromMemory ()");
            if (requestBroker == null || length <= 0)
            {
                return;
            }

            if (data.Length < length)
            {
                length = data.Length;
            }

            response.Write(data, 0, length);
        }
예제 #17
0
        public override string GetRawUrl()
        {
            WebTrace.WriteLine("GetRawUrl()");
            string result = path;

            if (pathInfo != null && pathInfo.Length > 0)
            {
                result += pathInfo;
            }

            if (queryString != null && queryString.Length > 0)
            {
                return(result + "?" + queryString);
            }

            return(result);
        }
예제 #18
0
        private void RunServer()
        {
            started = true;
            TcpClient client;

            while (!stop)
            {
                if (listen_socket.Pending())
                {
                    client = listen_socket.AcceptTcpClient();
                    WebTrace.WriteLine("Accepted connection.");
                    Worker worker = new Worker(client, this);
                    ThreadPool.QueueUserWorkItem(new WaitCallback(worker.Run));
                }
                else
                {
                    Thread.Sleep(50);
                }
            }
            listen_socket.Stop();
            started = false;
        }
예제 #19
0
        protected override bool GetRequestData()
        {
            try
            {
                if (!GetRequestLine())
                {
                    return(false);
                }

                if (protocol == null)
                {
                    protocol = "HTTP/1.0";
                }
                else if (!GetRequestHeaders())
                {
                    return(false);
                }

                WebTrace.WriteLine("verb: " + verb);
                WebTrace.WriteLine("path: " + path);
                WebTrace.WriteLine("queryString: " + queryString);
                WebTrace.WriteLine("protocol: " + protocol);
                if (headers != null)
                {
                    foreach (string key in headers.Keys)
                    {
                        WebTrace.WriteLine(key + ": " + headers [key]);
                    }
                }
            }
            catch (Exception)
            {
                return(false);
            }

            return(TryDirectory());
        }
예제 #20
0
        void RunServer()
        {
            spool.AddReadSocket(listen_socket);
            started = true;
            Socket client;
            int    w;

            while (!stop)
            {
                ArrayList wSockets = spool.SelectRead();
                w = wSockets.Count;
                for (int i = 0; i < w; i++)
                {
                    Socket s = (Socket)wSockets [i];
                    if (s == listen_socket)
                    {
                        try {
                            client          = s.Accept();
                            client.Blocking = true;
                        } catch (Exception e) {
                            continue;
                        }
                        WebTrace.WriteLine("Accepted connection.");
                        SetSocketOptions(client);
                        spool.AddReadSocket(client, DateTime.UtcNow);
                        continue;
                    }

                    spool.RemoveReadSocket(s);
                    IWorker worker = webSource.CreateWorker(s, this);
                    ThreadPool.QueueUserWorkItem(new WaitCallback(worker.Run));
                }
            }

            started = false;
        }
예제 #21
0
 public override byte [] GetPreloadedEntityBody()
 {
     WebTrace.WriteLine("GetPreloadedEntityBody");
     return(null);
 }
예제 #22
0
 public override bool HeadersSent()
 {
     WebTrace.WriteLine("HeadersSent() -> " + headersSent);
     return(headersSent);
 }
예제 #23
0
 public override bool IsClientConnected()
 {
     WebTrace.WriteLine("IsClientConnected()");
     return(requestBroker != null && requestBroker.IsConnected(requestId));
 }
예제 #24
0
 public override string GetHttpVersion()
 {
     WebTrace.WriteLine("GetHttpVersion()");
     return(protocol);
 }
예제 #25
0
 public override int GetRemotePort()
 {
     WebTrace.WriteLine("GetRemotePort()");
     return(((IPEndPoint)remoteEP).Port);
 }
예제 #26
0
 public override string GetRemoteAddress()
 {
     WebTrace.WriteLine("GetRemoteAddress()");
     return(((IPEndPoint)remoteEP).Address.ToString());
 }
예제 #27
0
 public override string GetPathInfo()
 {
     WebTrace.WriteLine("GetPathInfo()");
     return(pathInfo);
 }
예제 #28
0
 public override string GetQueryString()
 {
     WebTrace.WriteLine("GetQueryString()");
     return(queryString);
 }
예제 #29
0
 public override string GetLocalAddress()
 {
     WebTrace.WriteLine("GetLocalAddress()");
     return(localAddress);
 }
예제 #30
0
 public override int GetLocalPort()
 {
     WebTrace.WriteLine("GetLocalPort()");
     return(localPort);
 }