sqlite3
sql
#include <stdio.h>
#include <sqlite3.h>
int main(int argc, char const *argv[])
{
sqlite3 *db = NULL;
int ret = sqlite3_open("/home/linux/sec5_sqlite3/1.db", &db);
if (ret != SQLITE_OK)
{
fprintf(stderr, "sqlite3_open %s", sqlite3_errstr(ret));
return 1;
}
const char buf[2024] = "insert into user values(12,'lisan',28);";
char *errmsg = NULL;
int ret2 = sqlite3_exec(db, buf, NULL, NULL, &errmsg);
if (ret != SQLITE_OK)
{
fprintf(stderr, "sqlite3_exec %s", errmsg);
sqlite3_free(errmsg);
sqlite3_close(db);
return 1;
}
int ret3 = sqlite3_close(db);
if (ret != SQLITE_OK)
{
fprintf(stderr, "sqlite3_close %s", sqlite3_errstr(ret3));
sqlite3_free(errmsg);
return 1;
}
return 0;
}
sql_callback
#include <stdio.h>
#include <sqlite3.h>
int callback(void *data,int lie,char **argv,char **titile)
{
static int flag=0;
if(flag==0)
{
for(int n=0;n<lie;n++)
{
printf("%s\t",titile[n]);
}
printf("\n");
flag++;
}
for(int n=0;n<lie;n++)
{
printf("%s\t",argv[n]);
}
printf("\n");
return 0;
}
int main(int argc, char const *argv[])
{
sqlite3 *db = NULL;
int ret = sqlite3_open("/home/linux/sec5_sqlite3/1.db", &db);
if (ret != SQLITE_OK)
{
fprintf(stderr, "sqlite3_open %s", sqlite3_errstr(ret));
return 1;
}
const char buf[2024] = "select * from user;";
char *errmsg = NULL;
int ret2 = sqlite3_exec(db, buf, callback, NULL, &errmsg);
if (ret2 != SQLITE_OK)
{
fprintf(stderr, "sqlite3_exec %s", errmsg);
sqlite3_free(errmsg);
sqlite3_close(db);
return 1;
}
int ret3 = sqlite3_close(db);
if (ret3 != SQLITE_OK)
{
fprintf(stderr, "sqlite3_close %s", sqlite3_errstr(ret3));
sqlite3_free(errmsg);
return 1;
}
return 0;
}
sql_dict
#include <stdio.h>
#include <sqlite3.h>
#include <string.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <strings.h>
#include <time.h>
char *return_buf(char *order2, char *str)
{
char *start = str;
while (*start == ' ')
{
start++;
}
char *end = start;
while (*end != '\0' && *end != ' ')
{
end++;
}
char *next_start = end;
while (*next_start != '\0' && *next_start == ' ')
{
next_start++;
}
*end = '\0';
char *part1 = start;
char *part2 = next_start;
strcat(order2, part1);
strcat(order2, "\", \"");
strcat(order2, part2);
order2[strlen(order2) - 2] = '\0';
strcat(order2, "\");");
return order2;
}
int callback(void *data, int lie, char **argv, char **titile)
{
for (int n = 0; n < lie; n++)
{
printf("%s\t", argv[n]);
}
printf("\n");
*(int *)data = 1;
return 0;
}
int main(int argc, char const *argv[])
{
sqlite3 *db = NULL;
char *errmsg = NULL;
int ret = sqlite3_open("/home/linux/sec5_sqlite3/2.db", &db);
if (ret != SQLITE_OK)
{
fprintf(stderr, "sqlite3_open %s", sqlite3_errstr(ret));
return 1;
}
char sql_cmd[1024] = "drop table user3;";
sqlite3_exec(db, sql_cmd, NULL, NULL, &errmsg);
char order1[256] = "create table user3(word char,explain char);";
char order2[256] = "insert into user3 values(\"";
FILE *fp = fopen("/home/linux/sec5_sqlite3/dict.txt", "r");
char buf[1024] = {0};
sqlite3_exec(db, order1, NULL, NULL, &errmsg);
time_t tm;
time(&tm);
bzero(sql_cmd, sizeof(sql_cmd));
strcpy(sql_cmd, "BEGIN TRANSACTION;");
sqlite3_exec(db, sql_cmd, NULL, NULL, &errmsg);
while (1)
{
char *str = fgets(buf, sizeof(buf), fp);
char order2_2[1024] = {0};
strcpy(order2_2, order2);
if (str == NULL)
{
break;
}
char *order2_p = return_buf(order2_2, str);
ret = sqlite3_exec(db, order2_p, NULL, NULL, &errmsg);
if (ret != SQLITE_OK)
{
fprintf(stderr, "sqlite3_exec %s \n", errmsg);
return 1;
}
}
bzero(sql_cmd, sizeof(sql_cmd));
strcpy(sql_cmd, "COMMIT;");
sqlite3_exec(db, sql_cmd, NULL, NULL, &errmsg);
time_t tm2;
time(&tm2);
printf("spend time %lu s\n", tm2 - tm);
char *str2 = "#quit";
while (1)
{
char str1[56] = {0};
printf("input a word\n");
fflush(stdout);
fgets(str1, sizeof(str1), stdin);
str1[strlen(str1) - 1] = '\0';
if (strcmp(str1, str2) == 0)
{
break;
}
char str3[256] = {0};
bzero(str3, sizeof(str3));
sprintf(str3, "select *from user3 where word = '%s';", str1);
int flag = 0;
int ret = sqlite3_exec(db, str3, callback, &flag, &errmsg);
if (SQLITE_OK != ret)
{
fprintf(stderr, "sqlite3_exec [%s] [%s] \n", sql_cmd, errmsg);
sqlite3_free(errmsg);
sqlite3_close(db);
return 1;
}
if(flag==0)
{
printf("do not find a word\n");
}
}
sqlite3_close(db);
return 0;
}
html
html
01ser
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <sqlite3.h>
typedef struct sockaddr *(SA);
typedef enum
{
FILE_HTML,FILE_PNG,FILE_JPG
}FILE_TYPE;
int show(void *arg, int col, char **result, char **title)
{
printf("%s %s\n", result[1], result[2]);
*(int *)arg = 1;
printf("show_arg=%d\n", *(int *)arg);
return 0;
}
int send_head(int conn, char *filename,FILE_TYPE type)
{
char *http_cmd[5] = {NULL};
struct stat st;
int ret = stat(filename, &st);
if (-1 == ret)
{
fprintf(stderr, "send_head %s filename :%s\n", strerror(errno), filename);
return 1;
}
char buf[512] = {0};
http_cmd[0] = "HTTP/1.1 200 OK\r\n";
http_cmd[1] = "Date: Sat, 08 Feb 2025 09:13:29 GMT\r\n";
http_cmd[2] = "Content-Type: text/html;charset=utf-8\r\n";
switch(type)
{
case FILE_HTML:
http_cmd[2] = "Content-Type: text/html;charset=utf-8\r\n";
break;
case FILE_PNG:
http_cmd[2] = "content-type: image/png\r\n";
break;
case FILE_JPG:
http_cmd[2] = "content-type: image/jpeg\r\n";
break;
default:
http_cmd[2] = "Content-Type: text/html;charset=utf-8\r\n";
break;
}
http_cmd[3] = "Server: mywebser\r\n";
http_cmd[4] = buf;
sprintf(http_cmd[4], "Content-Length: %lu\r\n\r\n", st.st_size);
int i = 0;
for (i = 0; i < 5; i++)
{
send(conn, http_cmd[i], strlen(http_cmd[i]), 0);
}
return 0;
}
int send_file(int conn, char *filename,FILE_TYPE type)
{
send_head(conn, filename,type);
int fd = open(filename, O_RDONLY);
if (-1 == fd)
{
perror("open");
return 1;
}
char buf[4096] = {0};
while (1)
{
bzero(buf, sizeof(buf));
int rd_ret = read(fd, buf, sizeof(buf));
if (rd_ret <= 0)
{
break;
}
send(conn, buf, rd_ret, 0);
}
return 0;
}
int main(int argc, char **argv)
{
sqlite3 *db = NULL;
char *errmsg = NULL;
int ret = sqlite3_open("/home/linux/sec5_html/1.db", &db);
if (SQLITE_OK != ret)
{
fprintf(stderr, "sqlite3_open %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
int listfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == listfd)
{
perror("socket");
exit(1);
}
int on = 1;
setsockopt(listfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
setsockopt(listfd, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on));
struct sockaddr_in ser, cli;
ser.sin_family = AF_INET;
ser.sin_port = htons(80);
ser.sin_addr.s_addr = INADDR_ANY;
ret = bind(listfd, (SA)&ser, sizeof(ser));
if (-1 == ret)
{
perror("bind");
return 1;
}
listen(listfd, 3);
socklen_t len = sizeof(cli);
while (1)
{
int conn = accept(listfd, (SA)&cli, &len);
if (-1 == conn)
{
perror("accept");
close(conn);
continue;
}
char buf[2048] = {0};
int rd_ret = recv(conn, buf, sizeof(buf), 0);
if (rd_ret <= 0)
{
close(conn);
continue;
}
printf("%s\n", buf);
char *method = NULL;
char *url = NULL;
char *ver = NULL;
method = strtok(buf, " ");
url = strtok(NULL, " ");
ver = strtok(NULL, "\r");
if (0 == strcmp(url, "/"))
{
send_file(conn, "./03.html",FILE_HTML);
}
if (0 == strncmp(url, "/login", 6))
{
char *name = NULL;
char *pw = NULL;
char *end = NULL;
name = strchr(url, '=');
name += 1;
end = strchr(name, '&');
*end = '\0';
pw = strchr(end + 1, '=');
pw += 1;
while (1)
{
int flag = 0;
char sql_cmd[1024] = {0};
sprintf(sql_cmd, "select * from dict where word=\"%s\" and passport=\"%s\";", name, pw);
ret = sqlite3_exec(db, sql_cmd, show, &flag, &errmsg);
printf("name= %s pw=%s",name,pw);
printf("sql_cmd=%s\n",sql_cmd);
fflush(stdout);
if (SQLITE_OK != ret)
{
fprintf(stderr, "sqlite3_exec [%s] [%s] \n", sql_cmd, errmsg);
sqlite3_free(errmsg);
sqlite3_close(db);
return 1;
}
printf("---------------flag=%d\n", flag);
if (flag == 1)
{
send_file(conn, "./01.html",FILE_HTML);
printf("flag=%d\n", flag);
fflush(stdout);
break;
}
if (flag == 0)
{
send_file(conn, "./04.html",FILE_HTML);
printf("flag=%d\n", flag);
fflush(stdout);
break;
}
}
}
if (0 == strcmp(url, "/03.html"))
{
send_file(conn, "./03.html",FILE_HTML);
}
if (0 == strcmp(url, "/favicon.ico"))
{
send_file(conn, "./jd.png",FILE_PNG);
}
if(strlen(url)>=6&&strcmp(&url[strlen(url)-3],"png")==0)
{
send_file(conn,url+1,FILE_PNG);
}
if(strlen(url)>=6&&strcmp(&url[strlen(url)-3],"jpg")==0)
{
send_file(conn,url+1,FILE_JPG);
}
close(conn);
}
close(listfd);
return 0;
}
02ser
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include<sqlite3.h>
typedef struct sockaddr *(SA);
int show(void *arg, int col, char **result, char **title)
{
printf("%s %s\n",result[1],result[2]);
*(int*)arg = 1;
return 0;
}
int send_head(int conn, char *filename)
{
char *http_cmd[5] = {NULL};
struct stat st;
int ret = stat(filename, &st);
if (-1 == ret)
{
fprintf(stderr, "send_head %s filename :%s\n", strerror(errno), filename);
return 1;
}
char buf[512] = {0};
http_cmd[0] = "HTTP/1.1 200 OK\r\n";
http_cmd[1] = "Date: Sat, 08 Feb 2025 09:13:29 GMT\r\n";
http_cmd[2] = "Content-Type: text/html;charset=utf-8\r\n";
http_cmd[3] = "Server: mywebser\r\n";
http_cmd[4] = buf;
sprintf(http_cmd[4], "Content-Length: %lu\r\n\r\n", st.st_size);
int i = 0;
for (i = 0; i < 5; i++)
{
send(conn, http_cmd[i], strlen(http_cmd[i]), 0);
}
return 0;
}
int send_file(int conn, char *filename)
{
send_head(conn, filename);
int fd = open(filename, O_RDONLY);
if (-1 == fd)
{
perror("open");
return 1;
}
char buf[4096] = {0};
while (1)
{
bzero(buf, sizeof(buf));
int rd_ret = read(fd, buf, sizeof(buf));
if (rd_ret <= 0)
{
break;
}
send(conn, buf, rd_ret, 0);
}
return 0;
}
int main(int argc, char **argv)
{
sqlite3 *db = NULL;
char *errmsg = NULL;
int ret = sqlite3_open("/home/linux/sec5_html/1.db", &db);
if (SQLITE_OK != ret)
{
fprintf(stderr, "sqlite3_open %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
int listfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == listfd)
{
perror("socket");
exit(1);
}
int on = 1;
setsockopt(listfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
setsockopt(listfd, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on));
struct sockaddr_in ser, cli;
ser.sin_family = AF_INET;
ser.sin_port = htons(80);
ser.sin_addr.s_addr = INADDR_ANY;
int ret = bind(listfd, (SA)&ser, sizeof(ser));
if (-1 == ret)
{
perror("bind");
return 1;
}
listen(listfd, 3);
socklen_t len = sizeof(cli);
while (1)
{
int conn = accept(listfd, (SA)&cli, &len);
if (-1 == conn)
{
perror("accept");
close(conn);
continue;
}
char buf[2048] = {0};
int rd_ret = recv(conn, buf, sizeof(buf), 0);
if (rd_ret <= 0)
{
close(conn);
continue;
}
printf("%s\n", buf);
char *method = NULL;
char *url = NULL;
char *ver = NULL;
method = strtok(buf, " ");
url = strtok(NULL, " ");
ver = strtok(NULL, "\r");
if (0 == strcmp(url, "/"))
{
send_file(conn, "./03.html");
}
if (0 == strncmp(url, "/login", 6))
{
char *name = NULL;
char *pw = NULL;
char *end = NULL;
name = strchr(url, '=');
name += 1;
end = strchr(name, '&');
*end = '\0';
pw = strchr(end + 1, '=');
pw += 1;
if (0 == strcmp(name, "zhangsan") && 0 == strcmp(pw, "123"))
{
send_file(conn, "./01.html");
}
else
{
send_file(conn, "./04.html");
}
}
if (0 == strcmp(url, "/03.html"))
{
send_file(conn, "./03.html");
}
if(strlen(url)>=6&&strcmp(&url[strlen(url)-3],"png")==0)
{
send_file(conn,url+1);
}
if(strlen(url)>=6&&strcmp(&url[strlen(url)-3],"jpg")==0)
{
send_file(conn,url+1);
}
close(conn);
}
close(listfd);
return 0;
}
sql_work
#include <sqlite3.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
int show(void *arg, int col, char **result, char **title)
{
printf("%s %s\n",result[1],result[2]);
*(int*)arg = 1;
return 0;
}
int main(int argc, char **argv) {
sqlite3 *db = NULL;
char *errmsg = NULL;
int ret = sqlite3_open("/home/linux/20241230cd/sec5_db/1.db", &db);
if (SQLITE_OK != ret) {
fprintf(stderr, "sqlite3_open %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
char sql_cmd[1024] = "drop table dict;";
sqlite3_exec(db, sql_cmd, NULL, NULL, &errmsg);
bzero(sql_cmd, sizeof(sql_cmd));
strcpy(sql_cmd,
"create table dict(id INTEGER PRIMARY KEY ASC,word char ,mean text);");
sqlite3_exec(db, sql_cmd, NULL, NULL, &errmsg);
time_t tm;
time(&tm);
bzero(sql_cmd, sizeof(sql_cmd));
strcpy(sql_cmd,
"BEGIN TRANSACTION;");
sqlite3_exec(db, sql_cmd, NULL, NULL, &errmsg);
FILE *fp = fopen("/home/linux/dict.txt", "r");
if (NULL == fp) {
perror("fopen");
return 1;
}
while (1)
{
char linebuf[1024] = {0};
if (NULL == fgets(linebuf, sizeof(linebuf), fp))
{
break;
}
char *word = strtok(linebuf, " ");
char *mean = strtok(NULL, "\r");
bzero(sql_cmd, sizeof(sql_cmd));
sprintf(sql_cmd, "insert into dict values(NULL,\"%s\",\"%s\");", word,
mean);
ret = sqlite3_exec(db, sql_cmd, NULL, NULL, &errmsg);
if (SQLITE_OK != ret) {
fprintf(stderr, "sqlite3_exec [%s] [%s] \n", sql_cmd, errmsg);
sqlite3_free(errmsg);
sqlite3_close(db);
return 1;
}
}
bzero(sql_cmd, sizeof(sql_cmd));
strcpy(sql_cmd,
"COMMIT;");
sqlite3_exec(db, sql_cmd, NULL, NULL, &errmsg);
time_t tm2;
time(&tm2);
printf("spend time %lu s\n",tm2-tm);
while(1)
{
printf("input word:\n");
char want_word[50]={0};
fgets(want_word,sizeof(want_word),stdin);
want_word[strlen(want_word)-1]='\0';
if(0==strcmp(want_word,"#quit"))
{
break;
}
bzero(sql_cmd, sizeof(sql_cmd));
sprintf(sql_cmd, "select * from dict where word=\"%s\";",want_word);
int flag = 0 ;
ret = sqlite3_exec(db, sql_cmd, show, &flag, &errmsg);
if (SQLITE_OK != ret) {
fprintf(stderr, "sqlite3_exec [%s] [%s] \n", sql_cmd, errmsg);
sqlite3_free(errmsg);
sqlite3_close(db);
return 1;
}
if(0 == flag)
{
printf("cant find word, %s\n",want_word);
}
}
sqlite3_close(db);
return 0;
}
html_ser
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/stat.h>
#include <time.h>
typedef struct sockaddr *(SA);
typedef enum {FILE_HTML,FILE_PNG,FILE_JPG}FILE_TYPE;
int send_head(int conn, char *filename,FILE_TYPE type) {
char *http_cmd[5] = {NULL};
struct stat st;
int ret = stat(filename, &st);
if (-1 == ret) {
fprintf(stderr, "send_head %s filename :%s\n", strerror(errno), filename);
return 1;
}
char buf[512] = {0};
http_cmd[0] = "HTTP/1.1 200 OK\r\n";
http_cmd[1] = "Date: Sat, 08 Feb 2025 09:13:29 GMT\r\n";
switch (type)
{
case FILE_HTML:
http_cmd[2] = "Content-Type: text/html;charset=utf-8\r\n";
break;
case FILE_JPG:
http_cmd[2] = "content-type: image/jpeg\r\n";
break;
case FILE_PNG:
http_cmd[2] = "content-type: image/png\r\n";
break;
default:
http_cmd[2] = "Content-Type: text/html;charset=utf-8\r\n";
break;
}
http_cmd[3] = "Server: mywebser\r\n";
http_cmd[4] = buf;
sprintf(http_cmd[4], "Content-Length: %lu\r\n\r\n", st.st_size);
int i = 0;
for (i = 0; i < 5; i++) {
send(conn, http_cmd[i], strlen(http_cmd[i]), 0);
}
return 0;
}
int send_file(int conn, char *filename,FILE_TYPE type) {
send_head(conn, filename,type);
int fd = open(filename, O_RDONLY);
if (-1 == fd) {
perror("open");
return 1;
}
char buf[4096] = {0};
while (1) {
bzero(buf, sizeof(buf));
int rd_ret = read(fd, buf, sizeof(buf));
if (rd_ret <= 0) {
break;
}
send(conn, buf, rd_ret, 0);
}
return 0;
}
int main(int argc, char **argv) {
int listfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == listfd) {
perror("socket");
exit(1);
}
struct sockaddr_in ser, cli;
ser.sin_family = AF_INET;
ser.sin_port = htons(80);
ser.sin_addr.s_addr = INADDR_ANY;
int on = 1;
setsockopt(listfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
setsockopt(listfd, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on));
int ret = bind(listfd, (SA)&ser, sizeof(ser));
if (-1 == ret) {
perror("bind");
return 1;
}
listen(listfd, 3);
socklen_t len = sizeof(cli);
while (1) {
int conn = accept(listfd, (SA)&cli, &len);
if (-1 == conn) {
perror("accept");
close(conn);
continue;
}
char buf[2048] = {0};
int rd_ret = recv(conn, buf, sizeof(buf), 0);
if (rd_ret <= 0) {
close(conn);
continue;
}
printf("%s\n", buf);
char *method = NULL;
char *url = NULL;
char *ver = NULL;
method = strtok(buf, " ");
url = strtok(NULL, " ");
ver = strtok(NULL, "\r");
if (0 == strcmp(url, "/")) {
send_file(conn, "./03.html",FILE_HTML);
}
else if (0 == strncmp(url, "/login", 6)) {
char *name = NULL;
char *pw = NULL;
char *end = NULL;
name = strchr(url, '=');
name += 1;
end = strchr(name, '&');
*end = '\0';
pw = strchr(end + 1, '=');
pw += 1;
if (0 == strcmp(name, "zhangsan") && 0 == strcmp(pw, "123")) {
send_file(conn, "./01.html",FILE_HTML);
} else {
send_file(conn, "./04.html",FILE_HTML);
}
}
else if (strlen(url)>=6 && 0 == strcmp(&url[strlen(url)-3], "jpg") )
{
send_file(conn, url+1,FILE_JPG);
}
else if (strlen(url)>=6 && 0 == strcmp(&url[strlen(url)-3], "png") )
{
send_file(conn, url+1,FILE_PNG);
}
close(conn);
}
close(listfd);
return 0;
}
03.html
<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8"> <!-- 设置字符集防止乱码 -->
<title>登录页面</title>
<style>
body {
background-color: #E6E6FA;
}
</style>
</head>
<body>
<form action='http://www.baidu.com'>
用户名:<input type='text' name='username' required='required' placeholder='请输入qq的账号'>
密码: <input type='password' name='userpw' required='required' placeholder='请输入qq的密码'>
<input type='submit'>
</form>
</body>
</html>
webser
06ser
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <strings.h>
#include <sqlite3.h>
#define BURSIZE 2048
int hex2dec(char c);
char dec2hex(short int c);
void urlencode(char url[]);
void urldecode(char url[]);
typedef struct sockaddr *(SA);
typedef enum
{
FILE_HTML,
FILE_PNG,
FILE_JPG
} FILE_TYPE;
int show(void *arg, int col, char **result, char **title)
{
printf("%s %s\n", result[1], result[2]);
*(int *)arg = 1;
printf("show_arg=%d\n", *(int *)arg);
return 0;
}
int show2(void *arg, int col, char **result, char **title)
{
strcat(arg, result[0]);
((char *)arg)[strlen(arg)] = '\n';
return 0;
}
int show3(void *arg, int col, char **result, char **title)
{
int n = 0;
for (n = 0; n < col; n++)
{
char buf[128] = {0};
sprintf(buf, "%s:%s", title[n], result[n]);
strcat(arg, buf);
strcat(arg, "\n");
}
return 0;
}
int show5(void *arg, int col, char **result, char **title)
{
strcat(arg, result[0]);
return 0;
}
int show6(void *arg, int col, char **result, char **title)
{
for (int n = 0; n < col; n++)
{
strcat(arg, result[0]);
((char *)arg)[strlen(arg)] = '\n';
}
printf("----------------\n");
return 0;
}
int send_head(int conn, char *filename, FILE_TYPE type)
{
char *http_cmd[5] = {NULL};
struct stat st;
int ret = stat(filename, &st);
if (-1 == ret)
{
fprintf(stderr, "send_head %s filename :%s\n", strerror(errno), filename);
return 1;
}
char buf[512] = {0};
http_cmd[0] = "HTTP/1.1 200 OK\r\n";
http_cmd[1] = "Date: Sat, 08 Feb 2025 09:13:29 GMT\r\n";
http_cmd[2] = "Content-Type: text/html;charset=utf-8\r\n";
switch (type)
{
case FILE_HTML:
http_cmd[2] = "Content-Type: text/html;charset=utf-8\r\n";
break;
case FILE_PNG:
http_cmd[2] = "content-type: image/png\r\n";
break;
case FILE_JPG:
http_cmd[2] = "content-type: image/jpeg\r\n";
break;
default:
http_cmd[2] = "Content-Type: text/html;charset=utf-8\r\n";
break;
}
http_cmd[3] = "Server: mywebser\r\n";
http_cmd[4] = buf;
sprintf(http_cmd[4], "Content-Length: %lu\r\n\r\n", st.st_size);
int i = 0;
for (i = 0; i < 5; i++)
{
send(conn, http_cmd[i], strlen(http_cmd[i]), 0);
}
return 0;
}
int send_file(int conn, char *filename, FILE_TYPE type)
{
send_head(conn, filename, type);
int fd = open(filename, O_RDONLY);
if (-1 == fd)
{
perror("open");
return 1;
}
char buf[4096] = {0};
while (1)
{
bzero(buf, sizeof(buf));
int rd_ret = read(fd, buf, sizeof(buf));
if (rd_ret <= 0)
{
break;
}
send(conn, buf, rd_ret, 0);
}
return 0;
}
int main(int argc, char **argv)
{
sqlite3 *db = NULL;
char *errmsg = NULL;
int ret = sqlite3_open("/home/linux/sec5_webser/1.db", &db);
if (SQLITE_OK != ret)
{
fprintf(stderr, "sqlite3_open %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
sqlite3 *db2 = NULL;
char *errmsg2 = NULL;
int ret2 = sqlite3_open("/home/linux/sec5_webser/componet.db", &db2);
if (SQLITE_OK != ret2)
{
fprintf(stderr, "sqlite3_open %s\n", sqlite3_errmsg(db2));
sqlite3_close(db2);
return 1;
}
int flag2 = 0;
char sql_cmd2[1024] = {0};
sprintf(sql_cmd2, "select goods_img from goods ;");
char *p_buf = (char *)malloc(1024 * 1024);
memset(p_buf, 0, sizeof(p_buf));
ret2 = sqlite3_exec(db2, sql_cmd2, show2, p_buf, &errmsg);
const char *pathbuf = "/home/linux/sec5_webser/picture.txt";
int fd_buf = open(pathbuf, O_WRONLY | O_CREAT | O_TRUNC, 0666);
write(fd_buf, p_buf, strlen(p_buf));
printf("%s\n", p_buf);
const char *pathbuf_html12 = "/home/linux/sec5_webser/012.html";
int fd_012html = open(pathbuf_html12, O_RDONLY | O_CREAT, 0666);
const char *pathbuf_html11 = "/home/linux/sec5_webser/011.html";
int fd_011html = open(pathbuf_html11, O_WRONLY | O_CREAT | O_TRUNC, 0666);
char *p_buf011html = (char *)malloc(1024 * 1024);
memset(p_buf011html, 0, sizeof(p_buf011html));
while (1)
{
int longsize = read(fd_012html, p_buf011html, sizeof(p_buf011html));
write(fd_011html, p_buf011html, longsize);
if (longsize <= 0)
{
break;
}
}
memset(p_buf011html, 0, sizeof(p_buf011html));
char *p_picture = strtok(p_buf, "\n");
int n = 0;
char buf_picture[50][256] = {0};
while (1)
{
sprintf(buf_picture[n], "%s", p_picture);
char buf1[128] = {0};
int t = n + 1;
char buf2[128] = {0};
sprintf(buf2, "06_%d.html", t);
if (n % 4 == 0)
{
sprintf(buf1, "<tr><td><a href=\"%s\"><img src=\"%s\" width=\"30%%\" height=\"30%%\"></a></td>", buf2, p_picture);
}
else if (n % 4 == 3)
{
sprintf(buf1, "<td><a href=\"%s\"><img src=\"%s\" width=\"30%%\" height=\"30%%\"></a></td></tr>", buf2, p_picture);
buf1[strlen(buf1)] = '\n';
}
else
{
sprintf(buf1, "<td><a href=\"%s\"><img src=\"%s\" width=\"30%%\" height=\"30%%\"></a></td>", buf2, p_picture);
}
p_picture = strtok(NULL, "\n");
if (p_picture == NULL && n % 4 != 3)
{
strcat(buf1, "</tr>");
write(fd_011html, buf1, strlen(buf1));
}
if (p_picture == NULL)
{
break;
}
strcat(p_buf011html, buf1);
write(fd_011html, buf1, strlen(buf1));
n++;
}
char *buf2 = "</table></body></html>";
write(fd_011html, buf2, strlen(buf2));
int listfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == listfd)
{
perror("socket");
exit(1);
}
int on = 1;
setsockopt(listfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
setsockopt(listfd, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on));
struct sockaddr_in ser, cli;
ser.sin_family = AF_INET;
ser.sin_port = htons(80);
ser.sin_addr.s_addr = INADDR_ANY;
ret = bind(listfd, (SA)&ser, sizeof(ser));
if (-1 == ret)
{
perror("bind");
return 1;
}
listen(listfd, 3);
socklen_t len = sizeof(cli);
while (1)
{
int conn = accept(listfd, (SA)&cli, &len);
if (-1 == conn)
{
perror("accept");
close(conn);
continue;
}
char buf[2048] = {0};
int rd_ret = recv(conn, buf, sizeof(buf), 0);
if (rd_ret <= 0)
{
close(conn);
continue;
}
printf("%s\n", buf);
char *method = NULL;
char *url = NULL;
char *ver = NULL;
method = strtok(buf, " ");
url = strtok(NULL, " ");
ver = strtok(NULL, "\r");
if (0 == strcmp(url, "/"))
{
send_file(conn, "./03.html", FILE_HTML);
}
if (0 == strncmp(url, "/login", 6))
{
char *name = NULL;
char *pw = NULL;
char *end = NULL;
name = strchr(url, '=');
name += 1;
end = strchr(name, '&');
*end = '\0';
pw = strchr(end + 1, '=');
pw += 1;
while (1)
{
int flag = 0;
char sql_cmd[1024] = {0};
sprintf(sql_cmd, "select * from dict where word=\"%s\" and passport=\"%s\";", name, pw);
ret = sqlite3_exec(db, sql_cmd, show, &flag, &errmsg);
printf("name= %s pw=%s", name, pw);
printf("sql_cmd=%s\n", sql_cmd);
fflush(stdout);
if (SQLITE_OK != ret)
{
fprintf(stderr, "sqlite3_exec [%s] [%s] \n", sql_cmd, errmsg);
sqlite3_free(errmsg);
sqlite3_close(db);
return 1;
}
printf("---------------flag=%d\n", flag);
if (flag == 1)
{
send_file(conn, "./011.html", FILE_HTML);
printf("flag=%d\n", flag);
fflush(stdout);
break;
}
if (flag == 0)
{
send_file(conn, "./04.html", FILE_HTML);
printf("flag=%d\n", flag);
fflush(stdout);
break;
}
}
}
if (0 == strncmp(url, "/zhuce", 6))
{
char *name = NULL;
char *pw = NULL;
char *end = NULL;
name = strchr(url, '=');
name += 1;
end = strchr(name, '&');
*end = '\0';
pw = strchr(end + 1, '=');
pw += 1;
char sql_cmd[1024] = {0};
sprintf(sql_cmd, "insert into dict values(\"%s\",\"%s\");", name, pw);
ret = sqlite3_exec(db, sql_cmd, NULL, NULL, &errmsg);
send_file(conn, "./03.html", FILE_HTML);
}
if (0 == strcmp(url, "/03.html"))
{
send_file(conn, "./03.html", FILE_HTML);
}
if (0 == strcmp(url, "/05.html"))
{
send_file(conn, "./05.html", FILE_HTML);
}
if (0 == strcmp(url, "/011.html"))
{
send_file(conn, "./011.html", FILE_HTML);
}
if (0 == strcmp(url, "/06.html"))
{
send_file(conn, "./06.html", FILE_HTML);
}
if (strlen(url) >= 10 && strcmp(&url[strlen(url) - 4], "html") == 0 && url[3] == '_')
{
const char *pathbuf_html12 = "/home/linux/sec5_webser/012.html";
int fd_012html = open(pathbuf_html12, O_RDONLY | O_CREAT, 0666);
const char *pathbuf_html13 = "/home/linux/sec5_webser/013.html";
int fd_013html = open(pathbuf_html13, O_WRONLY | O_CREAT | O_TRUNC, 0666);
char *p_buf013html = (char *)malloc(1024 * 1024);
memset(p_buf013html, 0, sizeof(p_buf013html));
while (1)
{
int longsize = read(fd_012html, p_buf013html, sizeof(p_buf013html));
write(fd_013html, p_buf013html, longsize);
if (longsize <= 0)
{
break;
}
}
char *p_num1 = strtok(url, "_");
char *p_num2 = strtok(NULL, ".");
int num = atoi(p_num2) - 1;
printf("num=%d---------\n", num);
bzero(sql_cmd2, sizeof(sql_cmd2));
sprintf(sql_cmd2, "select * from goods where goods_img=\"%s\";", buf_picture[num]);
printf("%s\n", sql_cmd2);
sqlite3 *db2 = NULL;
char *errmsg2 = NULL;
int ret2 = sqlite3_open("/home/linux/sec5_webser/componet.db", &db2);
if (SQLITE_OK != ret)
{
fprintf(stderr, "sqlite3_open %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
char *p_buf3 = (char *)malloc(1024 * 1024);
memset(p_buf3, 0, 1024 * 1024);
sqlite3_exec(db2, sql_cmd2, show3, p_buf3, &errmsg);
printf("%s\n", p_buf3);
char *p_content = strtok(p_buf3, "\n");
while (1)
{
char buf1[158] = {0};
sprintf(buf1, "<tr><td>%s</td></tr>", p_content);
write(fd_013html, buf1, strlen(buf1));
p_content = strtok(NULL, "\n");
if (p_content == NULL)
{
break;
}
}
char *buf2 = "</table></body></html>";
write(fd_013html, buf2, strlen(buf2));
send_file(conn, "./013.html", FILE_HTML);
free(p_buf3);
close(fd_013html);
}
if (0 == strncmp(url, "/sousuo", 7))
{
const char *pathbuf_html12 = "/home/linux/sec5_webser/012.html";
int fd_012html = open(pathbuf_html12, O_RDONLY | O_CREAT, 0666);
const char *pathbuf_html11 = "/home/linux/sec5_webser/015.html";
int fd_011html = open(pathbuf_html11, O_WRONLY | O_CREAT | O_TRUNC, 0666);
memset(p_buf011html, 0, sizeof(p_buf011html));
while (1)
{
int longsize = read(fd_012html, p_buf011html, sizeof(p_buf011html));
write(fd_011html, p_buf011html, longsize);
if (longsize <= 0)
{
break;
}
}
char *name = NULL;
name = strchr(url, '=');
name += 1;
char char_buf[56] = {0};
sprintf(char_buf, "%s", name);
urldecode(name);
printf("%s\n", name);
char sql_cmd[1024] = {0};
sprintf(sql_cmd2, "select goods_img from goods where goods_name=\"%s\";", name);
printf("%s\n",sql_cmd2);
char *p_buf = (char *)malloc(1024 * 1024);
memset(p_buf, 0, 1024 * 1024);
ret2 = sqlite3_exec(db2, sql_cmd2, show6, p_buf, &errmsg);
char *p = strtok(p_buf, "\n");
for (int n = 0; n < 10; n++)
{
printf("%s\n", buf_picture[n]);
}
while (1)
{
char buf1[128]={0};
char buf2[128]={0};
char buf3[128]={0};
int n = 0;
while (1)
{
if (strcmp(buf_picture[n], p)==0)
{
break;
}
n++;
if(n>=50)
{
break;
}
}
if(n<50)
{
sprintf(buf2,"06_%d.html",n);
sprintf(buf1, "<tr><td><a href=\"%s\"><img src=\"%s\" width=\"30%%\" height=\"30%%\"></a></td>", buf2, p);
write(fd_011html, buf1, strlen(buf1));
}
p = strtok(NULL, "\n");
if (p == NULL)
{
break;
}
}
char *buf4 = "</table></body></html>";
write(fd_011html, buf4, strlen(buf4));
send_file(conn, "./015.html", FILE_HTML);
free(p_buf);
}
if (0 == strcmp(url, "/favicon.ico"))
{
send_file(conn, "./jd.png", FILE_PNG);
}
if (strlen(url) >= 6 && strcmp(&url[strlen(url) - 3], "png") == 0)
{
send_file(conn, url + 1, FILE_PNG);
}
if (strlen(url) >= 6 && strcmp(&url[strlen(url) - 3], "jpg") == 0)
{
char buf5[56] = "./img";
buf5[strlen(buf5)] = '/';
strcat(buf5, url + 1);
printf("%s\n", buf5);
send_file(conn, buf5, FILE_JPG);
}
close(conn);
}
free(p_buf);
close(listfd);
return 0;
}
int hex2dec(char c)
{
if ('0' <= c && c <= '9')
{
return c - '0';
}
else if ('a' <= c && c <= 'f')
{
return c - 'a' + 10;
}
else if ('A' <= c && c <= 'F')
{
return c - 'A' + 10;
}
else
{
return -1;
}
}
char dec2hex(short int c)
{
if (0 <= c && c <= 9)
{
return c + '0';
}
else if (10 <= c && c <= 15)
{
return c + 'A' - 10;
}
else
{
return -1;
}
}
void urlencode(char url[])
{
int i = 0;
int len = strlen(url);
int res_len = 0;
char res[BURSIZE];
for (i = 0; i < len; ++i)
{
char c = url[i];
if (('0' <= c && c <= '9') ||
('a' <= c && c <= 'z') ||
('A' <= c && c <= 'Z') ||
c == '/' || c == '.')
{
res[res_len++] = c;
}
else
{
int j = (short int)c;
if (j < 0)
j += 256;
int i1, i0;
i1 = j / 16;
i0 = j - i1 * 16;
res[res_len++] = '%';
res[res_len++] = dec2hex(i1);
res[res_len++] = dec2hex(i0);
}
}
res[res_len] = '\0';
strcpy(url, res);
}
void urldecode(char url[])
{
int i = 0;
int len = strlen(url);
int res_len = 0;
char res[BURSIZE];
for (i = 0; i < len; ++i)
{
char c = url[i];
if (c != '%')
{
res[res_len++] = c;
}
else
{
char c1 = url[++i];
char c0 = url[++i];
int num = 0;
num = hex2dec(c1) * 16 + hex2dec(c0);
res[res_len++] = num;
}
}
res[res_len] = '\0';
strcpy(url, res);
}
012.html
<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8"> <!-- 设置字符集防止乱码 -->
<title>登录页面</title>
<style>
body {
background-color: #E6E6FA;
}
</style>
</head>
<body>
<form action='/sousuo'>
搜索:<input type='text' name='username' required='required' placeholder='请输入你想找的东西'>
<input type='submit'>
</form>
<table>
<tr><td><a href="03.html"><img src="jd.png"></a></td><td><a href="03.html"><img src="jd.png"></a></td><td><a href="03.html"><img src="jd.png"></a></td></tr>
011.html
<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8"> <!-- 设置字符集防止乱码 -->
<title>登录页面</title>
<style>
body {
background-color: #E6E6FA;
}
</style>
</head>
<body>
<form action='/sousuo'>
搜索:<input type='text' name='username' required='required' placeholder='请输入你想找的东西'>
<input type='submit'>
</form>
<table>
<tr><td><a href="03.html"><img src="jd.png"></a></td><td><a href="03.html"><img src="jd.png"></a></td><td><a href="03.html"><img src="jd.png"></a></td></tr>
<tr><td><a href="06_1.html"><img src="EE69FB1398EECEEAF563D056C354584B.jpg" width="30%" height="30%"></a></td><td><a href="06_2.html"><img src="AF9D715B6FD6C4AF49E9A7B67DCDC457.jpg" width="30%" height="30%"></a></td><td><a href="06_3.html"><img src="0625E70AB256CA970EDA9B88FF38BB43.jpg" width="30%" height="30%"></a></td><td><a href="06_4.html"><img src="2761299595A268039EEDD14AEF6FECCF.jpg" width="30%" height="30%"></a></td></tr>
<tr><td><a href="06_5.html"><img src="74FA83969A438990B07FA0986BBCEA93.jpg" width="30%" height="30%"></a></td><td><a href="06_6.html"><img src="3760AFA1A19FA9E6EAF700E3B0C69C26.jpg" width="30%" height="30%"></a></td><td><a href="06_7.html"><img src="D04158E585B9DED4D1112DD363755F84.jpg" width="30%" height="30%"></a></td><td><a href="06_8.html"><img src="D0D2B72ACF1C48D7772427D35EB3125D.jpg" width="30%" height="30%"></a></td></tr>
<tr><td><a href="06_9.html"><img src="1EB4D9C2547FDCC160797998C2DC26A2.jpg" width="30%" height="30%"></a></td><td><a href="06_10.html"><img src="1A4405F7B988E42522DDCE882CB9BDDF.jpg" width="30%" height="30%"></a></td><td><a href="06_11.html"><img src="BC40F98482E97BA19A4574013DBE1A12.jpg" width="30%" height="30%"></a></td><td><a href="06_12.html"><img src="933168405AFD2AC717EA522CDDF3C2B0.jpg" width="30%" height="30%"></a></td></tr>
<tr><td><a href="06_13.html"><img src="7D869745A3AE2C0669E421CDC4E67FF7.jpg" width="30%" height="30%"></a></td><td><a href="06_14.html"><img src="0640573D31428DCEBE8F7BEC33F1F557.jpg" width="30%" height="30%"></a></td><td><a href="06_15.html"><img src="F3868D33D8461C614059112042ADDB88.jpg" width="30%" height="30%"></a></td><td><a href="06_16.html"><img src="F1C066E112046940148D873DF2785F53.jpg" width="30%" height="30%"></a></td></tr>
<tr><td><a href="06_17.html"><img src="D3008A398FA06367EA3460DD1D7FAEF0.jpg" width="30%" height="30%"></a></td><td><a href="06_18.html"><img src="B9E47B39A8EA28C91590723E749C4A7E.jpg" width="30%" height="30%"></a></td></tr></table></body></html>
013.html
<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8"> <!-- 设置字符集防止乱码 -->
<title>登录页面</title>
<style>
body {
background-color: #E6E6FA;
}
</style>
</head>
<body>
<form action='/sousuo'>
搜索:<input type='text' name='username' required='required' placeholder='请输入你想找的东西'>
<input type='submit'>
</form>
<table>
<tr><td><a href="03.html"><img src="jd.png"></a></td><td><a href="03.html"><img src="jd.png"></a></td><td><a href="03.html"><img src="jd.png"></a></td></tr>
<tr><td>goods_id:2</td></tr><tr><td>cat_id:1</td></tr><tr><td>goods_sn:C7493865</td></tr><tr><td>goods_name:铝电解电容器</td></tr><tr><td>good_spec:25V1000uF</td></tr><tr><td>goods_number:20</td></tr><tr><td>goods_weight:2.0158克(g)</td></tr><tr><td>market_price:0.375535</td></tr><tr><td>shop_price:0.475095</td></tr><tr><td>keywords:HRK(鸿瑞凯)</td></tr><tr><td>goods_desc:插件,D10xL17mm</td></tr><tr><td>goods_img:AF9D715B6FD6C4AF49E9A7B67DCDC457.jpg</td></tr></table></body></html>
015.html
<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8"> <!-- 设置字符集防止乱码 -->
<title>登录页面</title>
<style>
body {
background-color: #E6E6FA;
}
</style>
</head>
<body>
<form action='/sousuo'>
搜索:<input type='text' name='username' required='required' placeholder='请输入你想找的东西'>
<input type='submit'>
</form>
<table>
<tr><td><a href="03.html"><img src="jd.png"></a></td><td><a href="03.html"><img src="jd.png"></a></td><td><a href="03.html"><img src="jd.png"></a></td></tr>
<tr><td><a href="06_4.html"><img src="74FA83969A438990B07FA0986BBCEA93.jpg" width="30%" height="30%"></a></td><tr><td><a href="06_5.html"><img src="3760AFA1A19FA9E6EAF700E3B0C69C26.jpg" width="30%" height="30%"></a></td></table></body></html>
03.html
<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8"> <!-- 设置字符集防止乱码 -->
<title>登录页面</title>
<style>
body {
background-color: #E6E6FA;
}
</style>
</head>
<body>
<form action='/login'>
用户名:<input type='text' name='username' required='required' placeholder='请输入qq的账号'>
密码: <input type='password' name='userpw' required='required' placeholder='请输入qq的密码'>
<input type='submit'>
<br><a href="05.html">注册</a>
</form>
</body>
</html>
04.html
<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8"> <!-- 设置字符集防止乱码 -->
<style>
body {
background-color: #E6E6FA;
}
</style>
</head>
<body>
<br><h1 align="center">input error</h1>
<!----<br><a href="/">return</a>--->
<br><a href="03.html">return</a>
</body>
</html>
05.html
<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8"> <!-- 设置字符集防止乱码 -->
<style>
body {
background-color: #E6E6FA;
}
</style>
</head>
<body>
<form action='/zhuce'>
注册用户名:<input type='text' name='username' required='required' placeholder='请输入qq的账号'>
注册密码: <input type='password' name='userpw' required='required' placeholder='请输入qq的密码'>
<input type='submit'>
</form>
</body>
</html>
06.html
<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8"> <!-- 设置字符集防止乱码 -->
<style>
body {
background-color: #E6E6FA;
}
</style>
</head>
<body>
<br><h1 align="center">input error</h1>
<!----<br><a href="/">return</a>--->
<br><a href="03.html">return</a>
</body>
</html>