1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
| #include <stdio.h> #include <unistd.h> #include <sys/socket.h> #include <stdlib.h> #include <arpa/inet.h> #include <ctype.h> #include <assert.h> #include <netinet/in.h> #include <errno.h> #include <string.h> #include <pthread.h> #include <fcntl.h> #include <sys/epoll.h> #include <sys/types.h> #include <sys/wait.h>
#define MAX_SIZE 1024
struct fds{ int epollfd; int sockfd; };
void setnonblocking(int fd){ int old_option = fcntl(fd, F_GETFL); int new_option = old_option | O_NONBLOCK; fcntl(fd, F_SETFL, new_option); }
void addfd(int epollfd, int fd, bool oneshot){ epoll_event event; event.data.fd = fd; event.events = EPOLLIN | EPOLLET; if(oneshot){ event.events |= EPOLLONESHOT; } epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event); setnonblocking(fd); }
void removefd(int epollfd, int fd){ epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, NULL); close(fd); }
void reset_oneshot(int epollfd, int fd){ epoll_event event; event.data.fd = fd; event.events = EPOLLIN | EPOLLET | EPOLLONESHOT; epoll_ctl(epollfd, EPOLL_CTL_MOD, fd, &event); } void accept_request(int epollfd, int sockfd, char *s){ char method[32], filename[32], account[32], password[32], phone[32]; sscanf(s, "%s /%s", method, filename); if(strcasecmp(method, "POST") == 0){ int len = strlen(s); int i; for(i = 0; i < len; i++){ if(i+2<len && s[i]=='\n' && s[i+1]=='\r') break; } int j; i=i+12; for(j = 0; s[i+j]!='&'; j++) account[j] = s[j+i]; i = i+j+6; for(j = 0; s[i+j]!='&'; j++) password[j] = s[j+i]; pid_t pid; int status; int cgi_input[2]; int cgi_output[2]; char buff[1024]; if(pipe(cgi_input) < 0){ perror("pipe error"); exit(1); } if(pipe(cgi_output) < 0){ perror("pipe error"); exit(1); } if((pid = fork()) < 0){ perror("fork error!"); exit(-1); } if(pid == 0){ dup2(cgi_input[0], 0); dup2(cgi_output[1], 1); close(cgi_input[1]); close(cgi_output[0]); execl("hello", "anythingok", NULL); exit(0); }else if(pid > 0){ close(cgi_input[0]); close(cgi_output[1]); int len = strlen(account); write(cgi_input[1], account, len); int n = read(cgi_output[0], buff, sizeof(buff)); if(strcmp(buff, password)==0&&strlen(password) != 0){ puts("密码正确"); }else{ puts("密码错误"); strcpy(filename, "register.html"); } close(cgi_input[1]); close(cgi_output[0]); waitpid(pid, &status, 0); }
} puts(filename); char mime[64]; if(strstr(s, ".html")) strcpy(mime,"text/html"); else if(strstr(s, ".jpg")) strcpy(mime,"image/jpeg"); char response[MAX_SIZE+MAX_SIZE]; sprintf(response, "HTTP/1.1 200 OK\r\nContent-Type: %s\r\n\r\n", mime); int filefd = open(filename, O_RDONLY); if(filefd == -1){ perror("open error"); exit(1); } int len = strlen(response); int n = read(filefd, response+len, sizeof(response)-len); write(sockfd, response, len+n); close(filefd); }
void* worker(void* arg){ int sockfd = ((fds*)arg)->sockfd; int epollfd = ((fds*)arg)->epollfd; char buf[MAX_SIZE]; memset(buf, '\0', MAX_SIZE); while(1){ int ret = recv(sockfd, buf, MAX_SIZE-1, 0); if(ret == 0){ removefd(epollfd, sockfd); printf("foreiner closed the connection\n"); break; }else if(ret < 0){ if(errno == EAGAIN){ printf("read later\n"); break; } }else{ printf("get connection:\n"); accept_request(epollfd, sockfd, buf); sleep(5); break; } } }
int main(int argc, char *argv[]){ if(argc < 2){ printf("at least 2 arguments: file port, but you give %d\n", argc); return 1; } int port = atoi(argv[1]); int lfd, cfd, ret; struct sockaddr_in serv_addr, clie_addr; lfd = socket(AF_INET, SOCK_STREAM, 0); assert(lfd >= 0); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(port); serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); int opt = 1; setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); ret = bind(lfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr) ); assert(ret != -1); ret = listen(lfd, 128); assert(ret != -1); epoll_event events[MAX_SIZE]; int epollfd = epoll_create(10); assert(epollfd != -1); addfd(epollfd, lfd, false); while(1){ ret = epoll_wait(epollfd, events, MAX_SIZE, -1); if(ret < 0){ printf("epoll failure\n"); break; } for(int i = 0; i < ret; i++){ int sockfd = events[i].data.fd; if(sockfd == lfd){ socklen_t clie_addr_len; clie_addr_len = sizeof(clie_addr); cfd = accept(lfd, (struct sockaddr*)&clie_addr, &clie_addr_len ); assert(cfd >= 0); addfd(epollfd, cfd, false); }else if(events[i].events & EPOLLIN){ pthread_t tid; fds fds_for_new_worker; fds_for_new_worker.epollfd = epollfd; fds_for_new_worker.sockfd = sockfd; pthread_create(&tid, NULL, worker, (void*)&fds_for_new_worker); } } } close(lfd); return 0; }
|