git-svn-id: svn://ultimatepp.org/upp/trunk@6869 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2014-02-05 07:06:32 +00:00
parent 74b5af04aa
commit 4d8678305e
6 changed files with 22 additions and 158 deletions

View file

@ -1,9 +1,10 @@
uses
CtrlLib;
file
menu.cpp,
test.lay;
mainconfig
"" = "GUI";
uses
CtrlLib;
file
menu.cpp,
test.lay;
mainconfig
"" = "GUI";

4
uppdev/Menu/init Normal file
View file

@ -0,0 +1,4 @@
#ifndef _Menu_icpp_init_stub
#define _Menu_icpp_init_stub
#include "CtrlLib/init"
#endif

View file

@ -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))

View file

@ -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;
}

View file

@ -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();

View file

@ -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());