It shall be the duty of the court or judge forthwith to remand such party if it appears that the party is legally detained in custody, either:
(1) By virtue of process issued by any court, or judge or commissioner or any other officer thereof, of the United States, in a case where such court, or judge or officer thereof, has exclusive jurisdiction; or,
(2) By virtue of the judgment of any court, or of any execution issued upon such judgment; or,
(3) For any contempt, specially and plainly charged in the commitment, by some court, officer or body having authority to commit for the contempt so charged; and,
(4) That the time during which such party may legally be detained has not expired.