Overview
Comment: | Added vhost support |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
3298334221905511f3e91d731834f546 |
User & Date: | rkeene on 2014-02-18 03:47:37 |
Other Links: | manifest | tags |
Context
2014-02-18
| ||
05:16 | More logging optimizations check-in: 937df4b0c8 user: rkeene tags: trunk | |
03:47 | Added vhost support check-in: 3298334221 user: rkeene tags: trunk | |
03:45 | Fixed call to wrong function for finding end of HTTP path check-in: 773268af3c user: rkeene tags: trunk | |
Changes
Modified filed.1 from [5b3db65898] to [5afcb460b6].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | .PU .TH FILED 1 "12 Feb 14" "filed 1.7" .SH NAME filed \- serve files over HTTP .SH SYNOPSIS .ll +10 .B filed .RB [{ \-h | \-\-help }] .RB [{ \-d | \-\-daemon }] .RB [{ \-v | \-\-version }] .RB [{ \-b | \-\-bind } .IR address ] .RB [{ \-p | \-\-port } .IR port ] .RB [{ \-t | \-\-threads } .IR count ] .RB [{ \-c | \-\-cache } | > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | .PU .TH FILED 1 "12 Feb 14" "filed 1.7" .SH NAME filed \- serve files over HTTP .SH SYNOPSIS .ll +10 .B filed .RB [{ \-h | \-\-help }] .RB [{ \-d | \-\-daemon }] .RB [{ \-v | \-\-version }] .RB [{ \-V | \-\-vhost }] .RB [{ \-b | \-\-bind } .IR address ] .RB [{ \-p | \-\-port } .IR port ] .RB [{ \-t | \-\-threads } .IR count ] .RB [{ \-c | \-\-cache } |
︙ | ︙ | |||
42 43 44 45 46 47 48 49 50 51 52 53 54 55 | .TP .B -v (or --version) Instructs .B filed to print out its version number and then exit. .TP .B -b (or --bind) Specifies the address to listen for incoming HTTP requests on. .TP .B -p (or --port) | > > > > > | 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | .TP .B -v (or --version) Instructs .B filed to print out its version number and then exit. .TP .B -V (or --vhost) instructs filed to prepend all requests with their HTTP Host header. .TP .B -b (or --bind) Specifies the address to listen for incoming HTTP requests on. .TP .B -p (or --port) |
︙ | ︙ |
Modified filed.c from [91c2968a74] to [4157b8b146].
︙ | ︙ | |||
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | /* Default values */ #define PORT 80 #define THREAD_COUNT 5 #define BIND_ADDR "::" #define CACHE_SIZE 8209 #define LOG_FILE "-" /* Arguments for worker threads */ struct filed_worker_thread_args { int fd; }; /* Arguments for logging threads */ struct filed_logging_thread_args { FILE *fp; }; | > > > > > > | 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | /* Default values */ #define PORT 80 #define THREAD_COUNT 5 #define BIND_ADDR "::" #define CACHE_SIZE 8209 #define LOG_FILE "-" /* Configuration options that work threads need to be aware of */ struct filed_options { int vhosts_enabled; }; /* Arguments for worker threads */ struct filed_worker_thread_args { int fd; struct filed_options options; }; /* Arguments for logging threads */ struct filed_logging_thread_args { FILE *fp; }; |
︙ | ︙ | |||
55 56 57 58 59 60 61 | char etag[64]; }; /* Request variables */ struct filed_http_request { /** Buffers **/ struct filed_fileinfo fileinfo; | | > > > > > | 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 | char etag[64]; }; /* Request variables */ struct filed_http_request { /** Buffers **/ struct filed_fileinfo fileinfo; char tmpbuf[FILED_PATH_BUFFER_SIZE]; /** HTTP Request information **/ /*** Type of request (HEAD or GET) ***/ enum { FILED_REQUEST_METHOD_GET, FILED_REQUEST_METHOD_HEAD } method; /*** Path being requested ***/ char path[FILED_PATH_BUFFER_SIZE]; struct { struct { int present; off_t offset; /*** Range start ***/ off_t length; /*** Range length ***/ } range; struct { int present; char host[FILED_PATH_BUFFER_SIZE]; } host; } headers; }; /* Log record */ struct filed_log_entry { /* Type of log entry */ enum { |
︙ | ︙ | |||
623 624 625 626 627 628 629 | pthread_mutex_unlock(&cache->mutex); return(buffer); } /* Process an HTTP request and return the path requested */ | | > > > | 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 | pthread_mutex_unlock(&cache->mutex); return(buffer); } /* Process an HTTP request and return the path requested */ static struct filed_http_request *filed_get_http_request(FILE *fp, struct filed_http_request *buffer_st, struct filed_options *options) { char *method, *path; char *buffer, *workbuffer, *workbuffer_next; char *fgets_ret; size_t buffer_len; off_t range_start, range_end, range_length; int range_request; int snprintf_ret; int i; /* Set to default values */ range_start = 0; range_end = 0; range_request = 0; range_length = -1; buffer_st->headers.host.present = 0; buffer = buffer_st->tmpbuf; buffer_len = sizeof(buffer_st->tmpbuf); fgets_ret = fgets(buffer, buffer_len, fp); if (fgets_ret == NULL) { return(NULL); |
︙ | ︙ | |||
658 659 660 661 662 663 664 | *buffer = '\0'; buffer++; path = buffer; /* Terminate path component */ | | | 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 | *buffer = '\0'; buffer++; path = buffer; /* Terminate path component */ buffer = strpbrk(path, "\r\n "); if (buffer != NULL) { *buffer = '\0'; buffer++; } /* We only handle the "GET" and "HEAD' methods */ if (strcasecmp(method, "head") != 0) { |
︙ | ︙ | |||
709 710 711 712 713 714 715 716 717 718 719 720 721 722 | workbuffer++; if (*workbuffer != '\r' && *workbuffer != '\n') { range_end = strtoull(workbuffer, &workbuffer_next, 10); } } } } if (memcmp(buffer, "\r\n", 2) == 0) { break; } } | > > > > > > > > > > > > > > | 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 | workbuffer++; if (*workbuffer != '\r' && *workbuffer != '\n') { range_end = strtoull(workbuffer, &workbuffer_next, 10); } } } } else if (strncasecmp(buffer, "Host: ", 5) == 0) { buffer_st->headers.host.present = 1; workbuffer = strpbrk(buffer + 5, "\r\n:"); if (workbuffer != NULL) { *workbuffer = '\0'; } workbuffer = buffer + 5; while (*workbuffer == ' ') { workbuffer++; } strcpy(buffer_st->headers.host.host, workbuffer); } if (memcmp(buffer, "\r\n", 2) == 0) { break; } } |
︙ | ︙ | |||
735 736 737 738 739 740 741 742 743 744 745 746 747 748 | ); } /* Fill up structure to return */ buffer_st->headers.range.present = range_request; buffer_st->headers.range.offset = range_start; buffer_st->headers.range.length = range_length; return(buffer_st); } /* Return an error page */ static void filed_error_page(FILE *fp, const char *date_current, int error_number, int method) { char *error_string = "<html><head><title>ERROR</title></head><body>Unable to process request</body></html>"; | > > > > > > > > > > > > > > > > > > > | 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 | ); } /* Fill up structure to return */ buffer_st->headers.range.present = range_request; buffer_st->headers.range.offset = range_start; buffer_st->headers.range.length = range_length; /* If vhosts are enabled, compute new path */ if (options->vhosts_enabled) { if (buffer_st->headers.host.present == 1) { buffer = buffer_st->tmpbuf; buffer_len = sizeof(buffer_st->tmpbuf); snprintf_ret = snprintf(buffer, buffer_len, "/%s%s%s", buffer_st->headers.host.host, buffer_st->path[0] == '/' ? "" : "/", buffer_st->path ); if (snprintf_ret >= 0) { if (((unsigned int) snprintf_ret) < buffer_len) { strcpy(buffer_st->path, buffer); } } } } return(buffer_st); } /* Return an error page */ static void filed_error_page(FILE *fp, const char *date_current, int error_number, int method) { char *error_string = "<html><head><title>ERROR</title></head><body>Unable to process request</body></html>"; |
︙ | ︙ | |||
760 761 762 763 764 765 766 | fprintf(fp, "%s", error_string); } return; } /* Handle a single request from a client */ | | | | 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 | fprintf(fp, "%s", error_string); } return; } /* Handle a single request from a client */ static void filed_handle_client(int fd, struct filed_http_request *request, struct filed_log_entry *log, struct filed_options *options) { struct filed_fileinfo *fileinfo; ssize_t sendfile_ret; size_t sendfile_size; off_t sendfile_offset, sendfile_sent, sendfile_len; char *path; char *date_current, date_current_b[64]; int http_code; FILE *fp; /* Determine current time */ date_current = filed_format_time(date_current_b, sizeof(date_current_b), time(NULL)); /* Open socket as ANSI I/O for ease of use */ fp = fdopen(fd, "w+b"); if (fp == NULL) { close(fd); return; } request = filed_get_http_request(fp, request, options); if (request == NULL) { filed_error_page(fp, date_current, 500, FILED_REQUEST_METHOD_GET); log->buffer[0] = '\0'; log->http_code = 500; log->reason = "format"; |
︙ | ︙ | |||
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 | } /* Handle incoming connections */ static void *filed_worker_thread(void *arg_v) { struct filed_worker_thread_args *arg; struct filed_http_request request; struct filed_log_entry *log, local_dummy_log; struct sockaddr_in6 addr; socklen_t addrlen; int failure_count = 0, max_failure_count = FILED_MAX_FAILURE_COUNT; int master_fd, fd; /* Read arguments */ arg = arg_v; master_fd = arg->fd; while (1) { /* Failure loop prevention */ if (failure_count > max_failure_count) { break; } | > > | 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 | } /* Handle incoming connections */ static void *filed_worker_thread(void *arg_v) { struct filed_worker_thread_args *arg; struct filed_http_request request; struct filed_log_entry *log, local_dummy_log; struct filed_options *options; struct sockaddr_in6 addr; socklen_t addrlen; int failure_count = 0, max_failure_count = FILED_MAX_FAILURE_COUNT; int master_fd, fd; /* Read arguments */ arg = arg_v; master_fd = arg->fd; options = &arg->options; while (1) { /* Failure loop prevention */ if (failure_count > max_failure_count) { break; } |
︙ | ︙ | |||
1009 1010 1011 1012 1013 1014 1015 | } log->port = addr.sin6_port; /* Reset failure count*/ failure_count = 0; /* Handle socket */ | | | > | 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 | } log->port = addr.sin6_port; /* Reset failure count*/ failure_count = 0; /* Handle socket */ filed_handle_client(fd, &request, log, options); } /* Report error */ filed_log_msg("THREAD_DIED ABNORMAL"); return(NULL); /* local_dummy_log is only used if FILED_DONT_LOG is enabled, otherwise it's not used, but the compiler hates that idea. */ local_dummy_log.type = 0; local_dummy_log.type = local_dummy_log.type; } /* Create worker threads */ static int filed_worker_threads_init(int fd, int thread_count, struct filed_options *options) { struct filed_worker_thread_args *arg; pthread_t threadid; int pthread_ret; int i; for (i = 0; i < thread_count; i++) { arg = malloc(sizeof(*arg)); arg->fd = fd; memcpy(&arg->options, options, sizeof(*options)); pthread_ret = pthread_create(&threadid, NULL, filed_worker_thread, arg); if (pthread_ret != 0) { return(-1); } } |
︙ | ︙ | |||
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 | } fprintf(output, "Usage: filed [<options>]\n"); fprintf(output, " Options:\n"); fprintf(output, " -h, --help\n"); fprintf(output, " -d, --daemon\n"); fprintf(output, " -v, --version\n"); fprintf(output, " -b <address>, --bind <address>\n"); fprintf(output, " -p <port>, --port <port>\n"); fprintf(output, " -t <count>, --threads <count>\n"); fprintf(output, " -c <entries>, --cache <entries>\n"); fprintf(output, " -l <file>, --log <file>\n"); fprintf(output, " -u <user>, --user <user>\n"); fprintf(output, " -r <directory>, --root <directory>\n"); if (long_help) { fprintf(output, "\n"); fprintf(output, " Usage:\n"); fprintf(output, " -h (or --help) prints this usage information.\n"); fprintf(output, "\n"); fprintf(output, " -d (or --daemon) instructs filed to become a daemon after initializing\n"); fprintf(output, " the listening TCP socket and log files.\n"); fprintf(output, "\n"); fprintf(output, " -v (or --version) instructs filed print out the version number and exit.\n"); fprintf(output, "\n"); fprintf(output, " -b (or --bind) specifies the address to listen for incoming HTTP\n"); fprintf(output, " requests on. The default value is \"%s\".\n", BIND_ADDR); fprintf(output, "\n"); fprintf(output, " -p (or --port) specifies the TCP port number to listen for incoming HTTP\n"); fprintf(output, " requests on. The default is %u.\n", (unsigned int) PORT); fprintf(output, "\n"); | > > > > | 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 | } fprintf(output, "Usage: filed [<options>]\n"); fprintf(output, " Options:\n"); fprintf(output, " -h, --help\n"); fprintf(output, " -d, --daemon\n"); fprintf(output, " -v, --version\n"); fprintf(output, " -V, --vhost\n"); fprintf(output, " -b <address>, --bind <address>\n"); fprintf(output, " -p <port>, --port <port>\n"); fprintf(output, " -t <count>, --threads <count>\n"); fprintf(output, " -c <entries>, --cache <entries>\n"); fprintf(output, " -l <file>, --log <file>\n"); fprintf(output, " -u <user>, --user <user>\n"); fprintf(output, " -r <directory>, --root <directory>\n"); if (long_help) { fprintf(output, "\n"); fprintf(output, " Usage:\n"); fprintf(output, " -h (or --help) prints this usage information.\n"); fprintf(output, "\n"); fprintf(output, " -d (or --daemon) instructs filed to become a daemon after initializing\n"); fprintf(output, " the listening TCP socket and log files.\n"); fprintf(output, "\n"); fprintf(output, " -v (or --version) instructs filed print out the version number and exit.\n"); fprintf(output, "\n"); fprintf(output, " -V (or --vhost) instructs filed to prepend all requests with their HTTP\n"); fprintf(output, " Host header.\n"); fprintf(output, "\n"); fprintf(output, " -b (or --bind) specifies the address to listen for incoming HTTP\n"); fprintf(output, " requests on. The default value is \"%s\".\n", BIND_ADDR); fprintf(output, "\n"); fprintf(output, " -p (or --port) specifies the TCP port number to listen for incoming HTTP\n"); fprintf(output, " requests on. The default is %u.\n", (unsigned int) PORT); fprintf(output, "\n"); |
︙ | ︙ | |||
1222 1223 1224 1225 1226 1227 1228 | close(fd_out); return(0); } /* Run process */ int main(int argc, char **argv) { | | > > > > | > | | 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 | close(fd_out); return(0); } /* Run process */ int main(int argc, char **argv) { struct option options[12]; struct filed_options thread_options; const char *bind_addr = BIND_ADDR, *newroot = NULL, *log_file = LOG_FILE; FILE *log_fp; uid_t user = 0; int port = PORT, thread_count = THREAD_COUNT; int cache_size = CACHE_SIZE; int init_ret, chroot_ret, setuid_ret, lookup_ret, chdir_ret; int setuid_enabled = 0, daemon_enabled = 0; int ch; int fd; /* Set default values */ thread_options.vhosts_enabled = 0; /* Process arguments */ filed_getopt_long_setopt(&options[0], "port", required_argument, 'p'); filed_getopt_long_setopt(&options[1], "threads", required_argument, 't'); filed_getopt_long_setopt(&options[2], "cache", required_argument, 'c'); filed_getopt_long_setopt(&options[3], "bind", required_argument, 'b'); filed_getopt_long_setopt(&options[4], "user", required_argument, 'u'); filed_getopt_long_setopt(&options[5], "root", required_argument, 'r'); filed_getopt_long_setopt(&options[6], "help", no_argument, 'h'); filed_getopt_long_setopt(&options[7], "daemon", no_argument, 'd'); filed_getopt_long_setopt(&options[8], "log", required_argument, 'l'); filed_getopt_long_setopt(&options[9], "version", no_argument, 'v'); filed_getopt_long_setopt(&options[10], "vhost", no_argument, 'V'); filed_getopt_long_setopt(&options[11], NULL, 0, 0); while ((ch = getopt_long(argc, argv, "p:t:c:b:u:r:l:hdvV", options, NULL)) != -1) { switch(ch) { case 'p': port = atoi(optarg); break; case 't': thread_count = atoi(optarg); break; |
︙ | ︙ | |||
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 | newroot = strdup(optarg); break; case 'l': log_file = strdup(optarg); break; case 'd': daemon_enabled = 1; break; case 'v': printf("filed version %s\n", FILED_VERSION); return(0); case '?': case ':': | > > > > | 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 | newroot = strdup(optarg); break; case 'l': log_file = strdup(optarg); break; case 'd': daemon_enabled = 1; break; case 'V': thread_options.vhosts_enabled = 1; break; case 'v': printf("filed version %s\n", FILED_VERSION); return(0); case '?': case ':': |
︙ | ︙ | |||
1363 1364 1365 1366 1367 1368 1369 | if (init_ret != 0) { perror("filed_logging_thread_init"); return(4); } /* Create worker threads */ | | | 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 | if (init_ret != 0) { perror("filed_logging_thread_init"); return(4); } /* Create worker threads */ init_ret = filed_worker_threads_init(fd, thread_count, &thread_options); if (init_ret != 0) { perror("filed_worker_threads_init"); return(5); } /* Wait for threads to exit */ |
︙ | ︙ |