Skip to content

Commit

Permalink
完事了一大部分
Browse files Browse the repository at this point in the history
  • Loading branch information
chen-c committed Jul 23, 2022
1 parent 33f20b6 commit 0cb35cd
Show file tree
Hide file tree
Showing 51 changed files with 456 additions and 1,415 deletions.
30 changes: 28 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 2.6)
project(chenWeb)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 11)
set(CXX_FLAGS

-std=c++11
-rdynamic
)
if(CMAKE_BUILD_BITS EQUAL 32)
list(APPEND CXX_FLAGS "-m32")
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
list(APPEND CXX_FLAGS "-Wno-null-dereference")
list(APPEND CXX_FLAGS "-Wno-sign-conversion")
list(APPEND CXX_FLAGS "-Wno-unused-local-typedef")
list(APPEND CXX_FLAGS "-Wthread-safety")
list(REMOVE_ITEM CXX_FLAGS "-rdynamic")
endif()
string(REPLACE ";" " " CMAKE_CXX_FLAGS "${CXX_FLAGS}")

set(CMAKE_CXX_FLAGS_DEBUG "-O0")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

message(${PROJECT_BINARY_DIR})

include_directories(${PROJECT_SOURCE_DIR}) # 头文件


set(INC_DIR include)
include_directories(${INC_DIR})

add_subdirectory(lib/yaml-cpp)
add_subdirectory(config)

