Webserv
Loading...
Searching...
No Matches
tools.cpp
Go to the documentation of this file.
1#include "webserv.hpp"
2
3void *ft_memset(void *b, int c, size_t len) {
4 unsigned char *b_cpy;
5
6 b_cpy = (unsigned char *)b;
7 for (size_t i = 0; i < len; i++)
8 *(unsigned char *)(b_cpy + i) = (unsigned char)c;
9 return (b);
10}
11
12bool isAPort(string str) {
13 int n = 0;
14 for (string::iterator it = str.begin(); it < str.end(); it++)
15 if ((*it < '0' || *it > '9') && n++ < 5)
16 return false;
17 return true;
18}
19
20vec_string split(string str, string delim) {
21 string temp(str);
22 string token;
23 size_t pos;
24 vec_string tokens;
25
26 while ((pos = temp.find(delim)) != string::npos) {
27 token = temp.substr(0, pos);
28 tokens.push_back(token);
29 temp.erase(0, pos + delim.length());
30 }
31 tokens.push_back(temp);
32 return tokens;
33}
34
35ip_port_t get_ip_port_t(string listen) {
36 size_t sep_pos = listen.rfind(':');
37 if (sep_pos == string::npos) {
38 if (isAPort(listen))
39 return (ip_port_t){0, "0.0.0.0", std::atoi(listen.c_str())};
40 else
41 return (ip_port_t){0, listen == "localhost" ? "127.0.0.1" : listen, 80};
42 }
43 ip_port_t ret;
44 string tmp = listen.substr(0, sep_pos);
45 ret.ip = tmp == "localhost" ? "127.0.0.1" : tmp;
46 tmp = listen.substr(sep_pos + 1, listen.length() - sep_pos - 1).c_str();
47 ret.port = std::atoi(tmp.c_str());
48 return ret;
49}
50
51ip_port_t get_ip_port_t(string ip, int port) {
52 ip_port_t ret;
53 ret.ip = ip;
54 ret.port = port;
55 return ret;
56}
57
58string read_file(string path) {
59 struct stat info;
60 if (stat(path.c_str(), &info) != 0) {
61 std::cerr << "stat() error on " << path << ": " << strerror(errno) << "\n";
62 return "404";
63 } else if (S_ISDIR(info.st_mode))
64 return "404";
65
66 std::ifstream file(path.c_str());
67 if (!file.good())
68 return "404";
69 std::stringstream tmp;
70 tmp << file.rdbuf();
71 string body = tmp.str();
72 std::stringstream ret;
73 ret << "Content-type: " << getMime(path) << "\r\n"
74 << "Content-length: " << body.length() << "\r\n"
75 << "\r\n"
76 << body;
77 return (ret.str());
78}
79
80string getMime(string path) {
81 size_t pos = path.rfind('.');
82 string extension = (pos == string::npos) ? "txt" : path.substr(pos + 1);
83
84 if ((extension == "html") || (extension == "htm") || (extension == "shtml"))
85 return ("text/html");
86 else if ((extension == "css"))
87 return ("text/css");
88 else if ((extension == "xml"))
89 return ("text/xml");
90 else if ((extension == "gif"))
91 return ("image/gif");
92 else if ((extension == "jpeg") || (extension == "jpg"))
93 return ("image/jpeg");
94 else if ((extension == "js"))
95 return ("application/javascript");
96 else if ((extension == "atom"))
97 return ("application/atom+xml");
98 else if ((extension == "rss"))
99 return ("application/rss+xml");
100 else if ((extension == "mml"))
101 return ("text/mathml");
102 else if ((extension == "txt"))
103 return ("text/plain");
104 else if ((extension == "jad"))
105 return ("text/vnd.sun.j2me.app-descriptor");
106 else if ((extension == "wml"))
107 return ("text/vnd.wap.wml");
108 else if ((extension == "htc"))
109 return ("text/x-component");
110 else if ((extension == "avif"))
111 return ("image/avif");
112 else if ((extension == "png"))
113 return ("image/png");
114 else if ((extension == "svg") || (extension == "svgz"))
115 return ("image/svg+xml");
116 else if ((extension == "tif") || (extension == "tiff"))
117 return ("image/tiff");
118 else if ((extension == "wbmp"))
119 return ("image/vnd.wap.wbmp");
120 else if ((extension == "webp"))
121 return ("image/webp");
122 else if ((extension == "ico"))
123 return ("image/x-icon");
124 else if ((extension == "jng"))
125 return ("image/x-jng");
126 else if ((extension == "bmp"))
127 return ("image/x-ms-bmp");
128 else if ((extension == "woff"))
129 return ("font/woff");
130 else if ((extension == "woff2"))
131 return ("font/woff2");
132 else if ((extension == "jar") || (extension == "war") || (extension == "ear"))
133 return ("application/java-archive");
134 else if ((extension == "json"))
135 return ("application/json");
136 else if ((extension == "hqx"))
137 return ("application/mac-binhex40");
138 else if ((extension == "doc"))
139 return ("application/msword");
140 else if ((extension == "pdf"))
141 return ("application/pdf");
142 else if ((extension == "ps") || (extension == "eps") || (extension == "ai"))
143 return ("application/postscript");
144 else if ((extension == "rtf"))
145 return ("application/rtf");
146 else if ((extension == "m3u8"))
147 return ("application/vnd.apple.mpegurl");
148 else if ((extension == "xls") || (extension == "xlt") || (extension == "xlm") || (extension == "xld") ||
149 (extension == "xla") || (extension == "xlc") || (extension == "xlw") || (extension == "xll"))
150 return ("application/vnd.ms-excel");
151 else if ((extension == "ppt") || (extension == "pps"))
152 return ("application/vnd.ms-powerpoint");
153 else if ((extension == "wmlc"))
154 return ("application/vnd.wap.wmlc");
155 else if ((extension == "kml"))
156 return ("application/vnd.google-earth.kml+xml");
157 else if ((extension == "kmz"))
158 return ("application/vnd.google-earth.kmz");
159 else if ((extension == "7z"))
160 return ("application/x-7z-compressed");
161 else if ((extension == "cco"))
162 return ("application/x-cocoa");
163 else if ((extension == "jardiff"))
164 return ("application/x-java-archive-diff");
165 else if ((extension == "jnlp"))
166 return ("application/x-java-jnlp-file");
167 else if ((extension == "run"))
168 return ("application/x-makeself");
169 else if ((extension == "pl") || (extension == "pm"))
170 return ("application/x-perl");
171 else if ((extension == "pdb") || (extension == "pqr") || (extension == "prc") || (extension == "pde"))
172 return ("application/x-pilot");
173 else if ((extension == "rar"))
174 return ("application/x-rar-compressed");
175 else if ((extension == "rpm"))
176 return ("application/x-redhat-package-manager");
177 else if ((extension == "sea"))
178 return ("application/x-sea");
179 else if ((extension == "swf"))
180 return ("application/x-shockwave-flash");
181 else if ((extension == "sit"))
182 return ("application/x-stuffit");
183 else if ((extension == "tcl") || (extension == "tk"))
184 return ("application/x-tcl");
185 else if ((extension == "der") || (extension == "pem") || (extension == "crt"))
186 return ("application/x-x509-ca-cert");
187 else if ((extension == "xpi"))
188 return ("application/x-xpinstall");
189 else if ((extension == "xhtml") || (extension == "xht"))
190 return ("application/xhtml+xml");
191 else if ((extension == "zip"))
192 return ("application/zip");
193 else if ((extension == "bin") || (extension == "exe") || (extension == "dll"))
194 return ("application/octet-stream");
195 else if ((extension == "deb"))
196 return ("application/octet-stream");
197 else if ((extension == "dmg"))
198 return ("application/octet-stream");
199 else if ((extension == "eot"))
200 return ("application/octet-stream");
201 else if ((extension == "img") || (extension == "iso"))
202 return ("application/octet-stream");
203 else if ((extension == "msi") || (extension == "msp") || (extension == "msm"))
204 return ("application/octet-stream");
205 else if ((extension == "mid") || (extension == "midi") || (extension == "kar"))
206 return ("audio/midi");
207 else if ((extension == "mp3"))
208 return ("audio/mpeg");
209 else if ((extension == "ogg"))
210 return ("audio/ogg");
211 else if ((extension == "m4a"))
212 return ("audio/x-m4a");
213 else if ((extension == "ra"))
214 return ("audio/x-realaudio");
215 else if ((extension == "3gpp") || (extension == "3gp"))
216 return ("video/3gpp");
217 else if ((extension == "ts"))
218 return ("video/mp2t");
219 else if ((extension == "mp4"))
220 return ("video/mp4");
221 else if ((extension == "mpeg") || (extension == "mpg"))
222 return ("video/mpeg");
223 else if ((extension == "mov"))
224 return ("video/quicktime");
225 else if ((extension == "webm"))
226 return ("video/webm");
227 else if ((extension == "flv"))
228 return ("video/x-flv");
229 else if ((extension == "m4v"))
230 return ("video/x-m4v");
231 else if ((extension == "mng"))
232 return ("video/x-mng");
233 else if ((extension == "asx") || (extension == "asf"))
234 return ("video/x-ms-asf");
235 else if ((extension == "wmv"))
236 return ("video/x-ms-wmv");
237 else if ((extension == "avi"))
238 return ("video/x-msvideo");
239 else
240 return ("text/plain");
241}
int port
Definition: webserv.hpp:38
string ip
Definition: webserv.hpp:37
string read_file(string path)
Definition: tools.cpp:58
string getMime(string path)
Definition: tools.cpp:80
bool isAPort(string str)
Definition: tools.cpp:12
void * ft_memset(void *b, int c, size_t len)
Definition: tools.cpp:3
vec_string split(string str, string delim)
Definition: tools.cpp:20
ip_port_t get_ip_port_t(string listen)
Definition: tools.cpp:35
std::vector< string > vec_string
Definition: webserv.hpp:50