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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
| #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <fcntl.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <sys/stat.h> #include <string.h> #include <dirent.h> #include <time.h> #include <signal.h> #include <ctype.h> #include <errno.h> #include <event2/bufferevent.h> #include <event2/buffer.h> #include <event2/listener.h> #include "libevent_http.h"
#define _HTTP_CLOSE_ "Connection: close\r\n"
int response_http(struct bufferevent *bev, const char *method, char *path) { if(strcasecmp("GET", method) == 0){ strdecode(path, path); char *pf = &path[1];
if(strcmp(path, "/") == 0 || strcmp(path, "/.") == 0) { pf="./"; }
printf("***** http Request Resource Path = %s, pf = %s\n", path, pf);
struct stat sb; if(stat(pf,&sb) < 0) { perror("open file err:"); send_error(bev); return -1; }
if(S_ISDIR(sb.st_mode)) { send_header(bev, 200, "OK", get_file_type(".html"), -1); send_dir(bev, pf); } else { send_header(bev, 200, "OK", get_file_type(pf), sb.st_size); send_file_to_http(pf, bev); } }
return 0; }
const char *get_file_type(char *name) { char* dot;
dot = strrchr(name, '.');
if (dot == (char*)0) return "text/plain; charset=utf-8"; if (strcmp(dot, ".html") == 0 || strcmp(dot, ".htm") == 0) return "text/html; charset=utf-8"; if (strcmp(dot, ".jpg") == 0 || strcmp(dot, ".jpeg") == 0) return "image/jpeg"; if (strcmp(dot, ".gif") == 0) return "image/gif"; if (strcmp(dot, ".png") == 0) return "image/png"; if (strcmp(dot, ".css") == 0) return "text/css"; if (strcmp(dot, ".au") == 0) return "audio/basic"; if (strcmp( dot, ".wav") == 0) return "audio/wav"; if (strcmp(dot, ".avi") == 0) return "video/x-msvideo"; if (strcmp(dot, ".mov") == 0 || strcmp(dot, ".qt") == 0) return "video/quicktime"; if (strcmp(dot, ".mpeg") == 0 || strcmp(dot, ".mpe") == 0) return "video/mpeg"; if (strcmp(dot, ".vrml") == 0 || strcmp(dot, ".wrl") == 0) return "model/vrml"; if (strcmp(dot, ".midi") == 0 || strcmp(dot, ".mid") == 0) return "audio/midi"; if (strcmp(dot, ".mp3") == 0) return "audio/mpeg"; if (strcmp(dot, ".ogg") == 0) return "application/ogg"; if (strcmp(dot, ".pac") == 0) return "application/x-ns-proxy-autoconfig";
return "text/plain; charset=utf-8"; }
int send_file_to_http(const char *filename, struct bufferevent *bev) { int fd = open(filename, O_RDONLY); int ret = 0; char buf[4096] = {0};
while((ret = read(fd, buf, sizeof(buf)) ) ) { bufferevent_write(bev, buf, ret); memset(buf, 0, ret); } close(fd); return 0; }
int send_header(struct bufferevent *bev, int no, const char* desp, const char *type, long len) { char buf[256]={0};
sprintf(buf, "HTTP/1.1 %d %s\r\n", no, desp); bufferevent_write(bev, buf, strlen(buf)); sprintf(buf, "Content-Type:%s\r\n", type); bufferevent_write(bev, buf, strlen(buf)); sprintf(buf, "Content-Length:%ld\r\n", len); bufferevent_write(bev, buf, strlen(buf)); bufferevent_write(bev, _HTTP_CLOSE_, strlen(_HTTP_CLOSE_)); bufferevent_write(bev, "\r\n", 2);
return 0; }
int send_error(struct bufferevent *bev) { send_header(bev,404, "File Not Found", "text/html", -1); send_file_to_http("404.html", bev); return 0; }
int send_dir(struct bufferevent *bev,const char *dirname) { char encoded_name[1024]; char path[1024]; char timestr[64]; struct stat sb; struct dirent **dirinfo; int i;
char buf[4096] = {0}; sprintf(buf, "<html><head><meta charset=\"utf-8\"><title>%s</title></head>", dirname); sprintf(buf+strlen(buf), "<body><h1>The current directory: %s</h1><table>", dirname); int num = scandir(dirname, &dirinfo, NULL, alphasort); for(i=0; i<num; ++i) { strencode(encoded_name, sizeof(encoded_name), dirinfo[i]->d_name);
sprintf(path, "%s%s", dirname, dirinfo[i]->d_name); printf("############# path = %s\n", path); if (lstat(path, &sb) < 0) { sprintf(buf+strlen(buf), "<tr><td><a href=\"%s\">%s</a></td></tr>\n", encoded_name, dirinfo[i]->d_name); } else { strftime(timestr, sizeof(timestr), " %d %b %Y %H:%M", localtime(&sb.st_mtime)); if(S_ISDIR(sb.st_mode)) { sprintf(buf+strlen(buf), "<tr><td><a href=\"%s/\">%s/</a></td><td>%s</td><td>%ld</td></tr>\n", encoded_name, dirinfo[i]->d_name, timestr, sb.st_size); } else { sprintf(buf+strlen(buf), "<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%ld</td></tr>\n", encoded_name, dirinfo[i]->d_name, timestr, sb.st_size); } } bufferevent_write(bev, buf, strlen(buf)); memset(buf, 0, sizeof(buf)); } sprintf(buf+strlen(buf), "</table></body></html>"); bufferevent_write(bev, buf, strlen(buf)); printf("################# Dir Read OK !!!!!!!!!!!!!!\n");
return 0; }
void conn_readcb(struct bufferevent *bev, void *user_data) { printf("******************** begin call %s.........\n",__FUNCTION__); char buf[4096]={0}; char method[50], path[4096], protocol[32]; bufferevent_read(bev, buf, sizeof(buf)); printf("buf[%s]\n", buf); sscanf(buf, "%[^ ] %[^ ] %[^ \r\n]", method, path, protocol); printf("method[%s], path[%s], protocol[%s]\n", method, path, protocol); if(strcasecmp(method, "GET") == 0) { response_http(bev, method, path); } printf("******************** end call %s.........\n", __FUNCTION__); }
void conn_eventcb(struct bufferevent *bev, short events, void *user_data) { printf("******************** begin call %s.........\n", __FUNCTION__); if (events & BEV_EVENT_EOF) { printf("Connection closed.\n"); } else if (events & BEV_EVENT_ERROR) { printf("Got an error on the connection: %s\n", strerror(errno)); }
bufferevent_free(bev); printf("******************** end call %s.........\n", __FUNCTION__); }
void signal_cb(evutil_socket_t sig, short events, void *user_data) { struct event_base *base = user_data; struct timeval delay = { 1, 0 };
printf("Caught an interrupt signal; exiting cleanly in one seconds.\n"); event_base_loopexit(base, &delay); }
void listener_cb(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *sa, int socklen, void *user_data) { printf("******************** begin call-------%s\n",__FUNCTION__); struct event_base *base = user_data; struct bufferevent *bev; printf("fd is %d\n",fd); bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE); if (!bev) { fprintf(stderr, "Error constructing bufferevent!"); event_base_loopbreak(base); return; } bufferevent_flush(bev, EV_READ | EV_WRITE, BEV_NORMAL); bufferevent_setcb(bev, conn_readcb, NULL, conn_eventcb, NULL); bufferevent_enable(bev, EV_READ | EV_WRITE);
printf("******************** end call-------%s\n",__FUNCTION__); }
void strdecode(char *to, char *from) { for ( ; *from != '\0'; ++to, ++from) { if (from[0] == '%' && isxdigit(from[1]) && isxdigit(from[2])) { *to = hexit(from[1])*16 + hexit(from[2]); from += 2; } else { *to = *from; } } *to = '\0'; }
int hexit(char c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'a' && c <= 'f') return c - 'a' + 10; if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return 0; }
void strencode(char* to, size_t tosize, const char* from) { int tolen;
for (tolen = 0; *from != '\0' && tolen + 4 < tosize; ++from) { if (isalnum(*from) || strchr("/_.-~", *from) != (char*)0) { *to = *from; ++to; ++tolen; } else { sprintf(to, "%%%02x", (int) *from & 0xff); to += 3; tolen += 3; } } *to = '\0'; }
|