协议-tinyhttpd

一个非常轻量的httpd服务器,只有500行代码。

我后边用python写了一个很简陋的几十行代码的,可以看下那个来简单看一下。

这里只看了网络编程的一些东西,进程通信挖个坑,下次填。

很适合

https://github.com/EZLippi/Tinyhttpd.git

编译方式是直接make

编译执行后发现开启4000端口

image-20220720153445595

默认加载htdocs下的index.html

image-20220720154305858

当去访问一个不存在的文件a.html

image-20220720154327199

cgi问题

这里写一个简单的shell cgi来替代自带的cgi

#!/bin/bash
echo "Content-Type: text/html"
echo
echo "<HTML><BODY>"
echo "<CENTER>Today is:</CENTER>"
echo "<CENTER><B>"
date
echo "</B></CENTER>"
echo "</BODY></HTML>"

访问cgi文件,发现cgi并未被执行

image-20220720160433477

是因为没有执行权限,所以导致直接被当成静态文件,而有执行权限的话,静态文件也会被当作cgi执行。

尝试将所有的文件赋予权限

chmod 777 ./*

index页面无法正常显示

image-20220720155641576

image-20220720160541100

执行方式可以通过下图来理解

image-20220720152650036

注释代码

项目放代码阅读时的代码放gitee了需要可自行下载:https://gitee.com/p1piyang/backward-analysis/

/* J. David's webserver */
/* This is a simple webserver.
 * Created November 1999 by J. David Blackstone.
 * CSE 4344 (Network concepts), Prof. Zeigler
 * University of Texas at Arlington
 */
/* This program compiles for Sparc Solaris 2.6.
 * To compile for Linux:
 *  1) Comment out the #include <pthread.h> line.
 *  2) Comment out the line that defines the variable newthread.
 *  3) Comment out the two lines that run pthread_create().
 *  4) Uncomment the line that runs accept_request().
 *  5) Remove -lsocket from the Makefile.
 */
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <ctype.h>
#include <strings.h>
#include <string.h>
#include <sys/stat.h>
#include <pthread.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdint.h>

#define ISspace(x) isspace((int)(x))

#define SERVER_STRING "Server: jdbhttpd/0.1.0\r\n"
#define STDIN   0
#define STDOUT  1
#define STDERR  2

void accept_request(void *);
void bad_request(int);
void cat(int, FILE *);
void cannot_execute(int);
void error_die(const char *);
void execute_cgi(int, const char *, const char *, const char *);
int get_line(int, char *, int);
void headers(int, const char *);
void not_found(int);
void serve_file(int, const char *);
int startup(u_short *);
void unimplemented(int);

/**********************************************************************/
/* A request has caused a call to accept() on the server port to
 * return.  Process the request appropriately.
 * Parameters: the socket connected to the client */
