int explain(char *buf,int acc_sock) { char text[BUFSIZ]; char aaa[BUFSIZ]; char type[BUFSIZ]; struct stat info; int length; char *tmp; char *inputstring; sscanf(buf,"%s%s",aaa,text); //取得HTTP头前两个字符串 if(strcmp(text,"/") == 0){ strcpy(text,"/index.html"); } if(strcmp(aaa,"GET") == 0){ //GET方法 if((tmp = strchr(text,'?')) != NULL){ length = strlen(text) - strlen(tmp); //对GET方法请求的参数进行解析 strncpy(text,text,length); text[length] = '\0'; } if(lstat(text+1,&info)== -1){ char *head = "HTTP/1.1 404 NOT FOUND\r\n\r\n"; //请求的文件不存在 send(acc_sock,head,strlen(head),0); } send_file(text+1,acc_sock); }else if(strcmp(aaa,"POST") == 0){ //POST方法 bzero(type,sizeof(type)); seperate(buf,type); //对POST方法请求的参数进行解析 send_file2(text+1,acc_sock,type); } }
static void send_file(char *filename, int s) { static send_file_struct sfs; struct stat fileinfo; #ifdef HAVE_PTHREAD static pthread_t thread; #endif int i; char buff[6]; unsigned long filelen; char accept[10] = ""; if (xfer_in_progress) return; strncpy(sfs.filename, filename, 1024); sfs.s = s; stat(sfs.filename, &fileinfo); for (i = strlen(filename); i >= 0; i--) { if (filename[i] == '/') { break; } } snprintf(buff, 5, "%05ld", strlen(filename + i + 1)); write(s, buff, 5); write(s, filename + i + 1, strlen(filename + i + 1)); filelen = htonl(fileinfo.st_size); write(s, &filelen, 4); /* FD_ZERO(&set); FD_SET(s, &set); tv.tv_sec = 0; tv.tv_usec = 20; while(!select( s+1, &set, NULL, NULL, &tv ) ) { while (gtk_events_pending()) gtk_main_iteration(); } */ read(s, accept, 10); if (!strcmp(accept, "ACCEPT")) { progress_callback_data *pcd = calloc(1, sizeof(progress_callback_data)); char label[1024]; xfer_in_progress = 1; fp = fopen(filename, "rb"); printf("%s %s %ld %5d %p\n", filename, filename + i + 1, strlen(filename), htons(strlen(filename + i + 1)), fp); snprintf(label, 1024, "Transferring %s...", filename); pcd->tag = ay_progress_bar_add(label, fileinfo.st_size, NULL, NULL); #ifdef HAVE_PTHREAD pthread_mutex_init(&mutex, NULL); if (pthread_create(&thread, NULL, (void *)&send_file2, (void *)&sfs)) exit(1); #else send_file2(&sfs); #endif pcd->timer = eb_timeout_add(500, update_send_progress, pcd); } else { ay_do_error(_("Ayttm File Transfer"), _("Remote Side has aborted the file transfer")); } }