successfully transplant to Cowboy 0.8.x, need refactor later :)