htp: make htp state handling function more robust

Also, fix wrong cast that worked only luck.