Пример #1
0
 public void ResponseAsync(SocketAgent sa)
 {
 }
Пример #2
0
    // 开始处理应答
    public void BeginResponse(SocketAgent agent)
    {
        Console.WriteLine(m_req.method + " " + m_req.path);

        Response rsp = new Response();

        rsp.headers["Server"] = "SharpHttpServer";
        string path = (WebRoot + m_req.path).Replace('/', '\\');

        if (path.LastIndexOf('\\') == path.Length - 1)
        {
            path += "index.html";
        }

        var ct = GetContentType(path);
        {
            try
            {
                var st = File.OpenRead(path);
                rsp.data = new byte[st.Length];
                int readed = 0, offset = 0;
                do
                {
                    readed  = st.Read(rsp.data, offset, rsp.data.Length);
                    offset += readed;
                } while (offset < rsp.data.Length);
                st.Close();
                rsp.headers["Content-Type"] = ct.ContentTypeString;

                var rdc = new RequestDataContext()
                {
                    GET         = m_req.GetedData,
                    POST        = m_req.PostedData,
                    Connect     = agent,
                    RequestFile = path,
                    Response    = rsp,
                };
                if (ct.HandleSocket)
                {
                    //send by it self
                    if (ct.ServerScript != null)
                    {
                        ct.ServerScript.Execute(rsp.data, rdc);
                    }
                    else
                    {
                        throw new Exception("!!!! Invalid plugin while handle file : " + path);
                    }
                }
                else
                {
                    if (/*ct.IsText && */ ct.ServerScript != null)
                    {
                        rsp.data = ct.ServerScript.Execute(rsp.data, rdc);
                    }
                    agent.SendData(rsp.GetResponse(), _ => agent.Close());
                }
                //rsp.headers["Connection"] = "Keep-Alive";
            }
            catch (Exception ex)
            {
                rsp.state             = "404";
                rsp.state_description = "not found";
                rsp.strdata           = ex.ToString();
                agent.SendData(rsp.GetResponse(), _ => agent.Close());
            }
        }
        //return rsp.GetResponse();//Encoding.UTF8.GetBytes(rsp.ToString());
    }