Skip to content

Commit

Permalink
Merge pull request #1215 from grapenut/more-websock-oob
Browse files Browse the repository at this point in the history
Update oob() to also send the GMCP headers to websockets
  • Loading branch information
shawnw committed Aug 9, 2018
2 parents c684770 + aab3a17 commit d958abe
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
2 changes: 1 addition & 1 deletion hdrs/websock.h
Expand Up @@ -29,7 +29,7 @@ void to_websocket_frame(const char **bp, int *np, char channel);

int markup_websocket(char *buff, char **bp, char *data, int datalen, char *alt,
int altlen, char channel);
void send_websocket_object(DESC *d, cJSON *data);
void send_websocket_object(DESC *d, const char *header, cJSON *data);

FUNCTION_PROTO(fun_websocket_json);
FUNCTION_PROTO(fun_websocket_html);
Expand Down
2 changes: 1 addition & 1 deletion src/bsd.c
Expand Up @@ -3015,7 +3015,7 @@ FUNCTION(fun_oob)
if (d->player != who)
continue;
if (d->conn_flags & CONN_WEBSOCKETS) {
send_websocket_object(d, json);
send_websocket_object(d, args[1], json);
i++;
}
if (d->conn_flags & CONN_GMCP) {
Expand Down
47 changes: 41 additions & 6 deletions src/websock.c
Expand Up @@ -581,24 +581,59 @@ markup_websocket(char *buff, char **bp, char *data, int datalen, char *alt,
}

void
send_websocket_object(DESC *d, cJSON *data)
send_websocket_object(DESC *d, const char *header, cJSON *data)
{
char buff[BUFFER_LEN];
char *bp = buff;
int error = 0;
int must_free_ptr = 0;
cJSON *hdr = NULL;
cJSON *ptr;

if (!d || !(d->conn_flags & CONN_WEBSOCKETS) || !data) {
return;
}

if (cJSON_IsObject(data)) {
char *str = cJSON_PrintUnformatted(data);
error = markup_websocket(buff, &bp, str, strlen(str), NULL, 0, WEBSOCKET_CHANNEL_JSON);
*bp = '\0';
if (str) {
free(str);
ptr = data;
} else {
ptr = cJSON_CreateObject();

if (!ptr) {
return;
}
must_free_ptr = 1;

/* if json is valid, but not an object, we need to add it to the tmp object */
if (!cJSON_IsInvalid(data) && !cJSON_IsNull(data)) {

/* default to using header as the label, or "data" otherwise */
if (header && *header) {
cJSON_AddItemReferenceToObject(ptr, header, data);
} else {
cJSON_AddItemReferenceToObject(ptr, "data", data);
}
}
}

/* if header is present, add it using the "gmcp" label */
if (header && *header) {
hdr = cJSON_CreateString(header);
cJSON_AddItemToObject(ptr, "gmcp", hdr);
}

char *str = cJSON_PrintUnformatted(ptr);

/* check to see if we need to delete the tmp object */
if (must_free_ptr) {
cJSON_Delete(ptr);
}

error = markup_websocket(buff, &bp, str, strlen(str), NULL, 0, WEBSOCKET_CHANNEL_JSON);
*bp = '\0';
if (str) {
free(str);
}

if (!error) {
queue_newwrite(d, buff, strlen(buff));
Expand Down

0 comments on commit d958abe

Please sign in to comment.