Skip to content

Commit 4dae14d

Browse files
committed
fix cljs multipart handling
1 parent 5bd33d4 commit 4dae14d

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

src/main/com/yetanalytics/lrs/pedestal/http/multipart_mixed.cljc

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,40 +49,53 @@
4949
(throw (ex-info message
5050
{:type type-k}))))
5151

52+
(defn- boundary-pat-mid-simple
53+
[boundary]
54+
(format "\\r\\n--%s\\r\\n" boundary))
55+
5256
#?(:cljs
5357
(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"
5559
[boundary body]
5660
(let [open-re-pos (u/re-pos
5761
(re-pattern
5862
(boundary-pat-open boundary))
5963
body)
64+
;; Use the same mid pattern as JVM, but without negative lookbehind
6065
mid-re-pos (u/re-pos
6166
(re-pattern
62-
(boundary-pat-mid boundary))
67+
(boundary-pat-mid-simple boundary))
6368
body)
6469
close-re-pos (u/re-pos
6570
(re-pattern
6671
(boundary-pat-close boundary))
6772
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+
6882
all-pos (concat open-re-pos
69-
mid-re-pos
83+
actual-mid-pos
7084
close-re-pos)]
85+
7186
(assert-valid (= 1 (count open-re-pos))
7287
"Only one opening boundary can be present"
7388
::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)
7792
(assert-valid (= 1 (count close-re-pos))
7893
"Only one closing boundary can be present"
7994
::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)]
8397
(subs body
84-
(+ idx-a
85-
(count bound-a))
98+
(+ idx-a (count bound-a))
8699
idx-b)))))
87100

88101
(defn parse-part [^String part boundary]
@@ -133,6 +146,7 @@
133146
(split-multiparts boundary in))))
134147
(catch #?(:clj Exception
135148
:cljs js/Error) ex
149+
#?(:cljs (.log js/console "error" ex))
136150
(throw (ex-info "Invalid Multipart Body"
137151
{:type ::invalid-multipart-body}
138152
ex)))))

0 commit comments

Comments
 (0)