Esempio n. 1
0
        private async Task <ReqResult> HandlePost(HttpRequest request, HttpResponse response)
        {
            string path = request.Path;

            try {
                if (path == Path_Login)
                {
                    string?user;
                    string?pass;
                    using (var reader = new StreamReader(request.Body, Encoding.UTF8)) {
                        var obj = await StdJson.JObjectFromReaderAsync(reader);

                        user = (string?)obj["user"];
                        pass = (string?)obj["pass"];
                        if (user == null || pass == null)
                        {
                            return(ReqResult.Bad("Missing user and password."));
                        }
                    }

                    var        session = new Session();
                    Connection connection;
                    try {
                        connection = await HttpConnection.ConnectWithUserLogin("localhost", clientPort, user, pass, null, session, timeoutSeconds : 90);
                    }
                    catch (Exception exp) {
                        logWarn(exp.Message);
                        return(ReqResult.Bad(exp.Message));
                    }
                    await session.SetConnection(connection, model, moduleID, viewTypes);

                    sessions[session.ID] = session;

                    var result = new JObject();
                    result["sessionID"] = session.ID;
                    string str = StdJson.ObjectToString(uiModel);
                    JRaw   raw = new JRaw(str);
                    result["model"] = raw;
                    return(ReqResult.OK(result));
                }
                else if (path.StartsWith(Path_ViewReq))
                {
                    string viewRequest = path.Substring(Path_ViewReq.Length);

                    (Session session, string viewID) = GetSessionFromQuery(request.QueryString.ToString());

                    string content;
                    using (var reader = new StreamReader(request.Body, Encoding.UTF8)) {
                        content = await reader.ReadToEndAsync();
                    }
                    return(await session.OnViewCommand(viewID, viewRequest, DataValue.FromJSON(content)));
                }
                else if (path == Path_ActivateView)
                {
                    (Session session, string viewID) = GetSessionFromQuery(request.QueryString.ToString());
                    await session.OnActivateView(viewID);

                    return(ReqResult.OK());
                }
                else if (path == Path_DuplicateView)
                {
                    (Session session, string viewID) = GetSessionFromQuery(request.QueryString.ToString());
                    string newViewID = await session.OnDuplicateView(viewID);

                    uiModel = MakeUiModel(model, viewTypes);

                    return(ReqResult.OK(new {
                        newViewID,
                        model = uiModel
                    }));
                }
                else if (path == Path_DuplicateConvertView)
                {
                    (Session session, string viewID) = GetSessionFromQuery(request.QueryString.ToString());
                    string newViewID = await session.OnDuplicateConvertHistoryPlot(viewID);

                    uiModel = MakeUiModel(model, viewTypes);

                    return(ReqResult.OK(new {
                        newViewID,
                        model = uiModel
                    }));
                }
                else if (path == Path_RenameView)
                {
                    (Session session, string viewID) = GetSessionFromQuery(request.QueryString.ToString());

                    string?newViewName;
                    using (var reader = new StreamReader(request.Body, Encoding.UTF8)) {
                        var obj = await StdJson.JObjectFromReaderAsync(reader);

                        newViewName = (string?)obj["newViewName"];
                        if (newViewName == null)
                        {
                            return(ReqResult.Bad("Missing newViewName"));
                        }
                    }

                    await session.OnRenameView(viewID, newViewName);

                    uiModel = MakeUiModel(model, viewTypes);

                    return(ReqResult.OK(new {
                        model = uiModel
                    }));
                }
                else if (path == Path_MoveView)
                {
                    (Session session, string viewID) = GetSessionFromQuery(request.QueryString.ToString());

                    bool up = false;
                    using (var reader = new StreamReader(request.Body, Encoding.UTF8)) {
                        var obj = await StdJson.JObjectFromReaderAsync(reader);

                        up = (bool)obj["up"] !;
                    }

                    await session.OnMoveView(viewID, up);

                    uiModel = MakeUiModel(model, viewTypes);

                    return(ReqResult.OK(new {
                        model = uiModel
                    }));
                }
                else if (path == Path_DeleteView)
                {
                    (Session session, string viewID) = GetSessionFromQuery(request.QueryString.ToString());
                    await session.OnDeleteView(viewID);

                    uiModel = MakeUiModel(model, viewTypes);

                    return(ReqResult.OK(new {
                        model = uiModel
                    }));
                }
                else if (path == Path_Logout)
                {
                    string sessionID;
                    using (var reader = new StreamReader(request.Body, Encoding.UTF8)) {
                        sessionID = await reader.ReadToEndAsync();
                    }

                    if (sessions.ContainsKey(sessionID))
                    {
                        Session session = sessions[sessionID];
                        var     ignored = session.Close();
                        sessions.Remove(sessionID);
                    }

                    return(ReqResult.OK());
                }
                else
                {
                    return(ReqResult.Bad("Invalid path: " + path));
                }
            }
            catch (InvalidSessionException exp) {
                logWarn("HandlePost: " + exp.Message);
                return(ReqResult.Bad(exp.Message));
            }
            catch (Exception exp) {
                logWarn("HandlePost:", exp);
                return(ReqResult.Bad(exp.Message));
            }
        }