]> xenbits.xensource.com Git - xenclient/toolstack.git/commitdiff
Fixed a couple of bugs in http substring parsing.
authorPrashanth Mundkur <prashanth.mundkur@citrix.com>
Tue, 9 Jun 2009 19:51:53 +0000 (12:51 -0700)
committerPrashanth Mundkur <prashanth.mundkur@citrix.com>
Tue, 23 Jun 2009 16:32:19 +0000 (09:32 -0700)
libs/http/http.ml

index e4875665f47400bd36e5cf9054ea7f604759132a..3f701226dc36291af8f662dfe462bc644284a811 100644 (file)
@@ -453,7 +453,7 @@ module Request_header = struct
                | _ -> None
 
        let parse_substring state str ofs len =
-               let i = ref 0 in
+               let i = ref ofs in
                let iend = ofs + len in
                while get_parse_result state = None && !i < iend do
                        parse_char state str.[!i];
@@ -661,7 +661,7 @@ module Response_header = struct
                | _    -> None
 
        let parse_substring state str ofs len =
-               let i = ref 0 in
+               let i = ref ofs in
                let iend = ofs + len in
                while get_parse_result state = None && !i < iend do
                        parse_char state str.[!i];
@@ -1071,7 +1071,7 @@ module Request = struct
                | Parse_incomplete of state
                | Error of string
 
-       let rec parse_substring state str ofs len =
+       let rec parse_helper state str ofs len pre_consumed =
                match state.cursor with
                | In_request_header rs ->
                        (match Request_header.parse_substring rs str ofs len with
@@ -1085,31 +1085,34 @@ module Request = struct
                                (match Payload.init_from_request ?payload_callback:state.payload_callback v with
                                 | Payload.No_payload ->
                                        state.cursor <- Done;
-                                       Result ({ request = v; payload = None }, consumed)
+                                       Result ({ request = v; payload = None }, pre_consumed + consumed)
                                 | Payload.Error s ->
                                        state.cursor <- Done;
                                        Error s
                                 | Payload.Payload ps ->
                                        state.cursor <- In_payload ps;
                                        (* recurse on remaining input *)
-                                       parse_substring state str (ofs + consumed) (len - consumed)
+                                       parse_helper state str (ofs + consumed) (len - consumed) (pre_consumed + consumed)
                                )
                         | Request_header.Parse_incomplete rs ->
                                state.cursor <- In_request_header rs;
                                Parse_incomplete state
                        )
                | In_payload ps ->
-                       (match Payload.parse ps str with
+                       (match Payload.parse_substring ps str ofs len with
                         | Payload.Result (p, consumed) ->
                                state.num_bytes_parsed <- Int64.add state.num_bytes_parsed (Payload.num_bytes_parsed ps);
                                state.cursor <- Done;
-                               Result ({ request = optval state.s_request; payload = Some p }, consumed)
+                               Result ({ request = optval state.s_request; payload = Some p }, pre_consumed + consumed)
                         | Payload.Parse_incomplete ps ->
                                state.cursor <- In_payload ps;
                                Parse_incomplete state
                        )
                | Done -> raise_error (Internal_error "parse called on finished request!")
 
+       let rec parse_substring state str ofs len =
+               parse_helper state str ofs len 0
+
        let parse state str =
                parse_substring state str 0 (String.length str)
 
@@ -1181,10 +1184,10 @@ module Response = struct
                | Parse_incomplete of state
                | Error of string
 
-       let rec parse_substring state str ofs len =
+       let rec parse_helper state str ofs len pre_consumed =
                match state.cursor with
                | In_response_header rs ->
-                       (match Response_header.parse rs str with
+                       (match Response_header.parse_substring rs str ofs len with
                         | Response_header.Result (v, consumed) ->
                                let v = (match state.header_callback with
                                         | None -> v
@@ -1195,31 +1198,34 @@ module Response = struct
                                (match Payload.init_from_response ?payload_callback:state.payload_callback v with
                                 | Payload.No_payload ->
                                        state.cursor <- Done;
-                                       Result ({ response = v; payload = None }, consumed)
+                                       Result ({ response = v; payload = None }, pre_consumed + consumed)
                                 | Payload.Error s ->
                                        state.cursor <- Done;
                                        Error s
                                 | Payload.Payload ps ->
                                        state.cursor <- In_payload ps;
                                        (* recurse on remaining input *)
-                                       parse_substring state str (ofs + consumed) (len - consumed)
+                                       parse_helper state str (ofs + consumed) (len - consumed) (pre_consumed + consumed)
                                )
                         | Response_header.Parse_incomplete rs ->
                                state.cursor <- In_response_header rs;
                                Parse_incomplete state
                        )
                | In_payload ps ->
-                       (match Payload.parse ps str with
+                       (match Payload.parse_substring ps str ofs len with
                         | Payload.Result (p, consumed) ->
                                state.num_bytes_parsed <- Int64.add state.num_bytes_parsed (Payload.num_bytes_parsed ps);
                                state.cursor <- Done;
-                               Result ({ response = optval state.s_response; payload = Some p }, consumed)
+                               Result ({ response = optval state.s_response; payload = Some p }, pre_consumed + consumed)
                         | Payload.Parse_incomplete ps ->
                                state.cursor <- In_payload ps;
                                Parse_incomplete state
                        )
                | Done -> raise_error (Internal_error "parse called on finished response!")
 
+       let rec parse_substring state str ofs len =
+               parse_helper state str ofs len 0
+
        let parse state str =
                parse_substring state str 0 (String.length str)