Example #1
0
        private void FiddlerApplication_BeforeResponse(Session oSession) {

            if (oSession.PathAndQuery.StartsWith("/kcs/")) {
                string filePath = Utility.Config.Instance.CacheFolder + oSession.getFilePath();

                if (oSession.responseCode == 304) {

                    // code 304, 文件沒有修改, 使用本地文件
                    if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath)) {

                        oSession.bBufferResponse = true;
                        oSession.ResponseBody = File.ReadAllBytes(filePath);
                        oSession.oResponse.headers.HTTPResponseCode = 200;
                        oSession.oResponse.headers.HTTPResponseStatus = "200 OK";
                        oSession.oResponse.headers["Last-Modified"] = oSession.oRequest.headers["If-Modified-Since"];
                        oSession.oResponse.headers["Accept-Ranges"] = "bytes";
                        oSession.oResponse.headers.Remove("If-Modified-Since");
                        oSession.oRequest.headers.Remove("If-Modified-Since");
                        if (filePath.EndsWith(".swf"))
                            oSession.oResponse.headers["Content-Type"] = "application/x-shockwave-flash";

                        Utility.Logger.Add("Response > [304, 返回本地]" + filePath);
                    }
                } else if (oSession.responseCode == 200) {

                    // code 200, 更新緩存紀錄 
                    Cache.UpdataCache(oSession.PathAndQuery);

                    if (File.Exists(filePath)) {
                        string resBody = oSession.GetResponseBodyAsString();
                        string cacheBody = File.ReadAllText(filePath);

                        // 比對緩存 
                        if (resBody.Equals(cacheBody)) {

                            Utility.Logger.Add("Response > [200, 檔案相同]" + filePath);
                        } else {

                            if (Utility.Config.Instance.AutoBackupCache) {
                                // 保存舊緩存 
                                int index = filePath.LastIndexOf('.');
                                if (index > 0) {
                                    string iPath = filePath.Substring(0, index);
                                    string iType = filePath.Substring(index);   // .swf

                                    DateTime dateNow = DateTime.Now;
                                    string dateTime = (dateNow.Year % 100).ToString() + dateNow.Month.ToString("00") + dateNow.Day.ToString("00");
                                    string newFilePath = iPath + "_" + dateTime + iType;

                                    File.Move(filePath, newFilePath);
                                }
                            }
                            
                        try {
                            oSession.SaveResponseBody(filePath);
                        } catch (Exception ex) {
                            DateTime dateNow = DateTime.Now;
                            string dateTime = dateNow.Year.ToString() + dateNow.Month.ToString("00") + dateNow.Day.ToString("00") + "_" +
                                dateNow.Hour.ToString("00") + dateNow.Minute.ToString("00") + dateNow.Second.ToString("00");

                            Utility.Logger.CmdLog("dateTime: ");
                            Utility.Logger.CmdLog(ex.ToString());
                        }

                            Utility.Logger.Add("Response > [200, 更新緩存]" + filePath);
                        }
                        // code 200, 更新時間 
                        GMTHelper._SaveModifiedTime(filePath, oSession.oResponse.headers["Last-Modified"]);

                    } else {
                        // 儲存快取並設置時間 
                        try {
                            oSession.SaveResponseBody(filePath);
                        } catch (Exception ex) {
                            DateTime dateNow = DateTime.Now;
                            string dateTime = dateNow.Year.ToString() + dateNow.Month.ToString("00") + dateNow.Day.ToString("00") + "_" +
                                dateNow.Hour.ToString("00") + dateNow.Minute.ToString("00") + dateNow.Second.ToString("00");

                            Utility.Logger.CmdLog("dateTime: ");
                            Utility.Logger.CmdLog(ex.ToString());
                        }
                        GMTHelper._SaveModifiedTime(filePath, oSession.oResponse.headers["Last-Modified"]);
                        Utility.Logger.Add("Response > [200, 建立緩存]" + filePath);
                    }
                }

                // 魔改 
                if (filePath.Contains(@"kcs\resources\swf\ships\")) {

                    string fileName = filePath.getFileName();
                    if (CosManager.Instance.IsPaired(fileName)) {
                        Utility.Logger.Add("IsPaired > " + fileName);
                        //filePath = filePath.Replace(fileName, CosManager.Instance.GetPair(fileName));
                        filePath = Utility.Config.Instance.CostumeFolder + @"\" + CosManager.Instance.GetPair(fileName) + ".swf";
                        oSession.ResponseBody = File.ReadAllBytes(filePath);
                    }
                }


            } else if (oSession.PathAndQuery.StartsWith("/kcsapi/")) {

                string url = oSession.PathAndQuery;
                string body = oSession.GetResponseBodyAsString();

                Utility.Logger.Add("Response > " + url);

                try {
                    // 非同期で書き出し処理するので取っておく
                    // stringはイミュータブルなのでOK
                    Task.Run((Action)(() => { SaveResponse(url, body); }));
                } catch (Exception ex) {
                    Utility.ErrorReporter.SendErrorReport(ex, "通信内容保存失败。");
                }

                // api修改 
                if (oSession.bBufferResponse) {
                    Utility.Logger.Add("Response > LoadDynamicJson( " + url + " )");
                    string newbody = LoadDynamicJson(url, body);
                    if (newbody != null) {
                            oSession.utilSetResponseBody(newbody);
                    }
                }

            }
        }