-
Notifications
You must be signed in to change notification settings - Fork 52
/
wsHandshake.cpp
150 lines (126 loc) · 3.94 KB
/
wsHandshake.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* ,. , ,. . .---. . . .---.
* `| /| / ,-. |-. \___ ,-. ,-. | , ,-. |- \___ ,-. ,-. . , ,-. ,-.
* | / | / |-' | | \ | | | |< |-' | \ |-' | | / |-' |
* `' `' `-' ^-' `---' `-' `-' ' ` `-' `' `---' `-' ' `' `-' '
*
* Copyright 2012 by Alexander Thiemann <[email protected]>
*
* This file is part of WebSocketServer.
*
* WebSocketServer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* WebSocketServer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WebSocketServer. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "wsHandshake.h"
/**
* init the handshake
* @param payload
*/
wsHandshake::wsHandshake(string payload) {
_payload = payload;
_success = false;
}
/**
* destructor. does nothing
*/
wsHandshake::~wsHandshake() { }
/**
* extract a value in the key: value format
* @param line
* @param key
* @return
*/
string wsHandshake::extractValue(string line, string key) {
key = key.append(": ");
size_t i = key.length();
return line.substr(i);
}
/**
* return handshake response, ready to send to client
* @return
*/
string wsHandshake::getResponse() {
return _responsePayload;
}
/**
* does getResponse contain a valid response?
* @return
*/
bool wsHandshake::isSuccess() {
return _success;
}
/**
* build the handshake response
*/
void wsHandshake::generateResponse() {
// line by line
size_t lastPos = 0;
string line;
string delim = "\r\n";
while (lastPos != string::npos) {
size_t start = lastPos;
lastPos = _payload.find(delim, lastPos);
if (lastPos == string::npos) {
break;
}
line = _payload.substr(start, lastPos-start);
remove(line.begin(), line.end(), '\r');
remove(line.begin(), line.end(), '\t');
remove(line.begin(), line.end(), '\n');
// GET Line
if (line[0] == 'G' && line[1] == 'E' && line[2] == 'T') {
// get part
size_t end = _payload.find(" ", 4);
_get = line.substr(4, end-4);
//cout << "GET |" << _get << "|\n";
}
// version
if (line.find("Sec-WebSocket-Version") != string::npos) {
string val = extractValue(line, "Sec-WebSocket-Version");
_version = atoi(val.c_str());
cout << "VERS |" << _version << "| \n";
}
// key
if (line.find("Sec-WebSocket-Key") != string::npos) {
_key = extractValue(line, "Sec-WebSocket-Key");
cout << "KEY |" << _key << "|\n";
}
// other params are unhandled
/* @TODO
Upgrade:
Connection:
Host:
Sec-WebSocket-Origin:
*/
lastPos += 2;
}
// now check params
if (_version < 6) {
_success = false;
return;
}
// now calculate key
unsigned char hash[20];
char hex[41];
_key.append("258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
sha1::calc(_key.c_str(), _key.length(), hash);
string responseKey = base64_encode(hash, 20);
// build payload
_success = true;
_responsePayload = "HTTP/1.1 101 Switching Protocols\r\n";
_responsePayload.append("Upgrade: websocket\r\n");
_responsePayload.append("Connection: Upgrade\r\n");
_responsePayload.append("Sec-WebSocket-Accept: " + responseKey + "\r\n\r\n");
return;
}