@@ -40,7 +40,7 @@ bool is(char * mime, std::string type) {
4040 return (strstr (mime, type.c_str ()) != NULL );
4141}
4242
43- resource DataHandler::read_resource (std::string path) {
43+ DataHandler:: resource DataHandler::read_resource (std::string path) {
4444 // prepend cwd() to path
4545
4646 if (!verify_path (path))
@@ -51,16 +51,17 @@ resource DataHandler::read_resource(std::string path) {
5151 this ->logger ->debug (" Checking resource at: " + path);
5252
5353 // check mime type of resource
54+ DataHandler::Exec runner;
5455 std::string args[2 ] = { " /usr/bin/file" , path };
55- resource file_mime = run_command (args);
56+ DataHandler:: resource file_mime = runner. run_command (args);
5657 char * mime = file_mime.data ;
5758
58- resource output;
59+ DataHandler:: resource output;
5960 // now check for known mime types
6061 if (is (mime, " executable" )) {
6162 // run the script
6263 std::string args[2 ] = { path, " " }; // TODO: Fix me too!
63- resource script_output = run_command (args);
64+ DataHandler:: resource script_output = runner. run_command (args);
6465 output.data = script_output.data ;
6566 output.size = script_output.size ;
6667 output.type = " executable" ;
@@ -73,7 +74,8 @@ resource DataHandler::read_resource(std::string path) {
7374 else if (is (mime, " MS Windows icon" )) { output.type = " image/vnd.microsoft.icon" ; }
7475
7576 if (output.type .length () > 0 && output.type != " executable" ) {
76- resource f = get_file (path);
77+ DataHandler::Static getter;
78+ DataHandler::resource f = getter.get_file (path);
7779 output.data = f.data ;
7880 output.size = f.size ;
7981 }
@@ -95,32 +97,9 @@ std::string DataHandler::get_working_path() {
9597 return ( getcwd (temp, PATH_MAX) ? std::string ( temp ) : std::string (" " ) );
9698}
9799
98- resource DataHandler::get_file (std::string path) {
99- this ->logger ->debug (" Read file: " + path);
100- int fd = open (path.c_str (), O_RDONLY);
101- if (fd < 0 ) {
102- // TODO: replace with a proper HTTP CODE
103- char * err = std::strerror (errno);
104- throw DataHandler::FileNotFound (" Error while reading file contents at " + path + " : " + std::string (err ? err : " unknown error" ));
105- }
106-
107- long fsize = lseek (fd, 0 , SEEK_END);
108- lseek (fd, 0 , SEEK_SET);
109-
110- resource data;
111- data.data = (char *) malloc (fsize+1 );
112- read (fd, data.data , fsize);
113- close (fd);
114-
115- data.data [fsize] = ' \0 ' ;
116- data.size = fsize;
117-
118- return data;
119- }
120-
121- resource DataHandler::get_error_file (int error_code, std::string param) {
100+ DataHandler::resource DataHandler::get_error_file (int error_code, std::string param) {
122101 // start by reading the error template
123- resource output = read_resource (" /errors/" + std::to_string (error_code) +" .html" );
102+ DataHandler:: resource output = DataHandler:: read_resource (" /errors/" + std::to_string (error_code) +" .html" );
124103 // now prepare a place to write the filled template
125104 long new_size = output.size + param.length () - 3 ;
126105 char * data = (char *) malloc (new_size * sizeof (char ));
@@ -135,85 +114,3 @@ resource DataHandler::get_error_file(int error_code, std::string param) {
135114
136115 return output;
137116}
138-
139- resource DataHandler::run_command (std::string args[]) {
140- int comms[2 ];
141- // start with a small 1k buffer
142- int buf_pos = 0 , buf_max = 1024 , buf_blocks = 1 ;
143- resource output;
144- output.data = (char *) malloc (buf_blocks * buf_max * sizeof (char ));
145-
146- if (pipe (comms) < 0 ) {
147- char * err = std::strerror (errno);
148- throw DataHandler::Exception (" Cannot create pipe: " + std::string (err ? err : " unknown error" ));
149- }
150-
151- int pid = fork ();
152- if (pid == 0 ) {
153- // redirect STDOUT to our comms
154- if (dup2 (comms[1 ], STDOUT_FILENO) == -1 ) {
155- char * err = std::strerror (errno);
156- throw DataHandler::Exception (" Cannot redirect STDOUT to pipe: " + std::string (err ? err : " unknown error" ));
157- }
158-
159- // same with STDERR
160- if (dup2 (comms[1 ], STDERR_FILENO) == -1 ) {
161- char * err = std::strerror (errno);
162- throw DataHandler::Exception (" Cannot redirect STDERR to pipe: " + std::string (err ? err : " unknown error" ));
163- }
164-
165- // now close the pipe on our side
166- close (comms[0 ]);
167-
168- // now run the target
169- // TODO: Fix me! I'm ugly!
170- if (execl (args[0 ].c_str (), args[0 ].c_str (), args[1 ].c_str (), (char *) 0 ) < 0 ) {
171- char * err = std::strerror (errno);
172- throw DataHandler::Exception (
173- " Cannot exec '" + std::string (args[0 ]) + " ' due to: " + std::string (err ? err : " unknown error" )
174- );
175- }
176- }
177- else if (pid > 0 ) {
178- close (comms[1 ]);
179-
180- // Just a char by char read here, you can change it accordingly
181- char rchar;
182- while (read (comms[0 ], &rchar, 1 ) == 1 ) {
183- output.data [buf_pos++] = rchar;
184- if (buf_pos >= buf_max){
185- // extend the buffer by another 1k block
186- buf_blocks++;
187- // reset the curent position
188- buf_pos = 0 ;
189- // allocate more memory
190- output.data = (char *) realloc (output.data , buf_blocks * buf_max * sizeof (char ));
191- if (output.data == NULL ) {
192- char * err = std::strerror (errno);
193- throw DataHandler::Exception (
194- " Error while reading output from: " + std::string (args[0 ]) +" , "
195- + " because of: " + std::string (err ? err : " unknown error" )
196- );
197- }
198- }
199- }
200- // add NULL termination
201- output.data [buf_pos] = ' \0 ' ;
202- output.size = (buf_blocks - 1 ) * buf_max + buf_pos + 1 ;
203-
204- close (comms[0 ]);
205- }
206- else {
207- // fork failed
208- char * err = std::strerror (errno);
209- close (comms[0 ]);
210- close (comms[1 ]);
211- throw DataHandler::Exception (" Cannot fork: " + std::string (err ? err : " unknown error" ));
212- }
213-
214- if (strstr (output.data , " DataHandler::Exception" ) != NULL ) {
215- throw DataHandler::Exception (" Fork returned an exception: " + std::string (output.data ));
216- }
217-
218- return output;
219- }
0 commit comments