예제 #1
0
        internal void SendFile(OSHttpRequest req, OSHttpResponse response, FileRequest.FileResponse filedata)
        {
            response.StatusCode  = 200;
            response.SendChunked = false;
            response.ContentType = filedata.MimeType.MIME;
            long filelength = filedata.Data.LongLength;

            response.ContentLength64 = filelength;
            if (!filedata.MimeType.binary)
            {
                response.ContentEncoding = filedata.MimeType.Encoding;
            }

            try
            {
                response.OutputStream.Write(filedata.Data, 0, (int)filelength);
            }
            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);
                }
            }
        }
예제 #2
0
        private void HandleContentVerbs(OSHttpRequest request, OSHttpResponse response)
        {
            Stream requestStream = request.InputStream;

            Encoding encoding    = Encoding.UTF8;
            string   requestBody = string.Empty;
            string   host        = string.Empty;

            using (StreamReader reader = new StreamReader(requestStream, encoding))
            {
                requestBody = reader.ReadToEnd();
            }

            Hashtable keysvals    = new Hashtable();
            Hashtable headervals  = new Hashtable();
            Hashtable requestVars = new Hashtable();

            string[] querystringkeys = request.QueryString.AllKeys;
            string[] rHeaders        = request.Headers.AllKeys;

            foreach (string queryname in querystringkeys)
            {
                keysvals.Add(queryname, request.Query[queryname]);
            }
            foreach (string headername in rHeaders)
            {
                headervals.Add(headername, request.Headers[headername]);
            }

            keysvals.Add("body", requestBody);
            keysvals.Add("uri", request.RawUrl);
            keysvals.Add("content-type", request.ContentType);
            keysvals.Add("http-method", request.HttpMethod);

            if (request.ContentEncoding != null && !keysvals.ContainsKey("encoding"))
            {
                keysvals.Add("encoding", request.ContentEncoding);
            }

            keysvals.Add("accepttypes", request.AcceptTypes);

            foreach (string queryname in querystringkeys)
            {
                if (!keysvals.ContainsKey(queryname))
                {
                    keysvals.Add(queryname, request.QueryString[queryname]);
                }
                if (!requestVars.ContainsKey(queryname))
                {
                    requestVars.Add(queryname, keysvals[queryname]);
                }
            }

            foreach (string headername in rHeaders)
            {
                headervals[headername] = request.Headers[headername];
            }

            if (headervals.Contains("Host"))
            {
                host = (string)headervals["Host"];
            }
            keysvals.Add("headers", headervals);
            keysvals.Add("form", request.Form);
            foreach (HttpInputItem item in request.Form)
            {
                if (!keysvals.ContainsKey(item.Name))
                {
                    keysvals.Add(item.Name, item.Value);
                }
                //m_log.Output(string.Format("{0}={1}\r\n", item.Name, item.Value));
            }

            if (keysvals.Contains("method"))
            {
                string            method = (string)keysvals["method"];
                GenericHTTPMethod requestProcessor;
                bool foundHandler = TryGetHTTPHandler(method, out requestProcessor);
                if (foundHandler)
                {
                    Hashtable responsedata1 = requestProcessor(keysvals);
                    DoHTTPGruntWork(responsedata1, response);
                }
                else
                {
                    FileRequest.FileResponse fileoutput = null;
                    Exception exceptionout = null;
                    if (m_fileRequestHandler.TryGetFile(request, out fileoutput, out exceptionout))
                    {
                        if (exceptionout == null)
                        {
                            SendFile(request, response, fileoutput);
                        }
                        else
                        {
                            if (exceptionout is System.Security.SecurityException || exceptionout is UnauthorizedAccessException)
                            {
                                SendHTML403(response);
                            }
                            else if (exceptionout is FileNotFoundException || exceptionout is DirectoryNotFoundException || exceptionout is ArgumentException || exceptionout is ArgumentNullException)
                            {
                                SendHTML404(response, host);
                            }
                            else
                            {
                                SendHTML500(response);
                            }
                        }
                    }
                    else
                    {
                        SendHTML404(response, host);
                    }
                }
            }
            else
            {
                GenericHTTPMethod requestProcessor;
                bool foundHandler = TryGetHTTPHandlerPathBased(request.RawUrl, out requestProcessor);
                if (foundHandler)
                {
                    Hashtable responsedata2 = requestProcessor(keysvals);
                    DoHTTPGruntWork(responsedata2, response);
                }
                else
                {
                    FileRequest.FileResponse fileoutput = null;
                    Exception exceptionout = null;
                    if (m_fileRequestHandler.TryGetFile(request, out fileoutput, out exceptionout))
                    {
                        if (exceptionout == null)
                        {
                            SendFile(request, response, fileoutput);
                        }
                        else
                        {
                            if (exceptionout is System.Security.SecurityException || exceptionout is UnauthorizedAccessException)
                            {
                                SendHTML403(response);
                            }
                            else if (exceptionout is FileNotFoundException || exceptionout is DirectoryNotFoundException || exceptionout is ArgumentException || exceptionout is ArgumentNullException)
                            {
                                SendHTML404(response, host);
                            }
                            else
                            {
                                SendHTML500(response);
                            }
                        }
                    }
                    else
                    {
                        SendHTML404(response, host);
                    }
                }
            }
        }