Changeset 24 in Main
- Timestamp:
- Sep 14, 2024, 11:09:58 PM (2 months ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Server/config.c
r22 r24 83 83 config.root.dir_count = 0; 84 84 config.root.icon_count = 0; 85 config.root.index_count = 0; 85 86 config.vhost_count = 0; 86 87 config.module_count = 0; … … 182 183 current->mime_count = 0; 183 184 current->icon_count = 0; 185 current->index_count = 0; 184 186 int i; 185 187 current->name = cm_strdup(vhost); … … 282 284 break; 283 285 } 286 } 287 } else if(cm_strcaseequ(r[0], "DirectoryIndex")) { 288 for(i = 1; r[i] != NULL; i++) { 289 current->indexes[current->index_count++] = cm_strdup(r[i]); 284 290 } 285 291 } else { -
trunk/Server/server.c
r23 r24 170 170 "</html>\n" 171 171 172 void tw_process_page(SSL* ssl, int sock, const char* status, const char* type, FILE* f, const unsigned char* doc, size_t size) {172 void _tw_process_page(SSL* ssl, int sock, const char* status, const char* type, FILE* f, const unsigned char* doc, size_t size, char** headers) { 173 173 char construct[512]; 174 174 sprintf(construct, "%llu", (unsigned long long)size); … … 176 176 tw_write(ssl, sock, (char*)status, strlen(status)); 177 177 tw_write(ssl, sock, "\r\n", 2); 178 tw_write(ssl, sock, "Content-Type: ", 7 + 5 + 2); 179 tw_write(ssl, sock, (char*)type, strlen(type)); 180 tw_write(ssl, sock, "\r\n", 2); 178 if(type != NULL) { 179 tw_write(ssl, sock, "Content-Type: ", 7 + 5 + 2); 180 tw_write(ssl, sock, (char*)type, strlen(type)); 181 tw_write(ssl, sock, "\r\n", 2); 182 } 181 183 tw_write(ssl, sock, "Server: ", 6 + 2); 182 184 tw_write(ssl, sock, tw_server, strlen(tw_server)); 183 185 tw_write(ssl, sock, "\r\n", 2); 184 tw_write(ssl, sock, "Content-Length: ", 7 + 7 + 2); 185 tw_write(ssl, sock, construct, strlen(construct)); 186 if(size != 0) { 187 tw_write(ssl, sock, "Content-Length: ", 7 + 7 + 2); 188 tw_write(ssl, sock, construct, strlen(construct)); 189 tw_write(ssl, sock, "\r\n", 2); 190 } 191 int i; 192 if(headers != NULL) { 193 for(i = 0; headers[i] != NULL; i += 2) { 194 tw_write(ssl, sock, headers[i], strlen(headers[i])); 195 tw_write(ssl, sock, ": ", 2); 196 tw_write(ssl, sock, headers[i + 1], strlen(headers[i + 1])); 197 tw_write(ssl, sock, "\r\n", 2); 198 } 199 } 186 200 tw_write(ssl, sock, "\r\n", 2); 187 tw_write(ssl, sock, "\r\n", 2);201 if(doc == NULL && f == NULL) return; 188 202 size_t incr = 0; 189 203 while(1) { … … 201 215 } 202 216 217 void tw_process_page(SSL* ssl, int sock, const char* status, const char* type, FILE* f, const unsigned char* doc, size_t size) { _tw_process_page(ssl, sock, status, type, f, doc, size, NULL); } 218 203 219 const char* tw_http_status(int code) { 204 220 if(code == 200) { 205 221 return "200 OK"; 222 } else if(code == 308) { 223 return "308 Permanent Redirect"; 206 224 } else if(code == 400) { 207 225 return "400 Bad Request"; … … 415 433 tw_http_error(s, sock, 403, name, port); 416 434 } else if(S_ISDIR(st.st_mode)) { 417 char* str = malloc(1); 418 str[0] = 0; 419 char** items = cm_scandir(path); 420 addstring(&str, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"); 421 addstring(&str, "<html>\n"); 422 addstring(&str, " <head>\n"); 423 addstring(&str, " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"); 424 addstring(&str, " <title>Index of %h</title>\n", req.path); 425 addstring(&str, " </head>\n"); 426 addstring(&str, " <body>\n"); 427 addstring(&str, " <h1>Index of %h</h1>\n", req.path); 428 addstring(&str, " <hr>\n"); 429 addstring(&str, " <table border=\"0\">\n"); 430 addstring(&str, " <tr>\n"); 431 addstring(&str, " <th></th>\n"); 432 addstring(&str, " <th>Filename</th>\n"); 433 addstring(&str, " </tr>\n"); 434 if(items != NULL) { 435 for(i = 0; items[i] != NULL; i++) { 436 char* ext = NULL; 437 int j; 438 for(j = strlen(items[i]) - 1; j >= 0; j--) { 439 if(items[i][j] == '.') { 440 ext = cm_strdup(items[i] + j); 441 break; 435 if(req.path[strlen(req.path) - 1] != '/') { 436 char* headers[3] = {"Location", cm_strcat(req.path, "/"), NULL}; 437 _tw_process_page(s, sock, tw_http_status(308), NULL, NULL, NULL, 0, headers); 438 free(headers[1]); 439 } else { 440 char** indexes = vhost_entry->index_count == 0 ? config.root.indexes : vhost_entry->indexes; 441 int index_count = vhost_entry->index_count == 0 ? config.root.index_count : vhost_entry->index_count; 442 bool found = false; 443 for(i = 0; i < index_count; i++) { 444 char* p = cm_strcat3(path, "/", indexes[i]); 445 FILE* f = fopen(p, "rb"); 446 if(f != NULL) { 447 char* ext = NULL; 448 int j; 449 for(j = strlen(p) - 1; j >= 0; j--) { 450 if(p[j] == '.') { 451 ext = cm_strdup(p + j); 452 break; 453 } else if(p[j] == '/') { 454 break; 455 } 456 } 457 struct stat st; 458 stat(p, &st); 459 char* mime = tw_get_mime(ext, vhost_entry); 460 tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size); 461 fclose(f); 462 free(p); 463 found = true; 464 break; 465 } 466 free(p); 467 } 468 if(!found) { 469 char* str = malloc(1); 470 str[0] = 0; 471 char** items = cm_scandir(path); 472 addstring(&str, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"); 473 addstring(&str, "<html>\n"); 474 addstring(&str, " <head>\n"); 475 addstring(&str, " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"); 476 addstring(&str, " <title>Index of %h</title>\n", req.path); 477 addstring(&str, " </head>\n"); 478 addstring(&str, " <body>\n"); 479 addstring(&str, " <h1>Index of %h</h1>\n", req.path); 480 addstring(&str, " <hr>\n"); 481 addstring(&str, " <table border=\"0\">\n"); 482 addstring(&str, " <tr>\n"); 483 addstring(&str, " <th></th>\n"); 484 addstring(&str, " <th>Filename</th>\n"); 485 addstring(&str, " </tr>\n"); 486 if(items != NULL) { 487 for(i = 0; items[i] != NULL; i++) { 488 char* ext = NULL; 489 int j; 490 for(j = strlen(items[i]) - 1; j >= 0; j--) { 491 if(items[i][j] == '.') { 492 ext = cm_strdup(items[i] + j); 493 break; 494 } else if(items[i][j] == '/') { 495 break; 496 } 497 } 498 char* mime = tw_get_mime(ext, vhost_entry); 499 if(strcmp(items[i], "../") == 0) { 500 mime = "misc/parent"; 501 } else if(items[i][strlen(items[i]) - 1] == '/') { 502 mime = "misc/dir"; 503 } 504 char* icon = tw_get_icon(mime, vhost_entry); 505 if(ext != NULL) free(ext); 506 char* itm = cm_strdup(items[i]); 507 if(strlen(itm) >= 32) { 508 if(itm[strlen(itm) - 1] == '/') { 509 itm[31] = 0; 510 itm[30] = '/'; 511 itm[29] = '.'; 512 itm[28] = '.'; 513 itm[27] = '.'; 514 } else { 515 itm[31] = 0; 516 itm[30] = '.'; 517 itm[29] = '.'; 518 itm[28] = '.'; 519 } 520 } 521 addstring(&str, "<tr>\n"); 522 addstring(&str, " <td><img src=\"%s\" alt=\"icon\"></td>\n", icon); 523 addstring(&str, " <td><a href=\"%l\"><code>%h</code></a></td>\n", items[i], itm); 524 addstring(&str, "</tr>\n"); 525 free(itm); 442 526 } 443 527 } 444 char* mime = tw_get_mime(ext, vhost_entry); 445 if(strcmp(items[i], "../") == 0) { 446 mime = "misc/parent"; 447 } else if(items[i][strlen(items[i]) - 1] == '/') { 448 mime = "misc/dir"; 449 } 450 char* icon = tw_get_icon(mime, vhost_entry); 451 if(ext != NULL) free(ext); 452 char* itm = cm_strdup(items[i]); 453 if(strlen(itm) >= 32) { 454 if(itm[strlen(itm) - 1] == '/') { 455 itm[31] = 0; 456 itm[30] = '/'; 457 itm[29] = '.'; 458 itm[28] = '.'; 459 itm[27] = '.'; 460 } else { 461 itm[31] = 0; 462 itm[30] = '.'; 463 itm[29] = '.'; 464 itm[28] = '.'; 465 } 466 } 467 addstring(&str, "<tr>\n"); 468 addstring(&str, " <td><img src=\"%s\" alt=\"icon\"></td>\n", icon); 469 addstring(&str, " <td><a href=\"%l\"><code>%h</code></a></td>\n", items[i], itm); 470 addstring(&str, "</tr>\n"); 471 free(itm); 528 addstring(&str, " </table>\n"); 529 addstring(&str, " <hr>\n"); 530 addstring(&str, " <address>%s Server at %s Port %d</address>\n", tw_server, name, port); 531 addstring(&str, " </body>\n"); 532 addstring(&str, "</html>\n"); 533 tw_process_page(s, sock, tw_http_status(200), "text/html", NULL, str, strlen(str)); 534 free(str); 472 535 } 473 536 } 474 addstring(&str, " </table>\n");475 addstring(&str, " <hr>\n");476 addstring(&str, " <address>%s Server at %s Port %d</address>\n", tw_server, name, port);477 addstring(&str, " </body>\n");478 addstring(&str, "</html>\n");479 tw_process_page(s, sock, tw_http_status(200), "text/html", NULL, str, strlen(str));480 free(str);481 537 } else { 482 538 char* ext = NULL; … … 484 540 if(req.path[i] == '.') { 485 541 ext = cm_strdup(req.path + i); 542 break; 543 } else if(req.path[i] == '/') { 486 544 break; 487 545 } -
trunk/Server/tw_config.h
r22 r24 28 28 #define MAX_MIME 1024 29 29 #define MAX_ICON 1024 30 #define MAX_INDEX 1024 30 31 31 32 enum TW_DIR_TYPE { … … 62 63 struct tw_icon_entry icons[MAX_DIRS]; 63 64 int icon_count; 65 char* indexes[MAX_INDEX]; 66 int index_count; 64 67 }; 65 68 -
trunk/example.conf
r23 r24 17 17 Icon all /icons/unknown.png 18 18 Icon text/* /icons/text.png 19 Icon image/* /icons/image.png 19 20 Icon misc/dir /icons/folder.png 20 21 Icon misc/parent /icons/parent.png 22 23 DirectoryIndex index.html 21 24 22 25 DocumentRoot /var/www
Note:
See TracChangeset
for help on using the changeset viewer.