Simplify functions for calls
[src/app-framework-binder.git] / src / afb-api-ws.c
index cb7b4f5..89b83bc 100644 (file)
@@ -37,7 +37,7 @@
 
 #include "afb-common.h"
 
-#include "session.h"
+#include "afb-session.h"
 #include "afb-ws.h"
 #include "afb-msg-json.h"
 #include "afb-apis.h"
@@ -46,6 +46,7 @@
 #include "afb-evt.h"
 #include "afb-subcall.h"
 #include "verbose.h"
+#include "sd-fds.h"
 
 struct api_ws_memo;
 struct api_ws_event;
@@ -212,7 +213,9 @@ static struct api_ws *api_ws_make(const char *path)
        memcpy(api->path, path, length + 1);
 
        /* api name is at the end of the path */
-       api->api = strrchr(api->path, '/');
+       while (length && path[length - 1] != '/' && path[length - 1] != ':')
+               length = length - 1;
+       api->api = &api->path[length];
        if (api->api == NULL || !afb_apis_is_valid_api_name(++api->api)) {
                errno = EINVAL;
                goto error2;
@@ -314,21 +317,29 @@ static int api_ws_socket(const char *path, int server)
 {
        int fd, rc;
 
-       /* check for unix socket */
-       if (0 == strncmp(path, "unix:", 5))
-               fd = api_ws_socket_unix(path + 5, server);
-       else
-               fd = api_ws_socket_inet(path, server);
-
-       if (fd >= 0) {
-               fcntl(fd, F_SETFD, FD_CLOEXEC);
-               fcntl(fd, F_SETFL, O_NONBLOCK);
-               if (server) {
+       /* check for systemd socket */
+       if (0 == strncmp(path, "sd:", 3))
+               fd = sd_fds_for(path + 3);
+       else {
+               /* check for unix socket */
+               if (0 == strncmp(path, "unix:", 5))
+                       /* unix socket */
+                       fd = api_ws_socket_unix(path + 5, server);
+               else
+                       /* inet socket */
+                       fd = api_ws_socket_inet(path, server);
+
+               if (fd >= 0 && server) {
                        rc = 1;
                        setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &rc, sizeof rc);
                        rc = listen(fd, 5);
                }
        }
+       /* configure the socket */
+       if (fd >= 0) {
+               fcntl(fd, F_SETFD, FD_CLOEXEC);
+               fcntl(fd, F_SETFL, O_NONBLOCK);
+       }
        return fd;
 }
 
@@ -420,12 +431,6 @@ static int api_ws_write_uint32(struct writebuf *wb, uint32_t value)
        return 1;
 }
 
-static int api_ws_write_string_nz(struct writebuf *wb, const char *value, size_t length)
-{
-       uint32_t len = (uint32_t)length;
-       return (size_t)len == length && ++len && api_ws_write_uint32(wb, len) && api_ws_write_put(wb, value, length) && api_ws_write_char(wb, '\0');
-}
-
 static int api_ws_write_string_length(struct writebuf *wb, const char *value, size_t length)
 {
        uint32_t len = (uint32_t)++length;
@@ -804,7 +809,7 @@ static void api_ws_client_on_binary(void *closure, char *data, size_t size)
 }
 
 /* on call, propagate it to the ws service */
-static void api_ws_client_call_cb(void * closure, struct afb_req req, struct afb_context *context, const char *verb, size_t lenverb)
+static void api_ws_client_call_cb(void * closure, struct afb_req req, struct afb_context *context, const char *verb)
 {
        int rc;
        struct api_ws_memo *memo;
@@ -826,8 +831,8 @@ static void api_ws_client_call_cb(void * closure, struct afb_req req, struct afb
                goto internal_error;
        if (!api_ws_write_uint32(&wb, memo->msgid)
         || !api_ws_write_uint32(&wb, (uint32_t)context->flags)
-        || !api_ws_write_string_nz(&wb, verb, lenverb)
-        || !api_ws_write_string(&wb, ctxClientGetUuid(context->session))
+        || !api_ws_write_string(&wb, verb)
+        || !api_ws_write_string(&wb, afb_session_uuid(context->session))
         || !api_ws_write_string_length(&wb, raw, szraw))
                goto overflow;
 
@@ -977,7 +982,7 @@ static void api_ws_server_called(struct api_ws_client *client, struct readbuf *r
        /* makes the call */
        areq.itf = &afb_api_ws_req_itf;
        areq.closure = wreq;
-       afb_apis_call_(areq, &wreq->context, client->api, verb);
+       afb_apis_call(areq, &wreq->context, client->api, verb);
        api_ws_server_req_unref(wreq);
        return;