保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 9bdb95c9e34cef640534e5e5a1e2225a80442000
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
"use strict";
const {
  removeLeadingAndTrailingHTTPWhitespace,
  removeTrailingHTTPWhitespace,
  isHTTPWhitespaceChar,
  solelyContainsHTTPTokenCodePoints,
  soleyContainsHTTPQuotedStringTokenCodePoints,
  asciiLowercase
} = require("./utils.js");
 
module.exports = input => {
  input = removeLeadingAndTrailingHTTPWhitespace(input);
 
  let position = 0;
  let type = "";
  while (position < input.length && input[position] !== "/") {
    type += input[position];
    ++position;
  }
 
  if (type.length === 0 || !solelyContainsHTTPTokenCodePoints(type)) {
    return null;
  }
 
  if (position >= input.length) {
    return null;
  }
 
  // Skips past "/"
  ++position;
 
  let subtype = "";
  while (position < input.length && input[position] !== ";") {
    subtype += input[position];
    ++position;
  }
 
  subtype = removeTrailingHTTPWhitespace(subtype);
 
  if (subtype.length === 0 || !solelyContainsHTTPTokenCodePoints(subtype)) {
    return null;
  }
 
  const mimeType = {
    type: asciiLowercase(type),
    subtype: asciiLowercase(subtype),
    parameters: new Map()
  };
 
  while (position < input.length) {
    // Skip past ";"
    ++position;
 
    while (isHTTPWhitespaceChar(input[position])) {
      ++position;
    }
 
    let parameterName = "";
    while (position < input.length && input[position] !== ";" && input[position] !== "=") {
      parameterName += input[position];
      ++position;
    }
    parameterName = asciiLowercase(parameterName);
 
    if (position < input.length) {
      if (input[position] === ";") {
        continue;
      }
 
      // Skip past "="
      ++position;
    }
 
    let parameterValue = "";
    if (input[position] === "\"") {
      ++position;
 
      while (true) {
        while (position < input.length && input[position] !== "\"" && input[position] !== "\\") {
          parameterValue += input[position];
          ++position;
        }
 
        if (position < input.length && input[position] === "\\") {
          ++position;
          if (position < input.length) {
            parameterValue += input[position];
            ++position;
            continue;
          } else {
            parameterValue += "\\";
            break;
          }
        } else {
          break;
        }
      }
 
      while (position < input.length && input[position] !== ";") {
        ++position;
      }
    } else {
      while (position < input.length && input[position] !== ";") {
        parameterValue += input[position];
        ++position;
      }
 
      parameterValue = removeTrailingHTTPWhitespace(parameterValue);
 
      if (parameterValue === "") {
        continue;
      }
    }
 
    if (parameterName.length > 0 &&
        solelyContainsHTTPTokenCodePoints(parameterName) &&
        soleyContainsHTTPQuotedStringTokenCodePoints(parameterValue) &&
        !mimeType.parameters.has(parameterName)) {
      mimeType.parameters.set(parameterName, parameterValue);
    }
  }
 
  return mimeType;
};