summaryrefslogtreecommitdiffstats
path: root/comm/calendar/test/ICSServer.jsm
blob: c0f120ec2a46b15f18dad0a7775335f577a56616 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at http://mozilla.org/MPL/2.0/. */

const EXPORTED_SYMBOLS = ["ICSServer"];

const { Assert } = ChromeUtils.importESModule("resource://testing-common/Assert.sys.mjs");
const { CommonUtils } = ChromeUtils.importESModule("resource://services-common/utils.sys.mjs");
const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");

var ICSServer = {
  server: null,
  isOpen: false,

  ics: "",
  etag: "",
  open(username, password) {
    this.server = new HttpServer();
    this.server.start(-1);
    this.isOpen = true;

    this.username = username;
    this.password = password;
    this.server.registerPathHandler("/ping", this.ping);
    this.server.registerPathHandler(this.path, this.handleICS.bind(this));

    this.reset();
  },

  reset() {
    this.ics = "";
    this.etag = "";
  },

  close() {
    if (!this.isOpen) {
      return Promise.resolve();
    }
    return new Promise(resolve =>
      this.server.stop({
        onStopped: () => {
          this.isOpen = false;
          resolve();
        },
      })
    );
  },

  get origin() {
    return `http://localhost:${this.server.identity.primaryPort}`;
  },

  get path() {
    return "/test.ics";
  },

  get url() {
    return `${this.origin}${this.path}`;
  },

  get altPath() {
    return "/addressbooks/me/default/";
  },

  get altURL() {
    return `${this.origin}${this.altPath}`;
  },

  checkAuth(request, response) {
    if (!this.username || !this.password) {
      return true;
    }
    if (!request.hasHeader("Authorization")) {
      response.setStatusLine("1.1", 401, "Unauthorized");
      response.setHeader("WWW-Authenticate", `Basic realm="test"`);
      return false;
    }

    let value = request.getHeader("Authorization");
    if (!value.startsWith("Basic ")) {
      response.setStatusLine("1.1", 401, "Unauthorized");
      response.setHeader("WWW-Authenticate", `Basic realm="test"`);
      return false;
    }

    let [username, password] = atob(value.substring(6)).split(":");
    if (username != this.username || password != this.password) {
      response.setStatusLine("1.1", 401, "Unauthorized");
      response.setHeader("WWW-Authenticate", `Basic realm="test"`);
      return false;
    }

    return true;
  },

  ping(request, response) {
    response.setStatusLine("1.1", 200, "OK");
    response.setHeader("Content-Type", "text/plain");
    response.write("pong");
  },

  handleICS(request, response) {
    if (!this.checkAuth(request, response)) {
      return;
    }

    switch (request.method) {
      case "HEAD":
        this.headICS(request, response);
        return;
      case "GET":
        this.getICS(request, response);
        return;
      case "PUT":
        this.putICS(request, response);
        return;
    }

    Assert.report(true, undefined, undefined, "Should not have reached here");
    response.setStatusLine("1.1", 405, "Method Not Allowed");
    response.setHeader("Content-Type", "text/plain");
    response.write(`Method not allowed: ${request.method}`);
  },

  headICS(request, response) {
    response.setStatusLine("1.1", 200, "OK");
    response.setHeader("Content-Type", "text/calendar");
    response.setHeader("ETag", this.etag);
  },

  getICS(request, response) {
    this.headICS(request, response);
    response.write(this.ics);
  },

  async putICS(request, response) {
    response.processAsync();

    await this.putICSInternal(CommonUtils.readBytesFromInputStream(request.bodyInputStream));

    response.setStatusLine("1.1", 204, "No Content");
    response.setHeader("ETag", this.etag);

    response.finish();
  },

  async putICSInternal(ics) {
    this.ics = ics;

    let hash = await crypto.subtle.digest("sha-1", new TextEncoder().encode(this.ics));
    this.etag = Array.from(new Uint8Array(hash), c => c.toString(16).padStart(2, "0")).join("");
  },
};