-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.cpp
More file actions
executable file
·140 lines (133 loc) · 3.86 KB
/
Copy pathinit.cpp
File metadata and controls
executable file
·140 lines (133 loc) · 3.86 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
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
#include "cJSON.h"
#include "ThreadInfo.h"
#include "addone.h"
#include "etp_log/log.h"
extern int Port;
extern cJSON *cjsonfile;
extern LinkLib *Linklib;
extern cJSON *UserAgentIndexPage;
extern char LogPath[256];
SOCKET initBindSocket(const int port)
{
//创建套接字并且绑定端口
//准备工作
int opt = 1;
int Fd = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(Fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); //设置端口复用,防止重启程序后无法绑定端口
extrapidLog(LOG_INFO, "HTTP", "正在创建监听套接字");
if (-1 == Fd)
{
return execl("sudo", "./webserver", NULL);
}
int n = 1;
struct sockaddr_in addr = {0};
addr.sin_family = AF_INET; //和socket第一个参数一致
addr.sin_port = htons(port);
//小端转大端
addr.sin_addr.s_addr = INADDR_ANY; //当前电脑上任意一个网卡都可以
int sockfd;
int r = bind(Fd, (struct sockaddr *)&addr, sizeof(addr));
if (-1 == r)
{
extrapidLog(LOG_ERROR, "HTTP", "端口绑定失败,原因:%s", strerror(errno));
exit(0);
}
listen(Fd, 10);
return Fd; //返回绑定完成端口的套接字
}
int initserver()
{
extrapidLog(LOG_INFO, "HTTP", "正在初始化服务器");
if (access("conf/http_config.json", F_OK))
{
extrapidLog(LOG_WARN, "HTTP", "没有找到配置文件,尝试生成配置文件");
mkdirs("conf", S_IRWXU, 0);
cJSON *config = cJSON_CreateObject();
cJSON_AddStringToObject(config, "MimePath", "conf/mime.json");
cJSON_AddNumberToObject(config, "PORT", 8080);
cJSON_AddStringToObject(config, "LogPath", "logs/http");
cJSON *arry = cJSON_CreateArray();
cJSON_AddItemToObject(config, "UserAgentIndexPage", arry);
FILE *fp = fopen("conf/http_config.json", "w");
if (fp == NULL)
{
perror("无法创建配置文件,错误原因:");
exit(1);
}
char *p = cJSON_Print(config);
fprintf(fp, "%s", p);
free(p);
cJSON_Delete(config);
fclose(fp);
extrapidLog(LOG_INFO, "HTTP", "配置文件conf/http_config.json生成成功");
return 1;
}
FILE *fp = fopen("conf/http_config.json", "rb");
if (fp == NULL)
{
//没有配置文件
perror("无法打开配置文件,错误原因:");
exit(1);
}
else
{
char temp[1024 * 1024];
fread(temp, 1024, 1, fp);
cJSON *json = cJSON_Parse(temp);
if (json == NULL)
{
extrapidLog(LOG_WARN, "HTTP", "未找到配置文件conf/http_config.json");
exit(1);
}
cJSON *p = cJSON_GetObjectItem(json, "MimePath");
if (p == NULL)
{
extrapidLog(LOG_ERROR, "HTTP", "MimePath设置错误conf/http_config.json");
exit(1);
}
FILE *mimefile = fopen(p->valuestring, "rb");
if (mimefile == NULL)
{
extrapidLog(LOG_ERROR, "HTTP", "mime文件不存在");
exit(1);
}
fread(temp, 1024 * 1024, 1, mimefile);
cjsonfile = cJSON_Parse(temp);
if (cjsonfile == NULL)
{
extrapidLog(LOG_ERROR, "HTTP", "mime格式错误");
exit(1);
}
p = cJSON_GetObjectItem(json, "PORT");
if (p == NULL)
{
extrapidLog(LOG_WARN, "HTTP", "没有设置port,使用默认8001");
Port = 8001;
}
else
{
Port = p->valueint; //强制转换成正值
extrapidLog(LOG_INFO, "HTTP", "设置端口号:%d", Port);
}
p = cJSON_GetObjectItem(json, "LogPath");
if (p == NULL)
{
extrapidLog(LOG_ERROR, "HTTP", "没有设置日志文件目录,日志记录将无法生效");
}
else
{
strncpy(LogPath, p->valuestring, 256);
}
UserAgentIndexPage = cJSON_GetObjectItem(json, "UserAgentIndexPage");
// cJSON_Delete(json);//因为需要解析UA所以这个JSON数据不释放
//加载插件
extrapidLog(LOG_INFO, "HTTP", "开始加载插件");
Linklib = addone_MakeFuncLink(&Linklib, "app");
extrapidLog(LOG_INFO, "HTTP", "加载完成");
return 0;
}
}