Exemple #1
0
        /// <summary>
        /// Get a request from client, process it, then return response to client
        /// </summary>
        public void Process(object State)
        {
            NetworkStream ns         = new NetworkStream(_client);
            string        requestStr = "";
            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 += Encoding.UTF8.GetString(bytes, 0, bytesRead);
            } while (ns.DataAvailable);

            request = new HTTPRequest(requestStr);
            request.addProperty("RemoteEndPoint", _client.RemoteEndPoint.ToString());
            request.setip(_client.RemoteEndPoint.ToString()); // These will setip by used function from HTTPRequest we had create
            // We can handle only GET now
            if (request.Status != 200)
            {
                response = new HTTPResponse(request.Status);
            }
            else
            {
                bool processed = false;
                // pre processing
                foreach (KeyValuePair <string, PluginInfo> plugininfo in plugins)
                {
                    if (plugininfo.Value.preprocessing)
                    {
                        plugininfo.Value.reference.PreProcessing(request);
                    }
                }
                // plugins
                foreach (KeyValuePair <string, PluginInfo> plugininfo in plugins)
                {
                    if (request.Filename.StartsWith(plugininfo.Key))
                    {
                        response  = plugininfo.Value.reference.GetResponse(request);
                        processed = true;
                    }
                }
                // local file
                if (!processed)
                {
                    if (request.Filename.Equals(""))
                    {
                        response = getFile(ROOT + "/index.html");
                    }
                    else
                    {
                        response = getFile(ROOT + "/" + request.Filename);
                    }
                }
                // post processing pipe
                foreach (KeyValuePair <string, PluginInfo> plugininfo in 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();
        }