├── .cproject
├── .project
├── Makefile
├── base64.cpp
├── base64.h
├── debug_log.cpp
├── debug_log.h
├── main.cpp
├── network_interface.cpp
├── network_interface.h
├── sha1.cpp
├── sha1.h
├── test.html
├── websocket_handler.cpp
├── websocket_handler.h
├── websocket_request.cpp
├── websocket_request.h
├── websocket_respond.cpp
└── websocket_respond.h
/.cproject:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | websocket
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder
10 | clean,full,incremental,
11 |
12 |
13 |
14 |
15 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder
16 | full,incremental,
17 |
18 |
19 |
20 |
21 |
22 | org.eclipse.cdt.core.cnature
23 | org.eclipse.cdt.core.ccnature
24 | org.eclipse.cdt.managedbuilder.core.managedBuildNature
25 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature
26 |
27 |
28 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | CC = g++
2 | FLAG = -g
3 | INCLUDE =
4 | LIBDIR =
5 | LIB =
6 | BIN =
7 | TARGET = websocketserver
8 | SRCS = base64.cpp sha1.cpp network_interface.cpp debug_log.cpp websocket_handler.cpp \
9 | websocket_request.cpp main.cpp
10 |
11 | $(TARGET):$(SRCS:.cpp=.o)
12 | $(CC) $(FLAG) $(LIBDIR) $(LIB) -o $@ $^
13 | -rm -f *.o *.d
14 |
15 | %.o:%.cpp
16 | $(CC) $(FLAG) $(INCLUDE) -c -o $@ $<
17 |
18 | clean:
19 | -rm -f *.o *.d
20 |
--------------------------------------------------------------------------------
/base64.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | base64.cpp and base64.h
3 |
4 | Copyright (C) 2004-2008 René Nyffenegger
5 |
6 | This source code is provided 'as-is', without any express or implied
7 | warranty. In no event will the author be held liable for any damages
8 | arising from the use of this software.
9 |
10 | Permission is granted to anyone to use this software for any purpose,
11 | including commercial applications, and to alter it and redistribute it
12 | freely, subject to the following restrictions:
13 |
14 | 1. The origin of this source code must not be misrepresented; you must not
15 | claim that you wrote the original source code. If you use this source code
16 | in a product, an acknowledgment in the product documentation would be
17 | appreciated but is not required.
18 |
19 | 2. Altered source versions must be plainly marked as such, and must not be
20 | misrepresented as being the original source code.
21 |
22 | 3. This notice may not be removed or altered from any source distribution.
23 |
24 | René Nyffenegger rene.nyffenegger@adp-gmbh.ch
25 |
26 | */
27 |
28 | #include "base64.h"
29 | #include
30 |
31 | static const std::string base64_chars =
32 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
33 | "abcdefghijklmnopqrstuvwxyz"
34 | "0123456789+/";
35 |
36 |
37 | static inline bool is_base64(unsigned char c) {
38 | return (isalnum(c) || (c == '+') || (c == '/'));
39 | }
40 |
41 | std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
42 | std::string ret;
43 | int i = 0;
44 | int j = 0;
45 | unsigned char char_array_3[3];
46 | unsigned char char_array_4[4];
47 |
48 | while (in_len--) {
49 | char_array_3[i++] = *(bytes_to_encode++);
50 | if (i == 3) {
51 | char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
52 | char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
53 | char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
54 | char_array_4[3] = char_array_3[2] & 0x3f;
55 |
56 | for(i = 0; (i <4) ; i++)
57 | ret += base64_chars[char_array_4[i]];
58 | i = 0;
59 | }
60 | }
61 |
62 | if (i)
63 | {
64 | for(j = i; j < 3; j++)
65 | char_array_3[j] = '\0';
66 |
67 | char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
68 | char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
69 | char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
70 | char_array_4[3] = char_array_3[2] & 0x3f;
71 |
72 | for (j = 0; (j < i + 1); j++)
73 | ret += base64_chars[char_array_4[j]];
74 |
75 | while((i++ < 3))
76 | ret += '=';
77 |
78 | }
79 |
80 | return ret;
81 |
82 | }
83 |
84 | std::string base64_decode(std::string const& encoded_string) {
85 | size_t in_len = encoded_string.size();
86 | int i = 0;
87 | int j = 0;
88 | int in_ = 0;
89 | unsigned char char_array_4[4], char_array_3[3];
90 | std::string ret;
91 |
92 | while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
93 | char_array_4[i++] = encoded_string[in_]; in_++;
94 | if (i ==4) {
95 | for (i = 0; i <4; i++)
96 | char_array_4[i] = base64_chars.find(char_array_4[i]);
97 |
98 | char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
99 | char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
100 | char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
101 |
102 | for (i = 0; (i < 3); i++)
103 | ret += char_array_3[i];
104 | i = 0;
105 | }
106 | }
107 |
108 | if (i) {
109 | for (j = i; j <4; j++)
110 | char_array_4[j] = 0;
111 |
112 | for (j = 0; j <4; j++)
113 | char_array_4[j] = base64_chars.find(char_array_4[j]);
114 |
115 | char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
116 | char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
117 | char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
118 |
119 | for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
120 | }
121 |
122 | return ret;
123 | }
124 |
--------------------------------------------------------------------------------
/base64.h:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | std::string base64_encode(unsigned char const* , unsigned int len);
4 | std::string base64_decode(std::string const& s);
5 |
--------------------------------------------------------------------------------
/debug_log.cpp:
--------------------------------------------------------------------------------
1 | #include "debug_log.h"
2 |
3 | void DEBUG_LOG(const char *msg, ...) {
4 | char message[256] = {0};
5 | va_list args;
6 | va_start(args, msg);
7 | vsprintf(message, msg, args);
8 | va_end(args);
9 | Debug_LOG::log()->write_log(message);
10 | }
11 |
12 | Debug_LOG *Debug_LOG::m_log = NULL;
13 |
14 | Debug_LOG::Debug_LOG():
15 | tim(0),
16 | t(NULL),
17 | fp(NULL),
18 | filepath(),
19 | message(),
20 | last_log_time()
21 | {
22 | #ifdef __WRITE_FILE__
23 | create_log_file();
24 | #endif
25 | }
26 |
27 | Debug_LOG::~Debug_LOG(){
28 | #ifdef __WRITE_FILE__
29 | fclose(fp);
30 | #endif
31 | }
32 |
33 | void Debug_LOG::create_log_file(){
34 | if(fp != NULL)
35 | fclose(fp);
36 |
37 | sprintf(filepath, "./log/debuglog_");
38 | time(&tim);
39 | t = localtime(&tim);
40 | memcpy(&last_log_time, t, sizeof(struct tm));
41 | sprintf(filepath + 15, "%02d_%02d",t->tm_mon + 1, t->tm_mday);
42 | fp = fopen(filepath, "a+");
43 | }
44 |
45 | Debug_LOG *Debug_LOG::log(){
46 | if(m_log == NULL){
47 | m_log = new Debug_LOG();
48 | }
49 | return m_log;
50 | }
51 |
52 | void Debug_LOG::write_log(const char *msg){
53 | time(&tim);
54 | t = localtime(&tim);
55 | sprintf(message, "[%02d:%02d:%02d] %s\n", t->tm_hour, t->tm_min, t->tm_sec, msg);
56 | #ifdef __WRITE_FILE__
57 | if(t->tm_mday != last_log_time.tm_mday || t->tm_mon != last_log_time.tm_mon
58 | || t->tm_year != last_log_time.tm_year)
59 | create_log_file();
60 | fwrite(message, strlen(message), 1, fp);
61 | fflush(fp);
62 | #else
63 | printf("\n%s", message);
64 | fflush(stdout);
65 | #endif
66 | }
67 |
68 |
--------------------------------------------------------------------------------
/debug_log.h:
--------------------------------------------------------------------------------
1 | #ifndef __debug_log__
2 | #define __debug_log__
3 |
4 | //#define __WRITE_FILE__
5 |
6 | #include
7 | #include
8 | #include
9 | #include
10 |
11 | void DEBUG_LOG(const char *msg, ...);
12 |
13 | class Debug_LOG {
14 | private:
15 | Debug_LOG();
16 | ~Debug_LOG();
17 | void create_log_file();
18 | public:
19 | static Debug_LOG *log();
20 | void write_log(const char *msg);
21 | private:
22 | static Debug_LOG *m_log;
23 | time_t tim;
24 | struct tm *t;
25 | FILE *fp;
26 | char filepath[32];
27 | char message[256];
28 | struct tm last_log_time;
29 | };
30 |
31 | #endif
32 |
--------------------------------------------------------------------------------
/main.cpp:
--------------------------------------------------------------------------------
1 | #include "network_interface.h"
2 |
3 | int main(int argc, char **argv){
4 | NETWORK_INTERFACE->run();
5 | return 0;
6 | }
7 |
--------------------------------------------------------------------------------
/network_interface.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include