Changeset 22 in Main for trunk/Server/server.c
- Timestamp:
- Sep 14, 2024, 10:25:38 PM (2 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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);
Note:
See TracChangeset
for help on using the changeset viewer.