Beispiel #1
0
        public bool Exec(Target target, Env env, WebStream output)
        {
            _target = target;

            //出力用バッファ
            var sb = new StringBuilder();

            var encoding = MLang.GetEncoding(target.FullPath);

            //***************************************************
            // 対象ファイルの読み込み
            //***************************************************
            using (var sr = new StreamReader(target.FullPath, encoding)) {
                while (true)
                {
                    var str = sr.ReadLine();
                    if (str == null)
                    {
                        break;
                    }
                    //SSIキーワードを検索して置き換える(再帰処理)
                    SsiConvert(ref str, encoding);
                    sb.Append(str + "\r\n");
                }
                sr.Close();
            }
            output.Add(encoding.GetBytes(sb.ToString()));
            return(true);
        }
Beispiel #2
0
        public void getEncoding及びgetstringの確認()
        {
            //setUp
            string str = "あいうえお";

            string[] charsetList = new[] { "utf-8", "euc-jp", "iso-2022-jp", "shift_jis" };

            //verify
            foreach (string charset in charsetList)
            {
                byte[] bytes = Encoding.GetEncoding(charset).GetBytes(str);
                Assert.That(MLang.GetEncoding(bytes).WebName, Is.EqualTo(charset));
                Assert.That(MLang.GetString(bytes), Is.EqualTo(str));
            }
        }
Beispiel #3
0
        public void getEncoding_fileName_の確認()
        {
            //setUp
            string tempFile = Path.GetTempFileName();
            //File tempFile = File.createTempFile("tmp", ".txt");
            List <string> lines = new List <string>();

            lines.Add("あいうえお");
            File.WriteAllLines(tempFile, lines);

            Encoding sut      = MLang.GetEncoding(tempFile);
            string   expected = "utf-8";
            //exercise
            string actual = sut.WebName;

            //verify
            Assert.That(actual, Is.EqualTo(expected));
            //TearDown
            File.Delete(tempFile);
        }
Beispiel #4
0
        //バイナリデータであることが判明している場合は、noEncodeをtrueに設定する
        //これによりテキスト判断ロジックを省略できる
        protected void Trace(TraceKind traceKind, byte[] buf, bool noEncode)
        {
            if (buf == null || buf.Length == 0)
            {
                return;
            }

            if (Kernel.RunMode == RunMode.Remote)
            {
                return; //リモートクライアントの場合は、ここから追加されることはない
            }

            //Ver5.0.0-a22 サービス起動の場合は、このインスタンスは生成されていない
            bool enableDlg = Kernel.TraceDlg != null && Kernel.TraceDlg.Visible;

            if (!enableDlg && Kernel.RemoteConnect == null)
            {
                //どちらも必要ない場合は処置なし
                return;
            }

            bool     isText   = false; //対象がテキストかどうかの判断
            Encoding encoding = null;

            if (!noEncode)
            {
                //エンコード試験が必要な場合
                try{
                    encoding = MLang.GetEncoding(buf);
                } catch {
                    encoding = null;
                }
                if (encoding != null)
                {
                    //int codePage = encoding.CodePage;
                    if (encoding.CodePage == 20127 || encoding.CodePage == 65001 || encoding.CodePage == 51932 || encoding.CodePage == 1200 || encoding.CodePage == 932 || encoding.CodePage == 50220)
                    {
                        //"US-ASCII" 20127
                        //"Unicode (UTF-8)" 65001
                        //"日本語(EUC)" 51932
                        //"Unicode" 1200
                        //"日本語(シフトJIS)" 932
                        //日本語(JIS) 50220
                        isText = true;
                    }
                }
            }

            var ar = new List <String>();

            if (isText)
            {
                var lines = Inet.GetLines(buf);
                ar.AddRange(lines.Select(line => encoding.GetString(Inet.TrimCrlf(line))));
            }
            else
            {
                ar.Add(noEncode ? string.Format("binary {0} byte", buf.Length) : string.Format("Binary {0} byte", buf.Length));
            }
            foreach (var str in ar)
            {
                Ip ip = RemoteIp;

                if (enableDlg)
                {
                    //トレースダイアログが表示されてい場合、データを送る
                    Kernel.TraceDlg.AddTrace(traceKind, str, ip);
                }
                if (Kernel.RemoteConnect != null)
                {
                    //リモートサーバへもデータを送る(クライアントが接続中の場合は、クライアントへ送信される)
                    Kernel.RemoteConnect.AddTrace(traceKind, str, ip);
                }
            }
        }
