Exemplo n.º 1
0
        /// <summary>
        /// Get a request from client, process it, then return response to client
        /// </summary>
        public void Process()
        {
            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());

            // 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();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get a request from client, process it, then return response to client
        /// </summary>
        public void Process()
        {
            HttpListenerRequest  req      = _context.Request;
            HttpListenerResponse res      = _context.Response;
            HTTPRequest          request  = new HTTPRequest(req);
            HTTPResponse         response = new HTTPResponse(200);

            request.addProperty("RemoteEndPoint", req.RemoteEndPoint.ToString());

            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.Path.ToLower().Equals(plugininfo.Key.ToLower()))
                    {
                        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);
                    }
                }
            }
            res.StatusCode = response.Status;
            foreach (string key in response.Headers.Keys)
            {
                res.AddHeader(key, response.Headers[key]);
            }
            if (response.Body != null)
            {
                byte[] buffer = response.Body;
                res.ContentLength64 = buffer.Length;
                System.IO.Stream output = res.OutputStream;
                output.Write(buffer, 0, buffer.Length);
                output.Close();
            }
            res.Close();
        }