add_executable(chenWeb main.cpp
config/config.cpp pool/threadpool.cpp http/http_conn.cpp utils/utils.cpp
http/http_request.cpp http/http_response.cpp global/global.cpp)
Expand Down
Binary file modified chenWeb
Binary file not shown.
225 changes: 113 additions & 112 deletions http/http_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,130 +241,131 @@ HTTP_CODE HttpRequest::do_request() {
int len = strlen(std::string (g_str_cur_dir + "/resources").c_str());
const char *p = strrchr(m_url, '/');

//处理post请求 且是登录或者注册
if (m_ispost == 1 && (*(p + 1) == '2' || *(p + 1) == '3')) {

char *m_url_real = (char *)malloc(sizeof(char) * 200);
strcpy(m_url_real, "/");
strcat(m_url_real, m_url + 2);
strncpy(m_real_file + len, m_url_real, FILENAME_LEN - len - 1);
free(m_url_real);

//将用户名和密码提取出来
//user=abcd&password=asdasd
char name[100], password[100];
int name_index;
for (name_index = 5; m_string[name_index] != '&'; ++ name_index)
name[name_index - 5] = m_string[name_index];
name[name_index - 5] = '\0';

int passwd_index = 0; // 此时i在&这里,加10刚好到密码的第一个字符
for (name_index = name_index + 10; m_string[name_index] != '\0'; ++ name_index, ++ passwd_index)
password[passwd_index] = m_string[name_index];
password[passwd_index] = '\0';

if (*(p + 1) == '3') {
//注册,先检测数据库中是否有重名的
//没有重名的,进行增加数据
char *sql_insert = (char *)malloc(sizeof(char) * 200);
strcpy(sql_insert, "INSERT INTO user(username, password) VALUES(");
strcat(sql_insert, "'");
strcat(sql_insert, name);
strcat(sql_insert, "', '");
strcat(sql_insert, password);
strcat(sql_insert, "')");
if (users.find(name) == users.end())
{
m_lock_map.lock();
// int res = mysql_query(mysql, sql_insert);
int res = 0; // todo 这里假设总是注册成功
m_lock_map.unlock();

if (m_ispost == 0) {
if (!res) {
m_lock_map.lock();
users.insert(std::pair<std::string, std::string>(name, password));
m_lock_map.unlock();

// 访问index.html界面
if (*(p + 1) == '4') {
char *m_url_real = (char *)malloc(sizeof(char) * 200);
strcpy(m_url_real, "/index.html");
strncpy(m_real_file + len, m_url_real, strlen(m_url_real)); //拼接路径
free(m_url_real);
}
strcpy(m_url, "/log.html");
std::cout << "新用户注册成功:user = " << name << ", password = " << password << std::endl;
}
else {
strcpy(m_url, "/registerError.html");
std::cout << "新用户注册失败,数据库写入错误:user = " << name << ", password = " << password << std::endl;
}
}
else {
strcpy(m_url, "/registerError.html");
std::cout << "新用户注册失败,用户名重复:user = " << name << ", password = " << password << std::endl;

// 访问注册界面
else if (*(p + 1) == '0') {
char *m_url_real = (char *)malloc(sizeof(char) * 200);
strcpy(m_url_real, "/register.html");
strncpy(m_real_file + len, m_url_real, strlen(m_url_real)); //拼接路径
free(m_url_real);
}
}

// 访问登录界面
else if (*(p + 1) == '1') {
char *m_url_real = (char *)malloc(sizeof(char) * 200);
strcpy(m_url_real, "/log.html");
strncpy(m_real_file + len, m_url_real, strlen(m_url_real));
free(m_url_real);
// 如果是登录,直接判断
// 若浏览器端输入的用户名和密码在表中可以查找到,返回1,否则返回0
else if (*(p + 1) == '2') {
if (users.find(name) != users.end() && users[name] == password)
strcpy(m_url, "/welcome.html");
else
strcpy(m_url, "/logError.html");
}

// 访问图片界面
else if (*(p + 1) == '5') {
char *m_url_real = (char *)malloc(sizeof(char) * 200);
strcpy(m_url_real, "/picture.html");
strncpy(m_real_file + len, m_url_real, strlen(m_url_real));
free(m_url_real);
}
}

// 访问视频界面
else if (*(p + 1) == '6') {
char *m_url_real = (char *)malloc(sizeof(char) * 200);
strcpy(m_url_real, "/video.html");
strncpy(m_real_file + len, m_url_real, strlen(m_url_real));
free(m_url_real);
}
// 访问端口号或者index.html界面, 因为默认是访问这个界面,所有放在if第一行,提高命中率,减少if判断次数
if (*(p + 1) == '4') {
char *m_url_real = (char *)malloc(sizeof(char) * 200);
strcpy(m_url_real, "/index.html");
strncpy(m_real_file + len, m_url_real, strlen(m_url_real)); //拼接路径
free(m_url_real);
}

// 访问关注界面
else if (*(p + 1) == '7') {
char *m_url_real = (char *)malloc(sizeof(char) * 200);
strcpy(m_url_real, "/fans.html");
strncpy(m_real_file + len, m_url_real, strlen(m_url_real));
// 访问登录界面
else if (*(p + 1) == '1') {
char *m_url_real = (char *)malloc(sizeof(char) * 200);
strcpy(m_url_real, "/log.html");
strncpy(m_real_file + len, m_url_real, strlen(m_url_real));
free(m_url_real);
}

free(m_url_real);
}
else { // 其他界面
strncpy(m_real_file + len, m_url, FILENAME_LEN - len - 1);
}
// 访问注册界面
else if (*(p + 1) == '0') {
char *m_url_real = (char *)malloc(sizeof(char) * 200);
strcpy(m_url_real, "/register.html");
strncpy(m_real_file + len, m_url_real, strlen(m_url_real)); //拼接路径
free(m_url_real);
}
else if(m_ispost == 1) {

//处理post请求 且是登录或者注册
if ((*(p + 1) == '2' || *(p + 1) == '3')) {

char *m_url_real = (char *)malloc(sizeof(char) * 200);
strcpy(m_url_real, "/");
strcat(m_url_real, m_url + 2);
strncpy(m_real_file + len, m_url_real, FILENAME_LEN - len - 1);
free(m_url_real);

//将用户名和密码提取出来
//user=abcd&password=asdasd
char name[100], password[100];
int i;
for (i = 5; m_string[i] != '&'; ++i)
name[i - 5] = m_string[i];
name[i - 5] = '\0';

int j = 0; // 此时i在&这里,加10刚好到密码的第一个字符
for (i = i + 10; m_string[i] != '\0'; ++i, ++j)
password[j] = m_string[i];
password[j] = '\0';

if (*(p + 1) == '3') {
//注册,先检测数据库中是否有重名的
//没有重名的,进行增加数据
char *sql_insert = (char *)malloc(sizeof(char) * 200);
strcpy(sql_insert, "INSERT INTO user(username, password) VALUES(");
strcat(sql_insert, "'");
strcat(sql_insert, name);
strcat(sql_insert, "', '");
strcat(sql_insert, password);
strcat(sql_insert, "')");
if (users.find(name) == users.end())
{
m_lock_map.lock();
// int res = mysql_query(mysql, sql_insert);
int res = 0; // todo 这里假设总是注册成功
m_lock_map.unlock();

if (!res) {
m_lock_map.lock();
users.insert(std::pair<std::string, std::string>(name, password));
m_lock_map.unlock();

strcpy(m_url, "/log.html");
std::cout << "新用户注册成功:user = " << name << ", password = " << password << std::endl;
}
else {
strcpy(m_url, "/registerError.html");
std::cout << "新用户注册失败,数据库写入错误:user = " << name << ", password = " << password << std::endl;
}
}
else {
strcpy(m_url, "/registerError.html");
std::cout << "新用户注册失败,用户名重复:user = " << name << ", password = " << password << std::endl;

}
}
//如果是登录,直接判断
//若浏览器端输入的用户名和密码在表中可以查找到,返回1,否则返回0
else if (*(p + 1) == '2') {
if (users.find(name) != users.end() && users[name] == password)
strcpy(m_url, "/welcome.html");
else
strcpy(m_url, "/logError.html");
}
}

// 访问图片界面
else if (*(p + 1) == '5') {
char *m_url_real = (char *)malloc(sizeof(char) * 200);
strcpy(m_url_real, "/picture.html");
strncpy(m_real_file + len, m_url_real, strlen(m_url_real));
free(m_url_real);
}

// 访问视频界面
else if (*(p + 1) == '6') {
char *m_url_real = (char *)malloc(sizeof(char) * 200);
strcpy(m_url_real, "/video.html");
strncpy(m_real_file + len, m_url_real, strlen(m_url_real));
free(m_url_real);
}

// 访问关注界面
else if (*(p + 1) == '7') {
char *m_url_real = (char *)malloc(sizeof(char) * 200);
strcpy(m_url_real, "/fans.html");
strncpy(m_real_file + len, m_url_real, strlen(m_url_real));

free(m_url_real);
}
else { // 其他界面,跳转到index
strncpy(m_real_file + len, m_url, FILENAME_LEN - len - 1);
// char *m_url_real = (char *)malloc(sizeof(char) * 200);
// strcpy(m_url_real, "/index.html");
// strncpy(m_real_file + len, m_url_real, strlen(m_url_real)); //拼接路径
// free(m_url_real);
}


Expand Down
40 changes: 0 additions & 40 deletions include/yaml-cpp/contrib/anchordict.h

This file was deleted.

Loading

0 comments on commit 0cb35cd

Please sign in to comment.