The writ of habeas corpus shall extend to all cases of illegal confinement or detention by which any person is deprived of his liberty, or by which the rightful custody of any person is withheld from the person entitled thereto, except in the cases expressly excepted.