Commit 83647a74 authored by Grant's avatar Grant
Browse files

prevent express from mangling headers

parent 6174eafe
Loading
Loading
Loading
Loading
Loading
+15 −12
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ type MultipartMixedPart = {
  headers: {
    [k: string]: string;
  };
  body: any;
  body?: string;
};

export class MultipartMixed {
@@ -22,28 +22,31 @@ export class MultipartMixed {
      "Content-Type",
      `multipart/mixed; boundary="${this.boundary}"`
    );
    res.send(this.toString());
    // send buffer directly to avoid express modifying headers
    // @see https://github.com/expressjs/express/issues/2654
    res.send(Buffer.from(this.toString()));
  }

  toString() {
    const lines: string[] = [
      "sc07 matrix-discord-bridge multipart/mixed builder",
    ];
    const CRLF = "\r\n";
    let output = CRLF;

    for (const part of this.parts) {
      lines.push("--" + this.boundary);
      output += "--" + this.boundary + CRLF;

      for (const [header, value] of Object.entries(part.headers)) {
        lines.push(header + ": " + value);
        output += header + ": " + value + CRLF;
      }

      lines.push("");
      lines.push(part.body);
      lines.push("");
      output += CRLF;

      if ("body" in part) {
        output += part.body + CRLF;
      }
    }

    lines.push("--" + this.boundary + "--");
    output += "--" + this.boundary + "--";

    return lines.join("\r\n");
    return output;
  }
}
+2 −3
Original line number Diff line number Diff line
@@ -133,15 +133,14 @@ export class MediaRoutes extends Router {
    new MultipartMixed()
      .add({
        headers: {
          "Content-Type": "application/json",
          "content-type": "application/json",
        },
        body: JSON.stringify({}),
      })
      .add({
        headers: {
          Location: targetURL,
          location: targetURL,
        },
        body: "",
      })
      .send(res);
  }