├── README.md ├── main.cpp ├── .gitignore ├── socket_control.h ├── socket_control.cpp ├── questqueue.h ├── locker.h ├── docs ├── USAGE.md └── API.md ├── pthreadpool.h ├── static_value.h ├── http_conn.h ├── epoll_class.h ├── http_conn.cpp └── LICENSE /README.md: -------------------------------------------------------------------------------- 1 | # webserver_HTTP 2 | 使用了线程池,通过 epoll 实现的 Proactor 版本的 web 服务器。参考了游双老师的《Linux 高性能服务器编程》以及牛客网的《Linux 高并发服务器开发》课程。在自己复现的基础上进行模块的整合并添加一些小更改。所有代码拥有完备的注释。 3 | 4 | 访问的资源在同级目录 `resources/` 文件夹中。 5 | 6 | ## 文档 7 | - API 文档:[`docs/API.md`](docs/API.md) 8 | - 使用与示例:[`docs/USAGE.md`](docs/USAGE.md) 9 | 10 | 欢迎访问我对项目进行相关解读的专栏(CSDN):[链接如下](https://blog.csdn.net/qq_53157334/category_12075219.html) 11 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "epoll_class.h" 4 | 5 | // 通过port端口进行通信 6 | // port由命令行给出 7 | int main(int argc, char *argv[]) 8 | { 9 | // 判断传入参数 10 | if (argc <= 1) 11 | { 12 | printf("按照如下格式运行: %s port_number\n", basename(argv[0])); 13 | return -1; 14 | } 15 | 16 | // 获取端口号 17 | int port = atoi(argv[1]); 18 | 19 | epoll_class epoll_sample(port); 20 | epoll_sample.run(); 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # 生成文件 2 | main 3 | 4 | # debug 5 | .debug/* 6 | core 7 | 8 | # vscode 9 | .vscode/* 10 | 11 | # 资源文件 12 | resources/* 13 | test/* 14 | 15 | 16 | # Prerequisites 17 | *.d 18 | 19 | # Compiled Object files 20 | *.slo 21 | *.lo 22 | *.o 23 | *.obj 24 | 25 | # Precompiled Headers 26 | *.gch 27 | *.pch 28 | 29 | # Compiled Dynamic libraries 30 | *.so 31 | *.dylib 32 | *.dll 33 | 34 | # Fortran module files 35 | *.mod 36 | *.smod 37 | 38 | # Compiled Static libraries 39 | *.lai 40 | *.la 41 | *.a 42 | *.lib 43 | 44 | # Executables 45 | *.exe 46 | *.out 47 | *.app 48 | -------------------------------------------------------------------------------- /socket_control.h: -------------------------------------------------------------------------------- 1 | #ifndef _SOCKET_CONTROL_H_ 2 | #define _SOCKET_CONTROL_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | // 设置文件描述符非阻塞 18 | extern void setnonblocking(int fd); 19 | 20 | // 添加需要监听的文件描述符到epoll 21 | extern void addfd(int epollfd, int fd, bool one_shot); 22 | 23 | // 从epoll中删除文件描述符,并close文件描述符 24 | extern void removefd(int epollfd, int fd); 25 | 26 | // 修改文件描述符,重置socket上EPOLLONESHOT事件,确保下次可读时被触发 27 | extern void modfd(int epollfd, int fd, int ev); 28 | 29 | #endif -------------------------------------------------------------------------------- /socket_control.cpp: -------------------------------------------------------------------------------- 1 | #include "socket_control.h" 2 | 3 | // 设置文件描述符非阻塞 4 | void setnonblocking(int fd) 5 | { 6 | int old_flag = fcntl(fd, F_GETFL); 7 | int new_flag = old_flag | O_NONBLOCK; 8 | fcntl(fd, F_SETFL, new_flag); 9 | } 10 | 11 | // 添加需要监听的文件描述符到epoll 12 | void addfd(int epollfd, int fd, bool one_shot) 13 | { 14 | epoll_event event; 15 | event.data.fd = fd; 16 | // EPOLLRDHUP判断是否挂起 17 | // event.events=EPOLLIN | EPOLLRDHUP; 18 | event.events = EPOLLIN | EPOLLRDHUP; 19 | if (one_shot) 20 | event.events |= EPOLLONESHOT; 21 | 22 | epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event); 23 | 24 | // 设置文件描述符非阻塞 25 | setnonblocking(fd); 26 | } 27 | 28 | // 从epoll中删除文件描述符,并close文件描述符 29 | void removefd(int epollfd, int fd) 30 | { 31 | epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, NULL); 32 | close(fd); 33 | } 34 | 35 | // 修改文件描述符,重置socket上EPOLLONESHOT事件,确保下次可读时被触发 36 | void modfd(int epollfd, int fd, int ev) 37 | { 38 | epoll_event event; 39 | event.data.fd = fd; 40 | event.events = ev | EPOLLONESHOT | EPOLLRDHUP; 41 | epoll_ctl(epollfd, EPOLL_CTL_MOD, fd, &event); 42 | } 43 | -------------------------------------------------------------------------------- /questqueue.h: -------------------------------------------------------------------------------- 1 | #ifndef __QUEST_QUEUE_H_ 2 | #define __QUEST_QUEUE_H_ 3 | 4 | #include 5 | #include "locker.h" 6 | 7 | // 模板类 请求队列 8 | template 9 | class questqueue 10 | { 11 | private: 12 | // 请求队列 13 | std::list m_questqueue; 14 | // 请求队列的最大长度 15 | int m_max_queue; 16 | // 保护请求队列的互斥锁 17 | mutex m_queue_mutex; 18 | // 是否有任务需要处理,信号量 19 | sem m_queue_sem; 20 | 21 | public: 22 | questqueue(int max_queue); 23 | ~questqueue(); 24 | // 阻塞式取元素 25 | T *pop(); 26 | // 阻塞式填入元素 27 | bool push(T *quest); 28 | }; 29 | 30 | template 31 | questqueue::questqueue(int max_queue) : m_max_queue(max_queue) 32 | { 33 | if (max_queue <= 0) 34 | throw "队列的大小错误"; 35 | } 36 | 37 | template 38 | questqueue::~questqueue() {} 39 | 40 | // 阻塞式填入元素 41 | template 42 | bool questqueue::push(T *quest) 43 | { 44 | // 操作请求队列加锁 45 | m_queue_mutex.lock(); // lock 46 | if (m_questqueue.size() >= m_max_queue) 47 | { 48 | m_queue_mutex.unlock(); 49 | return false; 50 | } 51 | 52 | // 添加quest,并且更新 53 | m_questqueue.push_back(quest); 54 | m_queue_sem.post(); 55 | 56 | m_queue_mutex.unlock(); // unlock 57 | return true; 58 | } 59 | 60 | // 阻塞式取元素 61 | template 62 | T *questqueue::pop() 63 | { 64 | // 上锁 65 | m_queue_sem.wait(); 66 | m_queue_mutex.lock(); 67 | if (m_questqueue.empty()) 68 | { 69 | m_queue_mutex.unlock(); 70 | return NULL; 71 | } 72 | // 取出 73 | T *quest = m_questqueue.front(); 74 | m_questqueue.pop_front(); 75 | // 解锁 76 | m_queue_mutex.unlock(); 77 | 78 | return quest; 79 | } 80 | 81 | #endif -------------------------------------------------------------------------------- /locker.h: -------------------------------------------------------------------------------- 1 | #ifndef _LOCKER_H_ 2 | #define _LOCKER_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | // 互斥锁 9 | class mutex 10 | { 11 | private: 12 | pthread_mutex_t m_mutex; 13 | 14 | public: 15 | mutex(){ 16 | if (pthread_mutex_init(&m_mutex, NULL) != 0){ 17 | throw "初始化mutex错误"; 18 | } 19 | } 20 | ~mutex(){ 21 | pthread_mutex_destroy(&m_mutex); 22 | } 23 | bool lock(){ 24 | return pthread_mutex_lock(&m_mutex) == 0; 25 | } 26 | bool unlock(){ 27 | return pthread_mutex_unlock(&m_mutex) == 0; 28 | } 29 | }; 30 | 31 | // 条件变量 32 | class cond 33 | { 34 | private: 35 | pthread_cond_t m_cond; 36 | 37 | public: 38 | cond() 39 | { 40 | if (pthread_cond_init(&m_cond, NULL) != 0) 41 | { 42 | throw "初始化条件变量错误"; 43 | } 44 | } 45 | ~cond() 46 | { 47 | pthread_cond_destroy(&m_cond); 48 | } 49 | bool wait(pthread_mutex_t *x) 50 | { 51 | return pthread_cond_wait(&m_cond, x) == 0; 52 | } 53 | // 唤醒一个或多个等待进程 54 | bool signal() 55 | { 56 | return pthread_cond_signal(&m_cond); 57 | } 58 | // 全部唤醒 59 | bool broadcast() 60 | { 61 | return pthread_cond_signal(&m_cond); 62 | } 63 | }; 64 | 65 | // 信号量类 66 | class sem 67 | { 68 | private: 69 | sem_t m_sem; 70 | 71 | public: 72 | sem() 73 | { 74 | if (sem_init(&m_sem, 0, 0) != 0) 75 | { 76 | throw "初始化信号量错误"; 77 | } 78 | } 79 | sem(int num) 80 | { 81 | if (sem_init(&m_sem, 0, num) != 0) 82 | { 83 | throw "初始化信号量错误"; 84 | } 85 | } 86 | ~sem() 87 | { 88 | sem_destroy(&m_sem); 89 | } 90 | bool post() 91 | { 92 | sem_post(&m_sem); 93 | } 94 | bool wait() 95 | { 96 | sem_wait(&m_sem); 97 | } 98 | }; 99 | 100 | #endif 101 | -------------------------------------------------------------------------------- /docs/USAGE.md: -------------------------------------------------------------------------------- 1 | # 使用与示例 2 | 3 | ## 环境需求 4 | - Linux(内核支持 epoll) 5 | - g++(建议 9+),支持 `-pthread` 6 | 7 | ## 构建 8 | 9 | ### 直接编译 10 | ```bash 11 | g++ -std=c++11 -O2 -Wall -pthread \ 12 | main.cpp http_conn.cpp socket_control.cpp \ 13 | -o webserver 14 | ``` 15 | 16 | > 说明:`epoll_class.h` 为头文件内联实现,无需单独编译。 17 | 18 | ### 运行 19 | ```bash 20 | ./webserver 8080 21 | ``` 22 | - 访问静态资源目录:`resources/`(可在 `http_conn.cpp` 中修改 `root_directory`)。 23 | - 示例: 24 | - 浏览器访问 `http://127.0.0.1:8080/index.html` 25 | - 或使用 curl: 26 | ```bash 27 | curl -v http://127.0.0.1:8080/ 28 | ``` 29 | 30 | ## 典型用法 31 | 32 | ### 作为独立 Web 服务运行 33 | ```cpp 34 | #include "epoll_class.h" 35 | int main(int argc, char** argv){ 36 | int port = (argc>1? atoi(argv[1]) : 8080); 37 | epoll_class server(port); 38 | server.run(); 39 | } 40 | ``` 41 | 42 | ### 自定义线程池规模与队列长度 43 | `epoll_class` 默认构造了 `threadpool(8, 1000)`。如需调整,可在 `epoll_class` 构造函数中改为: 44 | ```cpp 45 | pool = new threadpool(16, 2000); // 16 线程,队列 2000 46 | ``` 47 | 48 | ### 请求处理流程(Proactor 模型) 49 | - 主线程仅做 IO 就绪事件分发: 50 | - `EPOLLIN`:读取 -> 任务入队 -> 线程池中调用 `http_conn::process()` 解析与准备响应 51 | - `EPOLLOUT`:非阻塞写出(`epoll_class::Write()`),按 keep-alive 决定是否关闭 52 | - 解析与应答:仅支持 GET;使用 `writev` 分散写(头 + `mmap` 文件内容) 53 | 54 | ### Keep-Alive 与连接管理 55 | - 默认短连接;当请求头 `Connection: keep-alive` 时保持连接并复用 `http_conn` 对象。 56 | 57 | ### 日志/调试 58 | 在 `http_conn.cpp` 顶部可按需取消注释调试宏(如 `print_writev_result`、`process_read_result` 等)以输出解析与发送细节。 59 | 60 | ## 组件示例 61 | 62 | ### socket_control(epoll 辅助) 63 | ```cpp 64 | int efd = epoll_create1(0); 65 | int fd = ::socket(AF_INET, SOCK_STREAM, 0); 66 | addfd(efd, fd, true); 67 | modfd(efd, fd, EPOLLOUT); 68 | removefd(efd, fd); 69 | ``` 70 | 71 | ### 线程同步(mutex/cond/sem) 72 | ```cpp 73 | mutex m; sem s(0); 74 | // Producer 75 | auto p = [&]{ /* produce */ s.post(); }; 76 | // Consumer 77 | auto c = [&]{ s.wait(); /* consume */ }; 78 | ``` 79 | 80 | ### 线程池与请求队列 81 | ```cpp 82 | struct Job { void process(){ /* ... */ } }; 83 | threadpool pool(8, 1000); 84 | Job* j = new Job(); 85 | pool.append(j); 86 | ``` 87 | 88 | ## 疑难解答 89 | - 无法访问文件:确认 `resources/` 下目标文件存在,且对其他用户具备读权限(`S_IROTH`)。 90 | - 404/403:分别表示资源不存在/权限不足;详见 `base::response_info`。 91 | - 高并发调优: 92 | - 增大线程数与队列长度(见上文) 93 | - 调整 `listen()` backlog(在 `epoll_class` 构造中) 94 | - 配置内核参数(例如 `net.core.somaxconn`、`fs.file-max` 等) 95 | -------------------------------------------------------------------------------- /pthreadpool.h: -------------------------------------------------------------------------------- 1 | #ifndef _PTHREADPOOL_H_ 2 | #define _PTHREADPOOL_H_ 3 | 4 | #include 5 | #include "locker.h" 6 | #include "questqueue.h" 7 | 8 | // #define 控制 9 | //#define show_create_pool 1 10 | //#define show_pool_append 1 11 | 12 | // 模板类 线程池 13 | template 14 | class threadpool 15 | { 16 | public: 17 | threadpool(int poolsize = 8, int maxquest = 1000); 18 | ~threadpool(); 19 | // 增加请求 20 | bool append(T *quest); 21 | 22 | private: 23 | // 子线程调用的的执行函数 24 | static void *worker(void *arg); 25 | // 因为worker是静态的,因此增加一个真正的执行函数 26 | void run(); 27 | 28 | private: 29 | // 请求队列 30 | questqueue m_questqueue; 31 | // 线程池大小 32 | int m_thread_poolsize; 33 | // 大小为m_thread_poolsize的 线程池 34 | pthread_t *m_threads; 35 | 36 | // 是否结束线程 37 | bool m_stoppool; 38 | }; 39 | 40 | template 41 | threadpool::threadpool(int poolsize, int maxquest) : 42 | m_thread_poolsize(poolsize), m_stoppool(false),m_questqueue(maxquest) 43 | { 44 | // check size 45 | if (poolsize <= 0) 46 | throw "线程池的大小错误"; 47 | 48 | m_threads = new pthread_t[m_thread_poolsize]; 49 | 50 | // 初始化线程池的线程 51 | for (int i = 0; i < m_thread_poolsize; i++) 52 | { 53 | #ifdef show_create_pool 54 | printf( "create the %dth thread\n", i+1); 55 | #endif 56 | if (pthread_create(m_threads + i, NULL, worker, this) != 0) 57 | { 58 | delete[] m_threads; 59 | throw "创建子线程时错误"; 60 | } 61 | } 62 | for (int i = 0; i < m_thread_poolsize; i++) 63 | { 64 | if (pthread_detach(m_threads[i]) != 0) 65 | { 66 | delete[] m_threads; 67 | throw "子线程分离时错误"; 68 | } 69 | } 70 | printf("thread pool ready \n"); 71 | } 72 | 73 | template 74 | threadpool::~threadpool() 75 | { 76 | m_stoppool = true; 77 | delete[] m_threads; 78 | } 79 | 80 | template 81 | bool threadpool::append(T *quest) 82 | { 83 | return m_questqueue.push(quest); 84 | } 85 | 86 | template 87 | void *threadpool::worker(void *arg) 88 | { 89 | threadpool *pool = (threadpool *)arg; 90 | pool->run(); 91 | return pool; 92 | } 93 | template 94 | void threadpool::run() 95 | { 96 | while (!m_stoppool) 97 | { 98 | // 从请求队列中阻塞取出待处理元素 99 | T* quest=m_questqueue.pop(); 100 | 101 | // 检查是否为空 102 | if (quest!=NULL) 103 | // 调用quest 104 | quest->process(); 105 | } 106 | } 107 | 108 | #endif -------------------------------------------------------------------------------- /static_value.h: -------------------------------------------------------------------------------- 1 | #ifndef _STATIC_VALUE_H_ 2 | #define _STATIC_VALUE_H_ 3 | 4 | #include 5 | #include 6 | using std::unordered_map; 7 | using std::tuple; 8 | 9 | 10 | class base 11 | { 12 | public: 13 | static const int READ_BUFFER_SIZE = 2048; // 读缓冲区的大小 14 | static const int WRITE_BUFFER_SIZE = 1024; // 写缓冲区的大小 15 | static const int FILENAME_MAXLEN = 200; // 文件名的最大长度 16 | 17 | //======================== 状态值 ============================ 18 | // HTTP请求方法,这里只支持GET 19 | enum METHOD {GET = 0, POST, HEAD, PUT, DELETE, TRACE, OPTIONS, CONNECT}; 20 | 21 | /* 22 | 解析客户端请求时,主状态机的状态 23 | CHECK_STATE_REQUESTLINE : 当前正在分析请求行 24 | CHECK_STATE_HEADER : 当前正在分析头部字段 25 | CHECK_STATE_CONTENT : 当前正在解析请求体 26 | */ 27 | enum CHECK_STATE { CHECK_STATE_REQUESTLINE = 0, CHECK_STATE_HEADER, CHECK_STATE_CONTENT }; 28 | 29 | /* 30 | 服务器处理HTTP请求的可能结果,报文解析的结果 31 | NO_REQUEST : 请求不完整,需要继续读取客户数据 32 | GET_REQUEST : 表示获得了一个完成的客户请求 33 | BAD_REQUEST : 表示客户请求语法错误 34 | NO_RESOURCE : 表示服务器没有资源 35 | FORBIDDEN_REQUEST : 表示客户对资源没有足够的访问权限 36 | FILE_REQUEST : 文件请求,获取文件成功 37 | INTERNAL_ERROR : 表示服务器内部错误 38 | CLOSED_CONNECTION : 表示客户端已经关闭连接了 39 | */ 40 | enum HTTP_CODE { NO_REQUEST, GET_REQUEST, BAD_REQUEST, NO_RESOURCE, FORBIDDEN_REQUEST, FILE_REQUEST, INTERNAL_ERROR, CLOSED_CONNECTION }; 41 | /* 42 | 从状态机的三种可能状态,即行的读取状态,分别表示 43 | LINE_OK : 读取到一个完整的行 44 | LINE_BAD : 行出错 45 | LINE_OPEN : 行数据尚且不完整 46 | */ 47 | enum LINE_STATUS { LINE_OK = 0, LINE_BAD, LINE_OPEN }; 48 | 49 | //================== HTTP响应的状态信息 ==================== 50 | // 定义HTTP响应的一些状态信息 51 | const char *ok_200_title = "OK"; 52 | const char *error_400_title = "Bad Request"; 53 | const char *error_400_form = "Your request has bad syntax or is inherently impossible to satisfy.\n"; 54 | const char *error_403_title = "Forbidden"; 55 | const char *error_403_form = "You do not have permission to get file from this server.\n"; 56 | const char *error_404_title = "Not Found"; 57 | const char *error_404_form = "The requested file was not found on this server.\n"; 58 | const char *error_500_title = "Internal Error"; 59 | const char *error_500_form = "There was an unusual problem serving the requested file.\n"; 60 | 61 | // 用hashmap来降低代码量 62 | unordered_map> response_info{ 63 | {FILE_REQUEST, {200, ok_200_title, NULL}}, 64 | {BAD_REQUEST, {400, error_400_title, error_400_form}}, 65 | {FORBIDDEN_REQUEST, {403, error_403_title, error_403_form}}, 66 | {NO_RESOURCE, {404, error_404_title, error_404_form}}, 67 | {INTERNAL_ERROR, {500, error_500_title, error_500_form}}, 68 | }; 69 | }; 70 | 71 | #endif -------------------------------------------------------------------------------- /http_conn.h: -------------------------------------------------------------------------------- 1 | #ifndef _HTTP_CONN_H_ 2 | #define _HTTP_CONN_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include "static_value.h" 24 | #include "socket_control.h" 25 | 26 | using std::unordered_map; 27 | using std::tuple; 28 | 29 | class http_conn : public base 30 | { 31 | public: 32 | //======================== static ============================ 33 | static int st_m_epollfd; // epoll fd 34 | static int st_m_usercount; // 用户数量 35 | 36 | public: 37 | http_conn() {} 38 | ~http_conn() {} 39 | 40 | // 处理客户端的请求 41 | void process(); 42 | //初始化新接收的连接 43 | void init(int sockfd,const sockaddr_in &addr); 44 | // 关闭这个对象的连接 45 | void close_conn(); 46 | // 非阻塞的写 47 | bool Write(); 48 | 49 | //================== 与epoll的交互 ==================== 50 | // 初始化基本的数值 51 | void clear(); 52 | 53 | //================== get ==================== 54 | int get_sockfd() { return m_sockfd; } // 获取sockfd 55 | int get_read_index() { return m_read_index; } // 获取m_read_index 56 | char *get_read_buf() { return m_read_buf; } // 获取 m_read_buf 57 | char *get_write_buf() { return m_write_buf; } // 获取 m_write_buf 58 | int get_bytes_to_send() { return bytes_to_send; } // 获取bytes_to_send 59 | iovec *get_iv() { return m_iv; } // 获取struct iovec[] 60 | int get_iv_count() { return m_iv_count; } // 获取iv_count 61 | char *get_address_mmap() { return m_address_mmap; } // 获取映射地址 62 | bool is_keepalive() { return m_keepalive; } // 是否保持连接 63 | //================== set ==================== 64 | void set_read_index(int index) { m_read_index = index; } // 设置m_read_index 65 | 66 | private: 67 | //================== socket通信的值 ==================== 68 | int m_sockfd; //该HTTP连接的socket 69 | sockaddr_in m_address; // 通信的socket地址 70 | 71 | //================== 读缓冲区 ==================== 72 | char m_read_buf[READ_BUFFER_SIZE]; // 读缓冲区 73 | int m_read_index; // 标识读缓冲区中以及读入的客户端数据的最后一个字节的下标 74 | 75 | int m_checked_index; // 当前正在分析的字符在读缓冲区的位置 76 | int m_start_line; // 当前正在解析的行的起始位置 77 | char *get_line() { return m_read_buf + m_start_line; } // 返回当前行的起始位置对应的指针 78 | 79 | //================== 写缓冲区 ==================== 80 | char m_write_buf[WRITE_BUFFER_SIZE]; // 写缓冲区 81 | int m_write_index; // 写缓冲区中待发送的字节数 82 | 83 | // writev成员 84 | struct iovec m_iv[2]; // 存储分散写的内容,0为报文头,1为报文内容 85 | int m_iv_count; // writev数量 86 | // 记录发送情况 87 | int bytes_to_send; // 将要发送的数据的字节数 88 | 89 | //================== 报文解析结果 ======================== 90 | //================== 请求行分析结果 ==================== 91 | char *m_url; // 请求目标文件的文件名 92 | char *m_version; // 协议版本,只支持 HTTP1.1 93 | METHOD m_method; // 请求方法 94 | //================== 请求头分析结果 ==================== 95 | char *m_host; // 主机名 96 | int m_content_length; // 描述HTTP消息实体的传输长度 97 | bool m_keepalive; // HTTP请求是否要保持连接 98 | //================== 返回数据 ==================== 99 | char m_filename[FILENAME_MAX]; // 客户请求的目标文件的完整目录,其内容为 root_directory+m_url 100 | struct stat m_file_stat; // 目标文件的状态 101 | char *m_address_mmap; // 客户请求的数据被mmap到的位置 102 | void unmap(); // 释放内存映射的空间 103 | //================== 状态机 ==================== 104 | CHECK_STATE m_check_state; // 主状态机当前所处的位置 105 | //============== 主状态机 =================== 106 | HTTP_CODE process_read(); // 解析HTTP请求 107 | HTTP_CODE parse_request_line(char *text); // 解析HTTP请求首行 108 | HTTP_CODE parse_headers(char *text); // 解析HTTP请求头 109 | HTTP_CODE parse_content_complete(char *text); // 解析HTTP请求内容 110 | HTTP_CODE do_request(); // 对报文做具体的处理 111 | //================== 从状态机 ==================== 112 | LINE_STATUS parse_line(); 113 | 114 | //================== 写响应报文 ==================== 115 | bool process_write(HTTP_CODE ret); // 写响应报文 116 | bool add_response(const char *format, ...); // 往写缓冲区中写数据 117 | bool add_status_line(int status, const char *title); // 添加状态行 118 | bool add_headers(int content_len, time_t time); // 添加响应头部 119 | // 响应头部组件 120 | bool add_content_length(int content_len); // content-length 121 | bool add_connection(); // keep_alive 122 | bool add_content_type(); // Content-Type 123 | bool add_date(time_t t); // 发送时间 124 | bool add_blank_line(); // 空白结束行 125 | //bool add_content(const char *content); // 添加响应正文 126 | }; 127 | 128 | 129 | #endif -------------------------------------------------------------------------------- /docs/API.md: -------------------------------------------------------------------------------- 1 | # WebServer HTTP API 文档 2 | 3 | > 本文档覆盖本仓库对外可用的公开类、函数与组件,包含概览、形参/返回说明与典型用例。示例均基于 Linux 环境(epoll + pthread)。 4 | 5 | - 组件总览 6 | - 基础常量与枚举:`base`(见 `static_value.h`) 7 | - 套接字/epoll 辅助:`setnonblocking` / `addfd` / `removefd` / `modfd`(见 `socket_control.h`) 8 | - 线程同步:`mutex` / `cond` / `sem`(见 `locker.h`) 9 | - 泛型请求队列:`questqueue`(见 `questqueue.h`) 10 | - 泛型线程池:`threadpool`(见 `pthreadpool.h`) 11 | - HTTP 连接处理:`http_conn`(见 `http_conn.h`) 12 | - 事件主循环与服务器:`epoll_class`(见 `epoll_class.h`) 13 | 14 | > 运行与快速上手请参考 `docs/USAGE.md`。 15 | 16 | --- 17 | 18 | ## 基础:`base`(`static_value.h`) 19 | 20 | 提供全局常量与 HTTP/解析相关枚举,并内置错误响应描述表。 21 | 22 | - 常量 23 | - `READ_BUFFER_SIZE = 2048` 24 | - `WRITE_BUFFER_SIZE = 1024` 25 | - `FILENAME_MAXLEN = 200` 26 | - 枚举 27 | - `METHOD { GET, POST, HEAD, PUT, DELETE, TRACE, OPTIONS, CONNECT }` 28 | - `CHECK_STATE { CHECK_STATE_REQUESTLINE, CHECK_STATE_HEADER, CHECK_STATE_CONTENT }` 29 | - `HTTP_CODE { NO_REQUEST, GET_REQUEST, BAD_REQUEST, NO_RESOURCE, FORBIDDEN_REQUEST, FILE_REQUEST, INTERNAL_ERROR, CLOSED_CONNECTION }` 30 | - `LINE_STATUS { LINE_OK, LINE_BAD, LINE_OPEN }` 31 | - 预置响应信息(节选) 32 | - `response_info[FILE_REQUEST] -> (200, "OK", nullptr)` 33 | - `response_info[NO_RESOURCE] -> (404, "Not Found", "The requested file was not found...\n")` 34 | 35 | 典型用途:作为其他类(如 `http_conn`, `epoll_class`)的基类提供统一常量/状态。 36 | 37 | --- 38 | 39 | ## 套接字/epoll 辅助(`socket_control.h`) 40 | 41 | - `void setnonblocking(int fd)` 42 | - 将 `fd` 设置为非阻塞模式(`O_NONBLOCK`)。 43 | - `void addfd(int epollfd, int fd, bool one_shot)` 44 | - 将 `fd` 以 `EPOLLIN | EPOLLRDHUP` 事件加入 `epollfd`;如 `one_shot==true`,附加 `EPOLLONESHOT`;并自动设置非阻塞。 45 | - `void removefd(int epollfd, int fd)` 46 | - 从 `epollfd` 删除 `fd` 并 `close(fd)`。 47 | - `void modfd(int epollfd, int fd, int ev)` 48 | - 使用 `EPOLL_CTL_MOD` 修改 `fd` 的事件为 `ev | EPOLLONESHOT | EPOLLRDHUP`。 49 | 50 | 示例: 51 | ```cpp 52 | int efd = epoll_create1(0); 53 | int fd = ::socket(AF_INET, SOCK_STREAM, 0); 54 | addfd(efd, fd, true); // 注册读事件,one-shot 55 | modfd(efd, fd, EPOLLOUT); // 切换为可写 56 | removefd(efd, fd); // 从 epoll 删除并关闭 57 | ``` 58 | 59 | --- 60 | 61 | ## 线程同步(`locker.h`) 62 | 63 | - `class mutex` 64 | - `bool lock()` / `bool unlock()`:加锁/解锁。 65 | - `class cond` 66 | - `bool wait(pthread_mutex_t *m)`:在外部互斥量上等待。 67 | - `bool signal()`:唤醒一个等待线程。 68 | - `bool broadcast()`:唤醒全部等待线程(当前实现等价于 `signal()`,如需广播可自行扩展)。 69 | - `class sem` 70 | - 构造:`sem()`(初始值0)/ `sem(int num)`(初始值num) 71 | - `bool wait()`:P 操作,计数-1,为0则阻塞。 72 | - `bool post()`:V 操作,计数+1,唤醒等待线程。 73 | 74 | 示例: 75 | ```cpp 76 | mutex m; 77 | sem s(0); 78 | 79 | // 生产者 80 | auto producer = [&]{ /* ... */ s.post(); }; 81 | // 消费者 82 | auto consumer = [&]{ s.wait(); /* ...处理... */ }; 83 | ``` 84 | 85 | --- 86 | 87 | ## 请求队列:`questqueue`(`questqueue.h`) 88 | 89 | 基于 `std::list` + `mutex` + `sem` 的有界阻塞队列。 90 | 91 | - 构造:`questqueue(int max_queue)`(`max_queue > 0`) 92 | - `bool push(T *quest)` 93 | - 满则返回 `false`,否则入队并 `post()` 通知。 94 | - `T* pop()` 95 | - 阻塞等待(`wait()`)直至有任务;返回队首元素。 96 | 97 | 示例: 98 | ```cpp 99 | questqueue q(1024); 100 | q.push(new MyTask(/*...*/)); 101 | MyTask* t = q.pop(); 102 | ``` 103 | 104 | --- 105 | 106 | ## 线程池:`threadpool`(`pthreadpool.h`) 107 | 108 | 通用固定大小线程池,工作线程循环从 `questqueue` 取任务并调用 `T::process()`。 109 | 110 | - 构造:`threadpool(int poolsize = 8, int maxquest = 1000)` 111 | - 析构:结束标志置位并释放线程句柄。 112 | - `bool append(T *quest)`:将任务入队(队满则 `false`)。 113 | 114 | 用法要点: 115 | - `T` 必须提供 `void process()` 成员函数。 116 | - 线程创建后立即 `pthread_detach`。 117 | 118 | 示例: 119 | ```cpp 120 | struct Job { void process(){ /* 业务逻辑 */ } }; 121 | threadpool pool(8, 1000); 122 | Job* j = new Job(); 123 | pool.append(j); 124 | ``` 125 | 126 | --- 127 | 128 | ## HTTP 连接:`http_conn`(`http_conn.h`/`http_conn.cpp`) 129 | 130 | 单连接请求的解析与响应构造。面向 Proactor:IO(读/写)由外层事件循环驱动,`http_conn` 专注于解析与生成响应。 131 | 132 | - 静态: 133 | - `static int st_m_epollfd`:共享 epoll fd。 134 | - `static int st_m_usercount`:当前连接数。 135 | - 生命周期: 136 | - `void init(int sockfd, const sockaddr_in &addr)`:加入 epoll(`EPOLLONESHOT`),设置端口复用与初始状态。 137 | - `void clear()`:复位内部状态与缓冲(供复用)。 138 | - `void close_conn()`:从 epoll 移除并关闭 fd,连接计数-1。 139 | - 处理流程: 140 | - `void process()`:主入口。调 `process_read()` 解析;若请求完整则 `process_write()` 生成响应并切换 `EPOLLOUT`。 141 | - 读写(对外可见的只读接口/查询): 142 | - 一系列 `get_*()` 访问器、`set_read_index(int)` 设值器。 143 | - 说明:头文件声明了 `bool Write()`,但当前版本的实际写入由 `epoll_class::Write(http_conn&)` 统一完成,该成员未在本仓库实现。 144 | 145 | 响应特性: 146 | - 仅支持 `GET`;默认不开启 `Keep-Alive`,当请求 `Connection: keep-alive` 时保持连接。 147 | - 文件根目录固定为 `resources/`(见 `http_conn.cpp` 中 `root_directory`)。 148 | - 成功(200)时使用 `writev` 分散写:报文头 + `mmap` 的文件内容。 149 | 150 | --- 151 | 152 | ## 事件主循环:`epoll_class`(`epoll_class.h`) 153 | 154 | 封装服务器整体:监听、accept、读/写事件分发、线程池调度。 155 | 156 | - 构造:`epoll_class(int port)` 157 | - 初始化:忽略 `SIGPIPE`、创建 `threadpool`(默认 8 线程/队列 1000)、监听 `port`、设置复用、加到 epoll。 158 | - 运行:`void run()` 159 | - `epoll_wait` 事件循环: 160 | - `EPOLLIN`:非阻塞读(内部 `Read(http_conn&)`),成功则将连接对象指针提交到线程池,在线程中执行 `http_conn::process()`; 161 | - `EPOLLOUT`:非阻塞写(内部 `Write(http_conn&)`),完成后依据 keep-alive 决定是否关闭; 162 | - 异常事件:关闭连接; 163 | - 新连接:`accept` 后调用 `http_conn::init(connfd, client_addr)`。 164 | - 析构:关闭 epoll/监听 fd,释放连接数组与线程池。 165 | 166 | 示例: 167 | ```cpp 168 | #include "epoll_class.h" 169 | 170 | int main(int argc, char** argv){ 171 | int port = 8080; // 或从命令行读取 172 | epoll_class server(port); 173 | server.run(); 174 | } 175 | ``` 176 | 177 | 扩展建议: 178 | - 可在 `epoll_class` 构造中将 `pool = new threadpool(threads, maxQueue);` 调整为期望规模。 179 | - 若需自定义静态目录,修改 `http_conn.cpp` 的 `root_directory`。 180 | 181 | --- 182 | 183 | ## 常见问答 184 | 185 | - Q:如何开启 Keep-Alive? 186 | - A:客户端请求头包含 `Connection: keep-alive` 时,服务端保持连接;否则写完即关。 187 | - Q:为什么我看不到 `http_conn::Write()` 的实现? 188 | - A:当前发送逻辑集中在 `epoll_class::Write(http_conn&)`,`Write()` 原型保留但未实现,后续可移除或实现为转发。 189 | - Q:如何增大线程池或请求队列? 190 | - A:修改 `epoll_class` 构造中线程池初始化参数,或在 `pthreadpool.h` 中调整默认值。 191 | -------------------------------------------------------------------------------- /epoll_class.h: -------------------------------------------------------------------------------- 1 | #ifndef _EPOLL_CLASS_ 2 | #define _EPOLL_CLASS_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include "http_conn.h" 18 | #include "locker.h" 19 | #include "pthreadpool.h" 20 | #include "socket_control.h" 21 | 22 | class epoll_class:public base 23 | { 24 | public: 25 | epoll_class(int port); 26 | ~epoll_class(); 27 | void run(); 28 | private: 29 | //================== const ==================== 30 | // 最大的文件描述符个数 31 | static const int MAX_FD = 65535; 32 | // 最大的监听的事件数量 33 | static const int MAX_EVENT_NUMBER = 10000; 34 | 35 | //================== value ==================== 36 | //线程池 37 | threadpool * pool; 38 | // 创建一个数组用于保存所有的客户端信息 39 | http_conn *users; 40 | //监听的套接字 41 | int listenfd; 42 | // 创建epoll对象和事件数组 43 | epoll_event events[MAX_EVENT_NUMBER]; 44 | int epollfd; 45 | 46 | //================== function ==================== 47 | // 添加信号捕捉 48 | void addsig(int sig, void(handler)(int)); 49 | // 非阻塞的读,循环读取 50 | bool Read(http_conn &conn); 51 | // 非阻塞的读 52 | bool Write(http_conn &conn); 53 | }; 54 | // 添加信号捕捉 55 | void epoll_class::addsig(int sig, void(handler)(int)) 56 | { 57 | struct sigaction sa; 58 | memset(&sa,'\0',sizeof(sa)); 59 | sa.sa_handler=handler; 60 | sigfillset(&sa.sa_mask); 61 | // 应用信号捕捉 62 | sigaction(sig,&sa,NULL); 63 | } 64 | // 非阻塞的读,循环读取 65 | bool epoll_class::Read(http_conn &conn) 66 | { 67 | int m_sockfd=conn.get_sockfd(); 68 | int m_read_index=conn.get_read_index(); 69 | char *m_read_buf=conn.get_read_buf(); 70 | 71 | // 缓冲已满 72 | if(m_read_index >= READ_BUFFER_SIZE){ 73 | return false; 74 | } 75 | // 读取字节 76 | while(true) 77 | { 78 | int bytes_read=recv(m_sockfd,m_read_buf+m_read_index,READ_BUFFER_SIZE-m_read_index,0); 79 | if(bytes_read==-1){ 80 | if(errno==EAGAIN || errno==EWOULDBLOCK) 81 | break; 82 | else 83 | return false; 84 | } 85 | else if(bytes_read==0){ 86 | return false; 87 | } 88 | m_read_index+=bytes_read; 89 | } 90 | // 更新值 91 | //printf("read data:\n%s",m_read_buf); 92 | conn.set_read_index(m_read_index); 93 | return true; 94 | } 95 | // 非阻塞的写 96 | bool epoll_class::Write(http_conn &conn) 97 | { 98 | const int m_sockfd = conn.get_sockfd(); 99 | const int bytes_to_send = conn.get_bytes_to_send(); 100 | iovec *m_iv = conn.get_iv(); 101 | const int m_iv_count = conn.get_iv_count(); 102 | 103 | if(bytes_to_send==0){ 104 | // 将要发送的字节为0 105 | modfd(epollfd,m_sockfd,EPOLLIN); 106 | conn.clear(); 107 | return true; 108 | } 109 | while (true) 110 | { 111 | int ret = writev(m_sockfd, m_iv, m_iv_count); 112 | if (ret <= -1) 113 | { 114 | // 发送失败 115 | if (errno == EAGAIN) 116 | {// 重试 117 | modfd(epollfd, m_sockfd, EPOLLOUT); 118 | return true; 119 | } 120 | return false; 121 | } 122 | // 本次写成功 123 | // 维护还需发送字节数和已发送字节数 124 | 125 | //分散写第一部分是否写完 126 | if (ret >= m_iv[0].iov_len) 127 | { // 第一部分写完了 128 | m_iv[1].iov_base = (char *)m_iv[1].iov_base + (ret - m_iv[0].iov_len); 129 | m_iv[1].iov_len -= (ret - m_iv[0].iov_len); 130 | m_iv[0].iov_len = 0; 131 | } 132 | else 133 | { // 第一部分还没写完 134 | m_iv[0].iov_base = (char *)(m_iv[0].iov_base) + ret; 135 | m_iv[0].iov_len -= ret; 136 | } 137 | 138 | // 发送结束 139 | if (m_iv[1].iov_len<=0) 140 | { // 发送HTTP响应成功,释放内存 141 | modfd(epollfd, m_sockfd, EPOLLIN); 142 | // 是否keep-alive 143 | if (conn.is_keepalive()) 144 | { 145 | conn.clear(); 146 | // 继续接受信息 147 | return true; 148 | } 149 | else 150 | { 151 | return false; 152 | } 153 | } 154 | } 155 | } 156 | 157 | epoll_class::epoll_class(int port) 158 | { 159 | // 对SIGPIPE信号处理 160 | // 避免因为SIGPIPE退出 161 | addsig(SIGPIPE,SIG_IGN); 162 | 163 | // 创建线程池,初始化线程池 164 | pool=NULL; 165 | try{ 166 | pool=new threadpool; 167 | } 168 | catch(const char* msg){ 169 | printf("error:%s\n",msg); 170 | exit(-1); 171 | } 172 | 173 | // 创建一个数组用于保存所有的客户端信息 174 | users=new http_conn[ MAX_FD ]; 175 | 176 | // 进行网络通信 177 | // 创建监听的套接字 178 | listenfd=socket(AF_INET,SOCK_STREAM,0); 179 | if(listenfd==-1){ 180 | perror("socket"); 181 | exit(-1); 182 | } 183 | 184 | //设置端口复用(在绑定之前) 185 | int reuse=1; 186 | setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)); 187 | 188 | // 绑定 189 | struct sockaddr_in addr; 190 | addr.sin_family=AF_INET; 191 | addr.sin_addr.s_addr=INADDR_ANY; 192 | addr.sin_port=htons(port); 193 | int ret=bind(listenfd,(struct sockaddr*)&addr,sizeof(addr)); 194 | if(ret==-1){ 195 | perror("bind");exit(-1); 196 | } 197 | 198 | // 监听 199 | ret=listen(listenfd,5); 200 | if(ret==-1){ 201 | perror("listen");exit(-1); 202 | } 203 | 204 | // 创建epoll 205 | epollfd=epoll_create(100); 206 | 207 | // 将监听的文件描述符添加到epoll中 208 | addfd(epollfd,listenfd,false); 209 | http_conn::st_m_usercount=0; 210 | http_conn::st_m_epollfd=epollfd; 211 | 212 | } 213 | void epoll_class::run() 214 | { 215 | // 进行监听 216 | while(true) 217 | { 218 | int num=epoll_wait(epollfd,events,MAX_EVENT_NUMBER,-1); 219 | if(num<0 && errno!=EINTR) 220 | { 221 | printf("epoll fail\n"); 222 | break; 223 | } 224 | 225 | // 循环遍历事件数组 226 | for(int i=0;i=MAX_FD) 243 | { 244 | // ?回写数据:服务器正忙 245 | // 连接满了 246 | close(connfd); 247 | continue; 248 | } 249 | // 将新的客户的数据初始化,放到数组中 250 | users[connfd].init(connfd,client_addr); 251 | } 252 | // 不是连接类型 253 | else if(events[i].events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR)) 254 | {// 异常断开等错误事件 255 | users[sockfd].close_conn(); 256 | } 257 | else if(events[i].events & EPOLLIN) 258 | { 259 | if(Read(users[sockfd])){ 260 | // 读完了 261 | pool->append(users+sockfd); 262 | } 263 | else{ 264 | // 读失败了 265 | users[sockfd].close_conn(); 266 | } 267 | } 268 | else if(events[i].events & EPOLLOUT) 269 | { 270 | if(Write(users[sockfd])){ 271 | // 写成功 272 | ; 273 | } 274 | else{ 275 | // 写失败了 / 写完不保持连接 276 | users[sockfd].close_conn(); 277 | } 278 | } 279 | } 280 | } 281 | } 282 | epoll_class::~epoll_class() 283 | { 284 | // 结束后close 285 | close(epollfd); 286 | close(listenfd); 287 | delete[] users; 288 | delete pool; 289 | } 290 | 291 | #endif -------------------------------------------------------------------------------- /http_conn.cpp: -------------------------------------------------------------------------------- 1 | #include "http_conn.h" 2 | 3 | // #define patse_message 0 4 | // #define check_write_header 0 5 | // #define check_write_content 1 6 | // #define show_read_data 1 7 | // #define process_read_result 1 8 | // #define mmap_print 1 9 | // #define print_writev_result 1 10 | 11 | //================== 初始化 ==================== 12 | int http_conn::st_m_epollfd = -1; 13 | int http_conn::st_m_usercount = 0; 14 | 15 | //================== 静态数据 ==================== 16 | const char *root_directory = "resources"; 17 | 18 | // 初始化新接收的连接 19 | void http_conn::init(int sockfd, const sockaddr_in &addr) 20 | { 21 | m_sockfd = sockfd; 22 | m_address = addr; 23 | 24 | // 端口复用 25 | int reuse = 1; 26 | setsockopt(m_sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)); 27 | 28 | // 添加到epoll对象 29 | // 添加文件描述符信息到epoll 30 | addfd(st_m_epollfd, m_sockfd, true); 31 | st_m_usercount++; 32 | 33 | clear(); 34 | } 35 | 36 | // 初始化连接 37 | void http_conn::clear() 38 | { 39 | unmap(); 40 | 41 | m_check_state = CHECK_STATE_REQUESTLINE; 42 | m_checked_index = 0; 43 | m_start_line = 0; 44 | m_read_index = 0; 45 | m_write_index = 0; 46 | bytes_to_send = 0; 47 | 48 | m_iv_count = 0; 49 | m_iv[0].iov_len = m_iv[1].iov_len = 0; 50 | m_url = NULL; 51 | m_method = GET; 52 | m_version = 0; 53 | // HTTP 1.1 中默认启用Keep-Alive,如果加入"Connection: close ",才关闭 54 | m_keepalive = false; 55 | 56 | m_host = NULL; 57 | m_content_length = 0; 58 | m_address_mmap = NULL; 59 | 60 | bzero(m_read_buf, READ_BUFFER_SIZE); 61 | bzero(m_write_buf, WRITE_BUFFER_SIZE); 62 | bzero(&m_address, sizeof(m_address)); 63 | bzero(m_iv, sizeof(m_iv)); 64 | bzero(m_filename, FILENAME_MAXLEN); 65 | } 66 | 67 | // 关闭连接 68 | void http_conn::close_conn() 69 | { 70 | if (m_sockfd != -1) 71 | { 72 | removefd(st_m_epollfd, m_sockfd); 73 | m_sockfd = -1; // 重置fd 74 | clear(); 75 | st_m_usercount--; // 维护cnt 76 | } 77 | } 78 | 79 | // 对内存映射区进行释放 80 | void http_conn::unmap() 81 | { 82 | if (m_address_mmap) 83 | { 84 | int ret = munmap(m_address_mmap, m_file_stat.st_size); 85 | if (ret == -1) 86 | perror("mmap"); 87 | m_address_mmap = NULL; 88 | } 89 | } 90 | 91 | // 处理客户端的请求 92 | // 业务逻辑 93 | void http_conn::process() 94 | { 95 | // 解析HTTP请求 96 | HTTP_CODE read_ret = process_read(); 97 | #ifdef process_read_result 98 | printf("process_read result : %d\n", read_ret); 99 | #endif 100 | if (read_ret == NO_REQUEST) 101 | { // 读的没有问题,则修改fd,让其再次使用 102 | modfd(st_m_epollfd, m_sockfd, EPOLLIN); 103 | return; 104 | } 105 | 106 | // 生成响应报文 107 | bool write_ret = process_write(read_ret); 108 | if (!write_ret) 109 | { 110 | close_conn(); 111 | } 112 | 113 | // close之后时候要执行modfd 114 | modfd(st_m_epollfd, m_sockfd, EPOLLOUT); 115 | } 116 | 117 | //============== 主状态机 =================== 118 | // 解析HTTP请求 119 | http_conn::HTTP_CODE http_conn::process_read() 120 | { 121 | LINE_STATUS line_status = LINE_OK; 122 | HTTP_CODE ret = NO_REQUEST; 123 | char *text = NULL; 124 | 125 | while ((m_check_state == CHECK_STATE_CONTENT && line_status == LINE_OK) \ 126 | || ((line_status = parse_line()) == LINE_OK)) 127 | { // 解析到了一行完整的数据 或者解析到了请求体,也是完整的数据 128 | // 获取一行数据 129 | text = get_line(); 130 | #ifdef patse_message 131 | printf("\n即将解析的数据: %s\n", text); 132 | #endif 133 | m_start_line = m_checked_index; 134 | // printf("got 1 http line : %s\n",text); 135 | 136 | switch (m_check_state) 137 | { 138 | case CHECK_STATE_REQUESTLINE: 139 | { 140 | ret = parse_request_line(text); 141 | // 分析返回值 142 | if (ret == BAD_REQUEST) 143 | { 144 | return BAD_REQUEST; 145 | } 146 | break; 147 | } 148 | case CHECK_STATE_HEADER: 149 | { 150 | ret = parse_headers(text); 151 | if (ret == BAD_REQUEST) 152 | { 153 | return BAD_REQUEST; 154 | } 155 | else if (ret == GET_REQUEST) 156 | { 157 | // 解析到完整的请求头 158 | return do_request(); 159 | } 160 | break; 161 | } 162 | case CHECK_STATE_CONTENT: 163 | { 164 | ret = parse_content_complete(text); 165 | if (ret == BAD_REQUEST){ 166 | return BAD_REQUEST; 167 | } 168 | else if (ret == GET_REQUEST){ 169 | // 解析到完整的请求头 170 | return do_request(); 171 | } 172 | line_status = LINE_OPEN; 173 | break; 174 | } 175 | default:{ 176 | return INTERNAL_ERROR; 177 | } 178 | } 179 | } 180 | return NO_REQUEST; 181 | } 182 | // 解析HTTP请求行,获取请求方法 ,目标URL,HTTP版本 183 | http_conn::HTTP_CODE http_conn::parse_request_line(char *text) 184 | { 185 | 186 | // 1.解析请求行 187 | // GET /index.html HTTP/1.1 188 | // 1.1 请求方法 URL 协议版本 189 | // 初始化以及填入\0进行分隔 190 | char *index=text; 191 | // method 192 | char *method = text; 193 | // m_url 194 | index=strpbrk(index, " \t"); 195 | if (!index) return BAD_REQUEST; 196 | *(index++) = '\0'; // 填充以及移动 197 | m_url=index; 198 | // m_version 199 | index=strpbrk(index, " \t"); 200 | if (!index) return BAD_REQUEST; 201 | *(index++) = '\0'; // 填充以及移动 202 | m_version=index; 203 | 204 | // 1.2 进行分析判断和进一步处理 205 | // method,只允许GET 206 | if (strcasecmp(method, "GET") == 0){ 207 | m_method = GET; 208 | } 209 | else{ 210 | return BAD_REQUEST; 211 | } 212 | // url分析 213 | // 比如http://192.168.110.129:10000/index.html 需要去掉 http:// 这7个字符 214 | if (strncasecmp(m_url, "http://", 7) == 0) 215 | { 216 | m_url += 7; 217 | // 跳到第一个/的位置 218 | m_url = strchr(m_url, '/'); 219 | } 220 | if (!m_url || m_url[0] != '/') 221 | return BAD_REQUEST; 222 | // version分析 223 | if (strcasecmp(m_version, "HTTP/1.1") != 0) 224 | return BAD_REQUEST; 225 | 226 | 227 | // 2.更新检测状态,检测完请求行以后需要检测请求头 228 | m_check_state = CHECK_STATE_HEADER; 229 | 230 | // 3.return 231 | #ifdef patse_message 232 | printf("请求头解析成功\n url:%s,version:%s,method:%s\n", m_url, m_version, method); 233 | #endif 234 | return NO_REQUEST; 235 | } 236 | // 解析HTTP请求头 237 | http_conn::HTTP_CODE http_conn::parse_headers(char *text) 238 | { 239 | #ifdef patse_message 240 | printf("分析请求头 : %s\n", text); 241 | #endif 242 | /** 243 | * "Connection:" 244 | * "Content-Length:" 245 | * "Host:" 246 | */ 247 | // 被perse_line处理过后,若text为空行,说明请求头已经结束 248 | if (text[0] == '\0') 249 | { 250 | // 若HTTP请求有消息体(m_content_length!=0) 则继续读取消息体 251 | if (m_content_length != 0) 252 | { 253 | m_check_state = CHECK_STATE_CONTENT; 254 | return NO_REQUEST; 255 | } 256 | // 否则读完成 257 | return GET_REQUEST; 258 | } 259 | else if (strncasecmp(text, "Connection:", 11) == 0) 260 | { 261 | text += 11; // 去除key 262 | text += strcspn(text, " \t"); // 去除开头的空格和\t 263 | 264 | if (strcasecmp(text, "keep-alive") == 0) 265 | { 266 | m_keepalive = true; 267 | } 268 | else if (strcasecmp(text, "close") == 0) 269 | { 270 | m_keepalive = false; 271 | } 272 | } 273 | else if (strncasecmp(text, "Content-Length:", 15) == 0) 274 | { 275 | text += 15; // 去除key 276 | text += strcspn(text, " \t"); // 去除开头的空格和\t 277 | m_content_length = atol(text); 278 | } 279 | else if (strncasecmp(text, "Host:", 5) == 0) 280 | { 281 | text += 5; // 去除key 282 | text += strcspn(text, " \t"); // 去除开头的空格和\t 283 | m_host = text; 284 | } 285 | else 286 | { 287 | #ifdef patse_message 288 | printf("解析失败,不知名的请求头: %s\n", text); 289 | #endif 290 | } 291 | return NO_REQUEST; 292 | } 293 | // 解析HTTP请求内容 294 | http_conn::HTTP_CODE http_conn::parse_content_complete(char *text) 295 | { 296 | // 根据m_content_length查看内容是否完全读入 297 | if (m_read_index >= (m_checked_index + m_content_length)) 298 | { 299 | // 数据完整 300 | text[m_content_length] = '\0'; 301 | return GET_REQUEST; 302 | }; 303 | // 返回不完整 304 | return NO_REQUEST; 305 | } 306 | 307 | // 在分析完成以后进行具体的处理 308 | http_conn::HTTP_CODE http_conn::do_request() 309 | { 310 | // 更新 311 | int sumlen = strlen(m_url) + strlen(root_directory) + 1; 312 | snprintf(m_filename, std::min((int)(FILENAME_MAXLEN), sumlen), "%s%s", root_directory, m_url); 313 | printf("m_filename :%s\n", m_filename); 314 | // m_filename = "resources" + "/xxxxxxx" 315 | 316 | // 获取文件相关信息 317 | int ret = stat(m_filename, &m_file_stat); 318 | if (ret == -1) 319 | { 320 | perror("stat"); 321 | return NO_RESOURCE; 322 | } 323 | 324 | // 判断访问权限 325 | if (!(m_file_stat.st_mode & S_IROTH)) 326 | { 327 | return FORBIDDEN_REQUEST; 328 | } 329 | 330 | // 判断是否是目录 331 | if (S_ISDIR(m_file_stat.st_mode)) 332 | { 333 | return BAD_REQUEST; 334 | } 335 | 336 | // 对文件操作 只读方式打开 337 | int fd = open(m_filename, O_RDONLY); 338 | // 创建内存映射 339 | m_address_mmap = (char *)mmap(0, m_file_stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 340 | #ifdef mmap_print 341 | printf("\nmmap :==================\n %s\n\n", m_address_mmap); 342 | #endif 343 | close(fd); 344 | 345 | return FILE_REQUEST; // 获取文件成功 346 | } 347 | 348 | //================== 从状态机 ==================== 349 | http_conn::LINE_STATUS http_conn::parse_line() 350 | { 351 | // 根据\r\n 352 | char temp; 353 | 354 | for (; m_checked_index < m_read_index; ++m_checked_index) 355 | { 356 | temp = m_read_buf[m_checked_index]; 357 | if (temp == '\r') 358 | { // 遇到'\r' 进行判断 359 | if ((m_checked_index + 1) == m_read_index){ 360 | // '\r'为最后一个,说明有数据未完 361 | return LINE_OPEN; 362 | } 363 | else if (m_read_buf[m_checked_index + 1] == '\n') 364 | { 365 | // 完整的一句,将 \r\n 变为 \0 366 | m_read_buf[m_checked_index++] = '\0'; 367 | m_read_buf[m_checked_index++] = '\0'; 368 | // 相当于 x x+1 = \0 then x+=2 369 | return LINE_OK; 370 | } 371 | return LINE_BAD; 372 | } 373 | else if (temp == '\n'){ 374 | if ((m_checked_index > 1 && m_read_buf[m_checked_index - 1] == '\r')) 375 | { // 这次的第一个和上一次的最后一个是一个分隔 376 | m_read_buf[m_checked_index - 1] = '\0'; 377 | m_read_buf[m_checked_index++] = '\0'; 378 | return LINE_OK; 379 | } 380 | } 381 | } 382 | return LINE_OPEN; 383 | } 384 | 385 | //================== 写入部分 ==================== 386 | // 往写缓冲区中写数据 387 | bool http_conn::add_response(const char *format, ...) 388 | { 389 | if (m_write_index >= WRITE_BUFFER_SIZE) 390 | { 391 | return false; // 已满 392 | } 393 | va_list args; 394 | va_start(args, format); 395 | int len = vsnprintf(m_write_buf + m_write_index, WRITE_BUFFER_SIZE - m_write_index - 1, format, args); 396 | // vsnprintf 用法类似snprintf,输入的最大长度为__maxlen-1 397 | // 调用args和format进行可变参数输入 398 | // 返回值为若空间足够则输入的长度 399 | if (len > WRITE_BUFFER_SIZE - m_write_index - 1) 400 | { 401 | // 说明输入的字符溢出 402 | return false; 403 | } 404 | m_write_index += len; // 更新写缓冲区的长度 405 | va_end(args); 406 | return true; 407 | } 408 | // 添加状态行 409 | bool http_conn::add_status_line(int status, const char *title) 410 | { 411 | return add_response("%s %d %s\r\n", "HTTP/1.1", status, title); 412 | } 413 | // 添加响应头部 414 | bool http_conn::add_headers(int content_len, time_t time) 415 | { 416 | if (!add_content_length(content_len)) return false; 417 | if (!add_content_type()) return false; 418 | if (!add_connection()) return false; 419 | if (!add_date(time)) return false; 420 | if (!add_blank_line()) return false; 421 | return true; 422 | } 423 | // 响应头部组件 424 | // content-length 425 | bool http_conn::add_content_length(int content_len) 426 | { 427 | return add_response("Content-Length: %d\r\n", content_len); 428 | } 429 | // Content-Type 430 | bool http_conn::add_content_type() 431 | { 432 | // 虑区分是图片 / html/css 433 | char *format_file = strrchr(m_filename, '.'); 434 | return add_response("Content-Type: %s\r\n", format_file == NULL ? "text/html" : (format_file + 1)); 435 | } 436 | // keep_alive / close 437 | bool http_conn::add_connection() 438 | { 439 | return add_response("Connection: %s\r\n", (m_keepalive == true) ? "keep-alive" : "close"); 440 | } 441 | // 发送时间 442 | bool http_conn::add_date(time_t t) 443 | { 444 | char timebuf[50]; 445 | strftime(timebuf, 80, "%Y-%m-%d %H:%M:%S", localtime(&t)); 446 | return add_response("Date: %s\r\n", timebuf); 447 | } 448 | // 空白结束行 449 | bool http_conn::add_blank_line() 450 | { 451 | return add_response("\r\n"); 452 | } 453 | // 添加响应正文 454 | // bool http_conn::add_content(const char *content) 455 | // { 456 | // return add_response("%s", content); 457 | // } 458 | 459 | //================== 生成返回的报文 ==================== 460 | bool http_conn::process_write(HTTP_CODE ret) 461 | { 462 | /* 463 | NO_REQUEST : 请求不完整,需要继续读取客户数据 464 | GET_REQUEST : 表示获得了一个完成的客户请求 465 | BAD_REQUEST : 表示客户请求语法错误 466 | NO_RESOURCE : 表示服务器没有资源 467 | FORBIDDEN_REQUEST : 表示客户对资源没有足够的访问权限 468 | FILE_REQUEST : 文件请求,获取文件成功 469 | INTERNAL_ERROR : 表示服务器内部错误 470 | CLOSED_CONNECTION : 表示客户端已经关闭连接了 471 | */ 472 | 473 | int status = std::get<0>(response_info[ret]); 474 | const char *title = std::get<1>(response_info[ret]); 475 | const char *form = std::get<2>(response_info[ret]); 476 | if (ret == FILE_REQUEST) 477 | { // OK,发送报文头和文件 478 | if (!add_status_line(status, title)) 479 | return false; 480 | if (!add_headers(m_file_stat.st_size, time(NULL))) 481 | return false; // 发送本地时间 482 | #ifdef check_write_header 483 | if (check_write_header) 484 | { 485 | printf("OK 的 报文头:\n"); 486 | write(STDOUT_FILENO, m_write_buf, m_write_index); 487 | } 488 | #endif 489 | m_iv[0].iov_base = m_write_buf; 490 | m_iv[0].iov_len = m_write_index; 491 | m_iv[1].iov_base = m_address_mmap; 492 | m_iv[1].iov_len = m_file_stat.st_size; 493 | m_iv_count = 2; 494 | 495 | // 维护发送长度 496 | bytes_to_send = m_write_index + m_file_stat.st_size; 497 | 498 | return true; 499 | } 500 | else if (response_info.find(ret) != response_info.end()) 501 | { // 发送错误信息 502 | if (!add_status_line(status, title)) 503 | return false; 504 | if (!add_headers(strlen(form), time(NULL))) 505 | return false; // 发送本地时间 506 | 507 | m_iv[0].iov_base = m_write_buf; 508 | m_iv[0].iov_len = m_write_index; 509 | m_iv[1].iov_base = (char *)form; 510 | m_iv[1].iov_len = strlen(form) + 1; 511 | m_iv_count = 2; 512 | // 维护发送长度 513 | bytes_to_send = m_iv[0].iov_len + m_iv[1].iov_len; 514 | 515 | // 若使用add_content 516 | // if (!add_content(form)) return false; 517 | // m_iv[0].iov_base = m_write_buf; 518 | // m_iv[0].iov_len = m_write_index; 519 | // m_iv_count = 1; 520 | // // 维护发送长度 521 | // bytes_to_send = m_write_index; 522 | 523 | return true; 524 | } 525 | else 526 | return false; 527 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------