void process_request_path(SWS_REQUEST* req_ptr){ struct stat st; char absolute_path[PATH_MAX_SIZE]; if (!check_path_str(req_ptr)){ clientError(400, "Bad Request", req_ptr); } if (req_ptr->is_cgi){ process_cgi(req_ptr); return; } memset(absolute_path, 0, PATH_MAX_SIZE); //printf("dir : %s\n", dir); snprintf(absolute_path, strlen(dir) + strlen(req_ptr->path) + 1, "%s%s", dir, req_ptr->path); //printf("Server: absolute path is %s\n", absolute_path); if (stat(absolute_path, &st) < 0){ //perror("stat"); clientError(404, "Not Found", req_ptr); } switch(st.st_mode&S_IFMT){ case S_IFREG: if (st.st_mode&(S_IRUSR | S_IRGRP | S_IROTH)){ int file_fd; file_fd = open(absolute_path, O_RDONLY); if (file_fd < 0){ clientError(500, "Internal Server Error", req_ptr); return; }else{ //handle both simple and non-simple request fprintf(stderr, "%s\n", "it's a file"); send_file(file_fd, req_ptr, absolute_path); } }else{ clientError(403, "Forbidden", req_ptr); } break; case S_IFDIR: if (st.st_mode&(S_IRUSR | S_IRGRP | S_IROTH)){ //process the index here indexGenerate( req_ptr, absolute_path); }else{ clientError(403, "Forbidden", req_ptr); } break; default: clientError(404, "Not Found", req_ptr); break; } }
// sending file and errors according to it void send_file(FILE *f, char *path, struct stat *statbuf) { // path = address? char data[4096]; int n; FILE *file = fopen(path, "r"); if (!file) { send_error(f, path, 403, "Forbidden", NULL, "Access denied."); } else if (strstr(url, ".cgi") != NULL) { // CGI_BIN request process_cgi(fd_http, fp, request_type, url); return; } else { int length = S_ISREG(statbuf->st_mode) ? statbuf->st_size : -1; // S_ISREG: checks if file is regular file send_headers(f, path, 200, "OK", NULL, get_mime_type(path), length, statbuf->st_mtime); while ((n = fread(data, 1, sizeof(data), file)) > 0) { fwrite(data, 1, n, f); // named pipe of tested.html output to tested.cgi input /*if (path = "/home/yeenayoon/work/web/testing/tested.html") { FILE *npfp; char readbuf[100]; umask(0); mknod(FIFO_FILE, S_IFIFO | 0666, 0); while (1) { npfp = fopen(FIFO_FILE, "r"); fgets(readbuf, 100, npfp); printf("%s\n", readbuf); fclose(npfp); } } */ }fclose(file); } }
static int http_get_info (const char *filename, struct File_Info *info) { extern struct ushare_t *ut; struct upnp_entry_t *entry = NULL; struct stat st; int upnp_id = 0; char *content_type = NULL; char *protocol = NULL; if (!filename || !info) return -1; log_verbose ("http_get_info, filename : %s\n", filename); if (!strcmp (filename, CDS_LOCATION)) { set_info_file (info, CDS_DESCRIPTION_LEN, SERVICE_CONTENT_TYPE); return 0; } if (!strcmp (filename, CMS_LOCATION)) { set_info_file (info, CMS_DESCRIPTION_LEN, SERVICE_CONTENT_TYPE); return 0; } if (!strcmp (filename, MSR_LOCATION)) { set_info_file (info, MSR_DESCRIPTION_LEN, SERVICE_CONTENT_TYPE); return 0; } if (ut->use_presentation && !strcmp (filename, USHARE_PRESENTATION_PAGE)) { if (build_presentation_page (ut) < 0) return -1; set_info_file (info, ut->presentation->len, PRESENTATION_PAGE_CONTENT_TYPE); return 0; } if (ut->use_presentation && !strncmp (filename, USHARE_CGI, strlen (USHARE_CGI))) { if (process_cgi (ut, (char *) (filename + strlen (USHARE_CGI) + 1)) < 0) return -1; set_info_file (info, ut->presentation->len, PRESENTATION_PAGE_CONTENT_TYPE); return 0; } upnp_id = atoi (strrchr (filename, '/') + 1); entry = upnp_get_entry (ut, upnp_id); if (!entry) return -1; if (!entry->fullpath) return -1; if (stat (entry->fullpath, &st) < 0) return -1; if (access (entry->fullpath, R_OK) < 0) { if (errno != EACCES) return -1; info->is_readable = 0; } else info->is_readable = 1; /* file exist and can be read */ info->file_length = st.st_size; info->last_modified = st.st_mtime; info->is_directory = S_ISDIR (st.st_mode); protocol = #ifdef HAVE_DLNA entry->dlna_profile ? dlna_write_protocol_info (DLNA_PROTOCOL_INFO_TYPE_HTTP, DLNA_ORG_PLAY_SPEED_NORMAL, DLNA_ORG_CONVERSION_NONE, DLNA_ORG_OPERATION_RANGE, ut->dlna_flags, entry->dlna_profile) : #endif /* HAVE_DLNA */ mime_get_protocol (entry->mime_type); content_type = strndup ((protocol + PROTOCOL_TYPE_PRE_SZ), strlen (protocol + PROTOCOL_TYPE_PRE_SZ) - PROTOCOL_TYPE_SUFF_SZ); free (protocol); if (content_type) { info->content_type = ixmlCloneDOMString (content_type); free (content_type); } else info->content_type = ixmlCloneDOMString (""); return 0; }