Exemplo n.º 1
0
        public void SendHTML404(OSHttpResponse response, string host)
        {
            response.StatusCode = 404;

            response.AddHeader("Content-Type", "text/html");
            string responseString = "<html><head><title>OMG 404 FailWhale OMEGALOL Kappa</title></head><body><H1>It didnt work!</H1><p>There is no module handling this request Kappa</p></body></html>";

            byte[] buffer = Encoding.UTF8.GetBytes(responseString);
            response.SendChunked     = false;
            response.ContentLength64 = buffer.LongLength;
            response.ContentEncoding = Encoding.UTF8;
            try
            {
                response.OutputStream.Write(buffer, 0, buffer.Length);
            }
            catch (Exception ex)
            {
                m_Output.LogMessage("error", "[HTTPD]: Error - " + ex.Message + Environment.NewLine);
            }
            finally
            {
                try
                {
                    response.Send();
                }
                catch (SocketException e)
                {
                    m_Output.LogMessage("error", "[HTTPD]: SocketError - " + e.Message + Environment.NewLine);
                }
            }
        }
Exemplo n.º 2
0
        public void SendHTML500(OSHttpResponse response)
        {
            // I know this statuscode is dumb, but the client doesn't respond to 404s and 500s
            response.StatusCode = (int)HttpStatusCode.OK;
            response.AddHeader("Content-type", "text/html");

            string responseString = "<html><head><title>OMG 500 It esploded</title></head><body><H1>Something had a problem!</H1><p>There was a problem doing the thing.   Only thing.  Sometimes it do be like that.</p></body></html>";;

            byte[] buffer = Encoding.UTF8.GetBytes(responseString);

            response.SendChunked     = false;
            response.ContentLength64 = buffer.Length;
            response.ContentEncoding = Encoding.UTF8;
            try
            {
                response.OutputStream.Write(buffer, 0, buffer.Length);
            }
            catch (Exception ex)
            {
                m_Output.LogMessage("error", "[HTTPD]: Error - " + ex.Message + Environment.NewLine);
            }
            finally
            {
                //response.OutputStream.Close();
                try
                {
                    response.Send();
                    //response.FreeContext();
                }
                catch (SocketException e)
                {
                    // This has to be here to prevent a Linux/Mono crash
                    m_Output.LogMessage("error", "[HTTPD] Socket issue " + e.Message + Environment.NewLine);
                }
            }
        }
Exemplo n.º 3
0
        internal void DoHTTPGruntWork(Hashtable responsedata, OSHttpResponse response)
        {
            int    responsecode   = (int)responsedata["int_response_code"];
            string responseString = string.Empty;

            if (responsedata.ContainsKey("str_response_string"))
            {
                responseString = (string)responsedata["str_response_string"];
            }

            string contentType = string.Empty;

            if (responsedata.ContainsKey("content_type"))
            {
                contentType = (string)responsedata["content_type"];
            }

            string RedirectLocationHeader = string.Empty;

            if (responsedata.ContainsKey("redirect_location"))
            {
                RedirectLocationHeader = (string)responsedata["redirect_location"];
            }

            if (responsedata.ContainsKey("error_status_text"))
            {
                response.StatusDescription = (string)responsedata["error_status_text"];
            }
            if (responsedata.ContainsKey("http_protocol_version"))
            {
                response.ProtocolVersion = (string)responsedata["http_protocolversion_version"];
            }
            if (responsedata.ContainsKey("keepalive"))
            {
                bool keepalive = (bool)responsedata["keepalive"];
                response.KeepAlive = keepalive;
            }

            if (responsedata.ContainsKey("reusecontext"))
            {
                response.ReuseContext = (bool)responsedata["reusecontext"];
            }

            if (string.IsNullOrEmpty(contentType))
            {
                contentType = "text/html";
            }
            response.StatusCode = responsecode;

            switch (responsecode)
            {
            case 302:
                response.AddHeader("Location", RedirectLocationHeader);
                break;

            default:
                response.AddHeader("Content-Type", contentType);
                break;
            }



            byte[] buffer;
            if (!contentType.Contains("image"))
            {
                buffer = Encoding.UTF8.GetBytes(responseString);
            }
            else
            {
                buffer = Convert.FromBase64String(responseString);
            }
            response.SendChunked     = false;
            response.ContentLength64 = buffer.LongLength;
            response.ContentEncoding = Encoding.UTF8;

            try
            {
                response.OutputStream.Write(buffer, 0, buffer.Length);
            }
            catch (Exception ex)
            {
                m_Output.LogMessage("error", "[HTTPD]: Error - " + ex.Message + Environment.NewLine);
            }
            finally
            {
                try
                {
                    response.OutputStream.Flush();
                    response.Send();
                }
                catch (SocketException e)
                {
                    m_Output.LogMessage("warn", "There was an underlying socket error.  Perhaps the socket disconnected." + e.Message + Environment.NewLine);
                }
                catch (IOException e)
                {
                    m_Output.LogMessage("warn", "There was an IO issue: " + e.Message + Environment.NewLine);
                }
            }
        }