/**********************************************************************/
void accept_request(void *arg)
{
    int client = (intptr_t)arg;
    char buf[1024];
    size_t numchars;
    char method[255];
    char url[255];
    char path[512];
    size_t i, j;
    struct stat st; //文件信息
    //struct stat{
        //   dev_t     st_dev;     /* ID of device containing file */文件使用的设备号
        //   ino_t     st_ino;     /* inode number */    索引节点号 
        //   mode_t    st_mode;    /* protection */  文件对应的模式,文件,目录等
        //   nlink_t   st_nlink;   /* number of hard links */    文件的硬连接数  
        //   uid_t     st_uid;     /* user ID of owner */    所有者用户识别号
        //   gid_t     st_gid;     /* group ID of owner */   组识别号  
        //   dev_t     st_rdev;    /* device ID (if special file) */ 设备文件的设备号
        //   off_t     st_size;    /* total size, in bytes */ 以字节为单位的文件容量   
        //   blksize_t st_blksize; /* blocksize for file system I/O */ 包含该文件的磁盘块的大小   
        //   blkcnt_t  st_blocks;  /* number of 512B blocks allocated */ 该文件所占的磁盘块  
        //   time_t    st_atime;   /* time of last access */ 最后一次访问该文件的时间   
        //   time_t    st_mtime;   /* time of last modification */ /最后一次修改该文件的时间   
        //   time_t    st_ctime;   /* time of last status change */ 最后一次改变该文件状态的时间   
        //};

    int cgi = 0;      /* becomes true if server decides this is a CGI
                       * program */
    char *query_string = NULL;
    //读取http请求的第一行数据
    numchars = get_line(client, buf, sizeof(buf));
    i = 0; j = 0;
    //吧请求方法存到,,method中
    while (!ISspace(buf[i]) && (i < sizeof(method) - 1))
    {
        method[i] = buf[i];
        i++;
    }
    j=i;
    method[i] = '\0';
    //判断如果不是get或者post方法,发送response给客户端表示无法实现
    if (strcasecmp(method, "GET") && strcasecmp(method, "POST"))
    {
        //使用sprintf函数将要返回的内容给buf,使用send函数返回给client
        unimplemented(client);
        return;
    }
    //判断为post方法
    if (strcasecmp(method, "POST") == 0)
        cgi = 1;

    i = 0;
    //跳过空格
    while (ISspace(buf[j]) && (j < numchars))
        j++;
    //获取url
    while (!ISspace(buf[j]) && (i < sizeof(url) - 1) && (j < numchars))
    {
        url[i] = buf[j];
        i++; j++;
    }
    url[i] = '\0';
    //get方法
    if (strcasecmp(method, "GET") == 0)
    {
        query_string = url;
        //用于记录带参数的GET方法请求中 ‘?’后的参数
        while ((*query_string != '?') && (*query_string != '\0'))
            query_string++;
        if (*query_string == '?')
        {
            cgi = 1;
            *query_string = '\0';
            query_string++;
        }
    }
    //将htdocs与url拼接到一起给path,即我们的资源文件都在htdocs下
    sprintf(path, "htdocs%s", url);
    //判断如果URL的最后一位是‘/’,加上index.html
    if (path[strlen(path) - 1] == '/')
        strcat(path, "index.html");

    //定义函数:int stat(const char * file_name, struct stat *buf);
    //函数说明:stat()用来将参数file_name 所指的文件状态, 复制到参数buf 所指的结构中。
    //返回值:执行成功则返回0,失败返回-1,错误代码存于errno。 
    //这里改了一下,把东西处理下可以看到是处理包头。
    //根据执行来看,这个if判断
    if (stat(path, &st) == -1) {
        while ((numchars > 0) && strcmp("\n", buf))  /* read & discard headers */
        {
            numchars = get_line(client, buf, sizeof(buf));
            char *test = buf;
            printf("for: %s", test);
        }
        //打印404返回页面
        not_found(client);
    }
    else
    {
        // 文件存在,那则跟常量S_IFMT相与,相与之后的值可以用来判断该文件是什么类型的
        // 此处与上边判断路径是不是以 \ 结尾的地方作用一样,可以省略,留着可重复确认。
        if ((st.st_mode & S_IFMT) == S_IFDIR)
            strcat(path, "/index.html");
        //判断权限的,前面有说过的
        if ((st.st_mode & S_IXUSR) ||
                (st.st_mode & S_IXGRP) ||
                (st.st_mode & S_IXOTH)    )
            cgi = 1;
        if (!cgi)
        {
            printf("nocgi\n");
            //不需要cgi的
            serve_file(client, path);
        }
        else
        {
            //需要cgi的
            printf("cgi\n");
            execute_cgi(client, path, method, query_string);
        }
    }

    close(client);
}

/**********************************************************************/
/* Inform the client that a request it has made has a problem.
 * Parameters: client socket */
/**********************************************************************/
void bad_request(int client)
{
    char buf[1024];

    sprintf(buf, "HTTP/1.0 400 BAD REQUEST\r\n");
    send(client, buf, sizeof(buf), 0);
    sprintf(buf, "Content-type: text/html\r\n");
    send(client, buf, sizeof(buf), 0);
    sprintf(buf, "\r\n");
    send(client, buf, sizeof(buf), 0);
    sprintf(buf, "<P>Your browser sent a bad request, ");
    send(client, buf, sizeof(buf), 0);
    sprintf(buf, "such as a POST without a Content-Length.\r\n");
    send(client, buf, sizeof(buf), 0);
}

