From 12d4e1588ebe055b17ee37903f08c220117ffe32 Mon Sep 17 00:00:00 2001 From: Paul-Elliot Date: Sat, 6 May 2023 12:29:01 +0200 Subject: [PATCH] upload-pack: Handle empty request The client uses empty requests when: - It is already up to date - `git ls-remote` Signed-off-by: Paul-Elliot --- fuzz/smart.ml | 10 ++++++---- src/not-so-smart/protocol.ml | 7 +++++-- src/not-so-smart/protocol.mli | 5 ++++- src/not-so-smart/smart.ml | 2 +- src/not-so-smart/smart.mli | 2 +- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/fuzz/smart.ml b/fuzz/smart.ml index 379fe9cdb..a94b956ae 100644 --- a/fuzz/smart.ml +++ b/fuzz/smart.ml @@ -193,7 +193,9 @@ let () = let wants = List.map Digestif.SHA1.to_hex wants in let v = Smart.Want.v ~capabilities:[] wants in let str = to_string v in - let res = of_string str in - Crowbar.check_eq ~pp:Smart.Want.pp - ~eq:(Smart.Want.equal ~uid:String.equal ~reference:String.equal) - v res + match of_string str with + | Some res -> + Crowbar.check_eq ~pp:Smart.Want.pp + ~eq:(Smart.Want.equal ~uid:String.equal ~reference:String.equal) + v res + | None -> Crowbar.failf "Invalid empty fetch request (%S)" str diff --git a/src/not-so-smart/protocol.ml b/src/not-so-smart/protocol.ml index 95ddf5f2b..ba20f3672 100644 --- a/src/not-so-smart/protocol.ml +++ b/src/not-so-smart/protocol.ml @@ -623,7 +623,9 @@ module Decoder = struct let v = peek_pkt decoder in if Sub.is_empty v then ( junk_pkt decoder; - return (Want.v ~capabilities (first_want :: List.rev wants)) decoder + return + (Some (Want.v ~capabilities (first_want :: List.rev wants))) + decoder (* TODO else if start with shallow or depth request or filter request then *)) else match Sub.cut ~sep:v_space v with @@ -639,7 +641,8 @@ module Decoder = struct let decode_first_want decoder = let v = peek_pkt decoder in - if Sub.is_prefix v ~affix:v_want then ( + if Sub.is_empty v then return None decoder + else if Sub.is_prefix v ~affix:v_want then ( let v = v |> Sub.with_range ~first:(Sub.length v_want) in (* NOTE(dinosaure): we accept more than Git. The BNF syntax of [first-want] is: diff --git a/src/not-so-smart/protocol.mli b/src/not-so-smart/protocol.mli index 0ab991b34..35cefa818 100644 --- a/src/not-so-smart/protocol.mli +++ b/src/not-so-smart/protocol.mli @@ -205,7 +205,10 @@ module Decoder : sig ?sideband:bool -> decoder -> (string Status.t, [> error ]) state val decode_packet : trim:bool -> decoder -> (string, [> error ]) state - val decode_want : decoder -> ((string, string) Want.t, [> error ]) state + + val decode_want : + decoder -> ((string, string) Want.t option, [> error ]) state + val decode_have : decoder -> (string Have.t, [> error ]) state val decode_commands : diff --git a/src/not-so-smart/smart.ml b/src/not-so-smart/smart.ml index b6c6dbcf8..9183adb7b 100644 --- a/src/not-so-smart/smart.ml +++ b/src/not-so-smart/smart.ml @@ -45,7 +45,7 @@ module Witness = struct | Ack : string Negotiation.t recv | Flush : unit recv | Shallows : string Shallow.t list recv - | Want : (string, string) Want.t recv + | Want : (string, string) Want.t option recv | Have : string Have.t recv end diff --git a/src/not-so-smart/smart.mli b/src/not-so-smart/smart.mli index a60e2d4c5..fc92e865a 100644 --- a/src/not-so-smart/smart.mli +++ b/src/not-so-smart/smart.mli @@ -250,7 +250,7 @@ val shallows : string Shallow.t list recv val status : bool -> string Status.t recv val packet : trim:bool -> string recv val send_advertised_refs : (string, string) Advertised_refs.t send -val recv_want : (string, string) Want.t recv +val recv_want : (string, string) Want.t option recv val recv_have : string Have.t recv val bind : ('a, 'err) t -> f:('a -> ('b, 'err) t) -> ('b, 'err) t val ( let* ) : ('a, 'err) t -> ('a -> ('b, 'err) t) -> ('b, 'err) t