首页 » 网站建设 » tinyhttpdphp技巧_C开源代码进修 Tinyhttpd

tinyhttpdphp技巧_C开源代码进修 Tinyhttpd

duote123 2024-11-19 0

扫一扫用手机浏览

文章目录 [+]

官网: http://tinyhttpd.sourceforge.net/仓库地址:https://sourceforge.net/projects/tinyhttpd/码云镜像:https://gitee.com/mirrors/tinyhttpd?utm_source=alading&utm_campaign=repo

二、环境搭建

本文开拓环境:

tinyhttpdphp技巧_C开源代码进修 Tinyhttpd

MacOSClion1. 新建一个Clion C措辞项目2. 把tinyhttpd的源代码拖进项目

这时目录可能是这样的:

tinyhttpdphp技巧_C开源代码进修 Tinyhttpd
(图片来自网络侵删)

(把稳这里的cmake-build-debug后面编译才会有)

3. 这里不须要simpleclient.c,把它删除4. 编译

这时会天生 cmake-build-debug目录。

5. 把htdocs拷进 cmake-build-debug

6. 设置一下权限

把 index.html index2.html取消可实行权限

chmod -x index.htmlchmod -x index2.html127. 运行程序

点main上的绿色倒三角运行程序。

8. 不雅观察掌握台输出的端口号,在浏览器打开网址

随便输入一种颜色,点提交,页面显示提交的颜色。

三、代码解析1. 程序入口

int main(void) { int server_sock = -1; u_short port = 0; int client_sock = -1; struct sockaddr_in client_name; //这边要为socklen_t类型 socklen_t client_name_len = sizeof(client_name); pthread_t newthread; server_sock = startup(&port); printf("httpd running on port %d\n", port); while (1) { //接管要求,函数原型 //#include <sys/types.h> //#include <sys/socket.h> //int accept(int sockfd, struct sockaddr addr, socklen_t addrlen); client_sock = accept(server_sock, (struct sockaddr ) &client_name, &client_name_len); if (client_sock == -1) error_die("accept"); / accept_request(client_sock); / //每次收到要求,创建一个线程来处理接管到的要求 //把client_sock转成地址作为参数传入pthread_create if (pthread_create(&newthread, NULL, (void ) accept_request, (void ) (intptr_t) client_sock) != 0) perror("pthread_create"); } close(server_sock); return (0);}

在main里面有一个while循环,用来吸收tcp连接,吸收到往后建立新线程对要求进行处理。
这里要引用系统的socket.h。

u_short变量 port 是指定确当地端口号,0表示随机端口,也可以写一个固定值用来固定端口号。
pthread_create是c的建立新线程函数,下面是函数原型。
可以看到 accept_request是处理要求的核心函数。
client_sock会作为参数传入accept_request函数里。

#include <pthread.h>int pthread_create( pthread_t restrict tidp, //新创建的线程ID指向的内存单元。
const pthread_attr_t restrict attr, //线程属性,默认为NULL void (start_rtn)(void ), //新创建的线程从start_rtn函数的地址开始运行 void restrict arg //默认为NULL。
若上述函数须要参数,将参数放入构造中并将地址作为arg传入。
);
2. 要求解析 accept_request 函数

这里引用了比较主要的一个函数是get_line,它用来从 socket 里读取一行。
读取后会进行后面的处理。
首先判断是不是POST/GET要求,如果不是则返回 unimplemented。

//判断是Get还是Post if (strcasecmp(method, "GET") && strcasecmp(method, "POST")) { unimplemented(client); return; }

未处理的方法会输出错误信息:

//如果方法没有实现,就返回此信息void unimplemented(int client) { char buf[1024]; sprintf(buf, "HTTP/1.0 501 Method Not Implemented\r\n"); send(client, buf, strlen(buf), 0); sprintf(buf, SERVER_STRING); send(client, buf, strlen(buf), 0); sprintf(buf, "Content-Type: text/html\r\n"); send(client, buf, strlen(buf), 0); sprintf(buf, "\r\n"); send(client, buf, strlen(buf), 0); sprintf(buf, "<HTML><HEAD><TITLE>Method Not Implemented\r\n"); send(client, buf, strlen(buf), 0); sprintf(buf, "</TITLE></HEAD>\r\n"); send(client, buf, strlen(buf), 0); sprintf(buf, "<BODY><P>HTTP request method not supported.\r\n"); send(client, buf, strlen(buf), 0); sprintf(buf, "</BODY></HTML>\r\n"); send(client, buf, strlen(buf), 0);}

这里可以看到,输出html采取的是打印字符串、再发送tcp。

3. get要求处理

如果是get要求,会解析地址有没有带文件名,如果没带就设置默认 index.html 。

//路径 sprintf(path, "htdocs%s", url); //默认地址,解析到的路径如果为/,则自动加上index.html if (path[strlen(path) - 1] == '/') strcat(path, "index.html");4. 文件处理

得到文件名,利用stat函数判断文件是否存在,如果没有找到,就返回not_found。
如果文件存在,则判断文件是否有可实行权限,对付可实行程序调用 execute_cgi函数,非可实行程序则调用serve_file返回给客户端。

5.serve_file读取文件返回客户端

//如果不是CGI文件,直接读取文件返回给要求的http客户端void serve_file(int client, const char filename) { FILE resource = NULL; int numchars = 1; char buf[1024]; //默认字符 buf[0] = 'A'; buf[1] = '\0'; while ((numchars > 0) && strcmp("\n", buf)) / read & discard headers / numchars = get_line(client, buf, sizeof(buf)); resource = fopen(filename, "r"); if (resource == NULL) not_found(client); else { headers(client, filename); cat(client, resource); } fclose(resource);}

这个函数紧张读取文件,加上http头。
读取发送文件的函数:

//得到文件内容,发送void cat(int client, FILE resource) { char buf[1024]; fgets(buf, sizeof(buf), resource); //循环读 while (!feof(resource)) { send(client, buf, strlen(buf), 0); fgets(buf, sizeof(buf), resource); }}6.execute_cgi可实行程序

//从output管道读到子进程处理后的信息,然后send出去 while (read(cgi_output[0], &c, 1) > 0) send(client, &c, 1, 0);

标签:

相关文章

PHP实现文字转图片的代码与应用

图片处理技术在各个领域得到了广泛应用。在PHP编程中,文字转图片功能同样具有很高的实用价值。本文将针对PHP实现文字转图片的代码进...

网站建设 2025-03-02 阅读1 评论0

NAN0017探索新型纳米材料的奥秘与应用

纳米技术作为一门新兴的交叉学科,近年来在材料科学、生物医学、电子工程等领域取得了举世瞩目的成果。其中,NAN0017作为一种新型纳...

网站建设 2025-03-02 阅读5 评论0

L26368XO代码其背后的创新与突破

编程语言在各个领域发挥着越来越重要的作用。在众多编程语言中,L26368XO代码以其独特的优势,成为了业界关注的焦点。本文将深入剖...

网站建设 2025-03-02 阅读1 评论0

HTML字体背景打造个化网页设计的关键元素

网页设计已经成为现代网络传播的重要手段。在众多网页设计元素中,字体和背景的搭配尤为关键。本文将从HTML字体背景设置的角度,探讨其...

网站建设 2025-03-02 阅读1 评论0