/**********************************************************************/
/* Put the entire contents of a file out on a socket.  This function
 * is named after the UNIX "cat" command, because it might have been
 * easier just to do something like pipe, fork, and exec("cat").
 * Parameters: the client socket descriptor
 *             FILE pointer for the file to cat */
/**********************************************************************/
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);
    }
}

/**********************************************************************/
/* Inform the client that a CGI script could not be executed.
 * Parameter: the client socket descriptor. */
/**********************************************************************/
void cannot_execute(int client)
{
    char buf[1024];

    sprintf(buf, "HTTP/1.0 500 Internal Server Error\r\n");
    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, "<P>Error prohibited CGI execution.\r\n");
    send(client, buf, strlen(buf), 0);
}

/**********************************************************************/
/* Print out an error message with perror() (for system errors; based
 * on value of errno, which indicates system call errors) and exit the
 * program indicating an error. */
/**********************************************************************/
void error_die(const char *sc)
{
    perror(sc);
    exit(1);
}

/**********************************************************************/
/* Execute a CGI script.  Will need to set environment variables as
 * appropriate.
 * Parameters: client socket descriptor
 *             path to the CGI script */
/**********************************************************************/
 // cgi用于动态网页的处理
 // execute_cgi函数创建了两个进程,子进程用于cgi文件的处理,父进程用于往socket读写数据
void execute_cgi(int client, const char *path,
        const char *method, const char *query_string)
{
    char buf[1024];
    int cgi_output[2];
    int cgi_input[2];
    pid_t pid;
    int status;
    int i;
    char c;
    int numchars = 1;
    int content_length = -1;
    buf[0] = 'A'; buf[1] = '\0';
    printf(">exec cgi\n");
    //get方法
    if (strcasecmp(method, "GET") == 0)
        while ((numchars > 0) && strcmp("\n", buf)) /* read & discard headers */
        { 
            printf(">get\n");
            numchars = get_line(client, buf, sizeof(buf));
        }
    //post方法
    else if (strcasecmp(method, "POST") == 0) /*POST*/
    {
        
        numchars = get_line(client, buf, sizeof(buf));
        while ((numchars > 0) && strcmp("\n", buf))
        {
            // "Content-Length:"长度为15个字符,所以将前15个字符比较。 
            buf[15] = '\0';
            // 如果是Content-Length,读取这个改字段转为整数
            if (strcasecmp(buf, "Content-Length:") == 0)
            {
                content_length = atoi(&(buf[16]));
                printf("lenght:%d\n",content_length);
            }
            numchars = get_line(client, buf, sizeof(buf));
        }
        //无法处理的话,400错误
        if (content_length == -1) {
            bad_request(client);
            return;
        }
    }
    else/*HEAD or other*/
    {
    }

    //创建管道

    //子进程写管道
    if (pipe(cgi_output) < 0) {
        //服务错误500
        cannot_execute(client);
        return;
    }
    //子进程写管道
    if (pipe(cgi_input) < 0) {
        //服务错误500
        cannot_execute(client);
        return;
    }

    //创建子进程
    if ( (pid = fork()) < 0 ) {
        cannot_execute(client);
        return;
    }
    //响应成功
    sprintf(buf, "HTTP/1.0 200 OK\r\n");
    send(client, buf, strlen(buf), 0);
    // 这下面是另一个坑,进程通信。
    if (pid == 0)  /* child: CGI script */
    {
        char meth_env[255];
        char query_env[255];
        char length_env[255];

        dup2(cgi_output[1], STDOUT);
        dup2(cgi_input[0], STDIN);
        close(cgi_output[0]);
        close(cgi_input[1]);
        sprintf(meth_env, "REQUEST_METHOD=%s", method);
        putenv(meth_env);
        if (strcasecmp(method, "GET") == 0) {
            sprintf(query_env, "QUERY_STRING=%s", query_string);
            putenv(query_env);
        }
        else {   /* POST */
            sprintf(length_env, "CONTENT_LENGTH=%d", content_length);
            putenv(length_env);
        }
        execl(path, NULL);
        exit(0);
    } else {    /* parent */
        close(cgi_output[1]);
        close(cgi_input[0]);
        if (strcasecmp(method, "POST") == 0)
            for (i = 0; i < content_length; i++) {
                recv(client, &c, 1, 0);
                write(cgi_input[1], &c, 1);
            }
        while (read(cgi_output[0], &c, 1) > 0)
            send(client, &c, 1, 0);

        close(cgi_output[0]);
        close(cgi_input[1]);
        waitpid(pid, &status, 0);
    }
}

