- Timestamp:
- Sep 14, 2024, 10:25:38 PM (2 months ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Common/dir.c
r21 r22 10 10 #include <string.h> 11 11 12 int cm_sort(const void* _a, const void* _b) {12 int cm_sort(const void* _a, const void* _b) { 13 13 char* a = *(char**)_a; 14 14 char* b = *(char**)_b; … … 16 16 } 17 17 18 char** cm_scandir(const char* path) {18 char** cm_scandir(const char* path) { 19 19 DIR* dir = opendir(path); 20 if(dir != NULL) {20 if(dir != NULL) { 21 21 char** r = malloc(sizeof(*r)); 22 22 r[0] = NULL; 23 23 struct dirent* d; 24 while((d = readdir(dir)) != NULL) {25 if(strcmp(d->d_name, ".") != 0 ){24 while((d = readdir(dir)) != NULL) { 25 if(strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0) { 26 26 struct stat s; 27 27 char* p = cm_strcat3(path, "/", d->d_name); … … 31 31 char** old = r; 32 32 int i; 33 for(i = 0; old[i] != NULL; i++); 33 for(i = 0; old[i] != NULL; i++) 34 ; 34 35 r = malloc(sizeof(*r) * (i + 2)); 35 36 for(i = 0; old[i] != NULL; i++) r[i] = old[i]; … … 40 41 } 41 42 int len; 42 for(len = 0; r[len] != NULL; len++); 43 for(len = 0; r[len] != NULL; len++) 44 ; 43 45 qsort(r, len, sizeof(char*), cm_sort); 46 47 char** old = r; 48 int i; 49 for(i = 0; old[i] != NULL; i++) 50 ; 51 r = malloc(sizeof(*r) * (i + 2)); 52 for(i = 0; old[i] != NULL; i++) r[i + 1] = old[i]; 53 r[0] = cm_strdup("../"); 54 r[i + 1] = NULL; 55 free(old); 56 44 57 return r; 45 } else{58 } else { 46 59 return NULL; 47 60 } -
trunk/Server/config.c
r21 r22 31 31 } 32 32 33 bool tw_permission_allowed(const char* path, SOCKADDR addr, struct tw_http_request req, struct tw_config_entry* vhost) {33 bool tw_permission_allowed(const char* path, SOCKADDR addr, struct tw_http_request req, struct tw_config_entry* vhost) { 34 34 int i; 35 35 bool found = false; … … 37 37 bool perm = false; 38 38 again: 39 for(i = 0; i < vhost->dir_count; i++) {39 for(i = 0; i < vhost->dir_count; i++) { 40 40 struct tw_dir_entry* e = &vhost->dirs[i]; 41 41 pathstart = false; 42 if(strlen(path) >= strlen(e->dir)) {42 if(strlen(path) >= strlen(e->dir)) { 43 43 pathstart = true; 44 44 int j; 45 for(j = 0; path[j] != 0 && e->dir[j] != 0; j++) {46 if(path[j] != e->dir[j]) {45 for(j = 0; path[j] != 0 && e->dir[j] != 0; j++) { 46 if(path[j] != e->dir[j]) { 47 47 pathstart = false; 48 48 break; … … 52 52 char* noslash = cm_strdup(e->dir); 53 53 noslash[strlen(noslash) - 1] = 0; 54 if(strcmp(e->dir, path) == 0 || strcmp(noslash, path) == 0 || pathstart) {54 if(strcmp(e->dir, path) == 0 || strcmp(noslash, path) == 0 || pathstart) { 55 55 found = true; 56 if(strcmp(e->name, "all") == 0) {56 if(strcmp(e->name, "all") == 0) { 57 57 perm = e->type == TW_DIR_ALLOW; 58 58 } … … 60 60 free(noslash); 61 61 } 62 if(!found && vhost != &config.root) {62 if(!found && vhost != &config.root) { 63 63 vhost = &config.root; 64 64 goto again; … … 82 82 config.root.mime_count = 0; 83 83 config.root.dir_count = 0; 84 config.root.icon_count = 0; 84 85 config.vhost_count = 0; 85 86 config.module_count = 0; … … 180 181 current->dir_count = 0; 181 182 current->mime_count = 0; 183 current->icon_count = 0; 182 184 int i; 183 185 current->name = cm_strdup(vhost); … … 247 249 cm_log("Config", "Missing extension at line %d", ln); 248 250 stop = 1; 249 } else if(r[2] == NULL) {251 } else if(r[2] == NULL) { 250 252 cm_log("Config", "Missing MIME at line %d", ln); 251 253 stop = 1; … … 254 256 e->ext = cm_strdup(r[1]); 255 257 e->mime = cm_strdup(r[2]); 258 } 259 } else if(cm_strcaseequ(r[0], "Icon")) { 260 if(r[1] == NULL) { 261 cm_log("Config", "Missing MIME at line %d", ln); 262 stop = 1; 263 } else if(r[2] == NULL) { 264 cm_log("Config", "Missing path at line %d", ln); 265 stop = 1; 266 } else { 267 struct tw_icon_entry* e = ¤t->icons[current->icon_count++]; 268 e->mime = cm_strdup(r[1]); 269 e->icon = cm_strdup(r[2]); 256 270 } 257 271 } else if(cm_strcaseequ(r[0], "LoadModule")) { -
trunk/Server/http.c
r20 r22 60 60 tv.tv_sec = 5; 61 61 tv.tv_usec = 0; 62 int n = select(FD_SETSIZE, &fds, NULL, NULL, &tv); 63 if(n == 0) break; 62 if(!SSL_has_pending(ssl)) { 63 int n = select(FD_SETSIZE, &fds, NULL, NULL, &tv); 64 if(n <= 0) { 65 free(header); 66 tw_free_request(req); 67 return -1; 68 } 69 } 64 70 int len = tw_read(ssl, sock, buffer, 512); 65 71 if(len <= 0) break; -
trunk/Server/server.c
r21 r22 166 166 size_t incr = 0; 167 167 while(1) { 168 if(f != NULL) {168 if(f != NULL) { 169 169 char buffer[128]; 170 170 fread(buffer, size < 128 ? size : 128, 1, f); 171 171 tw_write(ssl, sock, buffer, size < 128 ? size : 128); 172 } else{172 } else { 173 173 tw_write(ssl, sock, (unsigned char*)doc + incr, size < 128 ? size : 128); 174 174 } … … 269 269 } 270 270 271 char* tw_get_mime(const char* ext, struct tw_config_entry* vhost_entry) { 272 char* mime = "application/octet-stream"; 273 if(ext == NULL) return mime; 274 bool set = false; 275 int i; 276 for(i = 0; i < vhost_entry->mime_count; i++) { 277 if(strcmp(vhost_entry->mimes[i].ext, "all") == 0 || (ext != NULL && strcmp(vhost_entry->mimes[i].ext, ext) == 0)) { 278 mime = vhost_entry->mimes[i].mime; 279 set = true; 280 } 281 } 282 if(!set) { 283 for(i = 0; i < config.root.mime_count; i++) { 284 if(strcmp(config.root.mimes[i].ext, "all") == 0 || (ext != NULL && strcmp(config.root.mimes[i].ext, ext) == 0)) { 285 mime = config.root.mimes[i].mime; 286 } 287 } 288 } 289 return mime; 290 } 291 292 char* tw_get_icon(const char* mime, struct tw_config_entry* vhost_entry) { 293 char* icon = ""; 294 if(mime == NULL) return ""; 295 bool set = false; 296 int i; 297 for(i = 0; i < vhost_entry->icon_count; i++) { 298 if(strcmp(vhost_entry->icons[i].mime, "all") == 0 || (mime != NULL && strcmp(vhost_entry->icons[i].mime, mime) == 0)) { 299 icon = vhost_entry->icons[i].icon; 300 set = true; 301 } 302 } 303 if(!set) { 304 for(i = 0; i < config.root.icon_count; i++) { 305 if(strcmp(config.root.icons[i].mime, "all") == 0 || (mime != NULL && strcmp(config.root.icons[i].mime, mime) == 0)) { 306 icon = config.root.icons[i].icon; 307 } 308 } 309 } 310 return icon; 311 } 312 271 313 #ifdef __MINGW32__ 272 314 struct pass_entry { … … 307 349 char* vhost = cm_strdup(config.hostname); 308 350 int i; 309 for(i = 0; req.headers[i] != NULL; i += 2) {310 if(cm_strcaseequ(req.headers[i], "Host")) {351 for(i = 0; req.headers[i] != NULL; i += 2) { 352 if(cm_strcaseequ(req.headers[i], "Host")) { 311 353 free(vhost); 312 354 vhost = req.headers[i + 1]; … … 317 359 int port = s == NULL ? 80 : 443; 318 360 char* host = cm_strdup(vhost); 319 for(i = 0; vhost[i] != 0; i++) {320 if(vhost[i] == ':') {361 for(i = 0; vhost[i] != 0; i++) { 362 if(vhost[i] == ':') { 321 363 host[i] = 0; 322 364 port = atoi(host + i + 1); … … 347 389 cm_log("Server", "Filesystem path is %s", path); 348 390 struct stat st; 349 if(stat(path, &st) == 0) {350 if(!tw_permission_allowed(path, addr, req, vhost_entry)) {391 if(stat(path, &st) == 0) { 392 if(!tw_permission_allowed(path, addr, req, vhost_entry)) { 351 393 tw_http_error(s, sock, 403, name, port); 352 } else if(S_ISDIR(st.st_mode)){394 } else if(S_ISDIR(st.st_mode)) { 353 395 char* str = malloc(1); 354 396 str[0] = 0; … … 368 410 addstring(&str, " <th>Filename</th>\n"); 369 411 addstring(&str, " </tr>\n"); 370 if(items != NULL){ 371 for(i = 0; items[i] != NULL; i++){ 412 if(items != NULL) { 413 for(i = 0; items[i] != NULL; i++) { 414 char* ext = NULL; 415 int j; 416 for(j = strlen(items[i]) - 1; j >= 0; j--) { 417 if(items[i][j] == '.') { 418 ext = cm_strdup(items[i] + j); 419 break; 420 } 421 } 422 char* mime = tw_get_mime(ext, vhost_entry); 423 if(strcmp(items[i], "../") == 0) { 424 mime = "misc/parent"; 425 } else if(items[i][strlen(items[i]) - 1] == '/') { 426 mime = "misc/dir"; 427 } 428 char* icon = tw_get_icon(mime, vhost_entry); 429 if(ext != NULL) free(ext); 430 char* itm = cm_strdup(items[i]); 431 if(strlen(itm) >= 32) { 432 if(itm[strlen(itm) - 1] == '/') { 433 itm[31] = 0; 434 itm[30] = '/'; 435 itm[29] = '.'; 436 itm[28] = '.'; 437 itm[27] = '.'; 438 } else { 439 itm[31] = 0; 440 itm[30] = '.'; 441 itm[29] = '.'; 442 itm[28] = '.'; 443 } 444 } 372 445 addstring(&str, "<tr>\n"); 373 addstring(&str, " <td>< /td>\n");374 addstring(&str, " <td><a href=\"%l\"> %h</a></td>\n", items[i], items[i]);446 addstring(&str, " <td><img src=\"%s\" alt=\"icon\"></td>\n", icon); 447 addstring(&str, " <td><a href=\"%l\"><code>%h</code></a></td>\n", items[i], itm); 375 448 addstring(&str, "</tr>\n"); 449 free(itm); 376 450 } 377 451 } … … 383 457 tw_process_page(s, sock, tw_http_status(200), "text/html", NULL, str, strlen(str)); 384 458 free(str); 385 }else{ 386 char* mime = "application/octet-stream"; 387 bool set = false; 459 } else { 388 460 char* ext = NULL; 389 for(i = strlen(req.path) - 1; i >= 0; i--) {390 if(req.path[i] == '.') {461 for(i = strlen(req.path) - 1; i >= 0; i--) { 462 if(req.path[i] == '.') { 391 463 ext = cm_strdup(req.path + i); 392 464 break; 393 465 } 394 466 } 395 for(i = 0; i < vhost_entry->mime_count; i++){ 396 if(strcmp(vhost_entry->mimes[i].ext, "all") == 0 || (ext != NULL && strcmp(vhost_entry->mimes[i].ext, ext) == 0)){ 397 mime = vhost_entry->mimes[i].mime; 398 set = true; 399 } 400 } 401 if(!set){ 402 for(i = 0; i < config.root.mime_count; i++){ 403 if(strcmp(config.root.mimes[i].ext, "all") == 0 || (ext != NULL && strcmp(config.root.mimes[i].ext, ext) == 0)){ 404 mime = config.root.mimes[i].mime; 405 set = true; 406 } 407 } 408 } 467 char* mime = tw_get_mime(ext, vhost_entry); 409 468 if(ext != NULL) free(ext); 410 469 FILE* f = fopen(path, "rb"); … … 412 471 fclose(f); 413 472 } 414 } else{473 } else { 415 474 tw_http_error(s, sock, 404, name, port); 416 475 } … … 419 478 free(vhost); 420 479 free(host); 480 } else if(ret == -1) { 421 481 } else { 422 482 tw_http_error(s, sock, 400, name, port); -
trunk/Server/tw_config.h
r21 r22 22 22 #endif 23 23 24 #define MAX_PORTS 1024 25 #define MAX_VHOSTS 1024 26 #define MAX_MODULES 1024 27 #define MAX_DIRS 1024 28 #define MAX_MIME 1024 24 #define MAX_PORTS 1024 25 #define MAX_VHOSTS 1024 26 #define MAX_MODULES 1024 27 #define MAX_DIRS 1024 28 #define MAX_MIME 1024 29 #define MAX_ICON 1024 29 30 30 31 enum TW_DIR_TYPE { … … 44 45 }; 45 46 47 struct tw_icon_entry { 48 char* mime; 49 char* icon; 50 }; 51 46 52 struct tw_config_entry { 47 53 char* name; … … 54 60 struct tw_mime_entry mimes[MAX_DIRS]; 55 61 int mime_count; 62 struct tw_icon_entry icons[MAX_DIRS]; 63 int icon_count; 56 64 }; 57 65 -
trunk/example.conf
r21 r22 12 12 MIMEType all application/octet-stream 13 13 MIMEType .html text/html 14 MIMEType .txt text/plain 15 MIMEType .png image/png 14 16 15 DocumentRoot / 17 Icon all /icons/unknown.png 18 Icon text/plain /icons/text.png 19 Icon misc/dir /icons/folder.png 20 Icon misc/parent /icons/parent.png 21 22 DocumentRoot /var/www 16 23 17 24 BeginDirectory / … … 19 26 EndDirectory 20 27 21 BeginDirectory /var/www22 Deny all23 EndDirectory24 25 28 BeginVirtualHost nishinbsd-ssd 26 29 EndVirtualHost
Note:
See TracChangeset
for help on using the changeset viewer.