前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tinyhttpd 剖析

tinyhttpd 剖析

作者头像
bear_fish
发布2018-09-20 10:55:20
8440
发布2018-09-20 10:55:20
举报

http://blog.csdn.net/jcjc918/article/details/42129311

http://techlog.cn/article/list/10182680 (图片来源)

    主要函数

     这是所有函数的声明:

[cpp] view plaincopy

  1. void accept_request(int);  
  2. void bad_request(int);  
  3. void cat(intFILE *);  
  4. void cannot_execute(int);  
  5. void error_die(const char *);  
  6. void execute_cgi(intconst char *, const char *, const char *);  
  7. int get_line(intchar *, int);  
  8. void headers(intconst char *);  
  9. void not_found(int);  
  10. void serve_file(intconst char *);  
  11. int startup(u_short *);  
  12. void unimplemented(int);  

     先简单地解释每个函数的作用:

     accept_request:  处理从套接字上监听到的一个 HTTP 请求,在这里可以很大一部分地体现服务器处理请求流程。

     bad_request: 返回给客户端这是个错误请求,HTTP 状态吗 400 BAD REQUEST.

     cat: 读取服务器上某个文件写到 socket 套接字。

     cannot_execute: 主要处理发生在执行 cgi 程序时出现的错误。

     error_die: 把错误信息写到 perror 并退出。

     execute_cgi: 运行 cgi 程序的处理,也是个主要函数。

     get_line: 读取套接字的一行,把回车换行等情况都统一为换行符结束。

     headers: 把 HTTP 响应的头部写到套接字。

     not_found: 主要处理找不到请求的文件时的情况。

     sever_file: 调用 cat 把服务器文件返回给浏览器。

     startup: 初始化 httpd 服务,包括建立套接字,绑定端口,进行监听等。

     unimplemented: 返回给浏览器表明收到的 HTTP 请求所用的 method 不被支持。

     建议源码阅读顺序: main -> startup -> accept_request -> execute_cgi, 通晓主要工作流程后再仔细把每个函数的源码看一看。

     工作流程

     (1) 服务器启动,在指定端口或随机选取端口绑定 httpd 服务。

     (2)收到一个 HTTP 请求时(其实就是 listen 的端口 accpet 的时候),派生一个线程运行 accept_request 函数。

     (3)取出 HTTP 请求中的 method (GET 或 POST) 和 url,。对于 GET 方法,如果有携带参数,则 query_string 指针指向 url 中 ? 后面的 GET 参数。

     (4) 格式化 url 到 path 数组,表示浏览器请求的服务器文件路径,在 tinyhttpd 中服务器文件是在 htdocs 文件夹下。当 url 以 / 结尾,或 url 是个目录,则默认在 path 中加上 index.html,表示访问主页。

     (5)如果文件路径合法,对于无参数的 GET 请求,直接输出服务器文件到浏览器,即用 HTTP 格式写到套接字上,跳到(10)。其他情况(带参数 GET,POST 方式,url 为可执行文件),则调用 excute_cgi 函数执行 cgi 脚本。

    (6)读取整个 HTTP 请求并丢弃,如果是 POST 则找出 Content-Length. 把 HTTP 200  状态码写到套接字。

    (7) 建立两个管道,cgi_input 和 cgi_output, 并 fork 一个进程。

    (8) 在子进程中,把 STDOUT 重定向到 cgi_outputt 的写入端,把 STDIN 重定向到 cgi_input 的读取端,关闭 cgi_input 的写入端 和 cgi_output 的读取端,设置 request_method 的环境变量,GET 的话设置 query_string 的环境变量,POST 的话设置 content_length 的环境变量,这些环境变量都是为了给 cgi 脚本调用,接着用 execl 运行 cgi 程序。

    (9) 在父进程中,关闭 cgi_input 的读取端 和 cgi_output 的写入端,如果 POST 的话,把 POST 数据写入 cgi_input,已被重定向到 STDIN,读取 cgi_output 的管道输出到客户端,该管道输入是 STDOUT。接着关闭所有管道,等待子进程结束。这一部分比较乱,见下图说明:

                                                                                                         图 1    管道初始状态

                                                                                                        图 2  管道最终状态 

    (10) 关闭与浏览器的连接,完成了一次 HTTP 请求与回应,因为 HTTP 是无连接的。

      注释版源码

      源码已写了注释,放在 Github: 这里

     懒得跳转的同学看下面....

