/* check if (arg) is a valid wordlist file */ void parser_wordlist(char *arg) { struct stat wrd_stat; #ifdef HAVE_LIBMAGIC const char *target_mime = "text/plain;"; #endif if(arg == NULL) fatal("called with NULL argument."); else if(globals.options.dict == false) fatal("dictionary features OFF. unable to parse wordlist."); else if( stat(arg,&wrd_stat) ) // if can't get file stats pfatal(arg); else if( S_ISREG(wrd_stat.st_mode) == 0 ) // if isn't a regular file fatal_long("\"%s\" is not a regular file.",arg); else if( access(arg,R_OK)) pfatal(arg); else { #ifdef HAVE_LIBMAGIC if(strncmp(get_mime(arg),target_mime,strlen(target_mime)+1) != 0) report(warning,"\"%s\" is not a \"%s\" file.",arg,target_mime); #endif if((globals.wordlist = (const char *) get_full_path(arg)) == NULL) fatal("unable to resolve full path for wordlist."); } return; }
struct fbuf *load_file(char *filename) { struct _stat sbuf; if (_stat(filename, &sbuf) == -1) { return NULL; } struct fbuf *ct; if ((ct = (struct fbuf *)malloc(sizeof(struct fbuf))) == NULL) { return NULL; } ct->buf_size = sbuf.st_size + 1; ct->buf = (char *)calloc(ct->buf_size, sizeof(char)); ct->mime = (char *)calloc(24, sizeof(char)); FILE *fd; if (fopen_s(&fd, filename, "rb") != 0) { return NULL; } // load file data to content fread(ct->buf, sizeof(char), ct->buf_size, fd); ct->buf[ct->buf_size] = '\0'; strcpy_s(ct->mime, sizeof(char)*24, get_mime(filename)); // close the stream fclose(fd); return ct; }
/** *serveHead : serves the client with the requested HEAD METHOD * args: * response: response struct to be filled * fp : File pointer of the file to be sent *return: * none */ void serveHead(bufStruct *response, FILE *fp,char *uri) { char *mimeBuf; char mimeHeader[MAX_PATH] ="Content-Type: "; //currently sending 200 OK // fill the file transfer strcpy(((response->buffer)+(response->bufSize)),response200); response->bufSize += strlen(response200); //Server strcpy(((response->buffer)+(response->bufSize)),server); response->bufSize += strlen(server); //Get content type mimeBuf = get_mime(uri); strcat(mimeHeader,mimeBuf); strcat(mimeHeader,"\r\n"); strcpy(((response->buffer)+(response->bufSize)),mimeHeader); response->bufSize += strlen(mimeHeader); //Add Connection:close strcpy(((response->buffer)+(response->bufSize)),connectionClose); response->bufSize += strlen(connectionClose); //ending CRLF strcpy(((response->buffer)+(response->bufSize)),"\r\n"); response->bufSize += strlen("\r\n"); //No- entity response->entitySize =0; fclose(fp); return; }
/** *serveGet : serves the client with the requested GET METHOD * args: * response: response struct to be filled * fp : File pointer of the file to be sent *return: * none */ void serveGet(bufStruct *response,FILE *fp,char *uri) { char *mimeBuf; char mimeHeader[MAX_PATH] ="Content-Type: "; //currently sending 200 OK // fill the file transfer strcpy(((response->buffer)+(response->bufSize)),response200); response->bufSize += strlen(response200); //Server strcpy(((response->buffer)+(response->bufSize)),server); response->bufSize += strlen(server); //Get content type mimeBuf = get_mime(uri); strcat(mimeHeader,mimeBuf); strcat(mimeHeader,"\r\n"); strcpy(((response->buffer)+(response->bufSize)),mimeHeader); response->bufSize += strlen(mimeHeader); //Add Connection:close strcpy(((response->buffer)+(response->bufSize)),connectionClose); response->bufSize += strlen(connectionClose); //ending CRLF strcpy(((response->buffer)+(response->bufSize)),"\r\n"); response->bufSize += strlen("\r\n"); //Entity Body //Getting filesize fseek(fp,0,SEEK_END); response->entitySize = ftell(fp); rewind(fp); response->entityBuffer = malloc(response->entitySize); fread(response->entityBuffer,sizeof(char),response->entitySize,fp); //file is in buffer now fclose(fp); //end response return; }
//发送头部 void send_head(int client_sock,char *path){ char content_type[30]; get_mime(content_type,path); char buf[1024]; sprintf(buf,"HTTP/1.0 200 OK\r\n"); send(client_sock,buf,strlen(buf),0); sprintf(buf,SERVER_STRING); send(client_sock,buf,strlen(buf),0); sprintf(buf,"Content-Type:%s\r\n",content_type); send(client_sock,buf,strlen(buf),0); sprintf(buf,"\r\n"); send(client_sock,buf,strlen(buf),0); }
int http_router(pConnInfo conn_info) { if(conn_info->status_code != 0) send_error_response(conn_info); int sockfd = conn_info->clientfd; char path[PATH_LEN]; struct stat stat_buff; int i = 0; strcpy(path, conn_info->request_header->path); while(path[i] != 0) { if(path[i] == '?' || path[i] == '#') { path[i] = 0; break; } i++; } if(lstat(path, &stat_buff) != -1) { if( S_ISDIR(stat_buff.st_mode) ) { mime = uws_strdup("text/html"); printdir(path, conn_info); } else { mime = get_mime(path); printfile(path, conn_info); } } else { conn_info->status_code = 404; send_error_response(conn_info); return; } set_header(conn_info); write_response(conn_info, &header_body); //uws_free(header_body.header); sorry, reponse_header will be freed at the end of request uws_free(header_body.content); free_header_params(conn_info->response_header); }
int send_file(char **buffer, char *file_path) { FILE *file; int file_size; /** \brief Content-Type for supported MIME types */ const static char *content_types[] = { "application/octet-stream", "text/plain", "text/html", "text/javascript", "text/css", "image/png", "image/x-icon", "image/svg+xml" }; if((file = fopen(file_path, "r")) == NULL) { log_write("Could not open requested file", LOG_ERR); return -1; } // This possibly fixes the TOCTOU problem fseek(file, 0, SEEK_END); file_size = ftell(file); fseek(file, 0, SEEK_SET); if(file_size <= 0) { log_write("Could not read requested file", LOG_ERR); fclose(file); return -1; } *buffer=(char *)calloc(BUF_SIZE+file_size,1); sprintf(*buffer, "HTTP/1.1 200 OK\r\nContent-Type: %s\r\nContent-Length: %u\r\nConnection: close\r\nPragma: no-cache\r\nCache-Control: no-store\r\n\r\n", content_types[get_mime(file_path)], file_size); int header_len=strlen(*buffer); int read_ret = fread((*buffer+strlen(*buffer)), 1, file_size, file); if(read_ret != file_size) { fclose(file); return -1; } file_size+=header_len; fclose(file); return file_size; }
int request_file(HttpSession *session,char *rpath) { int fd; const char *mime=NULL; char *bname=NULL; char *full_fath=NULL; size_t path_len; unsigned long filesize=0; if(rpath==NULL) { path_len=strlen(session->request.path)+strlen(session->server->www_folder)+20; full_fath=malloc(path_len); sprintf(full_fath,"%s%s",session->server->www_folder,session->request.path); } else { full_fath=strdup(rpath); } //printf("mime:%s\n",mime); if(is_file_dir(full_fath) == 1 && enum_index(full_fath)==-1) { TraceErr("path %s has no inde.* ,return 404 !\n",full_fath); return 404; } TraceImport("request path:%s\n",full_fath); filesize=get_file_size((const char*)full_fath); if( filesize <= 0) { TraceErr("get_file_size %s fial,return 404 not found!\n",full_fath); return 404; } fd=open((const char *)full_fath,O_RDONLY|O_NONBLOCK); if(fd < 0 ) { TraceErr("open %s fial,return 404 not found!\n",full_fath); return 404; } TraceImport("open %s OK,size:%u !\n",full_fath,filesize); //path_len=get_file_size(full_fath); bname=basename(full_fath); mime=get_mime(strrchr(bname,'.')); if(mime!=NULL ) { session->response=buff_new(1500,0); buff_printf(session->response,"HTTP/1.0 200 OK\r\n"); buff_printf(session->response,"Server:whtc123\r\n"); buff_printf(session->response,"Content-type:%s\r\n",mime); buff_printf(session->response,"Content-length:%d; charset=GBK\r\n",filesize); buff_printf(session->response,"\r\n"); } else { } session->file_fd=fd; event_fd_setcallback(session->ev,httpsession_file_write); free(full_fath); return 200; }
gboolean main_include_task(gpointer _options) { GtkRecentManager *grm = gtk_recent_manager_get_default(); GtkRecentData *data; GSList* iterator = NULL; recent_file_options_t* options = _options; unsigned added = 0; /* Total files added */ static gchar* groups[] = { NULL }; for (iterator = options->fileNames; iterator; iterator = iterator->next) { char* _fileName = (char*)iterator->data; if (!file_exists(_fileName)) { if (!options->quiet) fprintf (stderr, "Error '%s' does not exist!\n", _fileName); continue; } char* fileName = realpath(_fileName, NULL); if (fileName==NULL) { if (!options->quiet) fprintf (stderr, "Error getting '%s' path!\n", _fileName); continue; } data = g_slice_new(GtkRecentData); data->display_name=g_strdup(fileName); data->description = NULL; data->mime_type=get_mime(fileName); data->app_name = (gchar*) g_get_application_name(); data->app_exec = g_strdup("recents"); data->groups = groups; data->is_private = FALSE; gchar *uri = g_filename_to_uri(fileName, NULL, NULL); if (gtk_recent_manager_add_full(grm, uri, data)) { if (!options->quiet) printf("File '%s' added successfully\n", fileName); ++added; } if (options->touchFile) { struct utimbuf utb; time_t now = time(NULL); utb.actime = now; utb.modtime = now; if ( (utime (fileName, &utb)<0) && (!options->quiet) ) { fprintf (stderr, "Could not touch '%s' (errno: %d, %s)\n", fileName, errno, strerror(errno)); } } free(fileName); } options->result = (added == g_slist_length(options->fileNames))?0:100; gtk_main_quit(); }
size_t View_types::get_hash_type(const Path& p) const { std::string mime = get_mime(p); return (mime.empty()) ? 0 : calculate_mime_hash(mime); }
int send_file(char **buffer, char *file_path) { FILE *file; int file_size; /** \brief Content-Type for supported MIME types */ static const char *content_types[] = { "application/octet-stream", "text/plain", "text/html", "text/javascript", "text/css", "image/png", "image/x-icon", "image/svg+xml" }; // Always use sec_fopen! if((file = sec_fopen(file_path, "r")) == NULL) { log_write(LOG_ERR, "Could not open requested file '%s'", file_path); goto ERROR; } // Get filesize fseek(file, 0, SEEK_END); file_size = ftell(file); fseek(file, 0, SEEK_SET); if(file_size <= 0) { log_write(LOG_ERR, "Could not read requested file"); goto ERROR_FCLOSE; } // Get memory to load file into buffer *buffer=(char *)calloc(BUF_SIZE+file_size,1); // Default answer header -> Disable browser caching! sprintf(*buffer, "HTTP/1.1 200 OK\r\nContent-Type: %s\r\nContent-Length: %u\r\nConnection: close\r\nPragma: no-cache\r\nCache-Control: no-store\r\n\r\n", content_types[get_mime(file_path)], file_size); int header_len=strlen(*buffer); // Check if the whole file was read int read_ret = fread((*buffer+strlen(*buffer)), 1, file_size, file); if(read_ret != file_size) { goto ERROR_BUF_FREE; } file_size+=header_len; fclose(file); return file_size; ERROR_BUF_FREE: nfree(*buffer); ERROR_FCLOSE: fclose(file); ERROR: *buffer=strdup(_("HTTP404", strdup(HTTP500))); return strlen(*buffer); }