The near-infrared structure and spectra of the bipolar nebulae M2-9 and AFGL 2688: The role of ultraviolet pumping and shocks in molecular hydrogen excitation