Example #1
0
        void SendResponse(ClientInfo ci, HttpRequest req, HttpResponse resp, bool close)
        {
            var en = Encoding.Default;

#if DEBUG
            Console.WriteLine("Response: " + resp.ReturnCode + Responses[resp.ReturnCode]);
#endif
            ByteBuilder bb = new ByteBuilder();
            bb.Add(en.GetBytes("HTTP/1.1 " + resp.ReturnCode + " " + Responses[resp.ReturnCode] +
                               "\r\nDate: " + DateTime.Now.ToString("R") +
                               "\r\nServer: RedCoronaEmbedded/1.0" +
                               "\r\nConnection: " + (close ? "close" : "Keep-Alive")));
            if (resp.RawContent == null)
            {
                //bb.Add(Encoding.UTF8.GetBytes("\r\nContent-Encoding: utf-8" +
                //    "\r\nContent-Length: " + resp.Content.Length));

                bb.Add(en.GetBytes("\r\nContent-Encoding: " + en.BodyName + "" +
                                   "\r\nContent-Length: " + (en.GetByteCount(resp.Content))));
            }
            else
            {
                bb.Add(en.GetBytes("\r\nContent-Length: " + resp.RawContent.Length));
            }
            if (resp.ContentType != null)
            {
                bb.Add(en.GetBytes("\r\nContent-Type: " + resp.ContentType));
            }
            if (req.Session != null)
            {
                bb.Add(en.GetBytes("\r\nSet-Cookie: _sessid=" + req.Session.ID + "; path=/"));
            }
            foreach (KeyValuePair <string, string> de in resp.Header)
            {
                //bb.Add(Encoding.UTF8.GetBytes("\r\n" + de.Key + ": " + de.Value));
                bb.Add(en.GetBytes("\r\n" + de.Key + ": " + de.Value));
            }
            bb.Add(en.GetBytes("\r\n\r\n")); // End of header
            if (resp.RawContent != null)
            {
                bb.Add(resp.RawContent);
            }
            else
            {
                //bb.Add(Encoding.UTF8.GetBytes(resp.Content));
                bb.Add(en.GetBytes(resp.Content));
            }
            ci.Send(bb.Read(0, bb.Length));
#if DEBUG
            Console.WriteLine("** SENDING\n" + resp.Content);
#endif
            if (close)
            {
                ci.Close();
            }
        }