Example #1
0
        public HTTPResponse ResponseFromObject(object obj, HTTPCode code)
        {
            if (obj is HTTPResponse)
            {
                return((HTTPResponse)obj);
            }

            if (obj is string)
            {
                return(HTTPResponse.FromString((string)obj, code, Settings.Compression));
            }

            if (obj is byte[])
            {
                return(HTTPResponse.FromBytes((byte[])obj));
            }

            if (obj is DataNode)
            {
                var root = (DataNode)obj;
                var json = JSONWriter.WriteToString(root);
                return(HTTPResponse.FromString(json, code, Settings.Compression, "application/json"));
            }

            var type = obj.GetType();

            Logger(LogLevel.Error, $"Can't serialize object of type '{type.Name}' into HTTP response");
            return(null);
        }
Example #2
0
 /// <summary>
 /// Writes the header to the output stream
 /// </summary>
 public void SendHeader()
 {
     CheckHeaderSent();
     m_writer.Write("HTTP/1.0 " + HTTPCode.ToString("000") + " " + HTTPMessage + "\r\n");
     foreach (DictionaryEntry pair in Header)
     {
         m_writer.Write(pair.Key + ": " + pair.Value + "\r\n");
     }
     m_writer.Write("\r\n");
     m_writer.Flush();
     m_status = ResponseStatus.HeaderSent;
 }
Example #3
0
        public static HTTPResponse FromString(string content, HTTPCode code = HTTPCode.OK, bool compress = false, string contentType = "text/html")
        {
            var bytes = System.Text.Encoding.UTF8.GetBytes(content);

            if (compress)
            {
                bytes = bytes.GZIPCompress();
            }

            var result = new HTTPResponse();

            result.code  = code;
            result.bytes = bytes;
            result.headers["Content-Type"] = contentType;

            if (compress)
            {
                result.headers["Content-Encoding"] = "gzip";
            }

            return(result);
        }