this function is modified from ```instaweb_handler.cc:404-436```