summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2016-04-08 00:11:56 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2016-04-08 00:11:56 +0200
commit54b2652e94cfa7840dbebcba46edd5459e7c6e86 (patch)
tree7ffee1167df39547c19939f496a249f47970f579
parentce7b0d0a381016057cedb99f1e33293c9f65b562 (diff)
refactored websockets
Change-Id: Ic41efc1cd0b89d183c5ed36182da989548be1c8d Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r--src/CMakeLists.txt2
-rw-r--r--src/afb-websock.c18
-rw-r--r--src/afb-ws-json.c329
-rw-r--r--src/afb-ws-json.h22
-rw-r--r--src/afb-ws.c387
-rw-r--r--src/afb-ws.h11
-rw-r--r--src/websock.c2
-rw-r--r--src/websock.h6
8 files changed, 514 insertions, 263 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7e25276c..e8205270 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -9,6 +9,8 @@ ADD_LIBRARY(src OBJECT
afb-method.c
afb-hreq.c
afb-websock.c
+ afb-ws.c
+ afb-ws-json.c
websock.c
verbose.c
utils-upoll.c
diff --git a/src/afb-websock.c b/src/afb-websock.c
index 885b986a..d92c7b00 100644
--- a/src/afb-websock.c
+++ b/src/afb-websock.c
@@ -28,6 +28,8 @@
#include "websock.h"
+#include "afb-ws-json.h"
+
#include "afb-req-itf.h"
#include "afb-method.h"
#include "afb-hreq.h"
@@ -185,12 +187,12 @@ struct afb_wsreq
struct afb_websock
{
int fd;
+ struct afb_wsreq *requests;
struct MHD_Connection *connection;
+ struct AFB_clientCtx *context;
struct websock *ws;
struct upoll *up;
- struct AFB_clientCtx *context;
struct json_tokener *tokener;
- struct afb_wsreq *requests;
};
static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name);
@@ -213,6 +215,15 @@ static const struct afb_req_itf wsreq_itf = {
struct afb_websock *afb_websock_create(struct afb_hreq *hreq)
{
+ return (void*)afb_ws_json_create(
+ dup(MHD_get_connection_info(hreq->connection,MHD_CONNECTION_INFO_CONNECTION_FD)->connect_fd),
+ afb_hreq_context(hreq),
+ (void*)MHD_resume_connection,
+ hreq->connection);
+}
+
+struct afb_websock *_afb_websock_create(struct afb_hreq *hreq)
+{
int fd;
struct afb_websock *result;
@@ -226,8 +237,9 @@ struct afb_websock *afb_websock_create(struct afb_hreq *hreq)
if (result == NULL)
goto error;
- result->connection = hreq->connection;
result->fd = fd;
+ result->requests = NULL;
+ result->connection = hreq->connection;
result->context = ctxClientGet(afb_hreq_context(hreq));
if (result->context == NULL)
goto error2;
diff --git a/src/afb-ws-json.c b/src/afb-ws-json.c
new file mode 100644
index 00000000..43c70a4f
--- /dev/null
+++ b/src/afb-ws-json.c
@@ -0,0 +1,329 @@
+/*
+ * Copyright 2016 IoT.bzh
+ * Author: José Bollo <jose.bollo@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define _GNU_SOURCE
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <assert.h>
+#include <errno.h>
+#include <string.h>
+
+#include <json.h>
+
+#include "afb-ws.h"
+#include "afb-ws-json.h"
+#include "session.h"
+#include "afb-req-itf.h"
+#include "afb-apis.h"
+
+static void aws_on_close(struct afb_ws_json *ws, uint16_t code, char *text, size_t size);
+static void aws_on_text(struct afb_ws_json *ws, char *text, size_t size);
+
+static struct afb_ws_itf aws_itf = {
+ .on_close = (void*)aws_on_close,
+ .on_text = (void*)aws_on_text,
+ .on_binary = NULL,
+};
+
+struct afb_wsreq;
+
+struct afb_ws_json
+{
+ void (*cleanup)(void*);
+ void *cleanup_closure;
+ struct afb_wsreq *requests;
+ struct AFB_clientCtx *context;
+ struct json_tokener *tokener;
+ struct afb_ws *ws;
+};
+
+struct afb_ws_json *afb_ws_json_create(int fd, struct AFB_clientCtx *context, void (*cleanup)(void*), void *closure)
+{
+ struct afb_ws_json *result;
+
+ assert(fd >= 0);
+ assert(context != NULL);
+
+ result = malloc(sizeof * result);
+ if (result == NULL)
+ goto error;
+
+ result->cleanup = cleanup;
+ result->cleanup_closure = closure;
+ result->requests = NULL;
+ result->context = ctxClientGet(context);
+ if (result->context == NULL)
+ goto error2;
+
+ result->tokener = json_tokener_new();
+ if (result->tokener == NULL)
+ goto error3;
+
+ result->ws = afb_ws_create(fd, &aws_itf, result);
+ if (result->ws == NULL)
+ goto error4;
+
+ return result;
+
+error4:
+ json_tokener_free(result->tokener);
+error3:
+ ctxClientPut(result->context);
+error2:
+ free(result);
+error:
+ close(fd);
+ return NULL;
+}
+
+static void aws_on_close(struct afb_ws_json *ws, uint16_t code, char *text, size_t size)
+{
+ /* do nothing */
+ free(text);
+}
+
+
+struct afb_wsreq
+{
+ struct afb_ws_json *aws;
+ struct afb_wsreq *next;
+ struct json_object *id;
+ struct json_object *name;
+ struct json_object *token;
+ struct json_object *request;
+};
+static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name);
+static void wsreq_iterate(struct afb_wsreq *wsreq, int (*iterator)(void *closure, struct afb_arg arg), void *closure);
+static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info);
+static void wsreq_success(struct afb_wsreq *wsreq, struct json_object *obj, const char *info);
+static int wsreq_session_create(struct afb_wsreq *wsreq);
+static int wsreq_session_check(struct afb_wsreq *wsreq, int refresh);
+static void wsreq_session_close(struct afb_wsreq *wsreq);
+
+static const struct afb_req_itf wsreq_itf = {
+ .get = (void*)wsreq_get,
+ .iterate = (void*)wsreq_iterate,
+ .fail = (void*)wsreq_fail,
+ .success = (void*)wsreq_success,
+ .session_create = (void*)wsreq_session_create,
+ .session_check = (void*)wsreq_session_check,
+ .session_close = (void*)wsreq_session_close
+};
+
+static int aws_handle_json(struct afb_ws_json *aws, struct json_object *obj)
+{
+ struct afb_req r;
+ int count, num;
+ struct json_object *type, *id, *name, *req, *token;
+ struct afb_wsreq *wsreq;
+ const char *api, *verb;
+ size_t lenapi, lenverb;
+
+ /* protocol inspired by http://www.gir.fr/ocppjs/ocpp_srpc_spec.shtml */
+
+ /* the object must be an array of 4 or 5 elements */
+ if (!json_object_is_type(obj, json_type_array))
+ goto error;
+ count = json_object_array_length(obj);
+ if (count < 4 || count > 5)
+ goto error;
+
+ /* get the 5 elements: type id name request token */
+ type = json_object_array_get_idx(obj, 0);
+ id = json_object_array_get_idx(obj, 1);
+ name = json_object_array_get_idx(obj, 2);
+ req = json_object_array_get_idx(obj, 3);
+ token = json_object_array_get_idx(obj, 4);
+
+ /* check the types: int string string object string */
+ if (!json_object_is_type(type, json_type_int))
+ goto error;
+ if (!json_object_is_type(id, json_type_string))
+ goto error;
+ if (!json_object_is_type(name, json_type_string))
+ goto error;
+ if (!json_object_is_type(req, json_type_object))
+ goto error;
+ if (token != NULL && !json_object_is_type(token, json_type_string))
+ goto error;
+
+ /* the type is only 2 */
+ num = json_object_get_int(type);
+ if (num != 2)
+ goto error;
+
+ /* checks the api/verb structure of name */
+ api = json_object_get_string(name);
+ for (lenapi = 0 ; api[lenapi] && api[lenapi] != '/' ; lenapi++);
+ if (!lenapi || !api[lenapi])
+ goto error;
+ verb = &api[lenapi+1];
+ for (lenverb = 0 ; verb[lenverb] && verb[lenverb] != '/' ; lenverb++);
+ if (!lenverb || verb[lenverb])
+ goto error;
+
+ /* allocates the request data */
+ wsreq = malloc(sizeof *wsreq);
+ if (wsreq == NULL)
+ goto error;
+
+ /* fill and record the request */
+ wsreq->aws = aws;
+ wsreq->id = json_object_get(id);
+ wsreq->name = json_object_get(name);
+ wsreq->token = json_object_get(token);
+ wsreq->request = json_object_get(req);
+ wsreq->next = aws->requests;
+ aws->requests = wsreq;
+ json_object_put(obj);
+
+ r.data = wsreq;
+ r.itf = &wsreq_itf;
+ afb_apis_call(r, aws->context, api, lenapi, verb, lenverb);
+ return 1;
+
+error:
+ json_object_put(obj);
+ return 0;
+}
+
+static void aws_on_text(struct afb_ws_json *ws, char *text, size_t size)
+{
+ struct json_object *obj;
+ json_tokener_reset(ws->tokener);
+ obj = json_tokener_parse_ex(ws->tokener, text, (int)size);
+ if (obj == NULL) {
+ afb_ws_close(ws->ws, 1008);
+ } else if (!aws_handle_json(ws, obj)) {
+ afb_ws_close(ws->ws, 1008);
+ }
+}
+
+static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name)
+{
+ struct afb_arg arg;
+ struct json_object *value;
+
+ if (json_object_object_get_ex(wsreq->request, name, &value)) {
+ arg.name = name;
+ arg.value = json_object_get_string(value);
+ arg.size = strlen(arg.value);
+ } else {
+ arg.name = NULL;
+ arg.value = NULL;
+ arg.size = 0;
+ }
+ arg.path = NULL;
+ return arg;
+}
+
+static void wsreq_iterate(struct afb_wsreq *wsreq, int (*iterator)(void *closure, struct afb_arg arg), void *closure)
+{
+ struct afb_arg arg;
+ struct json_object_iterator it = json_object_iter_begin(wsreq->request);
+ struct json_object_iterator end = json_object_iter_end(wsreq->request);
+
+ arg.size = 0;
+ arg.path = NULL;
+ while(!json_object_iter_equal(&it, &end)) {
+ arg.name = json_object_iter_peek_name(&it);
+ arg.value = json_object_get_string(json_object_iter_peek_value(&it));
+ if (!iterator(closure, arg))
+ break;
+ json_object_iter_next(&it);
+ }
+}
+
+static int wsreq_session_create(struct afb_wsreq *wsreq)
+{
+ struct AFB_clientCtx *context = wsreq->aws->context;
+ if (context->created)
+ return 0;
+ return wsreq_session_check(wsreq, 1);
+}
+
+static int wsreq_session_check(struct afb_wsreq *wsreq, int refresh)
+{
+ const char *token;
+ struct AFB_clientCtx *context = wsreq->aws->context;
+
+ if (wsreq->token == NULL)
+ return 0;
+
+ token = json_object_get_string(wsreq->token);
+ if (token == NULL)
+ return 0;
+
+ if (!ctxTokenCheck (context, token))
+ return 0;
+
+ if (refresh) {
+ ctxTokenNew (context);
+ }
+
+ return 1;
+}
+
+static void wsreq_session_close(struct afb_wsreq *wsreq)
+{
+ struct AFB_clientCtx *context = wsreq->aws->context;
+ ctxClientClose(context);
+}
+
+
+static void wsreq_reply(struct afb_wsreq *wsreq, int retcode, const char *status, const char *info, json_object *resp)
+{
+ json_object *root, *request, *reply;
+ const char *message;
+
+ /* builds the answering structure */
+ root = json_object_new_object();
+ json_object_object_add(root, "jtype", json_object_new_string("afb-reply"));
+ request = json_object_new_object();
+ json_object_object_add(root, "request", request);
+ json_object_object_add(request, "status", json_object_new_string(status));
+ if (info)
+ json_object_object_add(request, "info", json_object_new_string(info));
+ if (resp)
+ json_object_object_add(root, "response", resp);
+
+ /* make the reply */
+ reply = json_object_new_array();
+ json_object_array_add(reply, json_object_new_int(retcode));
+ json_object_array_add(reply, wsreq->id);
+ json_object_array_add(reply, root);
+ json_object_array_add(reply, json_object_new_string(wsreq->aws->context->token));
+
+ /* emits the reply */
+ message = json_object_to_json_string(reply);
+ afb_ws_text(wsreq->aws->ws, message, strlen(message));
+ json_object_put(reply);
+
+ /* TODO eliminates the wsreq */
+}
+
+static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info)
+{
+ wsreq_reply(wsreq, 4, status, info, NULL);
+}
+
+static void wsreq_success(struct afb_wsreq *wsreq, json_object *obj, const char *info)
+{
+ wsreq_reply(wsreq, 3, "success", info, obj);
+}
+
diff --git a/src/afb-ws-json.h b/src/afb-ws-json.h
new file mode 100644
index 00000000..2d3cfd75
--- /dev/null
+++ b/src/afb-ws-json.h
@@ -0,0 +1,22 @@
+/*
+ * Copyright 2016 IoT.bzh
+ * Author: José Bollo <jose.bollo@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+struct afb_ws_json;
+struct AFB_clientCtx;
+
+extern struct afb_ws_json *afb_ws_json_create(int fd, struct AFB_clientCtx *context, void (*cleanup)(void*), void *closure);
+
diff --git a/src/afb-ws.c b/src/afb-ws.c
index 49d836b2..3dae3390 100644
--- a/src/afb-ws.c
+++ b/src/afb-ws.c
@@ -16,14 +16,16 @@
*/
#define _GNU_SOURCE
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdint.h>
#include <assert.h>
#include <errno.h>
#include <sys/uio.h>
#include <string.h>
-#include <json.h>
-
#include "websock.h"
+#include "afb-ws.h"
#include "utils-upoll.h"
@@ -31,8 +33,11 @@ static ssize_t aws_writev(struct afb_ws *ws, const struct iovec *iov, int iovcnt
static ssize_t aws_readv(struct afb_ws *ws, const struct iovec *iov, int iovcnt);
static void aws_disconnect(struct afb_ws *ws);
static void aws_on_close(struct afb_ws *ws, uint16_t code, size_t size);
-static void aws_on_content(struct afb_ws *ws, int last, size_t size);
+static void aws_on_text(struct afb_ws *ws, int last, size_t size);
+static void aws_on_binary(struct afb_ws *ws, int last, size_t size);
+static void aws_on_continue(struct afb_ws *ws, int last, size_t size);
static void aws_on_readable(struct afb_ws *ws);
+static void aws_on_hangup(struct afb_ws *ws);
static struct websock_itf aws_itf = {
.writev = (void*)aws_writev,
@@ -42,84 +47,62 @@ static struct websock_itf aws_itf = {
.on_ping = NULL,
.on_pong = NULL,
.on_close = (void*)aws_on_close,
- .on_text = (void*)aws_on_content,
- .on_binary = (void*)aws_on_content,
- .on_continue = (void*)aws_on_content,
- .on_extension = NULL,
+ .on_text = (void*)aws_on_text,
+ .on_binary = (void*)aws_on_binary,
+ .on_continue = (void*)aws_on_continue,
+ .on_extension = NULL
};
-struct afb_wsreq
+struct buf
{
- struct afb_ws *aws;
- struct afb_wsreq *next;
- struct json_object *id;
- struct json_object *name;
- struct json_object *token;
- struct json_object *request;
+ char *buffer;
+ size_t size;
};
struct afb_ws
{
int fd;
- struct upoll *up;
+ enum { none, text, binary } type;
+ const struct afb_ws_itf *itf;
+ void *closure;
struct websock *ws;
- void (*cleanup)(void*);
- void *cleanup_closure;
- struct AFB_clientCtx *context;
- struct afb_wsreq *requests;
-};
-
-static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name);
-static void wsreq_iterate(struct afb_wsreq *wsreq, int (*iterator)(void *closure, struct afb_arg arg), void *closure);
-static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info);
-static void wsreq_success(struct afb_wsreq *wsreq, struct json_object *obj, const char *info);
-static int wsreq_session_create(struct afb_wsreq *wsreq);
-static int wsreq_session_check(struct afb_wsreq *wsreq, int refresh);
-static void wsreq_session_close(struct afb_wsreq *wsreq);
-
-static const struct afb_req_itf wsreq_itf = {
- .get = (void*)wsreq_get,
- .iterate = (void*)wsreq_iterate,
- .fail = (void*)wsreq_fail,
- .success = (void*)wsreq_success,
- .session_create = (void*)wsreq_session_create,
- .session_check = (void*)wsreq_session_check,
- .session_close = (void*)wsreq_session_close
+ struct upoll *up;
+ struct buf buffer;
};
-struct afb_ws *afb_ws_create(int fd, struct AFB_clientCtx *context, void (*cleanup)(void*), void *closure)
+struct afb_ws *afb_ws_create(int fd, const struct afb_ws_itf *itf, void *closure)
{
struct afb_ws *result;
assert(fd >= 0);
- assert(context != NULL);
result = malloc(sizeof * result);
if (result == NULL)
goto error;
result->fd = fd;
- result->cleanup = cleanup;
- result->cleanup_closure = closure;
- result->context = ctxClientGet(context);
- if (result->context == NULL)
- goto error2;
+ result->type = none;
+ result->itf = itf;
+ result->closure = closure;
result->ws = websock_create_v13(&aws_itf, result);
if (result->ws == NULL)
- goto error3;
+ goto error2;
result->up = upoll_open(result->fd, result);
if (result->up == NULL)
- goto error4;
+ goto error3;
+
+ result->buffer.buffer = NULL;
+ result->buffer.size = 0;
upoll_on_readable(result->up, (void*)aws_on_readable);
- upoll_on_hangup(result->up, (void*)aws_disconnect);
+ upoll_on_hangup(result->up, (void*)aws_on_hangup);
+
return result;
-error4:
- websock_destroy(result->ws);
+
error3:
- ctxClientPut(result->context);
+ websock_destroy(result->ws);
error2:
free(result);
error:
@@ -127,6 +110,33 @@ error:
return NULL;
}
+void afb_ws_disconnect(struct afb_ws *ws)
+{
+ struct upoll *up = ws->up;
+ struct websock *wsi = ws->ws;
+ ws->up = NULL;
+ ws->ws = NULL;
+ upoll_on_hangup(up, NULL);
+ upoll_on_readable(up, NULL);
+ upoll_close(up);
+ websock_destroy(wsi);
+}
+
+void afb_ws_close(struct afb_ws *ws, uint16_t code)
+{
+ websock_close_code(ws->ws, code);
+}
+
+void afb_ws_text(struct afb_ws *ws, const char *text, size_t length)
+{
+ websock_text(ws->ws, text, length);
+}
+
+void afb_ws_binary(struct afb_ws *ws, const void *data, size_t length)
+{
+ websock_binary(ws->ws, data, length);
+}
+
static ssize_t aws_writev(struct afb_ws *ws, const struct iovec *iov, int iovcnt)
{
ssize_t rc;
@@ -145,242 +155,105 @@ static ssize_t aws_readv(struct afb_ws *ws, const struct iovec *iov, int iovcnt)
return rc;
}
-static void aws_disconnect(struct afb_ws *ws)
-{
- upoll_close(ws->up);
- websock_destroy(ws->ws);
- close(ws->fd);
- MHD_resume_connection (ws->connection);
- ctxClientPut(ws->context);
- json_tokener_free(ws->tokener);
- free(ws);
-}
-
-static void aws_on_close(struct afb_ws *ws, uint16_t code, size_t size)
-{
- /* do nothing */
-}
-
static void aws_on_readable(struct afb_ws *ws)
{
websock_dispatch(ws->ws);
}
-static int aws_handle_json(struct afb_ws *aws, struct json_object *obj)
+static void aws_on_hangup(struct afb_ws *ws)
{
- struct afb_req r;
- int count, num;
- struct json_object *type, *id, *name, *req, *token;
- struct afb_wsreq *wsreq;
- const char *api, *verb;
- size_t lenapi, lenverb;
-
- /* protocol inspired by http://www.gir.fr/ocppjs/ocpp_srpc_spec.shtml */
-
- /* the object must be an array of 4 or 5 elements */
- if (!json_object_is_type(obj, json_type_array))
- goto error;
- count = json_object_array_length(obj);
- if (count < 4 || count > 5)
- goto error;
-
- /* get the 5 elements: type id name request token */
- type = json_object_array_get_idx(obj, 0);
- id = json_object_array_get_idx(obj, 1);
- name = json_object_array_get_idx(obj, 2);
- req = json_object_array_get_idx(obj, 3);
- token = json_object_array_get_idx(obj, 4);
-
- /* check the types: int string string object string */
- if (!json_object_is_type(type, json_type_int))
- goto error;
- if (!json_object_is_type(id, json_type_string))
- goto error;
- if (!json_object_is_type(name, json_type_string))
- goto error;
- if (!json_object_is_type(req, json_type_object))
- goto error;
- if (token != NULL && !json_object_is_type(token, json_type_string))
- goto error;
-
- /* the type is only 2 */
- num = json_object_get_int(type);
- if (num != 2)
- goto error;
-
- /* checks the api/verb structure of name */
- api = json_object_get_string(name);
- for (lenapi = 0 ; api[lenapi] && api[lenapi] != '/' ; lenapi++);
- if (!lenapi || !api[lenapi])
- goto error;
- verb = &api[lenapi+1];
- for (lenverb = 0 ; verb[lenverb] && verb[lenverb] != '/' ; lenverb++);
- if (!lenverb || verb[lenverb])
- goto error;
-
- /* allocates the request data */
- wsreq = malloc(sizeof *wsreq);
- if (wsreq == NULL)
- goto error;
-
- /* fill and record the request */
- wsreq->aws = aws;
- wsreq->id = json_object_get(id);
- wsreq->name = json_object_get(name);
- wsreq->token = json_object_get(token);
- wsreq->request = json_object_get(req);
- wsreq->next = aws->requests;
- aws->requests = wsreq;
- json_object_put(obj);
-
- r.data = wsreq;
- r.itf = &wsreq_itf;
- afb_apis_call(r, aws->context, api, lenapi, verb, lenverb);
- return 1;
-
-error:
- json_object_put(obj);
- return 0;
}
-static void aws_on_content(struct afb_ws *ws, int last, size_t size)
+static void aws_disconnect(struct afb_ws *ws)
{
- ssize_t rrc;
- char buffer[8000];
- struct json_object *obj;
-
- json_tokener_reset(ws->tokener);
- while(size) {
- rrc = websock_read(ws->ws, buffer,
- size > sizeof buffer ? sizeof buffer : size);
- if (rrc < 0) {
- websock_close(ws->ws);
- return;
- }
- size -= (size_t)rrc;
- obj = json_tokener_parse_ex(ws->tokener, buffer, (int)rrc);
- if (obj != NULL) {
- if (!aws_handle_json(ws, obj)) {
- websock_close(ws->ws);
- return;
- }
- } else if (json_tokener_get_error(ws->tokener) != json_tokener_continue) {
- websock_close(ws->ws);
- return;
- }
- }
}
-static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name)
+static inline struct buf aws_pick_buffer(struct afb_ws *ws)
{
- struct afb_arg arg;
- struct json_object *value;
-
- if (json_object_object_get_ex(wsreq->request, name, &value)) {
- arg.name = name;
- arg.value = json_object_get_string(value);
- arg.size = strlen(arg.value);
- } else {
- arg.name = NULL;
- arg.value = NULL;
- arg.size = 0;
- }
- arg.path = NULL;
- return arg;
+ struct buf result = ws->buffer;
+ ws->buffer.buffer = NULL;
+ ws->buffer.size = 0;
+ return result;
}
-static void wsreq_iterate(struct afb_wsreq *wsreq, int (*iterator)(void *closure, struct afb_arg arg), void *closure)
+static int aws_read(struct afb_ws *ws, size_t size)
{
- struct afb_arg arg;
- struct json_object_iterator it = json_object_iter_begin(wsreq->request);
- struct json_object_iterator end = json_object_iter_end(wsreq->request);
-
- arg.size = 0;
- arg.path = NULL;
- while(!json_object_iter_equal(&it, &end)) {
- arg.name = json_object_iter_peek_name(&it);
- arg.value = json_object_get_string(json_object_iter_peek_value(&it));
- if (!iterator(closure, arg))
- break;
- json_object_iter_next(&it);
+ ssize_t sz;
+ char *buffer;
+
+ if (size != 0) {
+ buffer = realloc(ws->buffer.buffer, ws->buffer.size + size + 1);
+ if (buffer == NULL)
+ return 0;
+ ws->buffer.buffer = buffer;
+ sz = websock_read(ws->ws, &buffer[ws->buffer.size], size);
+ if ((size_t)sz != size)
+ return 0;
+ ws->buffer.size += size;
}
+ return 1;
}
-static int wsreq_session_create(struct afb_wsreq *wsreq)
-{
- struct AFB_clientCtx *context = wsreq->aws->context;
- if (context->created)
- return 0;
- return wsreq_session_check(wsreq, 1);
-}
-
-static int wsreq_session_check(struct afb_wsreq *wsreq, int refresh)
+static void aws_on_close(struct afb_ws *ws, uint16_t code, size_t size)
{
- const char *token;
- struct AFB_clientCtx *context = wsreq->aws->context;
-
- if (wsreq->token == NULL)
- return 0;
-
- token = json_object_get_string(wsreq->token);
- if (token == NULL)
- return 0;
-
- if (!ctxTokenCheck (context, token))
- return 0;
-
- if (refresh) {
- ctxTokenNew (context);
+ struct buf b;
+
+ ws->type = none;
+ if (ws->itf->on_close == NULL)
+ websock_drop(ws->ws);
+ else {
+ aws_read(ws, size);
+ b = aws_pick_buffer(ws);
+ ws->itf->on_close(ws, code, b.buffer, b.size);
}
-
- return 1;
}
-static void wsreq_session_close(struct afb_wsreq *wsreq)
+static void aws_on_text(struct afb_ws *ws, int last, size_t size)
{
- struct AFB_clientCtx *context = wsreq->aws->context;
- ctxClientClose(context);
+ if (ws->type != none) {
+ websock_drop(ws->ws);
+ websock_close_code(ws->ws, WEBSOCKET_CODE_PROTOCOL_ERROR);
+ } else if (ws->itf->on_text == NULL) {
+ websock_drop(ws->ws);
+ websock_close_code(ws->ws, WEBSOCKET_CODE_CANT_ACCEPT);
+ } else {
+ ws->type = text;
+ aws_on_continue(ws, last, size);
+ }
}
-
-static void wsreq_reply(struct afb_wsreq *wsreq, int retcode, const char *status, const char *info, json_object *resp)
+static void aws_on_binary(struct afb_ws *ws, int last, size_t size)
{
- json_object *root, *request, *reply;
- const char *message;
-
- /* builds the answering structure */
- root = json_object_new_object();
- json_object_object_add(root, "jtype", json_object_new_string("afb-reply"));
- request = json_object_new_object();
- json_object_object_add(root, "request", request);
- json_object_object_add(request, "status", json_object_new_string(status));
- if (info)
- json_object_object_add(request, "info", json_object_new_string(info));
- if (resp)
- json_object_object_add(root, "response", resp);
-
- /* make the reply */
- reply = json_object_new_array();
- json_object_array_add(reply, json_object_new_int(retcode));
- json_object_array_add(reply, wsreq->id);
- json_object_array_add(reply, root);
- json_object_array_add(reply, json_object_new_string(wsreq->aws->context->token));
-
- /* emits the reply */
- message = json_object_to_json_string(reply);
- websock_text(wsreq->aws->ws, message, strlen(message));
- json_object_put(reply);
-
- /* TODO eliminates the wsreq */
+ if (ws->type != none) {
+ websock_drop(ws->ws);
+ websock_close_code(ws->ws, WEBSOCKET_CODE_PROTOCOL_ERROR);
+ } else if (ws->itf->on_binary == NULL) {
+ websock_drop(ws->ws);
+ websock_close_code(ws->ws, WEBSOCKET_CODE_CANT_ACCEPT);
+ } else {
+ ws->type = text;
+ aws_on_continue(ws, last, size);
+ }
}
-static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info)
+static void aws_on_continue(struct afb_ws *ws, int last, size_t size)
{
- wsreq_reply(wsreq, 4, status, info, NULL);
-}
+ struct buf b;
+ int istxt;
-static void wsreq_success(struct afb_wsreq *wsreq, json_object *obj, const char *info)
-{
- wsreq_reply(wsreq, 3, "success", info, obj);
+ if (ws->type == none) {
+ websock_drop(ws->ws);
+ websock_close_code(ws->ws, WEBSOCKET_CODE_PROTOCOL_ERROR);
+ } else {
+ if (!aws_read(ws, size)) {
+ aws_on_close(ws, WEBSOCKET_CODE_ABNORMAL, 0);
+ } else if (last) {
+ istxt = ws->type == text;
+ ws->type = none;
+ b = aws_pick_buffer(ws);
+ b.buffer[b.size] = 0;
+ (istxt ? ws->itf->on_text : ws->itf->on_binary)(ws->closure, b.buffer, b.size);
+ }
+ }
}
diff --git a/src/afb-ws.h b/src/afb-ws.h
index 6350fb9e..a8af90ac 100644
--- a/src/afb-ws.h
+++ b/src/afb-ws.h
@@ -17,4 +17,15 @@
struct afb_ws;
+struct afb_ws_itf
+{
+ void (*on_close) (void *, uint16_t code, char *, size_t size);
+ void (*on_text) (void *, char *, size_t size);
+ void (*on_binary) (void *, char *, size_t size);
+};
+
+extern struct afb_ws *afb_ws_create(int fd, const struct afb_ws_itf *itf, void *closure);
+extern void afb_ws_close(struct afb_ws *ws, uint16_t code);
+extern void afb_ws_text(struct afb_ws *ws, const char *text, size_t length);
+extern void afb_ws_binary(struct afb_ws *ws, const void *data, size_t length);
diff --git a/src/websock.c b/src/websock.c
index e583a49d..41e063f8 100644
--- a/src/websock.c
+++ b/src/websock.c
@@ -346,7 +346,7 @@ loop:
(size_t) ws->length);
else
ws->itf->on_close(ws->closure,
- WEBSOCKET_CODE_UNSET, 0);
+ WEBSOCKET_CODE_NOT_SET, 0);
ws->itf->disconnect(ws->closure);
return 0;
default:
diff --git a/src/websock.h b/src/websock.h
index 6376a37e..c40a364c 100644
--- a/src/websock.h
+++ b/src/websock.h
@@ -23,11 +23,13 @@
struct iovec;
-#define WEBSOCKET_CODE_UNSET 0
#define WEBSOCKET_CODE_OK 1000
#define WEBSOCKET_CODE_GOING_AWAY 1001
#define WEBSOCKET_CODE_PROTOCOL_ERROR 1002
-#define WEBSOCKET_CODE_RESERVED 1004 /* Protocol 8: frame too large */
+#define WEBSOCKET_CODE_CANT_ACCEPT 1003
+#define WEBSOCKET_CODE_RESERVED 1004
+#define WEBSOCKET_CODE_NOT_SET 1005
+#define WEBSOCKET_CODE_ABNORMAL 1006
#define WEBSOCKET_CODE_INVALID_UTF8 1007
#define WEBSOCKET_CODE_POLICY_VIOLATION 1008
#define WEBSOCKET_CODE_MESSAGE_TOO_LARGE 1009