Skip to content

Commit 52a68fe

Browse files
author
Vincent Balat
committed
implement full compliance of content-type for uploaded files w.r.t. RFC
1 parent 4a3cb17 commit 52a68fe

File tree

5 files changed

+33
-39
lines changed

5 files changed

+33
-39
lines changed

src/http/ocsigen_headers.ml

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ let get_keepalive http_header =
156156
(* RFC 2616, sect. 14.23 *)
157157
(* XXX Not so simple: the host name may contain a colon! (RFC 3986) *)
158158
let get_host_from_host_header =
159-
let host_re =
160-
Netstring_pcre.regexp "^(\\[[0-9A-Fa-f:.]+\\]|[^:]+)(:([0-9]+))?$"
159+
let host_re =
160+
Netstring_pcre.regexp "^(\\[[0-9A-Fa-f:.]+\\]|[^:]+)(:([0-9]+))?$"
161161
in
162162
fun http_frame ->
163163
try
@@ -166,9 +166,9 @@ let get_host_from_host_header =
166166
http_frame.Ocsigen_http_frame.frame_header Http_headers.host
167167
in
168168
match Netstring_pcre.string_match host_re hostport 0 with
169-
| Some m ->
169+
| Some m ->
170170
(Some (Netstring_pcre.matched_group m 1 hostport),
171-
try Some (int_of_string
171+
try Some (int_of_string
172172
(Netstring_pcre.matched_group m 3 hostport))
173173
with Not_found -> None | Failure _ -> raise Ocsigen_Bad_Request)
174174
| None -> raise Ocsigen_Bad_Request
@@ -237,16 +237,18 @@ let parse_content_type = function
237237
match String.split ';' s with
238238
| [] -> None
239239
| a::l ->
240+
let (typ, subtype) =
240241
try
241-
let (typ, subtype) = String.sep '/' a in
242-
let params =
243-
try
244-
List.map (String.sep '=') l
245-
with Not_found -> []
246-
in
242+
let typ, subtype = String.sep '/' a in (typ, Some subtype)
243+
with Not_found -> a, None
244+
in
245+
let params =
246+
try
247+
List.map (String.sep '=') l
248+
with Not_found -> []
249+
in
247250
(*VVV If syntax error, we return no parameter at all *)
248-
Some ((typ, subtype), params)
249-
with Not_found -> None
251+
Some ((typ, subtype), params)
250252
(*VVV If syntax error in type, we return None *)
251253

252254

@@ -336,4 +338,3 @@ let get_accept_language http_frame =
336338
(Http_header.get_headers_values
337339
http_frame.Ocsigen_http_frame.frame_header Http_headers.accept_language)
338340
with _ -> []
339-

src/http/ocsigen_senders.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ module Text_content =
114114
res_headers= Http_headers.dyn_headers;
115115
res_stream =
116116
(Ocsigen_stream.make
117-
(fun () ->
117+
(fun () ->
118118
Ocsigen_stream.cont c (fun () -> Ocsigen_stream.empty None)),
119119
None)
120-
120+
121121
}
122122

123123
end
@@ -195,7 +195,7 @@ module Streamlist_content =
195195
{default_result with
196196
res_content_length = None;
197197
res_etag = get_etag c;
198-
res_stream =
198+
res_stream =
199199
(Ocsigen_stream.make ~finalize (fun _ -> next_stream c), None);
200200
res_headers= Http_headers.dyn_headers;
201201
res_content_type = Some ct}

src/server/ocsigen_extensions.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ type file_info = {
193193
filesize: int64;
194194
raw_original_filename: string;
195195
original_basename: string ;
196-
file_content_type: (string * string option) option;
196+
file_content_type:
197+
((string * string option) * (string * string) list) option;
197198
}
198199

199200
type request_info =

src/server/ocsigen_extensions.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ type file_info = {
140140
filesize: int64;
141141
raw_original_filename: string;
142142
original_basename: string ;
143-
file_content_type: (string * string option) option;
143+
file_content_type:
144+
((string * string option) * (string * string) list) option;
144145
}
145146

146147
(** The request *)

src/server/ocsigen_server.ml

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ open Ocsigen_parseconfig
3131
open Ocsigen_cookies
3232
open Lazy
3333

34-
3534
exception Ocsigen_unsupported_media
3635
exception Ssl_Exception
3736
exception Ocsigen_upload_forbidden
@@ -79,17 +78,17 @@ let make_sockets addr port =
7978
corresponds to the net.ipv6.bindv6only=0 behaviour on Linux,
8079
but is portable and should work with
8180
net.ipv6.bindv6only=1 as well. *)
82-
let ipv6_socket =
83-
try [make_ipv6_socket Unix.inet6_addr_any port]
84-
with Unix.Unix_error
85-
((Unix.EAFNOSUPPORT | Unix.EPROTONOSUPPORT),
86-
_, _) -> []
87-
in
88-
(make_ipv4_socket Unix.inet_addr_any port)::ipv6_socket
81+
let ipv6_socket =
82+
try [make_ipv6_socket Unix.inet6_addr_any port]
83+
with Unix.Unix_error
84+
((Unix.EAFNOSUPPORT | Unix.EPROTONOSUPPORT),
85+
_, _) -> []
86+
in
87+
(make_ipv4_socket Unix.inet_addr_any port)::ipv6_socket
8988
| IPv4 addr ->
90-
[make_ipv4_socket addr port]
89+
[make_ipv4_socket addr port]
9190
| IPv6 addr ->
92-
[make_ipv6_socket addr port]
91+
[make_ipv6_socket addr port]
9392

9493
let sslctx = Ocsigen_http_client.sslcontext
9594

@@ -112,7 +111,8 @@ let find_field field content_disp =
112111

113112
type to_write =
114113
No_File of string * Buffer.t
115-
| A_File of (string * string * string * Unix.file_descr * (string * string option) option)
114+
| A_File of (string * string * string * Unix.file_descr
115+
* ((string * string option) * (string * string) list) option)
116116

117117
let counter = let c = ref (Random.int 1000000) in fun () -> c := !c + 1 ; !c
118118

@@ -127,8 +127,6 @@ let dbg sockaddr s =
127127
"While talking to " ^ ip ^ ": " ^ s)
128128

129129

130-
let r_content_type = Netstring_pcre.regexp "([^ ]*)"
131-
132130
let http_url_syntax = Hashtbl.find Neturl.common_url_syntax "http"
133131

134132
let rec find_post_params http_frame ct filenames =
@@ -176,14 +174,7 @@ and find_post_params_multipart_form_data body_gen ctparams filenames ci =
176174
let content_type =
177175
try
178176
let ct = List.assoc "content-type" hs in
179-
let content_type =
180-
let (_, res) = Netstring_pcre.search_forward r_content_type ct 0 in
181-
Netstring_pcre.matched_group res 1 ct
182-
in
183-
let charset = try
184-
Some (find_field "charset" ct)
185-
with _ -> None
186-
in Some (content_type, charset)
177+
Ocsigen_headers.parse_content_type (Some ct)
187178
with _ -> None
188179
in
189180
let cd = List.assoc "content-disposition" hs in

0 commit comments

Comments
 (0)