Skip to content

Commit

Permalink
Allow setting a custom protocol string.
Browse files Browse the repository at this point in the history
  • Loading branch information
voutilad committed Nov 30, 2023
1 parent 55e5ba8 commit c355d9b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions dws.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static const char HANDSHAKE_TEMPLATE[] =
"Upgrade: websocket\r\n"
"Connection: upgrade\r\n"
"Sec-WebSocket-Key: %s\r\n"
"Sec-WebSocket-Protocol: dumb-ws\r\n"
"Sec-WebSocket-Protocol: %s\r\n"
"Sec-WebSocket-Version: 13\r\n\r\n";

static int rng_initialized = 0;
Expand Down Expand Up @@ -366,7 +366,8 @@ dumb_frame(uint8_t *frame, uint8_t *data, size_t len)
* fatal error otherwise.
*/
int
dumb_handshake(struct websocket *ws, char *host, char *path)
dumb_handshake(struct websocket *ws, const char *host, const char *path,
const char *proto)
{
int len, ret = 0;
char key[25], buf[HANDSHAKE_BUF_SIZE];
Expand All @@ -375,7 +376,7 @@ dumb_handshake(struct websocket *ws, char *host, char *path)
dumb_key(key);

len = snprintf(buf, sizeof(buf), HANDSHAKE_TEMPLATE,
path, host, key);
path, host, key, proto);
if (len < 1)
return -1;

Expand Down Expand Up @@ -526,6 +527,7 @@ dumb_send(struct websocket *ws, void *payload, size_t len)
if (frame_len < 0)
crap(1, "%s: invalid frame payload length", __func__);

// TODO: handle under-writes.
if (ws->ctx)
n = tls_write(ws->ctx, frame, (size_t) frame_len);
else
Expand Down Expand Up @@ -566,6 +568,7 @@ dumb_recv(struct websocket *ws, void *out, size_t len)
if (frame == NULL)
return -1;

// TODO: handle under reads.
n = ws_read(ws, frame, len + FRAME_MAX_HEADER_SIZE + 1);
if (n < 1) {
free(frame);
Expand Down
2 changes: 1 addition & 1 deletion dws.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct websocket {

int dumb_connect(struct websocket *ws, char*, char*);
int dumb_connect_tls(struct websocket *ws, char*, char*, int);
int dumb_handshake(struct websocket *s, char*, char*);
int dumb_handshake(struct websocket *s, const char*, const char*, const char*);

ssize_t dumb_send(struct websocket *ws, void*, size_t);
ssize_t dumb_recv(struct websocket *ws, void*, size_t);
Expand Down

0 comments on commit c355d9b

Please sign in to comment.