diff --git a/uppdev/Menu/Menu.upp b/uppdev/Menu/Menu.upp index e8c2827d7..124c0997c 100644 --- a/uppdev/Menu/Menu.upp +++ b/uppdev/Menu/Menu.upp @@ -1,9 +1,10 @@ -uses - CtrlLib; - -file - menu.cpp, - test.lay; - -mainconfig - "" = "GUI"; +uses + CtrlLib; + +file + menu.cpp, + test.lay; + +mainconfig + "" = "GUI"; + diff --git a/uppdev/Menu/init b/uppdev/Menu/init new file mode 100644 index 000000000..f0b987d19 --- /dev/null +++ b/uppdev/Menu/init @@ -0,0 +1,4 @@ +#ifndef _Menu_icpp_init_stub +#define _Menu_icpp_init_stub +#include "CtrlLib/init" +#endif diff --git a/uppdev/Menu/menu.cpp b/uppdev/Menu/menu.cpp index 6e7d72422..b6af40b28 100644 --- a/uppdev/Menu/menu.cpp +++ b/uppdev/Menu/menu.cpp @@ -42,7 +42,7 @@ struct App : public TopWindow { bar.Add(numbers_enabled, "Numbers", CtrlImg::cut(), THISBACK(SubMenu)); bar.Add(numbers_enabled, "Numbers", CtrlImg::information(), THISBACK(SubMenu)); bar.Separator(); - bar.Add("Zoom &Out", CtrlImg::error(), THISBACK(Exit)); + bar.Add("Zoom &Out", CtrlImg::error(), THISBACK(Exit)).Bold(); bar.Add("Zoom Aut", CtrlImg::error(), THISBACK(Exit)); bar.Add("Zoom Aut", CtrlImg::error(), THISBACK(Exit)); bar.Add("Exit", THISBACK(Exit)) diff --git a/uppdev/WebSockets/WebSockets.cpp b/uppdev/WebSockets/WebSockets.cpp index 2824d1bd7..5e265dd1f 100644 --- a/uppdev/WebSockets/WebSockets.cpp +++ b/uppdev/WebSockets/WebSockets.cpp @@ -1,143 +1,2 @@ #include "WebSockets.h" -bool WebSocket::Handshake() -{ - HttpHeader hdr; - if(!hdr.Read(*this)) { - SetSockError("websocket handshake", ERROR_NOHEADER, "Failed to read HTTP header"); - return false; - } - String key = hdr["sec-websocket-key"]; - if(IsNull(key)) { - SetSockError("websocket handshake", ERROR_NOKEY, "Missing sec-websocket-key"); - return false; - } - - byte sha1[20]; - SHA1(sha1, key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"); - - DLOG( - "HTTP/1.1 101 Switching Protocols\r\n" - "Upgrade: websocket\r\n" - "Connection: Upgrade\r\n" - "Sec-WebSocket-Accept: " + Base64Encode((char *)sha1, 20) + "\r\n\r\n" - ); - - return PutAll( - "HTTP/1.1 101 Switching Protocols\r\n" - "Upgrade: websocket\r\n" - "Connection: Upgrade\r\n" - "Sec-WebSocket-Accept: " + Base64Encode((char *)sha1, 20) + "\r\n\r\n" - ); -} - -int64 WebSocket::ReadLen(int n) -{ - int64 len = 0; - while(n-- > 0) - len = (len << 8) | (byte)Get(); - return len; -} - -bool WebSocket::RecieveRaw() -{ - if(IsError()) - return false; - - opcode = Get(); - int64 len = Get(); - bool mask = len & 128; - len &= 127; - if(len == 127) - len = ReadLen(8); - if(len == 126) - len = ReadLen(2); - - byte key[4]; - if(mask) - Get(key, 4); - - if(IsError()) { - SetSockError("websocket recieve", ERROR_DATA, "Invalid data"); - return false; - } - - if(len > maxlen) { - SetSockError("websocket recieve", ERROR_LEN_LIMIT, "Frame limit exceeded, size " + AsString(len)); - return false; - } - - StringBuffer frame((int)len); // TODO int64 - char *buffer = ~frame; - if(!GetAll(buffer, (int)len)) { - SetSockError("websocket recieve", ERROR_DATA, "Invalid data"); - return false; - } - - if(mask) - for(int i = 0; i < len; i++) - buffer[i] ^= key[i & 3]; - - data = frame; - return true; -} - -String WebSocket::Recieve() -{ - for(;;) { - if(!RecieveRaw()) - return String::GetVoid(); - if(GetOpCode() == PING) - SendRaw(PONG, ~data, data.GetLength()); - else - if(GetOpCode() == CLOSE) - SendRaw(CLOSE, ~data, data.GetLength()); - else - break; - } - return data; -} - -bool WebSocket::SendRaw(int hdr, const void *data, int64 len) -{ - if(IsError()) - return false; - - ASSERT(len < INT_MAX); // temporary, todo - String b; - b.Cat(hdr); - if(len > 65535) { - b.Cat(127); - b.Cat(byte(len >> 56)); - b.Cat(byte(len >> 48)); - b.Cat(byte(len >> 40)); - b.Cat(byte(len >> 32)); - b.Cat(byte(len >> 24)); - b.Cat(byte(len >> 16)); - b.Cat(byte(len >> 8)); - b.Cat(byte(len)); - } - else - if(len > 125) { - b.Cat(126); - b.Cat(byte(len >> 8)); - b.Cat(byte(len)); - } - else - b.Cat((int)len); - - if(IsError() || !PutAll(~b, b.GetLength()) || !PutAll(data, (int)len)) { - SetSockError("websocket send", ERROR_SEND, "Failed to send data"); - return false; - } - - return true; -} - -void WebSocket::Reset() -{ - opcode = 0; - data.Clear(); - maxlen = 10 * 1024 * 1024; -} - diff --git a/uppdev/WebSockets/WebSockets.h b/uppdev/WebSockets/WebSockets.h index 3e9181bd8..520b6072d 100644 --- a/uppdev/WebSockets/WebSockets.h +++ b/uppdev/WebSockets/WebSockets.h @@ -12,6 +12,8 @@ class WebSocket : public TcpSocket { String data; int64 maxlen; + bool Handshake(); + public: enum { ERROR_NOHEADER = TcpSocket::ERROR_LAST, ERROR_NOKEY, ERROR_DATA, ERROR_SEND, ERROR_LEN_LIMIT @@ -26,7 +28,7 @@ public: PONG = 0xa, }; - bool Handshake(); + bool WebAccept(TcpSocket& server); bool RecieveRaw(); String Recieve(); diff --git a/uppdev/WebSockets/main.cpp b/uppdev/WebSockets/main.cpp index bf99f1e8f..43a245fe3 100644 --- a/uppdev/WebSockets/main.cpp +++ b/uppdev/WebSockets/main.cpp @@ -12,13 +12,11 @@ CONSOLE_APP_MAIN for(;;) { WebSocket ws; - if(ws.Accept(server)) { + if(ws.WebAccept(server)) { LOG("Accepted, trying to handshake"); - if(ws.Handshake()) { - LOG("Handshake successfull, trying to recieve"); - LOG(ws.Recieve()); - ws.SendText("This is some text..."); - } + LOG("Handshake successfull, trying to recieve"); + LOG(ws.Recieve()); + ws.SendText("This is some text..."); } if(ws.IsError()) LOG("ERROR: " << ws.GetErrorDesc());