int main (void) { FILE *startp; int c; int i; char *bytes; size_t fsize; startp = fopen ("filea", "r+"); if (NULL == startp) { perror ("fopen"); return -1; } fsize = get_fsize (startp); bytes = (char *) malloc (fsize); if (NULL == bytes) { perror ("malloc"); return -1; } /* save the bytes in an array */ i = 0; while (!feof (startp)) { c = bytes[i] = getc (startp); if (-1 != c) { ++i; /* printf ("%o ", c); */ } } /* write the reversed bytes in place */ rewind (startp); for (i = NELEMS (bytes) - 1; i >= 0; --i) { fputc (bytes[i], startp); } free (bytes); fclose (startp); return 0; }
int main(int argc, char ** argv) { int fd = open(argv[1], O_RDONLY); int iter = atoi(argv[2]); int num_records = iter; ssize_t size = get_fsize(fd); char * buf = (char *) malloc(size); read(fd, buf, min(1073741824, size)); // warmup close(fd); uint64_t start = mach_absolute_time(); off_t offset = 0; while (iter--) { fd = open(argv[1], O_RDONLY); while (offset < size) { offset += read(fd, buf, min(1073741824, size)); // warmup } close(fd); } uint64_t end = mach_absolute_time(); printf("%zu\t%llu\n", size, (end - start)/num_records); }
void put_serv(char *str, t_serv clt) { int size; int fd; char *name; name = getname(str); printf("Trying to recieve file : %s \n", name); write(clt.cs, "ok\0", 3); size = get_fsize(clt); printf("%d\n", size); if (size == -1) { printf("ERROR\n"); return ; } fd = open(name, O_CREAT | O_RDWR, 0777); get_fdata(clt, size, fd); close(fd); printf("SUCCES\n"); }
void* execution_thread(void* d) { struct requests r; int k; bool flag=false; std::string log; while(1) { pthread_mutex_lock(&exec_queue); if (!tqueue.empty()) { tqueue.get(r,k); flag = true; } pthread_mutex_unlock(&exec_queue); if(flag) { log=r.log; //std::cout<<r.path<<std::endl; int ifsize=get_fsize(r.path); char fsize[4]; sprintf(fsize,"%d",ifsize); char filept[100]; strcpy(filept,r.path); if ( strncmp(r.http_ver, "HTTP/1.0", 8)!=0 && strncmp(r.http_ver, "HTTP/1.1", 8)!=0 ) { write(r.sockfd, "HTTP/1.0 400 Bad Request\r\n", 26); write (r.sockfd, "\r\n",2); char *tstamp=get_timestamp(); log.append("] ["); log.append(tstamp); write(r.sockfd,tstamp,(((std::string)tstamp).length()-1)); write (r.sockfd, "\r\n\r\n",4); shutdown (r.sockfd, SHUT_RDWR); close(r.sockfd); log.erase(log.length()-1); log.append("] \""); log.append(r.request_type); log.append(" "); log.append(r.path); log.append(" "); log.append(r.http_ver); log.erase(log.length()-1); log.append("\" 400 -1\n"); } else { if(!r.is_dir) { if(!r.path_found) { write (r.sockfd, "HTTP/1.0 404 Not Found\r\n", 24); write(r.sockfd, "Date: ",6); char *tstamp=get_timestamp(); log.append("] ["); log.append(tstamp); write(r.sockfd,tstamp,(((std::string)tstamp).length()-1)); write(r.sockfd,"\r\n",2); write(r.sockfd, "Server: ",8); write(r.sockfd, "Myhttpd/1.0\r\n",13); write(r.sockfd, "Content-Type: text/html\r\n",25); write (r.sockfd, "\r\n",2); shutdown (r.sockfd, SHUT_RDWR); close(r.sockfd); log.erase(log.length()-1); log.append("] \""); log.append(r.request_type); log.append(" "); log.append(r.path); log.append(" "); log.append(r.http_ver); log.erase(log.length()-1); log.append("\" 404 -1\n"); } else { char *ftype=filetype(filept); if( strncmp(r.request_type,"GET\0",4)==0) { FILE *f; char *content=new char[ifsize]; int bytes=0; write(r.sockfd, "HTTP/1.0 200 OK\r\n", 17); write(r.sockfd, "Date: ",6); char *tstamp=get_timestamp(); log.append("] ["); log.append(tstamp); write(r.sockfd,tstamp,(((std::string)tstamp).length()-1)); write(r.sockfd,"\r\n",2); write(r.sockfd, "Server: ",8); write(r.sockfd, "Myhttpd/1.0\r\n",13); write(r.sockfd, "Last-Modified: ",15); write(r.sockfd,get_lastm(r.path),(((std::string)get_lastm(r.path)).length()-1)); write(r.sockfd,"\r\n",2); write(r.sockfd,"Content-Type: ",14); if((ftype==NULL)) { write(r.sockfd,"text/html\r\n",11); } else { if((strncmp(ftype,"html",4)==0)||(strncmp(ftype,"txt",3)==0)) { write(r.sockfd,"text/html\r\n",11); } if ((strncmp(ftype,"gif",3)==0)||(strncmp(ftype,"jpg",3)==0)) { write(r.sockfd,"image/gif\r\n",11); } } write(r.sockfd, "Content-Length: ",16); write(r.sockfd,(char *)fsize,strlen(fsize)); write(r.sockfd,"\r\n\r\n",4); f=fopen(r.path,"rb"); while((bytes=fread(content,sizeof(char),ifsize,f))>0) { write(r.sockfd,content,bytes); } fclose(f); delete content; write(r.sockfd,"\r\n",2); shutdown (r.sockfd, SHUT_RDWR); close(r.sockfd); log.erase(log.length()-1); log.append("] \""); log.append(r.request_type); log.append(" "); log.append(r.path); log.append(" "); log.append(r.http_ver); log.erase(log.length()-1); log.append("\" 200 "); log.append(fsize); log.append("\n"); } else { write(r.sockfd, "HTTP/1.0 200 OK\r\n", 17); write(r.sockfd, "Date: ",6); char *tstamp=get_timestamp(); log.append("] ["); log.append(tstamp); write(r.sockfd,tstamp,(((std::string)tstamp).length()-1)); write(r.sockfd,"\r\n",2); write(r.sockfd, "Server: ",8); write(r.sockfd, "Myhttpd/1.0\r\n",13); write(r.sockfd, "Last-Modified: ",15); write(r.sockfd,get_lastm(r.path),(((std::string)get_lastm(r.path)).length()-1)); write(r.sockfd,"\r\n",2); write(r.sockfd,"Content-Type: ",14); if((ftype==NULL)) { write(r.sockfd,"text/html\r\n",11); } else { if((strncmp(ftype,"html",4)==0)||(strncmp(ftype,"txt",3)==0)) { write(r.sockfd,"text/html\r\n",11); } if ((strncmp(ftype,"gif",3)==0)||(strncmp(ftype,"jpg",3)==0)) { write(r.sockfd,"image/gif\r\n",11); } } write(r.sockfd, "Content-Length: ",16); write(r.sockfd,(char *)fsize,strlen(fsize)); write(r.sockfd,"\r\n\r\n",4); shutdown (r.sockfd, SHUT_RDWR); close(r.sockfd); log.erase(log.length()-1); log.append("] \""); log.append(r.request_type); log.append(" "); log.append(r.path); log.append(" "); log.append(r.http_ver); log.erase(log.length()-1); log.append("\" 200 "); log.append(fsize); log.append("\n"); } } } else { DIR *directory=opendir(r.path); if(directory==NULL) { write (r.sockfd, "HTTP/1.0 404 Not Found\r\n", 24); write(r.sockfd, "Date: ",6); char *tstamp=get_timestamp(); log.append("] ["); log.append(tstamp); write(r.sockfd,tstamp,(((std::string)tstamp).length()-1)); write(r.sockfd,"\r\n",2); write(r.sockfd, "Server: ",8); write(r.sockfd, "Myhttpd/1.0\r\n",13); write(r.sockfd, "Content-Type: text/html\r\n",25); write (r.sockfd, "\r\n",2); shutdown (r.sockfd, SHUT_RDWR); close(r.sockfd); log.erase(log.length()-1); log.append("] \""); log.append(r.request_type); log.append(" "); log.append(r.path); log.append(" "); log.append(r.http_ver); log.erase(log.length()-1); log.append("\" 404 -1\n"); } else { strcat(filept,"index.html"); int f2size; char dir_size[4]; if( strncmp(r.request_type,"GET\0",4)==0) { if((f2size=get_fsize(filept))==-1) { std::string files_here; struct dirent *dir_ent=NULL; int i=0; dir_ent=readdir(directory); while(dir_ent!=NULL) { if(dir_ent->d_name[0]!='.') { files_here.append(dir_ent->d_name); files_here.append("\n"); } i++; dir_ent=readdir(directory); } write(r.sockfd, "HTTP/1.0 200 OK\r\n", 17); write(r.sockfd, "Date: ",6); char *tstamp=get_timestamp(); log.append("] ["); log.append(tstamp); write(r.sockfd,tstamp,(((std::string)tstamp).length()-1)); write(r.sockfd,"\r\n",2); write(r.sockfd, "Server: ",8); write(r.sockfd, "Myhttpd/1.0\r\n",13); write(r.sockfd, "Last-Modified: ",15); write(r.sockfd,get_lastm(filept),(((std::string)get_lastm(filept)).length()-1)); write(r.sockfd,"\r\n",2); write(r.sockfd,"Content-Type: text/html\r\n",25); write(r.sockfd, "Content-Length: ",16); sprintf(dir_size,"%d",files_here.length()); write(r.sockfd,(char *)dir_size,strlen(dir_size)); write(r.sockfd,"\r\n\r\n",4); write(r.sockfd,files_here.data(),files_here.length()); write(r.sockfd,"\r\n",2); shutdown (r.sockfd, SHUT_RDWR); close(r.sockfd); log.erase(log.length()-1); log.append("] \""); log.append(r.request_type); log.append(" "); log.append(r.path); log.append(" "); log.append(r.http_ver); log.erase(log.length()-1); log.append("\" 200 "); log.append(dir_size); log.append("\n"); } else { FILE *f; int bytes; char *content=new char[f2size]; write(r.sockfd, "HTTP/1.0 200 OK\r\n", 17); write(r.sockfd, "Date: ",6); char *tstamp=get_timestamp(); log.append("] ["); log.append(tstamp); write(r.sockfd,tstamp,(((std::string)tstamp).length()-1)); write(r.sockfd,"\r\n",2); write(r.sockfd, "Server: ",8); write(r.sockfd, "Myhttpd/1.0\r\n",13); write(r.sockfd, "Last-Modified: ",15); write(r.sockfd,get_lastm(filept),(((std::string)get_lastm(filept)).length()-1)); write(r.sockfd,"\r\n",2); write(r.sockfd,"Content-Type: text/html\r\n",25); write(r.sockfd, "Content-Length: ",16); sprintf(dir_size,"%d",f2size); write(r.sockfd,(char *)dir_size,strlen(dir_size)); write(r.sockfd,"\r\n\r\n",4); f=fopen(filept,"rb"); while((bytes=fread(content,sizeof(char),ifsize,f))>0) { write(r.sockfd,content,bytes); } fclose(f); delete content; shutdown (r.sockfd, SHUT_RDWR); close(r.sockfd); log.erase(log.length()-1); log.append("] \""); log.append(r.request_type); log.append(" "); log.append(r.path); log.append(" "); log.append(r.http_ver); log.erase(log.length()-1); log.append("\" 200 "); log.append(dir_size); log.append("\n"); } } else { if((f2size=get_fsize(filept))==-1) { std::string files_here; struct dirent *dir_ent=NULL; dir_ent=readdir(directory); while(dir_ent!=NULL) { if(dir_ent->d_name[0]!='.') { files_here.append(dir_ent->d_name); files_here.append("\r\n"); } dir_ent=readdir(directory); } write(r.sockfd, "HTTP/1.0 200 OK\r\n", 17); write(r.sockfd, "Date: ",6); char *tstamp=get_timestamp(); log.append("] ["); log.append(tstamp); write(r.sockfd,tstamp,(((std::string)tstamp).length()-1)); write(r.sockfd,"\r\n",2); write(r.sockfd, "Server: ",8); write(r.sockfd, "Myhttpd/1.0\r\n",13); write(r.sockfd, "Last-Modified: ",15); write(r.sockfd,get_lastm(filept),(((std::string)get_lastm(filept)).length()-1)); write(r.sockfd,"\r\n",2); write(r.sockfd,"Content-Type: text/html\r\n",25); write(r.sockfd, "Content-Length: ",16); sprintf(dir_size,"%d",files_here.length()); write(r.sockfd,(char *)dir_size,strlen(dir_size)); write(r.sockfd,"\r\n\r\n",4); shutdown (r.sockfd, SHUT_RDWR); close(r.sockfd); log.erase(log.length()-1); log.append("] \""); log.append(r.request_type); log.append(" "); log.append(r.path); log.append(" "); log.append(r.http_ver); log.erase(log.length()-1); log.append("\" 200 "); log.append(dir_size); log.append("\n"); } else { write(r.sockfd, "HTTP/1.0 200 OK\r\n", 17); write(r.sockfd, "Date: ",6); char *tstamp=get_timestamp(); log.append("] ["); log.append(tstamp); write(r.sockfd,tstamp,(((std::string)tstamp).length()-1)); write(r.sockfd,"\r\n",2); write(r.sockfd, "Server: ",8); write(r.sockfd, "Myhttpd/1.0\r\n",13); write(r.sockfd, "Last-Modified: ",15); write(r.sockfd,get_lastm(filept),(((std::string)get_lastm(filept)).length()-1)); write(r.sockfd,"\r\n",2); write(r.sockfd,"Content-Type: text/html\r\n",25); write(r.sockfd, "Content-Length: ",16); sprintf(dir_size,"%d",f2size); write(r.sockfd,(char *)dir_size,strlen(dir_size)); write(r.sockfd,"\r\n\r\n",4); shutdown (r.sockfd, SHUT_RDWR); close(r.sockfd); log.erase(log.length()-1); log.append("] \""); log.append(r.request_type); log.append(" "); log.append(r.path); log.append(" "); log.append(r.http_ver); log.erase(log.length()-1); log.append("\" 200 "); log.append(dir_size); log.append("\n"); } } } } } logger(log); if(DEBUG_MODE) { REQUEST_SERVED=true; } } flag=false; } return NULL; }
void handlerq(int n, std::string log){ char msg[1000], *headers[3]; char c; int rcvd,fsize; int i=0; int flag=0; struct requests req; do { rcvd=read(n, &c, 1); msg[i]=c; if(msg[i]== '\n'&& msg[i-1] == '\r') { flag++; } else if(msg[i]!='\r')flag=0; if(i==2) { if((msg[0]!='G')&&(msg[1]!='E')&&(msg[2]!='T')) { msg[2]='\0'; break; } } else if(i==3) { if((msg[0]!='H')&&(msg[1]!='E')&&(msg[2]!='A')&&(msg[3]!='D')) { msg[3]='\0'; break; } } if (flag==2) { msg[i+1]='\0'; break; } i++; }while(1); if (rcvd<0) { std::cerr<<"some error in recv()"<<std::endl; close(n); } else if (rcvd==0) { std::cerr<<"Client disconnected unexpectedly"<<std::endl; } else { log.append(" - ["); log.append(get_timestamp()); log.erase(log.length()-1); req.log=log; headers[0] = strtok (msg, " \t\n"); headers[1] = strtok (NULL, " \t"); headers[2] = strtok (NULL, " \t\n"); if ((headers[0]==NULL)||(headers[1]==NULL)||(headers[2]==NULL)) { shutdown (n, SHUT_RDWR); close(n); } else { int hlength=0; char fullpath[200]; strcpy(fullpath,root); if ( strncmp(headers[0], "GET\0", 4)==0 ) { req.is_dir=false; req.path_found=true; if(headers[1][1]=='~') { headers[1]+=2; strcat(fullpath,headers[1]); } else { strcpy(fullpath,headers[1]); } hlength=strlen(fullpath); if(fullpath[hlength-1]=='/') { req.is_dir=true; } else { if((fsize=get_fsize(fullpath))==-1) { req.path_found=false; } } strcpy(req.request_type,headers[0]); strcpy(req.path,fullpath); req.sockfd=n; strcpy(req.http_ver,headers[2]); q.add(req,fsize); } else if ( strncmp(headers[0], "HEAD\0", 4)==0) { req.is_dir=false; req.path_found=true; if(headers[1][1]=='~') { headers[1]+=2; strcat(fullpath,headers[1]); } else { strcpy(fullpath,headers[1]); } hlength=strlen(fullpath); if(fullpath[hlength-1]=='/') { req.is_dir=true; } else { if((fsize=get_fsize(fullpath))==-1) { req.path_found=false; } } strcpy(req.request_type,headers[0]); strcpy(req.path,fullpath); req.sockfd=n; strcpy(req.http_ver,headers[2]); q.add(req,0); } else { shutdown (n, SHUT_RDWR); close(n); } } } }
bool http_servlet::doParse(acl::HttpServletRequest& req, acl::HttpServletResponse& res) { const char* ptr = req.getParameter("name1"); if (ptr) param1_ = ptr; ptr = req.getParameter("name2"); if (ptr) param2_ = ptr; ptr = req.getParameter("name3"); if (ptr) param3_ = ptr; acl::string path; // 遍历所有的 MIME 结点,找出其中为文件结点的部分进行转储 const std::list<acl::http_mime_node*>& nodes = mime_->get_nodes(); std::list<acl::http_mime_node*>::const_iterator cit = nodes.begin(); for (; cit != nodes.end(); ++cit) { const char* name = (*cit)->get_name(); if (name == NULL) continue; acl::http_mime_t mime_type = (*cit)->get_mime_type(); if (mime_type == acl::HTTP_MIME_FILE) { const char* filename = (*cit)->get_filename(); if (filename == NULL) { logger("filename null"); continue; } // 有的浏览器(如IE)上传文件时会带着文件路径,所以 // 需要先将路径去掉 filename = acl_safe_basename(filename); #if defined(_WIN32) || defined(_WIN64) path.format("%s\\%s", var_cfg_var_path, filename); #else path.format("%s/%s", var_cfg_var_path, filename); #endif (void) (*cit)->save(path.c_str()); if (strcmp(name, "file1") == 0) { file1_ = filename; fsize1_ = get_fsize(var_cfg_var_path, filename); } else if (strcmp(name, "file2") == 0) { file2_ = filename; fsize2_ = get_fsize(var_cfg_var_path, filename); } else if (strcmp(name, "file3") == 0) { file3_ = filename; fsize3_ = get_fsize(var_cfg_var_path, filename); } } } // 查找上载的某个文件并转储 const acl::http_mime_node* node = mime_->get_node("file1"); if (node && node->get_mime_type() == acl::HTTP_MIME_FILE) { ptr = node->get_filename(); if (ptr) { // 有的浏览器(如IE)上传文件时会带着文件路径,所以 // 需要先将路径去掉 ptr = acl_safe_basename(ptr); #if defined(_WIN32) || defined(_WIN64) path.format("%s\\1_%s", var_cfg_var_path, ptr); #else path.format("%s/1_%s", var_cfg_var_path, ptr); #endif (void) node->save(path.c_str()); } } return doReply(req, res, "OK"); }