Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="socket"></param>
        void ProcessRequest(object socket)
        {
            Socket sock = socket as Socket;

            if (sock == null)
            {
                return;
            }
            byte[] inbuf = new byte[512]; // README: If Webserver fails with "server busy", increase this size
            sock.Receive(inbuf);
            string strRequest = Encoding.ASCII.GetString(inbuf);
            NameValueCollection queryString = null;
            Uri uriRequest = ValidateRequest(strRequest);

            Logger.Instance.LogDebug(uriRequest.ToString());

            if (uriRequest == null)
            {
                Logger.Instance.LogDebug("Invalid Request" + strRequest);
                SendErrorResponse(ref sock, HttpResponseCode.InvalidRequest, "Invalid request:\r\n" + strRequest);
                return;
            }

            Logger.Instance.LogDebug("Processing Request: " + uriRequest.ToString());

            if (uriRequest.Query.Length > 0)
            {
                queryString = MakeQueryString(uriRequest.Query);
            }
            string strPathFragment = uriRequest.LocalPath.Replace('/', '\\');

            if (strPathFragment.Equals("\\"))
            {
                strPathFragment = "index.html";
            }
            else if (strPathFragment[0] == '\\')
            {
                strPathFragment = strPathFragment.Substring(1);
            }
            string strFilePath = Path.Combine(m_strWebRoot, strPathFragment);

            if (!File.Exists(strFilePath))
            {
                SendErrorResponse(ref sock, HttpResponseCode.FileNotFound, Resources.FileNotFound);
                return;
            }

            BinaryReader reader = null;

            byte[] responseData;
            try
            {
                FileInfo finfo = new FileInfo(strFilePath);
                reader       = new BinaryReader(finfo.OpenRead());
                responseData = reader.ReadBytes((int)finfo.Length);
                reader.Close();
            }
            catch (IOException e)
            {
                Logger.Instance.LogException(e);
                if (reader != null)
                {
                    reader.Close();
                    reader = null;
                }
                SendErrorResponse(ref sock, HttpResponseCode.Conflict, e.ToString());
                return;
            }
            string strContentType;
            string strExtention = strFilePath.Substring(strFilePath.Length - 4);

            if (strExtention.Equals(".htm", StringComparison.InvariantCultureIgnoreCase) ||
                strExtention.Equals("html", StringComparison.InvariantCultureIgnoreCase))
            {
                strContentType = ContentType.HTML;

                string         strFileData = Encoding.UTF8.GetString(responseData);
                ReplaceTag     replaceTag  = new ReplaceTag(queryString);
                MatchEvaluator matchTag    = new MatchEvaluator(replaceTag.Replace);
                strFileData  = Regex.Replace(strFileData, "<\\$([^>]+)\\$>", matchTag);
                responseData = Encoding.UTF8.GetBytes(strFileData);
            }
            else if (strExtention.Equals(".jpg", StringComparison.InvariantCultureIgnoreCase) ||
                     strExtention.Equals("jpeg", StringComparison.InvariantCultureIgnoreCase))
            {
                strContentType = ContentType.IMAGE_JPEG;
            }
            else if (strExtention.Equals(".png", StringComparison.InvariantCultureIgnoreCase))
            {
                strContentType = ContentType.IMAGE_PNG;
            }
            else if (strExtention.Equals(".gif", StringComparison.InvariantCultureIgnoreCase))
            {
                strContentType = ContentType.IMAGE_GIF;
            }
            else if (strExtention.Equals(".css", StringComparison.InvariantCultureIgnoreCase))
            {
                strContentType = ContentType.CSS;
            }
            else if (strExtention.Equals(".ico", StringComparison.InvariantCultureIgnoreCase))
            {
                strContentType = ContentType.IMAGE_ICO;
            }
            else
            {
                SendErrorResponse(ref sock, HttpResponseCode.Unsupported, "Unsupported mime type requested");
                return;
            }

            SendResponse(ref sock, responseData, strContentType, responseData.Length);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="socket"></param>
        void ProcessRequest(object socket)
        {
            Socket sock = socket as Socket;
            if (sock == null)
                return;
            byte[] inbuf = new byte[512]; // README: If Webserver fails with "server busy", increase this size
            sock.Receive(inbuf);
            string strRequest = Encoding.ASCII.GetString(inbuf);
            NameValueCollection queryString = null;
            Uri uriRequest = ValidateRequest(strRequest);

            Logger.Instance.LogDebug(uriRequest.ToString());

            if (uriRequest == null)
            {
                Logger.Instance.LogDebug("Invalid Request" + strRequest);
                SendErrorResponse(ref sock, HttpResponseCode.InvalidRequest, "Invalid request:\r\n" + strRequest);
                return;
            }

            Logger.Instance.LogDebug("Processing Request: " + uriRequest.ToString());

            if (uriRequest.Query.Length > 0)
            {
               queryString = MakeQueryString(uriRequest.Query);
            }
            string strPathFragment = uriRequest.LocalPath.Replace('/', '\\');
            if (strPathFragment.Equals("\\"))
                strPathFragment = "index.html";
            else if (strPathFragment[0] == '\\')
                strPathFragment = strPathFragment.Substring(1);
            string strFilePath = Path.Combine(m_strWebRoot, strPathFragment);

            if (!File.Exists(strFilePath))
            {
                SendErrorResponse(ref sock, HttpResponseCode.FileNotFound, Resources.FileNotFound);
                return;
            }

            BinaryReader reader = null;
            byte[] responseData;
            try
            {
                FileInfo finfo = new FileInfo(strFilePath);
                reader = new BinaryReader(finfo.OpenRead());
                responseData = reader.ReadBytes((int)finfo.Length);
                reader.Close();
            }
            catch (IOException e)
            {
                Logger.Instance.LogException(e);
                if (reader != null)
                {
                    reader.Close();
                    reader = null;
                }
                SendErrorResponse(ref sock, HttpResponseCode.Conflict, e.ToString());
                return;
            }
            string strContentType;
            string strExtention = strFilePath.Substring(strFilePath.Length - 4);
            if (strExtention.Equals(".htm", StringComparison.InvariantCultureIgnoreCase)
                || strExtention.Equals("html", StringComparison.InvariantCultureIgnoreCase))
            {
                strContentType = ContentType.HTML;

                string strFileData = Encoding.UTF8.GetString(responseData);
                ReplaceTag replaceTag = new ReplaceTag(queryString);
                MatchEvaluator matchTag = new MatchEvaluator(replaceTag.Replace);
                strFileData = Regex.Replace(strFileData, "<\\$([^>]+)\\$>", matchTag);
                responseData = Encoding.UTF8.GetBytes(strFileData);
            }
            else if (strExtention.Equals(".jpg", StringComparison.InvariantCultureIgnoreCase)
                || strExtention.Equals("jpeg", StringComparison.InvariantCultureIgnoreCase))
            {
                strContentType = ContentType.IMAGE_JPEG;
            }
            else if (strExtention.Equals(".png", StringComparison.InvariantCultureIgnoreCase))
            {
                strContentType = ContentType.IMAGE_PNG;
            }
            else if (strExtention.Equals(".gif", StringComparison.InvariantCultureIgnoreCase))
            {
                strContentType = ContentType.IMAGE_GIF;
            }
            else if (strExtention.Equals(".css", StringComparison.InvariantCultureIgnoreCase))
            {
                strContentType = ContentType.CSS;
            }
            else if (strExtention.Equals(".ico", StringComparison.InvariantCultureIgnoreCase))
            {
                strContentType = ContentType.IMAGE_ICO;
            }
            else
            {
                SendErrorResponse(ref sock, HttpResponseCode.Unsupported, "Unsupported mime type requested");
                return;
            }

            SendResponse(ref sock, responseData, strContentType, responseData.Length);
        }