/**********************************************************************/
/* Get a line from a socket, whether the line ends in a newline,
 * carriage return, or a CRLF combination.  Terminates the string read
 * with a null character.  If no newline indicator is found before the
 * end of the buffer, the string is terminated with a null.  If any of
 * the above three line terminators is read, the last character of the
 * string will be a linefeed and the string will be terminated with a
 * null character.
 * Parameters: the socket descriptor
 *             the buffer to save the data in
 *             the size of the buffer
 * Returns: the number of bytes stored (excluding null) */
/**********************************************************************/
//处理包,大概流程是循环读取每个字符
//如果回车符(\r)的后面不是换行符(\n)或者读取失败就把当前读取的字符置为换行,从而终止循环
//如果没有成功接收到字符,以 \n 结尾,结束循环
//最后以\n结尾
int get_line(int sock, char *buf, int size)
{
    int i = 0;
    char c = '\0';
    int n;
    //
    while ((i < size - 1) && (c != '\n'))
    {
        n = recv(sock, &c, 1, 0);
        /* DEBUG printf("%02X\n", c); */
        if (n > 0)
        {
            if (c == '\r')
            {
                n = recv(sock, &c, 1, MSG_PEEK);
                /* DEBUG printf("%02X\n", c); */
                if ((n > 0) && (c == '\n'))
                    recv(sock, &c, 1, 0);
                else
                    c = '\n';
            }
            buf[i] = c;
            i++;
        }
        else
            c = '\n';
    }
    buf[i] = '\0';

    return(i);
}

/**********************************************************************/
/* Return the informational HTTP headers about a file. */
/* Parameters: the socket to print the headers on
 *             the name of the file */
/**********************************************************************/
//响应头
void headers(int client, const char *filename)
{
    char buf[1024];
    (void)filename;  /* could use filename to determine file type */

    strcpy(buf, "HTTP/1.0 200 OK\r\n");
    send(client, buf, strlen(buf), 0);
    strcpy(buf, SERVER_STRING);
    send(client, buf, strlen(buf), 0);
    sprintf(buf, "Content-Type: text/html\r\n");
    send(client, buf, strlen(buf), 0);
    strcpy(buf, "\r\n");
    send(client, buf, strlen(buf), 0);
}

/**********************************************************************/
/* Give a client a 404 not found status message. */
/**********************************************************************/
void not_found(int client)
{
    //将内容打印到缓存,并且发送出去
    char buf[1024];
    sprintf(buf, "HTTP/1.0 404 NOT FOUND\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><TITLE>Not Found</TITLE>\r\n");
    send(client, buf, strlen(buf), 0);
    sprintf(buf, "<BODY><P>The server could not fulfill\r\n");
    send(client, buf, strlen(buf), 0);
    sprintf(buf, "your request because the resource specified\r\n");
    send(client, buf, strlen(buf), 0);
    sprintf(buf, "is unavailable or nonexistent.\r\n");
    send(client, buf, strlen(buf), 0);
    sprintf(buf, "</BODY></HTML>\r\n");
    send(client, buf, strlen(buf), 0);
}

/**********************************************************************/
/* Send a regular file to the client.  Use headers, and report
 * errors to client if they occur.
 * Parameters: a pointer to a file structure produced from the socket
 *              file descriptor
 *             the name of the file to serve */
