-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.cpp
115 lines (102 loc) · 4.54 KB
/
helper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* helper.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ychen2 <ychen2@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/25 21:11:22 by ychen2 #+# #+# */
/* Updated: 2024/08/25 21:16:19 by ychen2 ### ########.fr */
/* */
/* ************************************************************************** */
#include "helper.hpp"
// Function to split a string by a delimiter
std::vector<std::string> split(const std::string &str, char delimiter) {
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(str);
while (std::getline(tokenStream, token, delimiter)) {
tokens.push_back(token);
}
return tokens;
}
// Function to check if a file is executable
bool isExecutable(const std::string &path) {
struct stat sb;
return (stat(path.c_str(), &sb) == 0 && sb.st_mode & S_IXUSR);
}
// Function to find the full path of the program
std::string find_cgi_path(const std::string &program) {
const char *pathEnv = std::getenv("PATH");
if (!pathEnv) {
throw std::runtime_error("PATH environment variable not found.\n");
}
std::string pathStr(pathEnv);
std::vector<std::string> directories = split(pathStr, ':');
for (std::vector<std::string>::iterator it = directories.begin(); it != directories.end(); ++it) {
std::string fullPath = *it + "/" + program;
if (isExecutable(fullPath)) {
return fullPath;
}
}
throw std::runtime_error("Program not found.\n");
}
std::string getMimeType(const std::string& fileName) {
// Define the MIME types using a map
std::map<std::string, std::string> mimeTypes;
mimeTypes[".html"] = "text/html";
mimeTypes[".htm"] = "text/html";
mimeTypes[".css"] = "text/css";
mimeTypes[".js"] = "application/javascript";
mimeTypes[".json"] = "application/json";
mimeTypes[".xml"] = "application/xml";
mimeTypes[".jpg"] = "image/jpeg";
mimeTypes[".jpeg"] = "image/jpeg";
mimeTypes[".png"] = "image/png";
mimeTypes[".gif"] = "image/gif";
mimeTypes[".svg"] = "image/svg+xml";
mimeTypes[".ico"] = "image/x-icon";
mimeTypes[".pdf"] = "application/pdf";
mimeTypes[".zip"] = "application/zip";
mimeTypes[".rar"] = "application/x-rar-compressed";
mimeTypes[".tar"] = "application/x-tar";
mimeTypes[".mp3"] = "audio/mpeg";
mimeTypes[".wav"] = "audio/wav";
mimeTypes[".ogg"] = "audio/ogg";
mimeTypes[".mp4"] = "video/mp4";
mimeTypes[".avi"] = "video/x-msvideo";
mimeTypes[".mpeg"] = "video/mpeg";
mimeTypes[".webm"] = "video/webm";
mimeTypes[".txt"] = "text/plain";
mimeTypes[".csv"] = "text/csv";
mimeTypes[".rtf"] = "application/rtf";
mimeTypes[".xls"] = "application/vnd.ms-excel";
mimeTypes[".xlsx"] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
mimeTypes[".doc"] = "application/msword";
mimeTypes[".docx"] = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
mimeTypes[".ppt"] = "application/vnd.ms-powerpoint";
mimeTypes[".pptx"] = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
mimeTypes[".epub"] = "application/epub+zip";
mimeTypes[".otf"] = "font/otf";
mimeTypes[".ttf"] = "font/ttf";
mimeTypes[".woff"] = "font/woff";
mimeTypes[".woff2"] = "font/woff2";
mimeTypes[".eot"] = "application/vnd.ms-fontobject";
// Find the last dot in the filename
size_t dotPos = fileName.find_last_of('.');
if (dotPos != std::string::npos) {
// Extract the extension from the file name
std::string extension = fileName.substr(dotPos);
// Convert extension to lowercase
for (size_t i = 0; i < extension.length(); ++i) {
extension[i] = std::tolower(extension[i]);
}
// Look up the MIME type in the map
std::map<std::string, std::string>::const_iterator it = mimeTypes.find(extension);
if (it != mimeTypes.end()) {
return it->second;
}
}
// Default MIME type if no match found
return "application/octet-stream";
}