comparison seobeo/s_websocket.c @ 172:0face9898d04

[PostDog] Small changes.
author MrJuneJune <me@mrjunejune.com>
date Mon, 19 Jan 2026 18:56:54 -0800
parents 058de208e640
children
comparison
equal deleted inserted replaced
169:295ac2e5ec00 172:0face9898d04
132 "Connection: Upgrade\r\n" 132 "Connection: Upgrade\r\n"
133 "Sec-WebSocket-Key: %s\r\n" 133 "Sec-WebSocket-Key: %s\r\n"
134 "Sec-WebSocket-Version: 13\r\n", 134 "Sec-WebSocket-Version: 13\r\n",
135 p_ws->path, p_ws->host, ws_key); 135 p_ws->path, p_ws->host, ws_key);
136 136
137 // Add custom headers if provided
138 if (headers && strlen(headers) > 0) 137 if (headers && strlen(headers) > 0)
139 { 138 {
140 // Parse newline-separated headers and convert to HTTP format
141 const char *line_start = headers; 139 const char *line_start = headers;
142 while (*line_start) 140 while (*line_start)
143 { 141 {
144 // Skip leading whitespace
145 while (*line_start == ' ' || *line_start == '\t') line_start++; 142 while (*line_start == ' ' || *line_start == '\t') line_start++;
146 if (*line_start == '\0') break; 143 if (*line_start == '\0') break;
147 144
148 // Find end of line
149 const char *line_end = line_start; 145 const char *line_end = line_start;
150 while (*line_end && *line_end != '\n') line_end++; 146 while (*line_end && *line_end != '\n') line_end++;
151 147
152 // Copy line if it contains a colon (valid header)
153 size_t line_len = line_end - line_start; 148 size_t line_len = line_end - line_start;
154 if (line_len > 0 && memchr(line_start, ':', line_len) != NULL) 149 if (line_len > 0 && memchr(line_start, ':', line_len) != NULL)
155 { 150 {
156 // Remove trailing whitespace/CR
157 while (line_len > 0 && (line_start[line_len-1] == '\r' || 151 while (line_len > 0 && (line_start[line_len-1] == '\r' ||
158 line_start[line_len-1] == ' ' || line_start[line_len-1] == '\t')) 152 line_start[line_len-1] == ' ' || line_start[line_len-1] == '\t'))
159 line_len--; 153 line_len--;
160 154
161 if (line_len > 0 && handshake_len + line_len + 4 < sizeof(handshake)) 155 if (line_len > 0 && handshake_len + line_len + 4 < sizeof(handshake))
165 handshake[handshake_len++] = '\r'; 159 handshake[handshake_len++] = '\r';
166 handshake[handshake_len++] = '\n'; 160 handshake[handshake_len++] = '\n';
167 } 161 }
168 } 162 }
169 163
170 // Move to next line
171 line_start = (*line_end == '\n') ? line_end + 1 : line_end; 164 line_start = (*line_end == '\n') ? line_end + 1 : line_end;
172 } 165 }
173 } 166 }
174 167
175 // End headers
176 handshake[handshake_len++] = '\r'; 168 handshake[handshake_len++] = '\r';
177 handshake[handshake_len++] = '\n'; 169 handshake[handshake_len++] = '\n';
178 handshake[handshake_len] = '\0'; 170 handshake[handshake_len] = '\0';
179 171
180 Seobeo_Handle_Queue(p_ws->p_handle, (uint8*)handshake, (uint32)handshake_len); 172 Seobeo_Handle_Queue(p_ws->p_handle, (uint8*)handshake, (uint32)handshake_len);
236 Seobeo_WebSocket *Seobeo_WebSocket_Connect(const char *url) 228 Seobeo_WebSocket *Seobeo_WebSocket_Connect(const char *url)
237 { 229 {
238 return Seobeo_WebSocket_Connect_With_Headers(url, NULL); 230 return Seobeo_WebSocket_Connect_With_Headers(url, NULL);
239 } 231 }
240 232
241 // Seobeo_WebSocket_Mask_Data moved to s_websocket_common.c
242
243 static int32 Seobeo_WebSocket_Send_Frame(Seobeo_WebSocket *p_ws, Seobeo_WebSocket_Opcode opcode, 233 static int32 Seobeo_WebSocket_Send_Frame(Seobeo_WebSocket *p_ws, Seobeo_WebSocket_Opcode opcode,
244 const uint8 *payload, size_t payload_length, boolean fin) 234 const uint8 *payload, size_t payload_length, boolean fin)
245 { 235 {
246 if (!p_ws || p_ws->state != SEOBEO_WS_STATE_OPEN) 236 if (!p_ws || p_ws->state != SEOBEO_WS_STATE_OPEN)
247 return -1; 237 return -1;
526 p_msg->is_final = fin; 516 p_msg->is_final = fin;
527 517
528 return p_msg; 518 return p_msg;
529 } 519 }
530 520
531 // Seobeo_WebSocket_Message_Destroy moved to s_websocket_common.c
532
533 int32 Seobeo_WebSocket_Close(Seobeo_WebSocket *p_ws, uint16 code, const char *reason) 521 int32 Seobeo_WebSocket_Close(Seobeo_WebSocket *p_ws, uint16 code, const char *reason)
534 { 522 {
535 if (!p_ws || p_ws->state == SEOBEO_WS_STATE_CLOSED) 523 if (!p_ws || p_ws->state == SEOBEO_WS_STATE_CLOSED)
536 return -1; 524 return -1;
537 525