/**********************************************************************/
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);
}

/**********************************************************************/
/* This function starts the process of listening for web connections
 * on a specified port.  If the port is 0, then dynamically allocate a
 * port and modify the original port variable to reflect the actual
 * port.
 * Parameters: pointer to variable containing the port to connect on
 * Returns: the socket */
/**********************************************************************/
//初始化 httpd 服务,包括建立套接字,绑定端口,进行监听等。
int startup(u_short *port)
{
    int httpd = 0;
    int on = 1;
    struct sockaddr_in name;
    //正常的socket创建流程
    httpd = socket(PF_INET, SOCK_STREAM, 0);
    if (httpd == -1)
        error_die("socket");
    memset(&name, 0, sizeof(name));
    name.sin_family = AF_INET;
    name.sin_port = htons(*port);
    name.sin_addr.s_addr = htonl(INADDR_ANY);
    //setsockopt()函数,用于任意类型、任意状态套接口的设置选项值 
    if ((setsockopt(httpd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) < 0)  
    {  
        error_die("setsockopt failed");
    }
    //绑定socket到端口,port等于0,系统会随机分配一个端口(bind函数里实现)
    if (bind(httpd, (struct sockaddr *)&name, sizeof(name)) < 0)
        error_die("bind");
    // 这个if的作用是将自动分配的端口传给port
    if (*port == 0)  /* if dynamically allocating a port */
    {
        socklen_t namelen = sizeof(name);
          // 获取socket绑定的地址信息
        if (getsockname(httpd, (struct sockaddr *)&name, &namelen) == -1)
            error_die("getsockname");
        *port = ntohs(name.sin_port);
    }
    //监听端口
    if (listen(httpd, 5) < 0)
        error_die("listen");
    return(httpd);
}

/**********************************************************************/
/* Inform the client that the requested web method has not been
 * implemented.
 * Parameter: the client socket */
/**********************************************************************/
void unimplemented(int client)
{
    char buf[1024];

    sprintf(buf, "HTTP/1.0 501 guan zhu jia ran, dun dun jie chan\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>if you watch this page, please follow JiaRan_Diana.\r\n");
    send(client, buf, strlen(buf), 0);
    sprintf(buf, "<br/><img src='https://img2.baidu.com/it/u=2323296913,2613522307&amp;fm=253&amp;app=138&amp;size=w931&amp;n=0&amp;f=JPEG&amp;fmt=auto?sec=1658854800&amp;t=7b90c5387e83fb57a89e051eccbb7eb9'>\r\n");
    send(client, buf, strlen(buf), 0);
    sprintf(buf, "</BODY></HTML>\r\n");
    send(client, buf, strlen(buf), 0);
}

/**********************************************************************/

int main(void)
{
    int server_sock = -1; //服务端套接字接口
    u_short port = 4000; //端口
    int client_sock = -1;//已连接套接字描述符,初始化为-1(客户端)
    struct sockaddr_in client_name; 
    socklen_t  client_name_len = sizeof(client_name);
    pthread_t newthread;
    
    //调用startup()函数,建立一个监听套接字,在对应的端口建立httpd服务
    server_sock = startup(&port);
    printf("httpd running on port %d\n", port);

    //循环调用accept()函数来等待客户端的连接,accept()会议阻塞的方式运行
    //有客户端链接后返回到client_sock,去创建新线程处理请求
    while (1)
    {
        client_sock = accept(server_sock, (struct sockaddr *)&client_name, &client_name_len);
        if (client_sock == -1)
            error_die("accept");
        //创建新线程用accept_request()函数处理新请求,同时将客户端socket作为参数传过去
        /* accept_request(&client_sock); */
        if (pthread_create(&newthread , NULL, (void *)accept_request, (void *)(intptr_t)client_sock) != 0)
            perror("pthread_create");
    }

    close(server_sock);

    return(0);
}


协议-tinyhttpd
http://example.com/article/68d281b.html
Author
p1yang
Posted on
August 1, 2022
Licensed under