Beispiel #5
0
        //�f�[�^�擾�i����f�[�^�́A�����������j
        public bool Recv(Logger logger, SockTcp tcpObj, int timeout, ILife iLife)
        {
            var buf = tcpObj.LineRecv(timeout, iLife);

            if (buf == null)
            {
                return(false);
            }
            buf = Inet.TrimCrlf(buf);

            _urlEncoding = MLang.GetEncoding(buf);//URL�G���R�[�h�̌`����ۑ�����

            //Ver5.9.8
            if (_urlEncoding == null)
            {
                var sb = new StringBuilder();
                for (int i = 0; i < buf.Length; i++)
                {
                    sb.Append(String.Format("0x{0:X},", buf[i]));
                }
                logger.Set(LogKind.Error, tcpObj, 9999, String.Format("_urlEncoding==null buf.Length={0} buf={1}", buf.Length, sb.ToString()));
                //���̂܂ܗ�O�֓˓�������
            }

            var str = _urlEncoding.GetString(buf);

            // ���\�b�h�EURI�E�o�[�W�����ɕ���
            //"GET http://hostname:port@user:pass/path/filename.ext?param HTTP/1.1"
            RequestStr = str;

            //(�󔒂ŕ�������)�@"GET <=> http://hostname:port@user:pass/path/filename.ext?param HTTP/1.1"
            var index = str.IndexOf(' ');

            if (index < 0) //Ver5.0.0-a8
            {
                return(false);
            }

            //(�O��) "GET"
            var methodStr = str.Substring(0, index);

            foreach (HttpMethod m in Enum.GetValues(typeof(HttpMethod)))
            {
                if (methodStr.ToUpper() == m.ToString().ToUpper())
                {
                    HttpMethod = m;
                    break;
                }
            }
            if (HttpMethod == HttpMethod.Unknown)
            {
                logger.Set(LogKind.Secure, tcpObj, 1, string.Format("{0}", RequestStr));//�T�|�[�g�O�̃��\�b�h�ł��i������p���ł��܂���j
                return(false);
            }
            if (HttpMethod == HttpMethod.Connect)
            {
                Protocol = ProxyProtocol.Ssl;
                Port     = 443;//�f�t�H���g�̃|�[�g�ԍ���443�ɂȂ�
            }

            //(�㔼) "http://*****:*****@user:pass/path/filename.ext?param HTTP/1.1"
            str = str.Substring(index + 1);


            //(�󔒂ŕ�������)�@"http://*****:*****@user:pass/path/filename.ext?param <=> HTTP/1.1"
            index = str.IndexOf(' ');
            if (index < 0) //Ver5.0.0-a8
            {
                return(false);
            }
            //(�㔼) "HTTP/1.1"
            HttpVer = str.Substring(index + 1);

            if (HttpVer != "HTTP/0.9" && HttpVer != "HTTP/1.0" && HttpVer != "HTTP/1.1")
            {
                logger.Set(LogKind.Secure, tcpObj, 2, RequestStr);//�T�|�[�g�O�̃o�[�W�����ł��i������p���ł��܂���j
                return(false);
            }

            //(�O��) "http://*****:*****@user:pass/path/filename.ext?param"
            str = str.Substring(0, index);

            if (Protocol == ProxyProtocol.Unknown)  //�v���g�R���擾
            //("://"�������)�@"http <=> hostname:port@user:pass/path/filename.ext?param <=> HTTP/1.1"
            {
                index = str.IndexOf("://");
                if (index < 0) //Ver5.0.0-a8
                {
                    return(false);
                }
                //(�O��) "http"
                var protocolStr = str.Substring(0, index);

                if (protocolStr.ToLower() == "ftp")
                {
                    Protocol = ProxyProtocol.Ftp; //�v���g�R����FTP�ɏC��
                    Port     = 21;                //FTP�ڑ��̃f�t�H���g�̃|�[�g�ԍ���21�ɂȂ�
                }
                else if (protocolStr.ToLower() != "http")
                {
                    //Ver5.6.7
                    //Msg.Show(MsgKind.Error,"�݌v�G���[�@Request.Recv()");
                    //�G���[�\����|�b�v�A�b�v���烍�O�ɕύX
                    logger.Set(LogKind.Error, tcpObj, 29, string.Format("protocolStr={0}", protocolStr));
                    return(false);
                }
                else
                {
                    Protocol = ProxyProtocol.Http;
                }
                //(�㔼) "hostname:port@user:pass/path/filename.ext?param"
                str = str.Substring(index + 3);
            }
            //(�ŏ���"/"�ŕ�������)�@"hostname:port@user:pass <=> /path/filename.ext?param"
            index    = str.IndexOf('/');
            HostName = str;
            if (0 <= index)
            {
                //(�O��) ""hostname:port@user:pass"
                HostName = str.Substring(0, index);

                //(�㔼) "/path/filename.ext?param"
                str = str.Substring(index);
            }
            else
            {
                // GET http://hostname HTTP/1.0 �̂悤�ɁA���[�g�f�B���N�g�����w�肳��Ă��Ȃ��ꍇ�̑Ώ�
                str = "/";
            }

            //�z�X�g�������Ƀ��[�U���F�p�X���[�h�������Ă���ꍇ�̏���
            index = HostName.IndexOf("@");
            if (0 <= index)
            {
                var userpass = HostName.Substring(0, index);

                //���[�U���F�p�X���[�h��j������
                HostName = HostName.Substring(index + 1);

                var i = userpass.IndexOf(':');
                if (i == -1)
                {
                    User = userpass;
                }
                else
                {
                    User = userpass.Substring(0, i);
                    Pass = userpass.Substring(i + 1);
                }
            }
            //Ver5.1.2 IPv6�A�h���X�\�L�̃z�X�g���ɑΉ�
            var tmp = HostName.Split(new[] { '[', ']' });

            if (tmp.Length == 3) //IPv6�A�h���X�\�L�ł���Ɣ��f����
            {
                HostName = string.Format("[{0}]", tmp[1]);
                index    = tmp[2].IndexOf(":");
                if (0 <= index)
                {
                    var s = tmp[2].Substring(index + 1);
                    Port = Convert.ToInt32(s);
                }
            }
            else
            {
                //�z�X�g�������Ƀ|�[�g�ԍ��������Ă���ꍇ�̏���
                index = HostName.IndexOf(":");
                if (0 <= index)
                {
                    var s = HostName.Substring(index + 1);
                    Port     = Convert.ToInt32(s);
                    HostName = HostName.Substring(0, index);
                }
            }

            Uri = str;

            //CGI����
            if (-1 != Uri.LastIndexOf('?'))
            {
                Cgi = true;
            }

            //�g���q�擾
            if (!Cgi)
            {
                index = Uri.LastIndexOf('/');
                if (index != -1)
                {
                    str = Uri.Substring(index + 1);
                }
                index = str.LastIndexOf('.');
                if (index != -1)
                {
                    Ext = str.Substring(index + 1);
                }
            }
            return(true);
        }