Update copyright dates
[src/app-framework-binder.git] / src / websock.c
index 0bbd56a..2632260 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016, 2017 "IoT.bzh"
+ * Copyright (C) 2015-2020 "IoT.bzh"
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -29,7 +29,9 @@
 
 #include "websock.h"
 
-#define BLOCK_DATA_SIZE              4096
+#if !defined(WEBSOCKET_DEFAULT_MAXLENGTH)
+#  define WEBSOCKET_DEFAULT_MAXLENGTH 1048500  /* 76 less than 1M, probably enougth for headers */
+#endif
 
 #define FRAME_GET_FIN(BYTE)         (((BYTE) >> 7) & 0x01)
 #define FRAME_GET_RSV1(BYTE)        (((BYTE) >> 6) & 0x01)
@@ -59,6 +61,8 @@
 #define STATE_LENGTH  2
 #define STATE_DATA    3
 
+static size_t default_maxlength = WEBSOCKET_DEFAULT_MAXLENGTH;
+
 struct websock {
        int state;
        uint64_t maxlength;
@@ -80,16 +84,6 @@ static ssize_t ws_readv(struct websock *ws, const struct iovec *iov, int iovcnt)
        return ws->itf->readv(ws->closure, iov, iovcnt);
 }
 
-#if 0
-static ssize_t ws_write(struct websock *ws, const void *buffer, size_t buffer_size)
-{
-       struct iovec iov;
-       iov.iov_base = (void *)buffer;  /* const cast */
-       iov.iov_len = buffer_size;
-       return ws_writev(ws, &iov, 1);
-}
-#endif
-
 static ssize_t ws_read(struct websock *ws, void *buffer, size_t buffer_size)
 {
        struct iovec iov;
@@ -562,7 +556,7 @@ struct websock *websock_create_v13(const struct websock_itf *itf, void *closure)
        if (result) {
                result->itf = itf;
                result->closure = closure;
-               result->maxlength = 65000;
+               result->maxlength = default_maxlength;
        }
        return result;
 }
@@ -571,3 +565,34 @@ void websock_destroy(struct websock *ws)
 {
        free(ws);
 }
+
+void websock_set_default_max_length(size_t maxlen)
+{
+       default_maxlength = maxlen;
+}
+
+void websock_set_max_length(struct websock *ws, size_t maxlen)
+{
+       ws->maxlength = (uint64_t)maxlen;
+}
+
+const char *websocket_explain_error(uint16_t code)
+{
+       static const char *msgs[] = {
+               "OK",                /* 1000 */
+               "GOING_AWAY",        /* 1001 */
+               "PROTOCOL_ERROR",    /* 1002 */
+               "CANT_ACCEPT",       /* 1003 */
+               "RESERVED",          /* 1004 */
+               "NOT_SET",           /* 1005 */
+               "ABNORMAL",          /* 1006 */
+               "INVALID_UTF8",      /* 1007 */
+               "POLICY_VIOLATION",  /* 1008 */
+               "MESSAGE_TOO_LARGE", /* 1009 */
+               "EXPECT_EXTENSION",  /* 1010 */
+               "INTERNAL_ERROR",    /* 1011 */
+       };
+       if (code < 1000 || (code - 1000) >= (sizeof msgs / sizeof *msgs))
+               return "?";
+       return msgs[code - 1000];
+}