public bool LoadFromCache(Session oSession)
        {
            bool success = false;
            string key = oSession.fullUrl;
            if (!cache.ContainsKey(key))
            {
                if (key.Contains('?')) {
                    key = key.Substring(0, key.IndexOf('?'));
                }
            }
            if (cache.ContainsKey(key)) 
            {
                CacheItem item = cache[key];
                if (item.CheckState == System.Windows.Forms.CheckState.Checked
                    && File.Exists(item.Local))
                {
                    oSession.utilCreateResponseAndBypassServer();
                    item.SetSessionResponse(oSession);                    
                    success = oSession.LoadResponseFromFile(item.Local);
                }
                
            }


            return success;
        }
Beispiel #2
0
		/// <summary>
		/// 从本地读取 api_start2 通信数据
		/// </summary>
		static object ReadSessionData(string path)
		{
			//var path = Settings.Current.CacheFolder + "\\api_start2.dat";
			//var bytes = Encoding.UTF8.GetBytes(File.ReadAllText(path));

			//var serializer = new DataContractJsonSerializer(typeof(svdata<kcsapi_start2>));
			//using (var stream = new MemoryStream(bytes))
			//{
			//	return serializer.ReadObject(stream) as svdata<kcsapi_start2>;
			//}

			Session session = new Session(new byte[] { 0 }, new byte[] { 0 });
			if (!session.LoadRequestBodyFromFile(path))
			{
				throw new ApplicationException("LoadRequestBodyFromFile Failed!! " + path);
			}
			if (!session.LoadResponseFromFile(path))
			{
				throw new ApplicationException("LoadResponseFromFile Failed!! " + path);
			}
			return session;
		}
 /// <summary>
 /// IgnoreResponse ignores the incoming response.
 /// </summary>
 /// <param name="relevantSession">The session whose response should be failed.</param>
 private static void IgnoreResponse(Session relevantSession)
 {
     relevantSession.LoadResponseFromFile(StockResponses.NoContent204);
 }
        public void AutoTamperRequestBefore(Session oSession)
        {
            if (!IsEnabled)
            {
                return;
            }

            string fullString = oSession.fullUrl.ToLower();

            if (fullString.EndsWith("imposter.js") && EnableAutoReload)
            {
                oSession.utilCreateResponseAndBypassServer();
                var js = Path.GetFullPath("Scripts\\imposter.js");
                oSession.LoadResponseFromFile(js);
                oSession.ResponseHeaders.Add("x-imposter", js);
            }

            if (fullString.ToLower().Contains("/imposter-poll-for-changes?profileid=") && EnableAutoReload)
            {
                var profileIdIndex = fullString.ToLower().IndexOf("/imposter-poll-for-changes?profileid=");
                var profileIdFragment = fullString.Substring(profileIdIndex + "/imposter-poll-for-changes?profileid=".Length);

                Guid profileId;
                var success = Guid.TryParse(profileIdFragment, out profileId);

                oSession.utilCreateResponseAndBypassServer();
                oSession.ResponseHeaders.Add("x-imposter", "AUTO RELOAD");

                if (success && _enabledProfiles.Any(p => p.ProfileId == profileId && p.HasChanges))
                {
                    oSession.utilSetResponseBody("true");
                    _enabledProfiles.ForEach(p => p.HasChanges = false);
                }
                else
                {
                    oSession.utilSetResponseBody("false");
                }
            }

            foreach (var profile in _enabledProfiles)
            {
                var path = profile.GetFileMatch(fullString);

                if (path == null)
                {
                    continue;
                }

                oSession.utilCreateResponseAndBypassServer();
                oSession.LoadResponseFromFile(path);
                oSession.ResponseHeaders.Add("x-imposter", path);
                if (oSession.ViewItem != null)
                {
                    oSession.ViewItem.BackColor = Color.SkyBlue;
                }
                // Only swap for the first match
                break;
            }
        }