Esempio n. 1
0
    private void Process(HttpListenerContext context)
    {
        int sessionId = 0;

        if (context.Request.Cookies["id"] != null)
        {
            if (context.Request.Cookies["id"].Expired ||
                !Int32.TryParse(context.Request.Cookies["id"].Value, out sessionId))
            {
                sessionId = 0;
            }
            if (sessionId >= _sessions.Count)
            {
                sessionId = 0;
            }
        }
        if (sessionId == 0)
        {
            for (int i = 1; i < _sessions.Count; i++)
            {
                if (_sessions[i].time.CompareTo(DateTime.Now) < 0)
                {
                    sessionId = i;
                }
            }
            if (sessionId == 0)
            {
                _sessions.Add(new SessionInfo());
                sessionId = _sessions.Count - 1;
            }
        }
        _sessions[sessionId].id   = sessionId;
        _sessions[sessionId].time = DateTime.Now.AddHours(1);
        Cookie cookie = new Cookie("id", "" + sessionId);

        cookie.Expires = _sessions[sessionId].time;
        context.Response.Cookies.Add(cookie);

        string filename = context.Request.Url.AbsolutePath;

        Logger.Log("Get request:" + filename);
        filename = filename.Substring(1);

        if (string.IsNullOrEmpty(filename))
        {
            foreach (string indexFile in _indexFiles)
            {
                if (File.Exists(Path.Combine(_rootDirectory, indexFile)))
                {
                    filename = indexFile;
                    break;
                }
            }
        }

        //API行为
        string apiAtt = "api/";

        if (filename.IndexOf(apiAtt) == 0)
        {
            string[] cmd    = filename.Substring(apiAtt.Length).Split("_");
            string   result = null;
            switch (cmd[0])
            {
            case "GetList":
            {
                result = "应该返回LIST看不见这玩意";
                if (cmd.Length > 1)
                {
                }
                else
                {
                    result = ArchiveManager.GetList(3, 5);
                }
            }
            break;

            case "LoadManga":
            {
                if (cmd.Length < 2)
                {
                    result = "{\"result\":\"Failed\",\"message\":\"请求参数不足。\"}";
                }
                else
                {
                    int mangaId = 0;
                    if (Int32.TryParse(cmd[1], out mangaId))
                    {
                        result = "{\"result\":\"{0}\",\"message\":\"{1}\"}";
                        if (_sessions[sessionId].info == null || !_sessions[sessionId].info.thread.IsAlive)
                        {
                            _sessions[sessionId].info = ArchiveManager.StartUnarchive(mangaId, sessionId);
                            if (_sessions[sessionId].info == null)
                            {
                                result = result.Replace("{0}", "Failed");
                                result = result.Replace("{1}", "文件错误。");
                            }
                            else
                            {
                                result = result.Replace("{0}", "Success");
                                result = result.Replace("{1}", Path.GetFileName(_sessions[sessionId].info.archivePath));
                            }
                        }
                        else
                        {
                            if (_sessions[sessionId].info.thread.IsAlive)
                            {
                                result = result.Replace("{0}", "Busy");
                                result = result.Replace("{1}", "等会。");
                            }
                            else
                            {
                                result = result.Replace("{0}", "Failed");
                                result = result.Replace("{1}", "不应该的错误。");
                            }
                        }
                    }
                    else
                    {
                        result = "{\"result\":\"Failed\",\"message\":\"非法请求。\"}";
                    }
                }
            }
            break;

            case "CheckManga":
            {
                if (_sessions[sessionId].pagePaths == null)
                {
                    _sessions[sessionId].pagePaths = new List <string>();
                }
                _sessions[sessionId].pagePaths.Clear();
                if (_sessions[sessionId].info == null)
                {
                }
                foreach (string path in Directory.GetFiles(_sessions[sessionId].info.tempDirPath))
                {
                    if (ArchiveManager.isImage(path))
                    {
                        _sessions[sessionId].pagePaths.Add(path);
                    }
                }
                _sessions[sessionId].pagePaths.Sort();
                result = "{\"result\":\"{0}\",\"message\":\"{1}\"}";
                result = result.Replace("{1}", _sessions[sessionId].pagePaths.Count.ToString());
                if (_sessions[sessionId].info.thread.IsAlive)
                {
                    result = result.Replace("{0}", "Loading");
                }
                else if (_sessions[sessionId].info.success)
                {
                    result = result.Replace("{0}", "Success");
                }
                else
                {
                    result = result.Replace("{0}", "Failed");
                }
            }
            break;

            case "GetPage":
            {
                int page = 0;
                if (cmd.Length >= 2)
                {
                    if (Int32.TryParse(cmd[1], out page))
                    {
                        if (page >= 0 && page < _sessions[sessionId].pagePaths.Count)
                        {
                            ServeFile(context, _sessions[sessionId].pagePaths[page]);
                        }
                    }
                }
            }
            break;

            case "ShutDown":
            {
                //不知道为啥GetContext不管是Stop Abort都会阻塞,又不让我强行停线程……反正也要做Web端就写成web api吧
                result = "溜了";
                _cts.Cancel();
            }
            break;
            }
            if (result != null)
            {
                try
                {
                    byte[] resultByte = Encoding.UTF8.GetBytes(result);
                    context.Response.ContentType     = "text/plain";
                    context.Response.ContentLength64 = resultByte.Length;
                    context.Response.OutputStream.Write(resultByte, 0, resultByte.Length);
                    context.Response.StatusCode = (int)HttpStatusCode.OK;
                    context.Response.OutputStream.Flush();
                }
                catch (Exception ex)
                {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    Logger.Log(ex.ToString());
                }
            }
        }
        else
        {
            //一般获取文件
            filename = Path.Combine(_rootDirectory, filename);
            ServeFile(context, filename);
        }
        context.Response.AddHeader("Server", "AeroMangaManager/0.1");
        context.Response.OutputStream.Close();
    }