Beispiel #1
0
        /// <summary>
        /// Get a request from client, process it, then return response to client
        /// </summary>
        public void Process()
        {
            NetworkStream ns         = new NetworkStream(_client);
            StringBuilder requestStr = new StringBuilder();
            HTTPRequest   request    = null;
            HTTPResponse  response   = null;

            byte[] bytes = new byte[1024];
            int    bytesRead;


            // Read all request
            do
            {
                bytesRead = ns.Read(bytes, 0, bytes.Length);
                requestStr.Append(Encoding.UTF8.GetString(bytes, 0, bytesRead));
            } while (ns.DataAvailable);

            request = new HTTPRequest(requestStr.ToString());
            request.AddProperty("RemoteEndPoint", _client.RemoteEndPoint.ToString());

            // We can handle only GET now
            if (request.Status != 200)
            {
                response = new HTTPResponse(request.Status);
            }
            else
            {
                bool processed = false;
                //FIXME, this seem duplicate with HTTPRequest
                string[] requestUrls = request.Url.Split("/");
                string[] paths       = requestUrls[1].Split("?");
                // pre processing
                foreach (KeyValuePair <string, PluginInfo> plugininfo in PM.Plugins)
                {
                    if (plugininfo.Value.preprocessing)
                    {
                        plugininfo.Value.reference.PreProcessing(request);
                    }
                }
                // plugins
                foreach (KeyValuePair <string, PluginInfo> plugininfo in PM.Plugins)
                {
                    if (paths[0].Equals(plugininfo.Key, StringComparison.InvariantCultureIgnoreCase))
                    {
                        //if(request.Url.StartsWith("/" + plugininfo.Key)) {
                        response  = plugininfo.Value.reference.GetResponse(request);
                        processed = true;
                    }
                }
                // local file
                if (!processed)
                {
                    if (request.Filename.Equals(""))
                    {
                        response = getFile(ROOT + "/" + request.Url + "/index.html");
                    }
                    else
                    {
                        response = getFile(ROOT + "/" + request.Url);
                    }
                }
                // post processing pipe
                foreach (KeyValuePair <string, PluginInfo> plugininfo in PM.Plugins)
                {
                    if (plugininfo.Value.postprocessing)
                    {
                        response = plugininfo.Value.reference.PostProcessing(response);
                    }
                }
            }
            // Generate response
            ns.Write(Encoding.UTF8.GetBytes(response.Header), 0, response.Header.Length);
            if (response.Body != null)
            {
                ns.Write(response.Body, 0, response.Body.Length);
            }

            // Shuting down
            //ns.Close();
            _client.Shutdown(SocketShutdown.Both);
            //_client.Close();
        }