void doPUTCommand(int sid) { Command c; Payload p; int status; c.code = htonl(CC_PUT); printf("Give a local filename:"); char fName[256]; scanf("%s",fName); strncpy(c.arg,fName,255); c.arg[255] = 0; status = send(sid,&c,sizeof(c),0);checkError(status,__LINE__); int fs = getFileSize(c.arg); p.code = ntohl(PL_FILE); p.length = ntohl(fs); status = send(sid,&p,sizeof(p),0);checkError(status,__LINE__); sendFileOverSocket(c.arg,sid); printf("transfer done\n"); }
void doMPUTCommand(int sid) { char params[256]; fgets(params,255,stdin); char *file; file = strtok(params," \n\r"); while( file ) { Command c; Payload p; int status; c.code = htonl(CC_PUT); strncpy(c.arg,file,255); c.arg[255] = 0; status = send(sid,&c,sizeof(c),0);checkError(status,__LINE__); int fs = getFileSize(c.arg); p.code = ntohl(PL_FILE); p.length = ntohl(fs); status = send(sid,&p,sizeof(p),0);checkError(status,__LINE__); sendFileOverSocket(c.arg,sid); file = strtok(NULL," \n\r"); } printf("transfer(s) done\n"); }
/*do_something - processes requests from client*/ void do_something(int client) { int len; struct stat statbuf; int fileType; printf("Connected\n"); if ((len = recv(client, buffer, MAXBUF, 0)) > 0 ) { FILE* ClientFP = fdopen(client, "w"); if ( ClientFP == NULL ) perror("fpopen"); else { char Req[MAXPATH]; char *args; int hasArgs=0; sscanf(buffer, "GET %s HTTP", Req); if (strpbrk(Req, "?") != 0) { char *Req1; printf("has args?\n"); hasArgs=1; Req1 = strtok(Req, "?"); args = strtok(NULL, "?"); printf("args: \"%s\"\n", args); } printf("Request: \"%s\"\n", Req); if (lstat(Req, &statbuf) < 0) { write(client,"HTTP/1.0 404 Not Found\n", 23); write(client,"Content-type: text/html\n", 24); write(client,"\n",1); sendFileOverSocket(client,"bad.html"); printf("Stat error.\n"); } else if (S_ISDIR(statbuf.st_mode)) { printf("Is Dir\n"); fprintf(ClientFP,"HTTP/1.0 200 OK\n"); fprintf(ClientFP,"Content-type: text/html\n"); fprintf(ClientFP,"\n"); DirListing(ClientFP, Req); } else { printf("checking file type\n"); fileType = checkExtension(Req); printf("file type is %d\n", fileType); if (fileType == 1) { printf("Is html file.\n"); write(client,"HTTP/1.0 200 OK\n",16 ); write(client,"Content-type: text/html\n", 25); write(client,"\n",1); sendFileOverSocket(client,Req); } else if (fileType == 2) { printf("Is jpeg file.\n"); write(client,"HTTP/1.0 200 OK\n",16 ); write(client,"Content-type: image/jpeg\n", 25); write(client,"\n",1); sendFileOverSocket(client,Req); } else if (fileType == 3) { printf("Is gif file.\n"); write(client,"HTTP/1.0 200 OK\n",16 ); write(client,"Content-type: image/gif\n", 24); write(client,"\n",1); sendFileOverSocket(client,Req); } else if (fileType == 4) { printf("is cgi file.\n"); dup2(client,1); write(client,"HTTP/1.0 200 OK\n",16 ); if(hasArgs==0){ printf("Executing with no args"); execlp(Req,Req,NULL); } else{ execlp(Req,Req,args,NULL); } } else { printf("Incorrect file.\n"); write(client,"HTTP/1.0 501 Not Implemented\n",23); write(client,"Content-type: text/plain\n", 25); write(client,"\n",1); write(client,"Viewing this file type has not been implemented",47); } } printf("about to close FP"); fclose(ClientFP); } } }