-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathhttp.cpp
More file actions
146 lines (133 loc) · 3.32 KB
/
Copy pathhttp.cpp
File metadata and controls
146 lines (133 loc) · 3.32 KB
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
#include "http.h"
#include <cstring>
#ifdef __linux__
#include <unistd.h>
#endif
#ifndef _WIN32
#define fopen_s(pFile,filename,mode) ((*(pFile))=fopen((filename), (mode)))==NULL
#endif
sb_Options options;
sb_Server* http_server;
std::string ip;
std::string port;
char server_data[] = {
"server|%s\n"
"port|%s\n"
"type|1\n"
"#maint|Under mainteance.\n"
"beta_server|%s\n"
"beta_port|1945\n"
"beta_type|1\n"
"meta|ni.com\n"
"RTENDMARKERBS1001\n"
};
std::string format(const char* msg, ...) {
char fmt[1024] = { 0 };
va_list va;
va_start(va, msg);
vsnprintf(fmt, 1024, msg, va);
va_end(va);
return std::string(fmt);
}
uint8_t* read_file(const char* file, uint32_t* size) {
FILE* fp;
fopen_s(&fp, file, "rb");
if (!fp)
return 0;
fseek(fp, 0, SEEK_END);
uint32_t fsize = ftell(fp);
fseek(fp, 0, SEEK_SET);
uint8_t* data = (uint8_t*)(malloc(fsize));
if (data) {
memset(data, 0, fsize);
fread(data, fsize, 1, fp);
fclose(fp);
if (size)
*size = fsize;
return data;
}
return NULL;
}
#define PRINT(msg, ...) printf("[HTTP]: " msg, ##__VA_ARGS__);
int http::handler(sb_Event* evt)
{
if (evt->type == SB_EV_REQUEST) {
PRINT("%s - %s %s\n", evt->address, evt->method, evt->path);
PRINT("got server data request.\n");
sb_send_status(evt->stream, 200, "OK");
sb_send_header(evt->stream, "Content-Type", "text/plain");
sb_writef(evt->stream, format(server_data, ip.c_str(), port.c_str(), ip.c_str()).c_str());
if ((strstr(evt->path, "/game/") != NULL || strstr(evt->path, "/social/") != NULL || strstr(evt->path, "/interface/") != NULL ||
strstr(evt->path, "/audio/") != NULL) &&
strstr(evt->method, "GET") != NULL) {
sb_send_status(evt->stream, 200, "OK");
uint32_t size = 0;
const char* path = evt->path + 1;
uint8_t* content = read_file(path, &size);
if (content) {
sb_send_header(evt->stream, "Content-Type", "application/x-www-form-urlencoded");
sb_send_header(evt->stream, "Content-Length", format("%d", size).c_str());
sb_send_header(evt->stream, "beserver", "06");
sb_send_header(evt->stream, "Connection", "keep-alive");
sb_send_header(evt->stream, "Accept-Ranges", "bytes");
PRINT("file served\n");
sb_write(evt->stream, content, size);
}
else {
PRINT("failed to serve file\n");
sb_send_header(evt->stream, "Content-Type", "text/plain");
sb_writef(evt->stream, "file not found");
}
}
else {
PRINT("unknown request\n");
sb_send_status(evt->stream, 200, "OK");
sb_send_header(evt->stream, "Content-Type", "text/plain");
sb_writef(evt->stream, "unknown");
}
}
return SB_RES_OK;
}
void http::start()
{
options.handler = handler;
options.host = "0.0.0.0";
#ifdef __linux__
if (geteuid() != 0)
{
PRINT("You are not root user, Running port 8080\n");
options.port = "8080";
}
else
{
PRINT("You are root user, Running port 80\n");
options.port = "80";
}
#elif _WIN32
options.port = "80";
#endif
http_server = sb_new_server(&options);
if (!http_server) {
PRINT("failed to start the http server!\n");
}
}
#ifdef _WIN32
#include <Windows.h>
#endif
void util_sleep(int32_t ms) {
#ifdef _WIN32
Sleep(ms);
#else
usleep(ms * 1000);
#endif
}
void http::run(std::string dest, std::string port2) {
ip = dest;
port = port2;
start();
while (true) {
sb_poll_server(http_server, 10);
util_sleep(1);
}
sb_close_server(http_server);
}