Skip to content

Commit

Permalink
CGI SQL Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
qinguoyi committed Apr 14, 2020
1 parent 25b21ad commit 4750fd1
Show file tree
Hide file tree
Showing 13 changed files with 344 additions and 71 deletions.
1 change: 1 addition & 0 deletions CGImysql/id_passwd.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0911 0911
68 changes: 57 additions & 11 deletions CGImysql/sign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,46 @@
#include <cstdio>
#include "sql_connection_pool.h"
#include <map>
#include <fstream>
#include <sstream>
using namespace std;

//#define CGISQL //不使用连接池
#define CGISQLPOOL //使用连接池

int main(int argc, char *argv[])
{
map<string, string> users;

pthread_mutex_t lock;
pthread_mutex_init(&lock, NULL);

//初始化数据库连接池,连接池为静态大小
//通过主机地址和登录账号,密码进入服务器数据库,选择qgydb库
connection_pool *connPool = connection_pool::GetInstance("localhost", "root", "root", "qgydb", 3306, 5);
#ifdef CGISQL

MYSQL *con = NULL;
con = mysql_init(con);

if (con == NULL)
{
cout << "Error:" << mysql_error(con);
exit(1);
}
con = mysql_real_connect(con, "localhost", "root", "root", "qgydb", 3306, NULL, 0);

if (con == NULL)
{
cout << "Error: " << mysql_error(con);
exit(1);
}

//在连接池中取一个连接
MYSQL *mysql = connPool->GetConnection();
//在user表中检索username,passwd数据,浏览器端输入
if (mysql_query(mysql, "SELECT username,passwd FROM user"))
if (mysql_query(con, "SELECT username,passwd FROM user"))
{
printf("INSERT error:%s\n", mysql_error(mysql));
printf("INSERT error:%s\n", mysql_error(con));
return -1;
}
//从表中检索完整的结果集
MYSQL_RES *result = mysql_store_result(mysql);
MYSQL_RES *result = mysql_store_result(con);
//返回结果集中的列数
int num_fields = mysql_num_fields(result);
//返回所有字段结构的数组
Expand Down Expand Up @@ -61,9 +78,8 @@ int main(int argc, char *argv[])
{
if (users.find(name) == users.end())
{

pthread_mutex_lock(&lock);
int res = mysql_query(mysql, sql_insert);
int res = mysql_query(con, sql_insert);
pthread_mutex_unlock(&lock);

if (!res)
Expand All @@ -88,5 +104,35 @@ int main(int argc, char *argv[])
//释放结果集使用的内存
mysql_free_result(result);

connPool->DestroyPool();
#endif

#ifdef CGISQLPOOL
ifstream out(argv[2]);
string linestr;
while (getline(out, linestr))
{
string str;
stringstream id_passwd(linestr);

getline(id_passwd, str, ' ');
string temp1(str);

getline(id_passwd, str, ' ');
string temp2(str);
users[temp1] = temp2;
}
out.close();

//只完成登录
string name(argv[0]);
const char *namep = name.c_str();
string passwd(argv[1]);
const char *passwdp = passwd.c_str();

if (users.find(name) != users.end() && users[name] == passwd)
printf("1\n");
else
printf("0\n");

#endif
}
109 changes: 92 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Update
----------
- [x] 解决请求服务器上大文件的Bug
- [x] 增加请求视频文件的页面
- [x] 解决数据库同步校验内存泄漏
- [x] 实现两种CGI数据库访问逻辑


Demo
Expand Down Expand Up @@ -108,46 +110,119 @@ web端测试
```C++
const char* doc_root="/home/qgy/TinyWebServer/root";
```
* 选择任一校验方式,代码中使用同步校验。当使用CGI时才进行如下修改,否则可跳过本步骤,直接生成server
* 选择任一校验方式,代码中使用同步校验。当使用CGI时才进行如下修改,否则可跳过本步骤.

- [ ] CGI多进程注册/登录校验
* 打开http_conn.cpp中CGI,关闭同步线程
- [x] 同步线程数据库校验
* 关闭main.c中CGISQLPOOL,打开SYNSQL

```C++
23 #define SYNSQL //同步数据库校验
24 //#define CGISQLPOOL //CGI数据库校验
```

* 关闭http_conn.cpp中两种CGI,打开SYNSQL

```C++
6 //同步线程登录校验
7 //#define SYN
7 //同步校验
8 #define SYNSQL

10 //CGI多进程使用链接池
11 //#define CGISQLPOOL

9 //CGI多进程登录校验
10 #define CGI
13 //CGI多进程不用连接池
14 //#define CGISQL
```

- [ ] CGI多进程数据库校验,不使用连接池
* 关闭main.c中SYNSQL和CGISQLPOOL

```C++
23 //#define SYNSQL //同步数据库校验
24 //#define CGISQLPOOL //CGI数据库校验
```

* 关闭http_conn.cpp中SYNSQL和CGISQLPOOL,打开CGISQL

```C++
7 //同步校验
8 //#define SYNSQL

10 //CGI多进程使用链接池
11 //#define CGISQLPOOL

13 //CGI多进程不用连接池
14 #define CGISQL
```

* 关闭sign.cpp中的CGISQLPOOL,打开CGISQL

```C++
12 #define CGISQL //不使用连接池
13 //#define CGISQLPOOL //使用连接池
```
* 修改sign.cpp中的数据库初始化信息

```C++
//root root为服务器数据库的登录名和密码
connection_pool *connPool=connection_pool::GetInstance("localhost","root","root","yourdb",3306,5);
```
* 生成check.cgi
* 生成CGISQL.cgi

```C++
make check.cgi
make CGISQL.cgi
```
* 将生成的check.cgi放到root文件夹

- [ ] CGI多进程数据库校验,使用连接池
* 关闭main.c中SYNSQL,打开CGISQLPOOL

```C++
23 //#define SYNSQL //同步数据库校验
24 #define CGISQLPOOL //CGI数据库校验
```

* 关闭http_conn.cpp中SYNSQL和CGISQL,打开CGISQLPOOL

```C++
7 //同步校验
8 //#define SYNSQL

10 //CGI多进程使用链接池
11 #define CGISQLPOOL

13 //CGI多进程不用连接池
14 //#define CGISQL
```
* 关闭sign.cpp中的CGISQL,打开CGISQLPOOL

```C++
cp ./check.cgi ./root
12 //#define CGISQL //不使用连接池
13 #define CGISQLPOOL //使用连接池
```
* 生成CGISQL.cgi

- [x] 同步线程注册/登录校验
* 关闭http_conn.cpp中CGI,打开同步线程
```C++
make CGISQL.cgi
```

* 选择任一日志方式,代码中使用同步日志。当测试不同写入方式时才进行如下修改,否则可跳过本步骤.

- [x] 同步写入日志
* 关闭main.c中ASYNLOG,打开同步写入SYNLOG

```C++
6 //同步线程登录校验
7 #define SYN
25 #define SYNLOG //同步写日志
26 //#define ASYNLOG /异步写日志
```

9 //CGI多进程登录校验
10 //#define CGI
- [ ] 异步写入日志
* 关闭main.c中SYNLOG,打开异步写入ASYNLOG

```C++
25 //#define SYNLOG //同步写日志
26 #define ASYNLOG /异步写日志
```


* 生成server

```C++
Expand Down
Loading

0 comments on commit 4750fd1

Please sign in to comment.