public static byte[] GetJsonSetup(stCoCServerConfig.CoCServerConfigData.Configuration conf) { try { if (CoCClientSetup._setupJsonByte == null) { string ver = stApp.AppInformation.GetAppVersion(); DataTable dt = SqliteConvertExtension.MapToDataTable <ServerSetup>(); DataRow dr = dt.NewRow(); dr["IRCServer"] = conf.Opt.IRCServer.value; dr["IRCPort"] = (System.Int64)conf.Opt.IRCPort.num; dr["IRCChannel"] = conf.Opt.IRCChannel.value; dr["IRCLanguage"] = conf.Opt.IRCLanguage.value; dr["NotifyUpdateTime"] = (System.Int64)conf.Opt.SQLDBUpdateTime.num; dr["URLClan"] = CoCClientSetup._GetIPFLocation(conf, stNet.WebHandleTypes.JsonWebRequest); dr["URLNotify"] = CoCClientSetup._GetIPFLocation(conf, stNet.WebHandleTypes.ServerSentEventWebRequest); dr["URLInformer"] = CoCClientSetup._GetIPFLocation(conf, stNet.WebHandleTypes.InformerWebRequest); dr["URLIrcLog"] = CoCClientSetup._GetIPFLocation(conf, stNet.WebHandleTypes.TemplateWebRequest); dr["URLWiki"] = CoCClientSetup._GetIPFLocation(conf, stNet.WebHandleTypes.WikiWebRequest); dr["ServerVersion"] = ver; dr["ServerMagic"] = ver.ToMD5(); dt.Rows.Add(dr); if ((CoCClientSetup._setupJsonByte = Encoding.UTF8.GetBytes( dt.ToJson(false, true, (conf.Opt.SQLDBUpdateTime.num * 60)) )) == null) { throw new ArgumentNullException(); } } return(CoCClientSetup._setupJsonByte); } catch (Exception e) { conf.ILog.LogError(e.Message); return(default(byte[])); } }
public static void SaveJsonSetup(stCoCServerConfig.CoCServerConfigData.Configuration conf) { try { CoCClientSetup._setupJsonByte = CoCClientSetup.GetJsonSetup(conf); if (CoCClientSetup._setupJsonByte == null) { throw new ArgumentNullException(); } File.WriteAllBytes( Path.Combine( conf.Opt.SYSROOTPath.value, conf.Opt.IPFLocation[0].value, Properties.Settings.Default.setJsonSetupFile ), CoCClientSetup._setupJsonByte ); } catch (Exception e) { throw e; } }
public static void JsonWebRequest(string url, object ctx, object udata) { byte[] msg; stCoCServerConfig.CoCServerConfigData.Configuration conf = udata as stCoCServerConfig.CoCServerConfigData.Configuration; HttpListenerContext context = ctx as HttpListenerContext; if ( (udata == null) || (conf.HttpSrv == null) ) { context.Response.Abort(); return; } if ( (conf.Api == null) || (!conf.Api.DBCheck()) ) { conf.HttpSrv.BadRequestJson(HttpStatusCode.InternalServerError.ToString(), context, (int)HttpStatusCode.InternalServerError); return; } string [] urlPart = url.Split('/'); urlPart = urlPart.Skip(1).Concat(urlPart.Take(1)).ToArray(); Array.Resize(ref urlPart, urlPart.Length - 1); if ((urlPart.Length < 3) || (urlPart.Length > 7)) { conf.HttpSrv.BadRequestJson(HttpStatusCode.BadRequest.ToString(), context, (int)HttpStatusCode.BadRequest); return; } try { stCoCAPI.CoCAPI.CoCEnum.ClanTypeReq cReq = stCoCAPI.CoCAPI.CoCEnum.ClanTypeReq.None; string query = string.Empty; query = conf.Api.GetQueryString(urlPart, ref cReq, conf.Opt.SQLDBFilterMemberTag.collection, conf.ILog.LogError); switch (cReq) { case stCoCAPI.CoCAPI.CoCEnum.ClanTypeReq.None: { throw new ArgumentNullException(); } case stCoCAPI.CoCAPI.CoCEnum.ClanTypeReq.ServerSetup: { msg = CoCClientSetup.GetJsonSetup(conf); break; } default: { if (string.IsNullOrWhiteSpace(query)) { throw new ArgumentNullException(); } try { #if DEBUG_PrintWebRequest stConsole.WriteHeader("JsonWebRequest -> URL: (" + url + ") Query: (" + query + ")"); #endif msg = Encoding.UTF8.GetBytes( conf.Api.QueryData(query).ToJson(true, true) ); #if DEBUG_PrintJson stConsole.WriteHeader("JsonWebRequest -> JSON: " + Encoding.UTF8.GetString(msg)); #endif } catch (Exception) { throw new ArgumentException(HttpStatusCode.BadRequest.ToString()); } break; } } } catch (Exception e) { conf.HttpSrv.BadRequestJson(e.Message, context, (int)HttpStatusCode.InternalServerError); return; } try { context.Response.AddHeader(conf.HttpSrv.httpContentType, HttpUtil.GetMimeType("", HttpUtil.MimeType.MimeJson)); context.Response.ContentLength64 = msg.Length; context.Response.OutputStream.Write(msg, 0, msg.Length); context.Response.OutputStream.Close(); } #if DEBUG catch (Exception e) { conf.ILog.LogError( string.Format( Properties.Resources.fmtMainError, string.Format(fmtClassName, "Json"), e.GetType().Name, e.Message ) ); #else catch (Exception) { #endif context.Response.Abort(); return; } context.Response.Close(); }