Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ControlHandler.cs « WebServer « Server « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 331d9bde156a35c5537c29171ccecb4f843dce6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
//  Copyright (C) 2014, Kenneth Skovhede

//  http://www.hexad.dk, opensource@hexad.dk
//
//  This library is free software; you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as
//  published by the Free Software Foundation; either version 2.1 of the
//  License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful, but
//  WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Linq;
using HttpServer;
using HttpServer.HttpModules;
using System.Collections.Generic;
using Duplicati.Server.Serialization;
using System.IO;

namespace Duplicati.Server.WebServer
{
    internal partial class ControlHandler : HttpModule
    {
        private delegate void ProcessSub(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter writer);
        private readonly Dictionary<string, ProcessSub> SUPPORTED_METHODS;

        public ControlHandler()
        {
            SUPPORTED_METHODS = new Dictionary<string, ProcessSub>(System.StringComparer.InvariantCultureIgnoreCase);

            //Make a list of all supported actions
            SUPPORTED_METHODS.Add("supported-actions", ListSupportedActions);
            SUPPORTED_METHODS.Add("system-info", ListSystemInfo);
            SUPPORTED_METHODS.Add("list-backups", ListBackups);
            SUPPORTED_METHODS.Add("get-current-state", GetCurrentState);
            SUPPORTED_METHODS.Add("get-progress-state", GetProgressState);
            SUPPORTED_METHODS.Add("list-application-settings", ListApplicationSettings);
            SUPPORTED_METHODS.Add("list-options", ListCoreOptions);
            SUPPORTED_METHODS.Add("send-command", SendCommand);
            SUPPORTED_METHODS.Add("get-backup-defaults", GetBackupDefaults);
            SUPPORTED_METHODS.Add("get-folder-contents", GetFolderContents);
            SUPPORTED_METHODS.Add("get-backup", GetBackup);
            SUPPORTED_METHODS.Add("add-backup", AddBackup);
            SUPPORTED_METHODS.Add("update-backup", UpdateBackup);
            SUPPORTED_METHODS.Add("delete-backup", DeleteBackup);
            SUPPORTED_METHODS.Add("validate-path", ValidatePath);
            SUPPORTED_METHODS.Add("list-tags", ListTags);
            SUPPORTED_METHODS.Add("test-backend", TestBackend);
            SUPPORTED_METHODS.Add("create-remote-folder", CreateRemoteFolder);
            SUPPORTED_METHODS.Add("list-remote-folder", ListRemoteFolder);
            SUPPORTED_METHODS.Add("list-backup-sets", ListBackupSets);
            SUPPORTED_METHODS.Add("search-backup-files", SearchBackupFiles);
            SUPPORTED_METHODS.Add("restore-files", RestoreFiles);
            SUPPORTED_METHODS.Add("read-log", ReadLogData);
            SUPPORTED_METHODS.Add("get-license-data", GetLicenseData);
            SUPPORTED_METHODS.Add("get-changelog", GetChangelog);
            SUPPORTED_METHODS.Add("get-acknowledgements", GetAcknowledgements);
            SUPPORTED_METHODS.Add("locate-uri-db", LocateUriDb);
            SUPPORTED_METHODS.Add("poll-log-messages", PollLogMessages);
            SUPPORTED_METHODS.Add("export-backup", ExportBackup);
            SUPPORTED_METHODS.Add("import-backup", ImportBackup);
            SUPPORTED_METHODS.Add("get-ui-settings", GetUISettings);
            SUPPORTED_METHODS.Add("set-ui-settings", SetUISettings);
            SUPPORTED_METHODS.Add("get-ui-schemes", GetUISettingSchemes);
            SUPPORTED_METHODS.Add("get-notifications", GetNotifications);
            SUPPORTED_METHODS.Add("dismiss-notification", DismissNotification);
            SUPPORTED_METHODS.Add("download-bug-report", DownloadBugReport);
            SUPPORTED_METHODS.Add("delete-local-data", DeleteLocalData);
            SUPPORTED_METHODS.Add("get-server-options", GetServerOptions);
            SUPPORTED_METHODS.Add("set-server-options", SetServerOptions);
        }