[cpp] view plaincopy

  1. /* J. David's webserver */
  2. /* This is a simple webserver.
  3.  * Created November 1999 by J. David Blackstone.
  4.  * CSE 4344 (Network concepts), Prof. Zeigler
  5.  * University of Texas at Arlington
  6.  */
  7. /* This program compiles for Sparc Solaris 2.6.
  8.  * To compile for Linux:
  9.  *  1) Comment out the #include <pthread.h> line.
  10.  *  2) Comment out the line that defines the variable newthread.
  11.  *  3) Comment out the two lines that run pthread_create().
  12.  *  4) Uncomment the line that runs accept_request().
  13.  *  5) Remove -lsocket from the Makefile.
  14.  */
  15. #include <stdio.h>
  16. #include <sys/socket.h>
  17. #include <sys/types.h>
  18. #include <netinet/in.h>
  19. #include <arpa/inet.h>
  20. #include <unistd.h>
  21. #include <ctype.h>
  22. #include <strings.h>
  23. #include <string.h>
  24. #include <sys/stat.h>
  25. #include <pthread.h>
  26. #include <sys/wait.h>
  27. #include <stdlib.h>
  28. #define ISspace(x) isspace((int)(x))
  29. #define SERVER_STRING "Server: jdbhttpd/0.1.0\r\n"
  30. void accept_request(int);  
  31. void bad_request(int);  
  32. void cat(intFILE *);  
  33. void cannot_execute(int);  
  34. void error_die(const char *);  
  35. void execute_cgi(intconst char *, const char *, const char *);  
  36. int get_line(intchar *, int);  
  37. void headers(intconst char *);  
  38. void not_found(int);  
  39. void serve_file(intconst char *);  
  40. int startup(u_short *);  
  41. void unimplemented(int);  
  42. /**********************************************************************/
  43. /* A request has caused a call to accept() on the server port to
  44.  * return.  Process the request appropriately.
  45.  * Parameters: the socket connected to the client */
  46. /**********************************************************************/
  47. void accept_request(int client)  
  48. {  
  49. char buf[1024];  
  50. int numchars;  
  51. char method[255];  
  52. char url[255];  
  53. char path[512];  
  54. size_t i, j;  
  55. struct stat st;  
  56. int cgi = 0;      /* becomes true if server decides this is a CGI program */
  57. char *query_string = NULL;  
  58. /*得到请求的第一行*/
  59.     numchars = get_line(client, buf, sizeof(buf));  
  60.     i = 0; j = 0;  
  61. /*把客户端的请求方法存到 method 数组*/
  62. while (!ISspace(buf[j]) && (i < sizeof(method) - 1))  
  63.     {  
  64.         method[i] = buf[j];  
  65.         i++; j++;  
  66.     }  
  67.     method[i] = '\0';  
  68. /*如果既不是 GET 又不是 POST 则无法处理 */
  69. if (strcasecmp(method, "GET") && strcasecmp(method, "POST"))  
  70.     {  
  71.         unimplemented(client);  
  72. return;  
  73.     }  
  74. /* POST 的时候开启 cgi */
  75. if (strcasecmp(method, "POST") == 0)  
  76.         cgi = 1;  
  77. /*读取 url 地址*/
  78.     i = 0;  
  79. while (ISspace(buf[j]) && (j < sizeof(buf)))  
  80.         j++;  
  81. while (!ISspace(buf[j]) && (i < sizeof(url) - 1) && (j < sizeof(buf)))  
  82.     {  
  83. /*存下 url */
  84.         url[i] = buf[j];  
  85.         i++; j++;  
  86.     }  
  87.     url[i] = '\0';  
  88. /*处理 GET 方法*/
  89. if (strcasecmp(method, "GET") == 0)  
  90.     {  
  91. /* 待处理请求为 url */
  92.         query_string = url;  
  93. while ((*query_string != '?') && (*query_string != '\0'))  
  94.             query_string++;  
  95. /* GET 方法特点,? 后面为参数*/
  96. if (*query_string == '?')  
  97.         {  
  98. /*开启 cgi */
  99.             cgi = 1;  
  100.             *query_string = '\0';  
  101.             query_string++;  
  102.         }  
  103.     }  
  104. /*格式化 url 到 path 数组,html 文件都在 htdocs 中*/
  105.     sprintf(path, "htdocs%s", url);  
  106. /*默认情况为 index.html */
  107. if (path[strlen(path) - 1] == '/')  
  108.         strcat(path, "index.html");  
  109. /*根据路径找到对应文件 */
  110. if (stat(path, &st) == -1) {  
  111. /*把所有 headers 的信息都丢弃*/
  112. while ((numchars > 0) && strcmp("\n", buf))  /* read & discard headers */
  113.             numchars = get_line(client, buf, sizeof(buf));  
  114. /*回应客户端找不到*/
  115.         not_found(client);  
  116.     }  
  117. else
  118.     {  
  119. /*如果是个目录,则默认使用该目录下 index.html 文件*/
  120. if ((st.st_mode & S_IFMT) == S_IFDIR)  
  121.             strcat(path, "/index.html");  
  122. if ((st.st_mode & S_IXUSR) || (st.st_mode & S_IXGRP) || (st.st_mode & S_IXOTH)    )  
  123.           cgi = 1;  
  124. /*不是 cgi,直接把服务器文件返回,否则执行 cgi */
  125. if (!cgi)  
  126.           serve_file(client, path);  
  127. else
  128.           execute_cgi(client, path, method, query_string);  
  129.     }  
  130. /*断开与客户端的连接(HTTP 特点:无连接)*/
  131.     close(client);  
  132. }  
  133. /**********************************************************************/
  134. /* Inform the client that a request it has made has a problem.
  135.  * Parameters: client socket */
  136. /**********************************************************************/
  137. void bad_request(int client)  
  138. {  
  139. char buf[1024];  
  140. /*回应客户端错误的 HTTP 请求 */
  141.     sprintf(buf, "HTTP/1.0 400 BAD REQUEST\r\n");  
  142.     send(client, buf, sizeof(buf), 0);  
  143.     sprintf(buf, "Content-type: text/html\r\n");  
  144.     send(client, buf, sizeof(buf), 0);  
  145.     sprintf(buf, "\r\n");  
  146.     send(client, buf, sizeof(buf), 0);  
  147.     sprintf(buf, "<P>Your browser sent a bad request, ");  
  148.     send(client, buf, sizeof(buf), 0);  
  149.     sprintf(buf, "such as a POST without a Content-Length.\r\n");  
  150.     send(client, buf, sizeof(buf), 0);  
  151. }  
  152. /**********************************************************************/
  153. /* Put the entire contents of a file out on a socket.  This function
  154.  * is named after the UNIX "cat" command, because it might have been
  155.  * easier just to do something like pipe, fork, and exec("cat").
  156.  * Parameters: the client socket descriptor
  157.  *             FILE pointer for the file to cat */
  158. /**********************************************************************/
  159. void cat(int client, FILE *resource)  
  160. {  
  161. char buf[1024];  
  162. /*读取文件中的所有数据写到 socket */
  163.     fgets(buf, sizeof(buf), resource);  
  164. while (!feof(resource))  
  165.     {  
  166.         send(client, buf, strlen(buf), 0);  
  167.         fgets(buf, sizeof(buf), resource);  
  168.     }  
  169. }  
  170. /**********************************************************************/
  171. /* Inform the client that a CGI script could not be executed.
  172.  * Parameter: the client socket descriptor. */
  173. /**********************************************************************/
  174. void cannot_execute(int client)  
  175. {  
  176. char buf[1024];  
  177. /* 回应客户端 cgi 无法执行*/
  178.     sprintf(buf, "HTTP/1.0 500 Internal Server Error\r\n");  
  179.     send(client, buf, strlen(buf), 0);  
  180.     sprintf(buf, "Content-type: text/html\r\n");  
  181.     send(client, buf, strlen(buf), 0);  
  182.     sprintf(buf, "\r\n");  
  183.     send(client, buf, strlen(buf), 0);  
  184.     sprintf(buf, "<P>Error prohibited CGI execution.\r\n");  
  185.     send(client, buf, strlen(buf), 0);  
  186. }  
  187. /**********************************************************************/
  188. /* Print out an error message with perror() (for system errors; based
  189.  * on value of errno, which indicates system call errors) and exit the
  190.  * program indicating an error. */
  191. /**********************************************************************/
  192. void error_die(const char *sc)  
  193. {  
  194. /*出错信息处理 */
  195.     perror(sc);  
  196.     exit(1);  
  197. }  
  198. /**********************************************************************/
  199. /* Execute a CGI script.  Will need to set environment variables as
  200.  * appropriate.
  201.  * Parameters: client socket descriptor
  202.  *             path to the CGI script */
  203. /**********************************************************************/
  204. void execute_cgi(int client, const char *path, const char *method, const char *query_string)  
  205. {  
  206. char buf[1024];  
  207. int cgi_output[2];  
  208. int cgi_input[2];  
  209.     pid_t pid;  
  210. int status;  
  211. int i;  
  212. char c;  
  213. int numchars = 1;  
  214. int content_length = -1;  
  215.     buf[0] = 'A'; buf[1] = '\0';  
  216. if (strcasecmp(method, "GET") == 0)  
  217. /*把所有的 HTTP header 读取并丢弃*/
  218. while ((numchars > 0) && strcmp("\n", buf))  /* read & discard headers */
  219.             numchars = get_line(client, buf, sizeof(buf));  
  220. else /* POST */
  221.     {  
  222. /* 对 POST 的 HTTP 请求中找出 content_length */
  223.         numchars = get_line(client, buf, sizeof(buf));  
  224. while ((numchars > 0) && strcmp("\n", buf))  
  225.         {  
  226. /*利用 \0 进行分隔 */
  227.             buf[15] = '\0';  
  228. /* HTTP 请求的特点*/
  229. if (strcasecmp(buf, "Content-Length:") == 0)  
  230.                 content_length = atoi(&(buf[16]));  
  231.             numchars = get_line(client, buf, sizeof(buf));  
  232.         }  
  233. /*没有找到 content_length */
  234. if (content_length == -1) {  
  235. /*错误请求*/
  236.             bad_request(client);  
  237. return;  
  238.         }  
  239.     }  
  240. /* 正确,HTTP 状态码 200 */
  241.     sprintf(buf, "HTTP/1.0 200 OK\r\n");  
  242.     send(client, buf, strlen(buf), 0);  
  243. /* 建立管道*/
  244. if (pipe(cgi_output) < 0) {  
  245. /*错误处理*/
  246.         cannot_execute(client);  
  247. return;  
  248.     }  
  249. /*建立管道*/
  250. if (pipe(cgi_input) < 0) {  
  251. /*错误处理*/
  252.         cannot_execute(client);  
  253. return;  
  254.     }  
  255. if ((pid = fork()) < 0 ) {  
  256. /*错误处理*/
  257.         cannot_execute(client);  
  258. return;  
  259.     }  
  260. if (pid == 0)  /* child: CGI script */
  261.     {  
  262. char meth_env[255];  
  263. char query_env[255];  
  264. char length_env[255];  
  265. /* 把 STDOUT 重定向到 cgi_output 的写入端 */
  266.         dup2(cgi_output[1], 1);  
  267. /* 把 STDIN 重定向到 cgi_input 的读取端 */
  268.         dup2(cgi_input[0], 0);  
  269. /* 关闭 cgi_input 的写入端 和 cgi_output 的读取端 */
  270.         close(cgi_output[0]);  
  271.         close(cgi_input[1]);  
  272. /*设置 request_method 的环境变量*/
  273.         sprintf(meth_env, "REQUEST_METHOD=%s", method);  
  274.         putenv(meth_env);  
  275. if (strcasecmp(method, "GET") == 0) {  
  276. /*设置 query_string 的环境变量*/
  277.             sprintf(query_env, "QUERY_STRING=%s", query_string);  
  278.             putenv(query_env);  
  279.         }  
  280. else {   /* POST */
  281. /*设置 content_length 的环境变量*/
  282.             sprintf(length_env, "CONTENT_LENGTH=%d", content_length);  
  283.             putenv(length_env);  
  284.         }  
  285. /*用 execl 运行 cgi 程序*/
  286.         execl(path, path, NULL);  
  287.         exit(0);  
  288.     } else {    /* parent */
  289. /* 关闭 cgi_input 的读取端 和 cgi_output 的写入端 */
  290.         close(cgi_output[1]);  
  291.         close(cgi_input[0]);  
  292. if (strcasecmp(method, "POST") == 0)  
  293. /*接收 POST 过来的数据*/
  294. for (i = 0; i < content_length; i++) {  
  295.                 recv(client, &c, 1, 0);  
  296. /*把 POST 数据写入 cgi_input,现在重定向到 STDIN */
  297.                 write(cgi_input[1], &c, 1);  
  298.             }  
  299. /*读取 cgi_output 的管道输出到客户端,该管道输入是 STDOUT */
  300. while (read(cgi_output[0], &c, 1) > 0)  
  301.             send(client, &c, 1, 0);  
  302. /*关闭管道*/
  303.         close(cgi_output[0]);  
  304.         close(cgi_input[1]);  
  305. /*等待子进程*/
  306.         waitpid(pid, &status, 0);  
  307.     }  
  308. }  
  309. /**********************************************************************/
  310. /* Get a line from a socket, whether the line ends in a newline,
  311.  * carriage return, or a CRLF combination.  Terminates the string read
  312.  * with a null character.  If no newline indicator is found before the
  313.  * end of the buffer, the string is terminated with a null.  If any of
  314.  * the above three line terminators is read, the last character of the
  315.  * string will be a linefeed and the string will be terminated with a
  316.  * null character.
  317.  * Parameters: the socket descriptor
  318.  *             the buffer to save the data in
  319.  *             the size of the buffer
  320.  * Returns: the number of bytes stored (excluding null) */
  321. /**********************************************************************/
  322. int get_line(int sock, char *buf, int size)  
  323. {  
  324. int i = 0;  
  325. char c = '\0';  
  326. int n;  
  327. /*把终止条件统一为 \n 换行符,标准化 buf 数组*/
  328. while ((i < size - 1) && (c != '\n'))  
  329.     {  
  330. /*一次仅接收一个字节*/
  331.         n = recv(sock, &c, 1, 0);  
  332. /* DEBUG printf("%02X\n", c); */
  333. if (n > 0)  
  334.         {  
  335. /*收到 \r 则继续接收下个字节,因为换行符可能是 \r\n */
  336. if (c == '\r')  
  337.             {  
  338. /*使用 MSG_PEEK 标志使下一次读取依然可以得到这次读取的内容,可认为接收窗口不滑动*/
  339.                 n = recv(sock, &c, 1, MSG_PEEK);  
  340. /* DEBUG printf("%02X\n", c); */
  341. /*但如果是换行符则把它吸收掉*/
  342. if ((n > 0) && (c == '\n'))  
  343.                     recv(sock, &c, 1, 0);  
  344. else
  345.                     c = '\n';  
  346.             }  
  347. /*存到缓冲区*/
  348.             buf[i] = c;  
  349.             i++;  
  350.         }  
  351. else
  352.             c = '\n';  
  353.     }  
  354.     buf[i] = '\0';  
  355. /*返回 buf 数组大小*/
  356. return(i);  
  357. }  
  358. /**********************************************************************/
  359. /* Return the informational HTTP headers about a file. */
  360. /* Parameters: the socket to print the headers on
  361.  *             the name of the file */
  362. /**********************************************************************/
  363. void headers(int client, const char *filename)  
  364. {  
  365. char buf[1024];  
  366.     (void)filename;  /* could use filename to determine file type */
  367. /*正常的 HTTP header */
  368.     strcpy(buf, "HTTP/1.0 200 OK\r\n");  
  369.     send(client, buf, strlen(buf), 0);  
  370. /*服务器信息*/
  371.     strcpy(buf, SERVER_STRING);  
  372.     send(client, buf, strlen(buf), 0);  
  373.     sprintf(buf, "Content-Type: text/html\r\n");  
  374.     send(client, buf, strlen(buf), 0);  
  375.     strcpy(buf, "\r\n");  
  376.     send(client, buf, strlen(buf), 0);  
  377. }  
  378. /**********************************************************************/
  379. /* Give a client a 404 not found status message. */
  380. /**********************************************************************/
  381. void not_found(int client)  
  382. {  
  383. char buf[1024];  
  384. /* 404 页面 */
  385.     sprintf(buf, "HTTP/1.0 404 NOT FOUND\r\n");  
  386.     send(client, buf, strlen(buf), 0);  
  387. /*服务器信息*/
  388.     sprintf(buf, SERVER_STRING);  
  389.     send(client, buf, strlen(buf), 0);  
  390.     sprintf(buf, "Content-Type: text/html\r\n");  
  391.     send(client, buf, strlen(buf), 0);  
  392.     sprintf(buf, "\r\n");  
  393.     send(client, buf, strlen(buf), 0);  
  394.     sprintf(buf, "<HTML><TITLE>Not Found</TITLE>\r\n");  
  395.     send(client, buf, strlen(buf), 0);  
  396.     sprintf(buf, "<BODY><P>The server could not fulfill\r\n");  
  397.     send(client, buf, strlen(buf), 0);  
  398.     sprintf(buf, "your request because the resource specified\r\n");  
  399.     send(client, buf, strlen(buf), 0);  
  400.     sprintf(buf, "is unavailable or nonexistent.\r\n");  
  401.     send(client, buf, strlen(buf), 0);  
  402.     sprintf(buf, "</BODY></HTML>\r\n");  
  403.     send(client, buf, strlen(buf), 0);  
  404. }  
  405. /**********************************************************************/
  406. /* Send a regular file to the client.  Use headers, and report
  407.  * errors to client if they occur.
  408.  * Parameters: a pointer to a file structure produced from the socket
  409.  *              file descriptor
  410.  *             the name of the file to serve */
  411. /**********************************************************************/
  412. void serve_file(int client, const char *filename)  
  413. {  
  414. FILE *resource = NULL;  
  415. int numchars = 1;  
  416. char buf[1024];  
  417. /*读取并丢弃 header */
  418.     buf[0] = 'A'; buf[1] = '\0';  
  419. while ((numchars > 0) && strcmp("\n", buf))  /* read & discard headers */
  420.         numchars = get_line(client, buf, sizeof(buf));  
  421. /*打开 sever 的文件*/
  422.     resource = fopen(filename, "r");  
  423. if (resource == NULL)  
  424.         not_found(client);  
  425. else
  426.     {  
  427. /*写 HTTP header */
  428.         headers(client, filename);  
  429. /*复制文件*/
  430.         cat(client, resource);  
  431.     }  
  432.     fclose(resource);  
  433. }  
  434. /**********************************************************************/
  435. /* This function starts the process of listening for web connections
  436.  * on a specified port.  If the port is 0, then dynamically allocate a
  437.  * port and modify the original port variable to reflect the actual
  438.  * port.
  439.  * Parameters: pointer to variable containing the port to connect on
  440.  * Returns: the socket */
  441. /**********************************************************************/
  442. int startup(u_short *port)  
  443. {  
  444. int httpd = 0;  
  445. struct sockaddr_in name;  
  446. /*建立 socket */
  447.     httpd = socket(PF_INET, SOCK_STREAM, 0);  
  448. if (httpd == -1)  
  449.         error_die("socket");  
  450.     memset(&name, 0, sizeof(name));  
  451.     name.sin_family = AF_INET;  
  452.     name.sin_port = htons(*port);  
  453.     name.sin_addr.s_addr = htonl(INADDR_ANY);  
  454. if (bind(httpd, (struct sockaddr *)&name, sizeof(name)) < 0)  
  455.         error_die("bind");  
  456. /*如果当前指定端口是 0,则动态随机分配一个端口*/
  457. if (*port == 0)  /* if dynamically allocating a port */
  458.     {  
  459. int namelen = sizeof(name);  
  460. if (getsockname(httpd, (struct sockaddr *)&name, &namelen) == -1)  
  461.             error_die("getsockname");  
  462.         *port = ntohs(name.sin_port);  
  463.     }  
  464. /*开始监听*/
  465. if (listen(httpd, 5) < 0)  
  466.         error_die("listen");  
  467. /*返回 socket id */
  468. return(httpd);  
  469. }  
  470. /**********************************************************************/
  471. /* Inform the client that the requested web method has not been
  472.  * implemented.
  473.  * Parameter: the client socket */
  474. /**********************************************************************/
  475. void unimplemented(int client)  
  476. {  
  477. char buf[1024];  
  478. /* HTTP method 不被支持*/
  479.     sprintf(buf, "HTTP/1.0 501 Method Not Implemented\r\n");  
  480.     send(client, buf, strlen(buf), 0);  
  481. /*服务器信息*/
  482.     sprintf(buf, SERVER_STRING);  
  483.     send(client, buf, strlen(buf), 0);  
  484.     sprintf(buf, "Content-Type: text/html\r\n");  
  485.     send(client, buf, strlen(buf), 0);  
  486.     sprintf(buf, "\r\n");  
  487.     send(client, buf, strlen(buf), 0);  
  488.     sprintf(buf, "<HTML><HEAD><TITLE>Method Not Implemented\r\n");  
  489.     send(client, buf, strlen(buf), 0);  
  490.     sprintf(buf, "</TITLE></HEAD>\r\n");  
  491.     send(client, buf, strlen(buf), 0);  
  492.     sprintf(buf, "<BODY><P>HTTP request method not supported.\r\n");  
  493.     send(client, buf, strlen(buf), 0);  
  494.     sprintf(buf, "</BODY></HTML>\r\n");  
  495.     send(client, buf, strlen(buf), 0);  
  496. }  
  497. /**********************************************************************/
  498. int main(void)  
  499. {  
  500. int server_sock = -1;  
  501.     u_short port = 0;  
  502. int client_sock = -1;  
  503. struct sockaddr_in client_name;  
  504. int client_name_len = sizeof(client_name);  
  505.     pthread_t newthread;  
  506. /*在对应端口建立 httpd 服务*/
  507.     server_sock = startup(&port);  
  508.     printf("httpd running on port %d\n", port);  
  509. while (1)  
  510.     {  
  511. /*套接字收到客户端连接请求*/
  512.         client_sock = accept(server_sock,(struct sockaddr *)&client_name,&client_name_len);  
  513. if (client_sock == -1)  
  514.             error_die("accept");  
  515. /*派生新线程用 accept_request 函数处理新请求*/
  516. /* accept_request(client_sock); */
  517. if (pthread_create(&newthread , NULL, accept_request, client_sock) != 0)  
  518.             perror("pthread_create");  
  519.     }  
  520.     close(server_sock);  
  521. return(0);  
  522. }  
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年12月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  •     主要函数
  •      工作流程
  •       注释版源码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档