X-Git-Url: http://www.dolda2000.com/gitweb/?p=ashd.git;a=blobdiff_plain;f=src%2Fsendfile.c;h=85ea9bb2a9c5eb7f4938e32e18d9c161f6f9a9e8;hp=7124c25eba849780eb502496adbf2e92cbdaa3da;hb=595adb9922885c2a05bc6917ee8f8f02f496e618;hpb=cbc5f085ef03ebab38b9501acc3ec7aeca10e7d3 diff --git a/src/sendfile.c b/src/sendfile.c index 7124c25..85ea9bb 100644 --- a/src/sendfile.c +++ b/src/sendfile.c @@ -92,7 +92,7 @@ static char *attrmimetype(char *file) return(NULL); found: for(i = 0; i < sz; i++) { - if((buf[sz] < 32) || (buf[sz] >= 128)) + if((buf[i] < 32) || (buf[i] >= 128)) return(NULL); } buf[sz] = 0; @@ -141,11 +141,6 @@ static void checkcache(char *file, struct stat *sb) } } -static void usage(void) -{ - flog(LOG_ERR, "usage: sendfile [-c CONTENT-TYPE] METHOD URL REST"); -} - static void sendwhole(int fd, struct stat *sb, const char *contype, int head) { printf("HTTP/1.1 200 OK\n"); @@ -227,21 +222,32 @@ error: sendwhole(fd, sb, contype, head); } +static void usage(void) +{ + flog(LOG_ERR, "usage: sendfile [-c CONTENT-TYPE] [-f FILE] METHOD URL REST"); +} + int main(int argc, char **argv) { int c; char *file, *hdr; struct stat sb; - int fd, ishead; + int fd, ishead, ignrest; const char *contype; setlocale(LC_ALL, ""); contype = NULL; - while((c = getopt(argc, argv, "c:")) >= 0) { + file = NULL; + ignrest = 0; + while((c = getopt(argc, argv, "c:f:")) >= 0) { switch(c) { case 'c': contype = optarg; break; + case 'f': + file = optarg; + ignrest = 1; + break; default: usage(); exit(1); @@ -252,11 +258,11 @@ int main(int argc, char **argv) usage(); exit(1); } - if((file = getenv("REQ_X_ASH_FILE")) == NULL) { - flog(LOG_ERR, "sendfile: needs to be called with the X-Ash-File header"); + if((file == NULL) && ((file = getenv("REQ_X_ASH_FILE")) == NULL)) { + flog(LOG_ERR, "sendfile: needs to be called with either the X-Ash-File header or the -f option"); exit(1); } - if(*argv[optind + 2]) { + if(!ignrest && *argv[optind + 2]) { simpleerror(1, 404, "Not Found", "The requested URL has no corresponding resource."); exit(0); } @@ -265,13 +271,24 @@ int main(int argc, char **argv) simpleerror(1, 500, "Internal Error", "The server could not access its own data."); exit(1); } - if(contype == NULL) - contype = getmimetype(file, &sb); + if(!strcasecmp(argv[optind], "get")) { + ishead = 0; + } else if(!strcasecmp(argv[optind], "head")) { + ishead = 1; + } else { + simpleerror(1, 405, "Method not allowed", "The requested method is not defined for this resource."); + return(0); + } + if(contype == NULL) { + if((hdr = getenv("REQ_X_ASH_CONTENT_TYPE")) != NULL) + contype = hdr; + else + contype = getmimetype(file, &sb); + } contype = ckctype(contype); checkcache(file, &sb); - ishead = !strcasecmp(argv[optind], "head"); if((hdr = getenv("REQ_RANGE")) != NULL) sendrange(fd, &sb, contype, hdr, ishead); else