From b966a9ecccaf132878a7574acc8775edd0eac117 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 2 Dec 2024 14:56:35 -0500 Subject: [PATCH] feat multipart-form: expose content_disposition --- src/multipart_form/tiny_httpd_multipart_form_data.ml | 8 ++------ src/multipart_form/tiny_httpd_multipart_form_data.mli | 5 ++++- src/multipart_form/utils_.ml | 10 ++++++++++ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/multipart_form/tiny_httpd_multipart_form_data.ml b/src/multipart_form/tiny_httpd_multipart_form_data.ml index 315b35e0..0868899b 100644 --- a/src/multipart_form/tiny_httpd_multipart_form_data.ml +++ b/src/multipart_form/tiny_httpd_multipart_form_data.ml @@ -2,6 +2,7 @@ open Tiny_httpd module Slice = Iostream.Slice +module Content_disposition = Content_disposition let spf = Printf.sprintf @@ -216,12 +217,7 @@ let parse_content_type (hs : Tiny_httpd.Headers.t) : _ option = match Utils_.split1_on ~c:'=' @@ String.trim s with | Some ("boundary", "") -> () | Some ("boundary", s) -> - let s = - if s.[0] = '"' && s.[String.length s - 1] = '"' then - String.sub s 1 (String.length s - 2) - else - s - in + let s = Utils_.remove_quotes s in boundary := Some (`boundary s) | _ -> ()) tl; diff --git a/src/multipart_form/tiny_httpd_multipart_form_data.mli b/src/multipart_form/tiny_httpd_multipart_form_data.mli index a1747e5d..dd7bbebc 100644 --- a/src/multipart_form/tiny_httpd_multipart_form_data.mli +++ b/src/multipart_form/tiny_httpd_multipart_form_data.mli @@ -1,4 +1,7 @@ -(** Parser for multipart/form-data *) +(** Streaming parser for multipart/form-data *) + +module Content_disposition = Content_disposition + type st (** Parser state *) diff --git a/src/multipart_form/utils_.ml b/src/multipart_form/utils_.ml index a1f993ce..cb3d8a5b 100644 --- a/src/multipart_form/utils_.ml +++ b/src/multipart_form/utils_.ml @@ -1,3 +1,5 @@ +let spf = Printf.sprintf + let string_eq ~a ~a_start ~b ~len : bool = assert (len <= String.length b); if String.length a >= a_start + len then ( @@ -16,3 +18,11 @@ let split1_on ~c s = match String.index s c with | exception Not_found -> None | i -> Some (String.sub s 0 i, String.sub s (i + 1) (String.length s - i - 1)) + +let remove_quotes s : string = + if String.length s < 2 then + s + else if s.[0] = '"' && s.[String.length s - 1] = '"' then + String.sub s 1 (String.length s - 2) + else + s