#include #include #include #include #include #include #include #include #include static void send_all(int fd, const std::string& data) { for (size_t at=0;at frame(int fd) { auto h=receive_exact(fd,2); size_t n=unsigned(h[1])&127; if(n==126){auto x=receive_exact(fd,2);n=(unsigned((unsigned char)x[0])<<8)|(unsigned char)x[1];} else if(n==127) throw std::runtime_error("unexpected oversized frame"); return {unsigned(h[0])&15,receive_exact(fd,n)}; } static void exchange(unsigned opcode, const std::string& payload, const std::vector>& expected) { int fd=socket(AF_INET,SOCK_STREAM,0); sockaddr_in addr{};addr.sin_family=AF_INET;addr.sin_port=htons(80);inet_pton(AF_INET,"127.0.0.1",&addr.sin_addr);if(connect(fd,(sockaddr*)&addr,sizeof(addr)))throw std::runtime_error("WebSocket connect failed"); send_all(fd,"GET /tests/capy-websocket.capy HTTP/1.1\r\nHost: bearer.openfu.com\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: MTIzNDU2Nzg5MDEyMzQ1Ng==\r\nSec-WebSocket-Version: 13\r\n\r\n");std::string response;while(response.find("\r\n\r\n")==std::string::npos)response+=receive_exact(fd,1);if(!response.starts_with("HTTP/1.1 101 "))throw std::runtime_error("Capy WebSocket upgrade failed");std::string f;f.push_back(char(128|opcode));f.push_back(char(128|payload.size()));const std::array mask={0x12,0x34,0x56,0x78};f.append((const char*)mask.data(),4);for(size_t i=0;i