Homotopy properties of horizontal loop spaces and applications to closed sub-Riemannian geodesics