refactoring setcookie: less memory allocation
fix errors from valgrind memcheck