|
49 | 49 | (throw (ex-info message |
50 | 50 | {:type type-k})))) |
51 | 51 |
|
| 52 | +(defn- boundary-pat-mid-simple |
| 53 | + [boundary] |
| 54 | + (format "\\r\\n--%s\\r\\n" boundary)) |
| 55 | + |
52 | 56 | #?(:cljs |
53 | 57 | (defn split-multiparts |
54 | | - "Splits multipart body parts, ensuring start + end and at least 2 parts" |
| 58 | + "Splits multipart body parts, ensuring start + end and at least 1 part" |
55 | 59 | [boundary body] |
56 | 60 | (let [open-re-pos (u/re-pos |
57 | 61 | (re-pattern |
58 | 62 | (boundary-pat-open boundary)) |
59 | 63 | body) |
| 64 | + ;; Use the same mid pattern as JVM, but without negative lookbehind |
60 | 65 | mid-re-pos (u/re-pos |
61 | 66 | (re-pattern |
62 | | - (boundary-pat-mid boundary)) |
| 67 | + (boundary-pat-mid-simple boundary)) |
63 | 68 | body) |
64 | 69 | close-re-pos (u/re-pos |
65 | 70 | (re-pattern |
66 | 71 | (boundary-pat-close boundary)) |
67 | 72 | body) |
| 73 | + |
| 74 | + ;; Filter out any mid matches that are actually part of the opening |
| 75 | + actual-mid-pos |
| 76 | + (filter (fn [[idx _]] |
| 77 | + (let [open-end (when-let [[open-idx open-match] (first open-re-pos)] |
| 78 | + (+ open-idx (count open-match)))] |
| 79 | + (or (nil? open-end) (>= idx open-end)))) |
| 80 | + mid-re-pos) |
| 81 | + |
68 | 82 | all-pos (concat open-re-pos |
69 | | - mid-re-pos |
| 83 | + actual-mid-pos |
70 | 84 | close-re-pos)] |
| 85 | + |
71 | 86 | (assert-valid (= 1 (count open-re-pos)) |
72 | 87 | "Only one opening boundary can be present" |
73 | 88 | ::invalid-one-opening-boundary) |
74 | | - (assert-valid (<= 1 (count mid-re-pos)) |
75 | | - "At least one mid boundary must be present" |
76 | | - ::invalid-at-least-one-mid-boundary) |
| 89 | + (assert-valid (>= (count actual-mid-pos) 0) |
| 90 | + "Mid boundaries must be valid" |
| 91 | + ::invalid-mid-boundaries) |
77 | 92 | (assert-valid (= 1 (count close-re-pos)) |
78 | 93 | "Only one closing boundary can be present" |
79 | 94 | ::invalid-one-closing-boundary) |
80 | | - (for [[[idx-a |
81 | | - bound-a] |
82 | | - [idx-b]] (partition 2 1 all-pos)] |
| 95 | + |
| 96 | + (for [[[idx-a bound-a] [idx-b]] (partition 2 1 all-pos)] |
83 | 97 | (subs body |
84 | | - (+ idx-a |
85 | | - (count bound-a)) |
| 98 | + (+ idx-a (count bound-a)) |
86 | 99 | idx-b))))) |
87 | 100 |
|
88 | 101 | (defn parse-part [^String part boundary] |
|
133 | 146 | (split-multiparts boundary in)))) |
134 | 147 | (catch #?(:clj Exception |
135 | 148 | :cljs js/Error) ex |
| 149 | + #?(:cljs (.log js/console "error" ex)) |
136 | 150 | (throw (ex-info "Invalid Multipart Body" |
137 | 151 | {:type ::invalid-multipart-body} |
138 | 152 | ex))))) |
|
0 commit comments