using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Threading; using Sog; using SimpleHttpServer; using LitJson; namespace SMWebServer { public class HttpApiRootHandler : Singleton { public static string tokenname = "x-access-token"; private Route m_RouteConfig; public Route Init() { Route rootRoute = new Route() { Name = "Root Handler", UrlRegex = @"^/api/.*", Method = "*", Callable = this.ProccessRequest, }; m_RouteConfig = rootRoute; return m_RouteConfig; } public Tuple checktoken(HttpContext queryParams) { var errres = new Tuple(false, ""); var Headers = queryParams.httpRequest.Headers; if (!Headers.ContainsKey(tokenname)) { return errres; } var token = Headers[tokenname]; //Headers头部有空格 原因未知 这里单独去掉 token = token.Replace(" ", ""); if (!SyncHttpServerService.tokenList.ContainsKey(token)) { return errres; } long now = SMWebServerUtils.GetTimeSecond(); if (SyncHttpServerService.tokenList[token].time + 3 * 3600 < now) { return errres; } SyncHttpServerService.tokenList[token].time = now; return new Tuple(true, token); } public TokenInfo gettokeninfo(HttpContext queryParams) { var info = checktoken(queryParams); TokenInfo tokeninfo = new TokenInfo(); if (info.Item1) { SyncHttpServerService.tokenList.TryGetValue(info.Item2, out tokeninfo); return tokeninfo; } return tokeninfo; } private void ProccessRequest(HttpContext httpContext) { TraceLog.Trace("HttpApiRootHandler.ProccessRequest Method {0} url {1}", httpContext.httpRequest.Method, httpContext.httpRequest.Url); string funName = GetFuntionNameByUrl(httpContext.httpRequest.Url); httpContext.httpResponse = new HttpResponse { ContentAsUTF8 = "no process", ReasonPhrase = "OK", StatusCode = "200" }; LitJson.JsonData builder = new LitJson.JsonData(); //权限信息 TokenInfo tokeninfo=null; if (funName != "login" && funName != "install") { var checksuccess = checktoken(httpContext); if (checksuccess.Item1 == false) { builder["code"] = 1; builder["msg"] = "请登录"; string contJson = builder.ToJson(); httpContext.httpResponse.ContentAsUTF8 = contJson; TraceLog.Trace("return content {0}", contJson); return;//这里必须return掉 } } else { //获取用户 tokeninfo = gettokeninfo(httpContext); } switch (funName) { case "login": var q_Login = httpContext.httpRequest.Content.ToJson(); var s_Login = new S_login(); new Login().ProccessRequest(q_Login, s_Login); httpContext.httpResponse.ContentAsUTF8 = s_Login.ToJsonstr(); return; case "getmenu": var q_menu = httpContext.httpRequest.Content.ToJson(); var s_menu = new S_menu(); new Menu().ProccessRequest(q_menu, s_menu, tokeninfo); httpContext.httpResponse.ContentAsUTF8 = s_menu.ToJsonstr(); return; case "gettheservers": var q_TheServers = httpContext.httpRequest.Content.ToJson(); var S_TheServers = new S_TheServers(); new TheServers().ProccessRequest(q_TheServers, S_TheServers, tokeninfo,httpContext); httpContext.httpResponse.ContentAsUTF8 = S_TheServers.ToJsonstr(); return; } string contentJson = builder.ToJson(); httpContext.httpResponse.ContentAsUTF8 = contentJson; TraceLog.Trace("http fun {0} return content {1}", funName, contentJson); } private string GetFuntionNameByUrl(string url) { // "/api/xxxxxxxxxx?param1=yyyy¶m2=zzzzz" int indexend = url.IndexOf('?'); if (indexend == -1) { return url.Split('/')[url.Split('/').Length - 1]; } else { string newurl = url.Substring(5, indexend - 5); if (newurl.IndexOf("/") == -1) { return newurl; } else { return newurl.Substring(0, newurl.IndexOf("/")); } } } } }