forked from javapretty/websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket_request.cpp
104 lines (91 loc) · 2.04 KB
/
websocket_request.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
#include "websocket_request.h"
Websocket_Request::Websocket_Request():
fin_(),
opcode_(),
mask_(),
masking_key_(),
payload_length_(),
payload_()
{
}
Websocket_Request::~Websocket_Request(){
}
int Websocket_Request::fetch_websocket_info(char *msg){
int pos = 0;
fetch_fin(msg, pos);
fetch_opcode(msg, pos);
fetch_mask(msg, pos);
fetch_payload_length(msg, pos);
fetch_masking_key(msg, pos);
return fetch_payload(msg, pos);
}
void Websocket_Request::print(){
DEBUG_LOG("WEBSOCKET PROTOCOL\n"
"FIN: %d\n"
"OPCODE: %d\n"
"MASK: %d\n"
"PAYLOADLEN: %d\n"
"PAYLOAD: %s",
fin_, opcode_, mask_, payload_length_, payload_);
reset();
}
void Websocket_Request::reset(){
fin_ = 0;
opcode_ = 0;
mask_ = 0;
memset(masking_key_, 0, sizeof(masking_key_));
payload_length_ = 0;
memset(payload_, 0, sizeof(payload_));
}
int Websocket_Request::fetch_fin(char *msg, int &pos){
fin_ = (unsigned char)msg[pos] >> 7;
return 0;
}
int Websocket_Request::fetch_opcode(char *msg, int &pos){
opcode_ = msg[pos] & 0x0f;
pos++;
return 0;
}
int Websocket_Request::fetch_mask(char *msg, int &pos){
mask_ = (unsigned char)msg[pos] >> 7;
return 0;
}
int Websocket_Request::fetch_masking_key(char *msg, int &pos){
if(mask_ != 1)
return 0;
for(int i = 0; i < 4; i++)
masking_key_[i] = msg[pos + i];
pos += 4;
return 0;
}
int Websocket_Request::fetch_payload_length(char *msg, int &pos){
payload_length_ = msg[pos] & 0x7f;
pos++;
if(payload_length_ == 126){
uint16_t length = 0;
memcpy(&length, msg + pos, 2);
pos += 2;
payload_length_ = ntohs(length);
}
else if(payload_length_ == 127){
uint32_t length = 0;
memcpy(&length, msg + pos, 4);
pos += 4;
payload_length_ = ntohl(length);
}
return 0;
}
int Websocket_Request::fetch_payload(char *msg, int &pos){
memset(payload_, 0, sizeof(payload_));
if(mask_ != 1){
memcpy(payload_, msg + pos, payload_length_);
}
else {
for(uint i = 0; i < payload_length_; i++){
int j = i % 4;
payload_[i] = msg[pos + i] ^ masking_key_[j];
}
}
pos += payload_length_;
return 0;
}