        public override bool Process (HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session)
        {
            //We use the fake entry point /control.cgi to listen for requests
            //This ensures that the rest of the webserver can just serve plain files
            if (!request.Uri.AbsolutePath.Equals("/control.cgi", StringComparison.InvariantCultureIgnoreCase))
                return false;

            HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString;

            string action = input["action"].Value ?? "";

            //Lookup the actual handler method
            ProcessSub method;
            SUPPORTED_METHODS.TryGetValue(action, out method);

            if (method == null) {
                response.Status = System.Net.HttpStatusCode.NotImplemented;
                response.Reason = "Unsupported action: " + (action == null ? "<null>" : "");
                response.Send();
            } else {
                //Default setup
                response.Status = System.Net.HttpStatusCode.OK;
                response.Reason = "OK";
                #if DEBUG
                response.ContentType = "text/plain";
                #else
                response.ContentType = "text/json";
                #endif
                using (BodyWriter bw = new BodyWriter(response))
                {
                    try
                    {
                        method(request, response, session, bw);
                    }
                    catch (Exception ex)
                    {
                        Program.DataConnection.LogError("", string.Format("Request for {0} gave error", action), ex);
                        Console.WriteLine(ex.ToString());

                        try
                        {
                            if (!response.HeadersSent)
                            {
                                response.Status = System.Net.HttpStatusCode.InternalServerError;
                                response.Reason = "Error";
                                response.ContentType = "text/plain";

                                bw.WriteJsonObject(new
                                {
                                    Message = ex.Message,
                                    Type = ex.GetType().Name,
                                    #if DEBUG
                                    Stacktrace = ex.ToString()
                                    #endif
                                });
                                bw.Flush();
                            }
                        }
                        catch (Exception flex)
                        {
                            Program.DataConnection.LogError("", "Reporting error gave error", flex);
                        }
                    }
                }
            }

            return true;
        }

        private void ReportError(HttpServer.IHttpResponse response, BodyWriter bw, string message)
        {
            response.Status = System.Net.HttpStatusCode.InternalServerError;
            response.Reason = message;

            bw.WriteJsonObject(new { Error = message });
        }

        private List<Dictionary<string, object>> DumpTable(System.Data.IDbCommand cmd, string tablename, string pagingfield, string offset_str, string pagesize_str)
        {
            var result = new List<Dictionary<string, object>>();

            long pagesize;
            if (!long.TryParse(pagesize_str, out pagesize))
                pagesize = 100;

            pagesize = Math.Max(10, Math.Min(500, pagesize));

            cmd.CommandText = "SELECT * FROM \"" + tablename + "\"";
            long offset = 0;
            if (!string.IsNullOrWhiteSpace(offset_str) && long.TryParse(offset_str, out offset) && !string.IsNullOrEmpty(pagingfield))
            {
                var p = cmd.CreateParameter();
                p.Value = offset;
                cmd.Parameters.Add(p);

                cmd.CommandText += " WHERE \"" + pagingfield + "\" < ?";
            }

            if (!string.IsNullOrEmpty(pagingfield))
                cmd.CommandText += " ORDER BY \"" + pagingfield + "\" DESC";
            cmd.CommandText += " LIMIT " + pagesize.ToString();

            using(var rd = cmd.ExecuteReader())
            {
                var names = new List<string>();
                for(var i = 0; i < rd.FieldCount; i++)
                    names.Add(rd.GetName(i));

                while (rd.Read())
                {
                    var dict = new Dictionary<string, object>();
                    for(int i = 0; i < names.Count; i++)
                        dict[names[i]] = rd.GetValue(i);

                    result.Add(dict);                                    
                }
            }

            return result;
        }

        private void LocateUriDb(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString;
            var uri = input["uri"].Value;

            if (string.IsNullOrWhiteSpace(uri))
            {
                ReportError(response, bw, "Invalid request, missing uri");
            }
            else
            {
                var path = Library.Main.DatabaseLocator.GetDatabasePath(uri, null, false, false);

                bw.SetOK();
                bw.WriteJsonObject(new {Exists = !string.IsNullOrWhiteSpace(path)});
            }
        }

        private void GetAcknowledgements(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            var path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "acknowledgements.txt");
                bw.SetOK();
                bw.WriteJsonObject(new {
                    Status = "OK",
                    Acknowledgements = System.IO.File.ReadAllText(path)
                });            
        }

