X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=src%2Fafb-ws-client.c;h=de44a398e5046233650c345cf5c609e8c0708205;hb=1d0f869ce3379089b4f4c5285e3d30e971e5b93d;hp=2569d1350fba05f0b64ea914aaccc49fe42872be;hpb=8112056eecede587bf884f56629858c8b21bdbec;p=src%2Fapp-framework-binder.git diff --git a/src/afb-ws-client.c b/src/afb-ws-client.c index 2569d135..de44a398 100644 --- a/src/afb-ws-client.c +++ b/src/afb-ws-client.c @@ -26,8 +26,10 @@ #include #include #include +#include #include "afb-wsj1.h" +#include "afb-common.h" /**************** WebSocket handshake ****************************/ @@ -110,7 +112,7 @@ static char *strjoin(int count, const char **strings, const char *separ) /* creates the http message for the request */ static int make_request(char **request, const char *path, const char *host, const char *key, const char *protocols) { - int rc = asprintf(request, + int rc = asprintf(request, "GET %s HTTP/1.1\r\n" "Host: %s\r\n" "Upgrade: websocket\r\n" @@ -200,15 +202,15 @@ static int receive_response(int fd, const char **protocols, const char *ack) goto error; len = strcspn(line, " "); if (len != 8 || 0 != strncmp(line, "HTTP/1.1", 8)) - goto error; + goto abort; it = line + len; len = strspn(it, " "); if (len == 0) - goto error; + goto abort; it += len; len = strcspn(it, " "); if (len != 3 || 0 != strncmp(it, "101", 3)) - goto error; + goto abort; /* reads the rest of the response until empty line */ clen = 0; @@ -249,15 +251,13 @@ static int receive_response(int fd, const char **protocols, const char *ack) if (clen > 0) { while (read(fd, line, len) < 0 && errno == EINTR); } - if (haserr != 0) - result = -1; - else if (result < 0) { - result = 0; - while(protocols[result] != NULL) - result++; - } -error: + if (haserr != 0 || result < 0) + goto abort; return result; +abort: + errno = ECONNABORTED; +error: + return -1; } static int negociate(int fd, const char **protocols, const char *path, const char *host) @@ -315,7 +315,7 @@ invalid: errno = EINVAL; error: return -1; - + } @@ -365,8 +365,10 @@ struct afb_wsj1 *afb_ws_client_connect_wsj1(const char *uri, struct afb_wsj1_itf rc = negociate(fd, proto_json1, path, xhost); if (rc == 0) { result = afb_wsj1_create(fd, itf, closure); - if (result != NULL) + if (result != NULL) { + fcntl(fd, F_SETFL, O_NONBLOCK); break; + } } } close(fd); @@ -405,4 +407,15 @@ static char *makequery(const char *path, const char *uuid, const char *token) } #endif +/* + * + * Returns the internal event loop coming from afb-common + * + * Returns the handle to the event loop + */ +struct sd_event *afb_ws_client_get_event_loop() +{ + return afb_common_get_event_loop(); +} +