-
Notifications
You must be signed in to change notification settings - Fork 63
/
websocket.h
87 lines (66 loc) · 1.66 KB
/
websocket.h
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
/**
*
* filename: websocket.h
* summary:
* author: caosiyang
* email: [email protected]
*
*/
#ifndef WEBSOCKET_H
#define WEBSOCKET_H
#include "stdio.h"
#include "string.h"
#include "tools.h"
#include "frame.h"
#include "openssl/sha.h"
#include "base64.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//WebSocket request
typedef struct WebsocketRequest {
string req;
string connection;
string upgrade;
string host;
string origin;
string cookie;
string sec_websocket_key;
string sec_websocket_version;
} ws_req_t;
//WebSocket response
typedef struct WebsocketResponse {
string resp;
string date;
string connection;
string server;
string upgrade;
string access_control_allow_origin;
string access_control_allow_credentials;
string sec_websocket_accept;
string access_control_allow_headers;
} ws_resp_t;
//steps of receiving a frame
enum Step {
ZERO, //before websocket handshake
ONE, //0-2 bytes, fin, opcode, mask, payload length
TWO, //extended payload length
THREE, //masking-key
FOUR, //payload data
UNKNOWN
};
//parse websocket request
int32_t parse_websocket_request(const char *src, ws_req_t *ws_req);
//print websocket request
void print_websocket_request(const ws_req_t *ws_req);
//generate websocket response
string generate_websocket_response(const ws_req_t *ws_req);
//generate websocket key
string generate_key(const string &key);
//parse fin, opcode, mask, payload len
int32_t parse_frame_header(const char *buf, frame_t *frame);
//unmask payload-data
int32_t unmask_payload_data(frame_t *frame);
int32_t unmask_payload_data(const char *masking_key, char *payload_data, uint32_t payload_len);
#endif