        private void GetChangelog(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString;
            var fromUpdate = input["from-update"].Value;

            if (!Library.Utility.Utility.ParseBool(fromUpdate, false))
            {
                var path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "changelog.txt");
                bw.SetOK();
                bw.WriteJsonObject(new {
                    Status = "OK",
                    Version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(),
                    Changelog = System.IO.File.ReadAllText(path)
                });
            }
            else
            {
                var updateInfo = Program.DataConnection.ApplicationSettings.UpdatedVersion;
                if (updateInfo == null)
                {
                    ReportError(response, bw, "No update found");
                }
                else
                {
                    bw.SetOK();
                    bw.WriteJsonObject(new {
                        Status = "OK",
                        Version = updateInfo.Version,
                        Changelog = updateInfo.ChangeInfo
                    });
                }
            }
        }

        private void GetLicenseData(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            bw.OutputOK(Duplicati.License.LicenseReader.ReadLicenses(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "licenses")));
        }

        private void RestoreFiles(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString;
            var bk = Program.DataConnection.GetBackup(input["id"].Value);
            if (bk == null)
            {
                ReportError(response, bw, "Invalid or missing backup id");
                return;
            }

            var filters = input["paths"].Value.Split(new string[] { System.IO.Path.PathSeparator.ToString() }, StringSplitOptions.RemoveEmptyEntries);
            var time = Duplicati.Library.Utility.Timeparser.ParseTimeInterval(input["time"].Value, DateTime.Now);
            var restoreTarget = input["restore-path"].Value;
            var overwrite = Duplicati.Library.Utility.Utility.ParseBool(input["overwrite"].Value, false);
            var task = Runner.CreateRestoreTask(bk, filters, time, restoreTarget, overwrite);
            Program.WorkThread.AddTask(task);

            bw.OutputOK(new { TaskID = task.TaskID });

        }

        private void ListBackupSets(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString;
            var bk = Program.DataConnection.GetBackup(input["id"].Value);
            if (bk == null)
            {
                ReportError(response, bw, "Invalid or missing backup id");
                return;
            }

            var extra = new Dictionary<string, string>();
            extra["list-sets-only"] = "true";
            if (input["include-metadata"].Value != null)
                extra["list-sets-only"] = (!Library.Utility.Utility.ParseBool(input["include-metadata"].Value, false)).ToString();

            var r = Runner.Run(Runner.CreateTask(DuplicatiOperation.List, bk, extra), false) as Duplicati.Library.Interface.IListResults;

            bw.OutputOK(r.Filesets);
        }

        private void ListRemoteFolder(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString;
            if (input["url"] == null || input["url"].Value == null)
            {
                ReportError(response, bw, "The url parameter was not set");
                return;
            }

            try
            {
                using(var b = Duplicati.Library.DynamicLoader.BackendLoader.GetBackend(input["url"].Value, new Dictionary<string, string>()))
                    bw.OutputOK(new { Status = "OK", Folders = b.List() });

            }
            catch (Exception ex)
            {
                ReportError(response, bw, ex.Message);
            }
        }
        private void CreateRemoteFolder(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString;
            if (input["url"] == null || input["url"].Value == null)
            {
                ReportError(response, bw, "The url parameter was not set");
                return;
            }

            try
            {
                using(var b = Duplicati.Library.DynamicLoader.BackendLoader.GetBackend(input["url"].Value, new Dictionary<string, string>()))
                    b.CreateFolder();

                bw.OutputOK();
            }
            catch (Exception ex)
            {
                ReportError(response, bw, ex.Message);
            }
        }

        private void TestBackend(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString;
            if (input["url"] == null || input["url"].Value == null)
            {
                ReportError(response, bw, "The url parameter was not set");
                return;
            }

            var modules = (from n in Library.DynamicLoader.GenericLoader.Modules
                                    where n is Library.Interface.IConnectionModule
                                    select n).ToArray();

            try
            {
                var url = input["url"].Value;
                var uri = new Library.Utility.Uri(url);
                var qp = uri.QueryParameters;

                var opts = new Dictionary<string, string>();
                foreach(var k in qp.Keys.Cast<string>())
                    opts[k] = qp[k];

                foreach(var n in modules)
                    n.Configure(opts);

                using(var b = Duplicati.Library.DynamicLoader.BackendLoader.GetBackend(url, new Dictionary<string, string>()))
                    b.Test();

                bw.OutputOK();
            }
            catch (Duplicati.Library.Interface.FolderMissingException)
            {
                ReportError(response, bw, "missing-folder");
            }
            catch (Duplicati.Library.Utility.SslCertificateValidator.InvalidCertificateException icex)
            {
                if (string.IsNullOrWhiteSpace(icex.Certificate))
                    ReportError(response, bw, icex.Message);
                else
                    ReportError(response, bw, "incorrect-cert:" + icex.Certificate);
            }
            catch (Exception ex)
            {
                ReportError(response, bw, ex.Message);
            }
            finally
            {
                foreach(var n in modules)
                    if (n is IDisposable)
                        ((IDisposable)n).Dispose();
            }
        }

        private void ListSystemInfo(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            bw.OutputOK(new
            {
                APIVersion = 1,
                PasswordPlaceholder = Duplicati.Server.WebServer.Server.PASSWORD_PLACEHOLDER,
                ServerVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(),
                ServerVersionName = Duplicati.License.VersionNumbers.Version,
                ServerTime = DateTime.Now,
                OSType = Library.Utility.Utility.IsClientLinux ? (Library.Utility.Utility.IsClientOSX ? "OSX" : "Linux") : "Windows",
                DirectorySeparator = System.IO.Path.DirectorySeparatorChar,
                PathSeparator = System.IO.Path.PathSeparator,
                CaseSensitiveFilesystem = Duplicati.Library.Utility.Utility.IsFSCaseSensitive,
                MonoVersion = Duplicati.Library.Utility.Utility.IsMono ? Duplicati.Library.Utility.Utility.MonoVersion.ToString() : null,
                MachineName = System.Environment.MachineName,
                NewLine = System.Environment.NewLine,
                CLRVersion = System.Environment.Version.ToString(),
                CLROSInfo = new
                {
                    Platform = System.Environment.OSVersion.Platform.ToString(),
                    ServicePack = System.Environment.OSVersion.ServicePack,
                    Version = System.Environment.OSVersion.Version.ToString(),
                    VersionString = System.Environment.OSVersion.VersionString
                },
                Options = Serializable.ServerSettings.Options,
                CompressionModules =  Serializable.ServerSettings.CompressionModules,
                EncryptionModules = Serializable.ServerSettings.EncryptionModules,
                BackendModules = Serializable.ServerSettings.BackendModules,
                GenericModules = Serializable.ServerSettings.GenericModules,
                WebModules = Serializable.ServerSettings.WebModules,
                ConnectionModules = Serializable.ServerSettings.ConnectionModules,
                UsingAlternateUpdateURLs = Duplicati.Library.AutoUpdater.AutoUpdateSettings.UsesAlternateURLs,
                LogLevels = Enum.GetNames(typeof(Duplicati.Library.Logging.LogMessageType))
            });
        }

        private void ListSupportedActions(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            bw.OutputOK(new { Version = 1, Methods = SUPPORTED_METHODS.Keys });
        }

        private void ListBackups (HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            var schedules = Program.DataConnection.Schedules;
            var backups = Program.DataConnection.Backups;

            var all = from n in backups
                select new AddOrUpdateBackupData() {
                Backup = (Database.Backup)n,
                Schedule = 
                    (from x in schedules
                        where x.Tags != null && x.Tags.Contains("ID=" + n.ID)
                        select (Database.Schedule)x).FirstOrDefault()
                };

            bw.OutputOK(all.ToArray());
        }

        private void ListTags(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            var r = 
                from n in 
                Serializable.ServerSettings.CompressionModules
                    .Union(Serializable.ServerSettings.EncryptionModules)
                    .Union(Serializable.ServerSettings.BackendModules)
                    .Union(Serializable.ServerSettings.GenericModules)
                    select n.Key.ToLower();

            // Append all known tags
            r = r.Union(from n in Program.DataConnection.Backups select n.Tags into p from x in p select x.ToLower());
            bw.OutputOK(r);
        }

        private void ValidatePath(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString;
            if (input["path"] == null || input["path"].Value == null)
            {
                ReportError(response, bw, "The path parameter was not set");
                return;
            }

            try
            {
                string path = SpecialFolders.ExpandEnvironmentVariables(input["path"].Value);                
                if (System.IO.Path.IsPathRooted(path) && System.IO.Directory.Exists(path))
                {
                    bw.OutputOK();
                    return;
                }
            }
            catch
            {
            }

            ReportError(response, bw, "File or folder not found");
            return;
        }

        private bool LongPollCheck(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, BodyWriter bw, EventPollNotify poller, ref long id, out bool isError)
        {
            HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString;
            if (Library.Utility.Utility.ParseBool(input["longpoll"].Value, false))
            {
                long lastEventId;
                if (!long.TryParse(input["lasteventid"].Value, out lastEventId))
                {
                    ReportError(response, bw, "When activating long poll, the request must include the last event id");
                    isError = true;
                    return false;
                }

                TimeSpan ts;
                try { ts = Library.Utility.Timeparser.ParseTimeSpan(input["duration"].Value); }
                catch (Exception ex)
                {
                    ReportError(response, bw, "Invalid duration: " + ex.Message);
                    isError = true;
                    return false;
                }

                if (ts <= TimeSpan.FromSeconds(10) || ts.TotalMilliseconds > int.MaxValue)
                {
                    ReportError(response, bw, "Invalid duration, must be at least 10 seconds, and less than " + int.MaxValue + " milliseconds");
                    isError = true;
                    return false;
                }

                isError = false;
                id = poller.Wait(lastEventId, (int)ts.TotalMilliseconds);
                return true;
            }

            isError = false;
            return false;
        }

        private void GetProgressState(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            if (Program.GenerateProgressState == null)
            {
                ReportError(response, bw, "No active backup");
            }
            else
            {
                var ev = Program.GenerateProgressState();
                bw.OutputOK(ev);
            }
        }

        private void GetCurrentState (HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            bool isError;
            long id = 0;
            if (LongPollCheck(request, response, bw, Program.StatusEventNotifyer, ref id, out isError))
            {
                //Make sure we do not report a higher number than the eventnotifyer says
                var st = new Serializable.ServerStatus();
                st.LastEventID = id;
                bw.OutputOK(st);
            }
            else if (!isError)
            {
                bw.OutputOK(new Serializable.ServerStatus());
            }
        }

        private void ListCoreOptions(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            bw.OutputOK(new Duplicati.Library.Main.Options(new Dictionary<string, string>()).SupportedCommands);
        }

        private void ListApplicationSettings(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            bw.OutputOK(Program.DataConnection.ApplicationSettings);
        }

        private void DownloadBugReport(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString;
            long id;
            long.TryParse(input["id"].Value, out id);

            var tf = Program.DataConnection.GetTempFiles().Where(x => x.ID == id).FirstOrDefault();
            if (tf == null)
            {
                ReportError(response, bw, "Invalid or missing backup id");
                return;
            }

            if (!System.IO.File.Exists(tf.Path))
            {
                ReportError(response, bw, "File is missing");
                return;
            }

            var filename = "bugreport.zip";
            using(var fs = System.IO.File.OpenRead(tf.Path))
            {
                response.ContentLength = fs.Length;
                response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", filename));
                response.ContentType = "application/octet-stream";

                bw.SetOK();
                response.SendHeaders();
                fs.CopyTo(response.Body);
                response.Send();
            }
        }

        private void DeleteLocalData(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString;
            var bk = Program.DataConnection.GetBackup(input["id"].Value);
            if (bk == null)
            {
                ReportError(response, bw, "Invalid or missing backup id");
                return;
            }

            System.IO.File.Delete(bk.DBPath);
            bw.OutputOK();
        }

        private void GetServerOptions(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            var adv_props = from n in Program.DataConnection.GetSettings(Database.Connection.APP_SETTINGS_ID)
                            select new KeyValuePair<string, string>(n.Name, n.Value);

            bw.OutputOK(adv_props.ToDictionary(x => x.Key, x => x.Value));
        }

        private void SetServerOptions(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            string str = request.Form["data"].Value;
            if (string.IsNullOrWhiteSpace(str))
            {
                ReportError(response, bw, "Missing data object");
                return;
            }

            Dictionary<string, string> data = null;
            try
            {
                data = Serializer.Deserialize<Dictionary<string, string>>(new StringReader(str));
                if (data == null)
                {
                    ReportError(response, bw, "Data object had no backup entry");
                    return;
                }

                Program.DataConnection.ApplicationSettings.UpdateSettings(data);

                bw.OutputOK();
            }
            catch (Exception ex)
            {
                if (data == null)
                    ReportError(response, bw, string.Format("Unable to parse data object: {0}", ex.Message));
                else
                    ReportError(response, bw, string.Format("Unable to save settings: {0}", ex.Message));
            }

        }

        private class ImportExportStructure
        {
            public string CreatedByVersion { get; set; }
            public Duplicati.Server.Database.Schedule Schedule { get; set; }
            public Duplicati.Server.Database.Backup Backup { get; set; }
            public Dictionary<string, string> DisplayNames { get; set; }
        }

        private void ExportBackup(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString;
            var bk = Program.DataConnection.GetBackup(input["id"].Value);
            if (bk == null)
            {
                ReportError(response, bw, "Invalid or missing backup id");
                return;
            }

            var cmdline = Library.Utility.Utility.ParseBool(input["cmdline"].Value, false);
            if (cmdline)
            {
                bw.OutputOK(new { Command = Runner.GetCommandLine(Runner.CreateTask(DuplicatiOperation.Backup, bk)) });
            }
            else
            {
                var passphrase = input["passphrase"].Value;
                var scheduleId = Program.DataConnection.GetScheduleIDsFromTags(new string[] { "ID=" + bk.ID });

                var ipx = new ImportExportStructure() {
                    CreatedByVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(),
                    Backup = (Database.Backup)bk,
                    Schedule = (Database.Schedule)(scheduleId.Any() ? Program.DataConnection.GetSchedule(scheduleId.First()) : null),
                    DisplayNames = GetSourceNames(bk)
                };

                byte[] data;
                using(var ms = new System.IO.MemoryStream())
                using(var sw = new System.IO.StreamWriter(ms))
                {
                    Serializer.SerializeJson(sw, ipx, true);

                    if (!string.IsNullOrWhiteSpace(passphrase))
                    {
                        ms.Position = 0;
                        using(var ms2 = new System.IO.MemoryStream())
                        using(var m = new Duplicati.Library.Encryption.AESEncryption(passphrase, new Dictionary<string, string>()))
                        {
                            m.Encrypt(ms, ms2);
                            data = ms2.ToArray();
                        }
                    }
                    else
                        data = ms.ToArray();
                }

                var filename = Library.Utility.Uri.UrlEncode(bk.Name) + "-duplicati-config.json";
                if (!string.IsNullOrWhiteSpace(passphrase))
                    filename += ".aes";

                response.ContentLength = data.Length;
                response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", filename));
                response.ContentType = "application/octet-stream";

                bw.SetOK();
                response.SendHeaders();
                response.SendBody(data);
            }
        }

        private void ImportBackup(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            var output_template = "<html><body><script type=\"text/javascript\">var rp = null; try { rp = parent['CBM']; } catch (e) {}; rp = rp || alert; rp('MSG');</script></body></html>";
            //output_template = "<html><body><script type=\"text/javascript\">alert('MSG');</script></body></html>";
            try
            {
                HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString;
                var cmdline = Library.Utility.Utility.ParseBool(input["cmdline"].Value, false);
                output_template = output_template.Replace("CBM", input["callback"].Value);
                if (cmdline)
                {
                    response.ContentType = "text/html";
                    bw.Write(output_template.Replace("MSG", "Import from commandline not yet implemented"));
                }
                else
                {
                    ImportExportStructure ipx;

                    var file = request.Form.GetFile("config");
                    if (file == null)
                        throw new Exception("No file uploaded");

                    var buf = new byte[3];
                    using(var fs = System.IO.File.OpenRead(file.Filename))
                    {
                        fs.Read(buf, 0, buf.Length);

                        fs.Position = 0;
                        if (buf[0] == 'A' && buf[1] == 'E' && buf[2] == 'S')
                        {
                            var passphrase = input["passphrase"].Value;
                            using(var m = new Duplicati.Library.Encryption.AESEncryption(passphrase, new Dictionary<string, string>()))
                            using(var m2 = m.Decrypt(fs))
                            using(var sr = new System.IO.StreamReader(m2))
                                ipx = Serializer.Deserialize<ImportExportStructure>(sr);
                        }
                        else
                        {
                            using(var sr = new System.IO.StreamReader(fs))
                                ipx = Serializer.Deserialize<ImportExportStructure>(sr);
                        }
                    }

                    ipx.Backup.ID = null;
                    ((Database.Backup)ipx.Backup).DBPath = null;

                    if (ipx.Schedule != null)
                        ipx.Schedule.ID = -1;

                    lock(Program.DataConnection.m_lock)
                    {
                        var basename = ipx.Backup.Name;
                        var c = 0;
                        while (c++ < 100 && Program.DataConnection.Backups.Where(x => x.Name.Equals(ipx.Backup.Name, StringComparison.InvariantCultureIgnoreCase)).Any())
                            ipx.Backup.Name = basename + " (" + c.ToString() + ")"; 
                        
                        if (Program.DataConnection.Backups.Where(x => x.Name.Equals(ipx.Backup.Name, StringComparison.InvariantCultureIgnoreCase)).Any())
                        {
                            bw.SetOK();
                            response.ContentType = "text/html";
                            bw.Write(output_template.Replace("MSG", "There already exists a backup with the name: " + basename.Replace("\'", "\\'")));
                        }

                        Program.DataConnection.AddOrUpdateBackupAndSchedule(ipx.Backup, ipx.Schedule);
                    }

                    response.ContentType = "text/html";
                    bw.Write(output_template.Replace("MSG", "OK"));

                    //bw.OutputOK(new { status = "OK", ID = ipx.Backup.ID });
                }
            }
            catch (Exception ex)
            {
                Program.DataConnection.LogError("", "Failed to import backup", ex);
                response.ContentType = "text/html";
                bw.Write(output_template.Replace("MSG", ex.Message.Replace("\'", "\\'").Replace("\r", "\\r").Replace("\n", "\\n")));
            }

        }

        private void GetNotifications(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            bw.OutputOK(Program.DataConnection.GetNotifications());
        }

        private void DismissNotification(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString;
            var str = input["id"].Value;
            long id;
            long.TryParse(str, out id);

            if (Program.DataConnection.DismissNotification(id))
                bw.OutputOK();
            else
                ReportError(response, bw, "No such notification");

        }

        private void GetUISettings(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString;
            var scheme = input["scheme"].Value;
            if (string.IsNullOrWhiteSpace(scheme))
            {
                ReportError(response, bw, "No scheme supplied");
                return;
            }

            bw.OutputOK(Program.DataConnection.GetUISettings(scheme));
        }

        private void SetUISettings(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString;
            var scheme = input["scheme"].Value;
            var str = input["data"].Value;
            if (string.IsNullOrWhiteSpace(scheme))
            {
                ReportError(response, bw, "No scheme supplied");
                return;
            }

            if (string.IsNullOrWhiteSpace(str))
            {
                ReportError(response, bw, "No data supplied");
                return;
            }

            IDictionary<string, string> data;
            try
            {
                data = Serializer.Deserialize<Dictionary<string, string>>(new StringReader(str));
            }
            catch (Exception ex)
            {
                ReportError(response, bw, string.Format("Unable to parse settings object: {0}", ex.Message));
                return;
            }

            if (data == null)
            {
                ReportError(response, bw, string.Format("Unable to parse settings object"));
                return;
            }

            Program.DataConnection.SetUISettings(scheme, data);
            bw.OutputOK();
        }

        private void GetUISettingSchemes(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            bw.OutputOK(Program.DataConnection.GetUISettingsSchemes());
        }

        private static void MergeJsonObjects(Newtonsoft.Json.Linq.JObject self, Newtonsoft.Json.Linq.JObject other)
        {
            foreach(var p in other.Properties())
            {
                var sp = self.Property(p.Name);
                if (sp == null)
                    self.Add(p);
                else
                {
                    switch (p.Type)
                    {
                        // Primitives override
                        case Newtonsoft.Json.Linq.JTokenType.Boolean:
                        case Newtonsoft.Json.Linq.JTokenType.Bytes:
                        case Newtonsoft.Json.Linq.JTokenType.Comment:
                        case Newtonsoft.Json.Linq.JTokenType.Constructor:
                        case Newtonsoft.Json.Linq.JTokenType.Date:
                        case Newtonsoft.Json.Linq.JTokenType.Float:
                        case Newtonsoft.Json.Linq.JTokenType.Guid:
                        case Newtonsoft.Json.Linq.JTokenType.Integer:
                        case Newtonsoft.Json.Linq.JTokenType.String:
                        case Newtonsoft.Json.Linq.JTokenType.TimeSpan:
                        case Newtonsoft.Json.Linq.JTokenType.Uri:
                        case Newtonsoft.Json.Linq.JTokenType.None:
                        case Newtonsoft.Json.Linq.JTokenType.Null:
                        case Newtonsoft.Json.Linq.JTokenType.Undefined:
                            self.Replace(p);
                            break;

                            // Arrays merge
                        case Newtonsoft.Json.Linq.JTokenType.Array:
                            if (sp.Type == Newtonsoft.Json.Linq.JTokenType.Array)
                                sp.Value = new Newtonsoft.Json.Linq.JArray(((Newtonsoft.Json.Linq.JArray)sp.Value).Union((Newtonsoft.Json.Linq.JArray)p.Value));
                            else
                            {
                                var a = new Newtonsoft.Json.Linq.JArray(sp.Value);
                                sp.Value = new Newtonsoft.Json.Linq.JArray(a.Union((Newtonsoft.Json.Linq.JArray)p.Value));
                            }

                            break;

                            // Objects merge
                        case Newtonsoft.Json.Linq.JTokenType.Object:
                            if (sp.Type == Newtonsoft.Json.Linq.JTokenType.Object)
                                MergeJsonObjects((Newtonsoft.Json.Linq.JObject)sp.Value, (Newtonsoft.Json.Linq.JObject)p.Value);
                            else
                                sp.Value = p.Value;
                            break;

                            // Ignore other stuff                                
                        default:
                            break;
                    }
                }
            }
        }

        private void GetBackupDefaults(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {   
            // Start with a scratch object
            var o = new Newtonsoft.Json.Linq.JObject();

            // Add application wide settings
            o.Add("ApplicationOptions", new Newtonsoft.Json.Linq.JArray(Program.DataConnection.Settings));

            try
            {
                // Add built-in defaults
                Newtonsoft.Json.Linq.JObject n;
                using(var s = new System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".newbackup.json")))
                    n = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(s.ReadToEnd());

                MergeJsonObjects(o, n);
            }
            catch
            {
            }

            try
            {
                // Add install defaults/overrides, if present
                var path = System.IO.Path.Combine(Duplicati.Library.AutoUpdater.UpdaterManager.InstalledBaseDir, "newbackup.json");
                if (System.IO.File.Exists(path))
                {
                    Newtonsoft.Json.Linq.JObject n;
                    n = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(System.IO.File.ReadAllText(path));

                    MergeJsonObjects(o, n);
                }
            }
            catch
            {
            }

            bw.OutputOK(new
            {
                success = true,
                data = o
            });
        }

        private void PollLogMessages(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString;
            var level_str = input["level"].Value ?? "";
            var id_str = input["id"].Value ?? "";

            Library.Logging.LogMessageType level;
            long id;

            long.TryParse(id_str, out id);
            Enum.TryParse(level_str, true, out level);

            bw.OutputOK(Program.LogHandler.AfterID(id, level));
        }

        private void SendCommand(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw)
        {
            HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString;

            string command = input["command"].Value ?? "";

            switch (command.ToLowerInvariant())
            {
                case "check-update":
                    Program.UpdatePoller.CheckNow();
                    bw.OutputOK();
                    return;

                case "install-update":
                    Program.UpdatePoller.InstallUpdate();
                    bw.OutputOK();
                    return;

                case "activate-update":
                    if (Program.WorkThread.CurrentTask != null || Program.WorkThread.CurrentTasks.Count != 0)
                    {
                        ReportError(response, bw, "Cannot activate update while task is running or scheduled");
                    }
                    else
                    {
                        Program.UpdatePoller.ActivateUpdate();
                        bw.OutputOK();
                    }
                    return;

                case "pause":
                    if (input.Contains("duration") && !string.IsNullOrWhiteSpace(input["duration"].Value))
                    {
                        TimeSpan ts;
                        try
                        {
                            ts = Library.Utility.Timeparser.ParseTimeSpan(input["duration"].Value);
                        }
                        catch (Exception ex)
                        {
                            ReportError(response, bw, ex.Message);
                            return;
                        }
                        if (ts.TotalMilliseconds > 0)
                            Program.LiveControl.Pause(ts);
                        else
                            Program.LiveControl.Pause();
                    }
                    else
                    {
                        Program.LiveControl.Pause();
                    }

                    bw.OutputOK();
                    return;
                case "resume":
                    Program.LiveControl.Resume();
                    bw.OutputOK();
                    return;

                case "stop":
                case "abort":
                    {
                        var task = Program.WorkThread.CurrentTask;
                        var tasks = Program.WorkThread.CurrentTasks;
                        long taskid;
                        if (!input.Contains("taskid") || !long.TryParse(input["taskid"].Value ?? "", out taskid))
                        {
                            ReportError(response, bw, "Invalid or missing taskid");
                            return;
                        }

                        if (task != null)
                            tasks.Insert(0, task);

                        task = tasks.Where(x => x.TaskID == taskid).FirstOrDefault();
                        if (task == null)
                        {
                            ReportError(response, bw, "No such task");
                            return;
                        }

                        if (string.Equals(command, "abort", StringComparison.InvariantCultureIgnoreCase))
                            task.Abort();
                        else
                            task.Stop();

                        bw.OutputOK();
                        return;
                    }

                case "is-backup-active":
                    {
                        var backup = Program.DataConnection.GetBackup(input["id"].Value);
                        if (backup == null)
                        {
                            ReportError(response, bw, string.Format("No backup found for id: {0}", input["id"].Value));
                            return;
                        }

                        var t = Program.WorkThread.CurrentTask;
                        var bt = t == null ? null : t.Backup;
                        if (bt != null && backup.ID == bt.ID)
                        {
                            bw.OutputOK(new { Status = "OK", Active = true });
                            return;
                        }
                        else if (Program.WorkThread.CurrentTasks.Where(x =>
                        { 
                            var bn = x == null ? null : x.Backup;
                            return bn == null || bn.ID == backup.ID;
                        }).Any())
                        {
                            bw.OutputOK(new { Status = "OK", Active = true });
                            return;
                        }
                        else
                        {
                            bw.OutputOK(new { Status = "OK", Active = false });
                            return;
                        }
                    }

                case "run":
                case "run-backup":
                    {

                        var backup = Program.DataConnection.GetBackup(input["id"].Value);
                        if (backup == null)
                        {
                            ReportError(response, bw, string.Format("No backup found for id: {0}", input["id"].Value));
                            return;
                        }

                        var t = Program.WorkThread.CurrentTask;
                        var bt = t == null ? null : t.Backup;
                        if (bt != null && backup.ID == bt.ID)
                        {
                            // Already running
                        }
                        else if (Program.WorkThread.CurrentTasks.Where(x => { 
                            var bn = x == null ? null : x.Backup;
                            return bn == null || bn.ID == backup.ID;
                        }).Any())
                        {
                            // Already in queue
                        }
                        else
                        {
                            Program.WorkThread.AddTask(Runner.CreateTask(DuplicatiOperation.Backup, backup));
                            Program.StatusEventNotifyer.SignalNewEvent();
                        }
                    }
                    bw.OutputOK();
                    return;

                case "run-verify":
                    {
                        var backup = Program.DataConnection.GetBackup(input["id"].Value);
                        if (backup == null)
                        {
                            ReportError(response, bw, string.Format("No backup found for id: {0}", input["id"].Value));
                            return;
                        }

                        var task = Runner.CreateTask(DuplicatiOperation.Verify, backup);
                        Program.WorkThread.AddTask(task);
                        Program.StatusEventNotifyer.SignalNewEvent();

                        bw.OutputOK(new {Status = "OK", ID = task.TaskID});
                    }
                    return;

                case "run-repair":
                    {
                        var backup = Program.DataConnection.GetBackup(input["id"].Value);
                        if (backup == null)
                        {
                            ReportError(response, bw, string.Format("No backup found for id: {0}", input["id"].Value));
                            return;
                        }

                        var task = Runner.CreateTask(DuplicatiOperation.Repair, backup);
                        Program.WorkThread.AddTask(task);
                        Program.StatusEventNotifyer.SignalNewEvent();

                        bw.OutputOK(new {Status = "OK", ID = task.TaskID});
                    }
                    return;
                case "create-report":
                    {
                        var backup = Program.DataConnection.GetBackup(input["id"].Value);
                        if (backup == null)
                        {
                            ReportError(response, bw, string.Format("No backup found for id: {0}", input["id"].Value));
                            return;
                        }

                        var task = Runner.CreateTask(DuplicatiOperation.CreateReport, backup);
                        Program.WorkThread.AddTask(task);
                        Program.StatusEventNotifyer.SignalNewEvent();

                        bw.OutputOK(new { Status = "OK", ID = task.TaskID });
                    }
                    return;

                default:

                    var m = Duplicati.Library.DynamicLoader.WebLoader.Modules.Where(x => x.Key.Equals(command, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                    if (m == null)
                    {
                        ReportError(response, bw, string.Format("Unsupported command {0}", command));
                        return;
                    }

                    bw.OutputOK(new { 
                        Status = "OK", 
                        Result = m.Execute(input.Where(x => 
                            !x.Name.Equals("command", StringComparison.InvariantCultureIgnoreCase)
                            &&
                            !x.Name.Equals("action", StringComparison.InvariantCultureIgnoreCase)
                        ).ToDictionary(x => x.Name, x => x.Value))
                    });
                    return;
            }
        }

    }
}