int send_response( t_pb8 &headers, int code, const void *p, long sz ) { #if defined( _WIN32 ) || defined( WIN32 ) // Ensure stdout is in binary mode _setmode( fileno( stdout ), O_BINARY ); #endif // NULL terminated? if ( p && 0 >= sz ) while ( ((char*)p)[ sz ] ) sz++; // Set content size if ( 0 <= sz ) headers[ "content-length" ] = sz; // Send response headers printf( "HTTP/1.1 %d %s\n%s\n\n", code, response_msg( code ), parser::EncodeHttpHeaders( headers ).c_str() ); // Write the data if ( p && 0 < sz ) fwrite( p, 1, sz, stdout ); return 0; }
static void get_name(int fd) { if (0 != response_msg(fd, 0, strlen(g_name)+ 1, g_name)) { DEBUG_ERROR("Response name failed"); } }
/** * Main command operations */ void CommandsSession::cmd_process() { binn *read_binn; read_binn = binn_open((void*)&m_read_msg[0]); unsigned char command; if (!binn_list_get_uint8(read_binn, 1, &command)) { response_msg(STATUS_ERROR, "Invalid Binn data: reading command failed", true); return; } switch (command) { case COMMAND_EXEC: { char * exec_command; char * work_dir; if (!binn_list_get_str(read_binn, 2, &exec_command)) { response_msg(STATUS_ERROR, "Invalid exec command", true); break; } if (!binn_list_get_str(read_binn, 3, &work_dir)) { response_msg(STATUS_ERROR, "Invalid work directory", true); break; } if (strlen(work_dir) == 0) { response_msg(STATUS_ERROR, "Empty work directory", true); break; } if (!fs::is_directory(work_dir)) { response_msg(STATUS_ERROR, "Invalid work directory", true); break; } fs::current_path(work_dir); std::cout << "Executing command: \"" << exec_command << "\"" << std::endl; std::string out; int exit_code = GameAP::exec(std::string(exec_command), out); binn *write_binn = binn_list(); binn_list_add_uint32(write_binn, STATUS_OK); binn_list_add_int32(write_binn, exit_code); binn_list_add_str(write_binn, &out[0]); m_write_msg = std::string(static_cast<char*>(binn_ptr(write_binn)), static_cast<uint>(binn_size(write_binn))); break; }; default: { std::cerr << "Unknown Command" << std::endl; response_msg(STATUS_UNKNOWN_COMMAND, "Unknown command", true); return; }; } do_write(); }
int send_error( int code ) { t_pb8 headers; send_response( headers, code, response_msg( code ), 0 ); return code; }