inline std::string BuildPath(const std::string& path) { std::vector<std::string> dir; PathSegment(path, dir); std::string pathtest; for (auto directory : dir) { pathtest += directory; if (TypeOfFile(pathtest) == FileType::REGULAR) return ""; if (TypeOfFile(pathtest) == FileType::INVALID) { if (Mkdir(pathtest) == false) return ""; } pathtest += SLASHSEP; } return pathtest; }
/** * Sends the HTTP Response along with the web * content to the socket file descriptor. * * @param fileToSend: location of file to display to client * @param sock: Socket file descriptor * @param home: * @param content: * @param response: Holds information regarding HTTP request */ void SendDataBin(char *fileToSend, int sock, char *home, char *content, HTTP_Response *response) { char *fullPathToFile; //[256]; char Header[1024]; char buffer[256]; char *endChar; int size; bzero(Header, sizeof(Header)); /* * Build the full path to the file */ bool hasSlash; if ((last_char(content) == "/") || (first_char(fileToSend) == '/')) hasSlash = TRUE; else hasSlash = FALSE; size = strlen(home) + strlen(content) + strlen(fileToSend); if (hasSlash) { size += 2; fullPathToFile = malloc(size); sprintf(fullPathToFile, "%s/%s%s", home, content, fileToSend); } else { size += 3; fullPathToFile = malloc(size); sprintf(fullPathToFile, "%s/%s/%s", home, content, fileToSend); } //int file_open = open(fullPathToFile, O_RDONLY); int fileType = TypeOfFile(fullPathToFile); printf("Filetype: %d\n", fileType); if ((fileType == FILE_NOT_FOUND) || (fileType == ERROR_FILE)) { //Requested page not available. bzero(fullPathToFile, strlen(fullPathToFile)); size = strlen(home) + strlen(content); if (last_char(content) == "/") { size += 16; fullPathToFile = realloc(fullPathToFile, size); sprintf(fullPathToFile, "%s/%snot_found.html", home, content); } else { size += 17; fullPathToFile = realloc(fullPathToFile, size); sprintf(fullPathToFile, "%s/%s/not_found.html", home, content); } } else if (fileType == DIRECTORY) { //Append index.html size += 10; fullPathToFile = realloc(fullPathToFile, size); strcat(fullPathToFile, "index.html"); } printf("File to open: %s\n", fullPathToFile); int file_open; FILE *file_fd = NULL; if (fileType == EXECUTABLE_FILE) { if ((file_fd = popen(fullPathToFile, "r")) == NULL) { perror("popen"); exit(-1); } file_open = fileno(file_fd); //Convert from FILE to file descriptor integer } else { file_open = open(fullPathToFile, O_RDONLY); } if (file_open < 0) { perror("Open file error"); exit(-1); } long unsigned filesize = lseek(file_open, (off_t)0, SEEK_END); lseek(file_open, (off_t)0, SEEK_SET); //Set back to starting position setupHeader(Header, response, filesize); printf("\nHeader:\n%s\n\n", Header); /* * Send the header, open the requested file. * Send requested file, close file. */ printf("file: %s\n", fullPathToFile); write(sock, Header, strlen(Header)); // If request was for HEAD, do not send the body information if (strncmp(response -> HTTP_Type, "HEAD", 4) == 0) { free (fullPathToFile); fullPathToFile = NULL; return; } bzero(buffer, sizeof(buffer)); while (read(file_open, buffer, sizeof(buffer)) > 0) { write(sock, buffer, sizeof(buffer)); bzero(buffer, sizeof(buffer)); } if (fileType == EXECUTABLE_FILE) pclose(file_fd); else close(file_open); // Deallocate allocated pointers free (fullPathToFile); fullPathToFile = NULL; }