Groundwork for response parsing.

This commit is contained in:
Omar Rizwan 2018-11-11 11:44:36 -08:00
parent 4ca75894c1
commit 5c2b15068e
2 changed files with 41 additions and 10 deletions

View file

@ -3,9 +3,13 @@ const ws = new WebSocket("ws://localhost:8888");
ws.onmessage = function(event) { ws.onmessage = function(event) {
const req = JSON.parse(event.data); const req = JSON.parse(event.data);
const response = { let response;
names: [".", "..", "hi.txt"] if (req.op === "readdir") {
}; response = {
op: "readdir",
names: [".", "..", "hi.txt"]
};
}
ws.send(JSON.stringify(response)); ws.send(JSON.stringify(response));
}; };

View file

@ -25,8 +25,8 @@ struct wby_server server;
struct wby_con *con; struct wby_con *con;
typedef struct response_readdir_t { typedef struct response_readdir_t {
char **names; char **entries;
size_t num_names; size_t num_entries;
} response_readdir_t; } response_readdir_t;
response_readdir_t *response; response_readdir_t *response;
@ -74,30 +74,33 @@ hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
// send [READDIR, path] to the websocket handler // send [READDIR, path] to the websocket handler
{ {
char *msg; char *data;
{ {
cJSON *req = cJSON_CreateObject(); cJSON *req = cJSON_CreateObject();
cJSON_AddStringToObject(req, "op", "readdir"); cJSON_AddStringToObject(req, "op", "readdir");
cJSON_AddStringToObject(req, "path", path); cJSON_AddStringToObject(req, "path", path);
msg = cJSON_Print(req); data = cJSON_Print(req);
printf("%s\n", msg); printf("%s\n", data);
cJSON_Delete(req); cJSON_Delete(req);
} }
wby_frame_begin(con, WBY_WSOP_TEXT_FRAME); wby_frame_begin(con, WBY_WSOP_TEXT_FRAME);
wby_write(con, msg, strlen(msg)); wby_write(con, data, strlen(data));
wby_frame_end(con); wby_frame_end(con);
free(msg); free(data);
} }
if (response) free(response);
response = NULL; response = NULL;
do { do {
wby_update(&server); wby_update(&server);
} while (response == NULL); } while (response == NULL);
printf("response: %d files\n", response->num_entries);
filler(buf, ".", NULL, 0); /* Current directory (.) */ filler(buf, ".", NULL, 0); /* Current directory (.) */
filler(buf, "..", NULL, 0); /* Parent directory (..) */ filler(buf, "..", NULL, 0); /* Parent directory (..) */
filler(buf, file_path + 1, NULL, 0); /* The only file we have. */ filler(buf, file_path + 1, NULL, 0); /* The only file we have. */
@ -159,12 +162,19 @@ websocket_connected(struct wby_con *connection, void *userdata)
static int static int
websocket_frame(struct wby_con *connection, const struct wby_frame *frame, void *userdata) websocket_frame(struct wby_con *connection, const struct wby_frame *frame, void *userdata)
{ {
unsigned char data[1024] = {0};
int i = 0; int i = 0;
printf("WebSocket frame incoming\n"); printf("WebSocket frame incoming\n");
printf(" Frame OpCode: %d\n", frame->opcode); printf(" Frame OpCode: %d\n", frame->opcode);
printf(" Final frame?: %s\n", (frame->flags & WBY_WSF_FIN) ? "yes" : "no"); printf(" Final frame?: %s\n", (frame->flags & WBY_WSF_FIN) ? "yes" : "no");
printf(" Masked? : %s\n", (frame->flags & WBY_WSF_MASKED) ? "yes" : "no"); printf(" Masked? : %s\n", (frame->flags & WBY_WSF_MASKED) ? "yes" : "no");
printf(" Data Length : %d\n", (int) frame->payload_length); printf(" Data Length : %d\n", (int) frame->payload_length);
if ((unsigned long) frame->payload_length > sizeof(data)) {
printf("Data too long!\n");
}
while (i < frame->payload_length) { while (i < frame->payload_length) {
unsigned char buffer[16]; unsigned char buffer[16];
int remain = frame->payload_length - i; int remain = frame->payload_length - i;
@ -182,8 +192,25 @@ websocket_frame(struct wby_con *connection, const struct wby_frame *frame, void
for (k = 0; k < read_size; ++k) for (k = 0; k < read_size; ++k)
printf("%c", isprint(buffer[k]) ? buffer[k] : '?'); printf("%c", isprint(buffer[k]) ? buffer[k] : '?');
printf("\n"); printf("\n");
for (k = 0; k < read_size; ++k)
data[i + k] = buffer[k];
i += (int)read_size; i += (int)read_size;
} }
if ((int) strlen((const char *) data) != frame->payload_length) {
printf("Null in data! [%s]\n", data);
}
cJSON *ret = cJSON_Parse((const char *) data);
cJSON *op = cJSON_GetObjectItemCaseSensitive(ret, "op");
if (strcmp(op->valuestring, "readdir") == 0) {
response = malloc(sizeof(response));
response->entries = malloc(sizeof(char *) * 10);
response->entries[0] = "a";
response->entries[1] = "b";
response->num_entries = 2;
}
return 0; return 0;
} }