Prepare #717. Cookie injection and head injection share the same logic.