void httperror (iostream &sstream, int status, string s_str, string relURL) { string errorFile; con.errorLog (s_str, relURL); // display error page for corresponding error if (status == 403) errorFile = "public_www/403error.html"; else if (status == 404) errorFile = "public_www/404error.html"; else if (status == 400) errorFile = "public_www/400error.html"; else if (status == 501) errorFile = "public_www/501error.html"; else errorFile = "public_www/000error.html"; ifstream inputfile2(errorFile.c_str(), ios::in); string header = "HTTP/1.1 200 OK\r\n"; sstream.write (header.c_str(), header.size()); header = "Content-Type:text/html\r\n"; sstream.write (header.c_str(), header.size()); int filesize = FileSize (errorFile.c_str()); header = "Content-Length:" + strconvert(filesize) + "\r\n"; sstream.write (header.c_str(), header.size()); // end with empty line sstream.write ("\r\n", 2); char c2; for (; filesize >0; filesize--) { c2 = inputfile2.get(); sstream.put (c2); //cout.put(c2); if (sstream.fail()) { // socket stream failed. Closed by client??? // nothing we can do, but stop break; } } sstream.flush (); inputfile2.close (); }
void makeDirectory (iostream &sstream, string relURL) { string makedir = "make_directory.html"; DIR *pdir; struct dirent *pent; string requestedFile = ROOT_DIR + relURL; pdir=opendir(requestedFile.c_str()); ofstream myfile; myfile.open (makedir.c_str()); myfile << "<html><head>" << endl; myfile << "<title>Listing Directory Contents of " << relURL << "</title>" << endl; myfile << "</head><body>" << endl; myfile << "<h1>Listing Directory Contents of " << relURL << "</h1>" << endl; while ((pent=readdir(pdir))){ if (!strcmp(pent->d_name, ".") || !strcmp(pent->d_name, "..")) continue; myfile << "<a href=\"" << relURL << pent->d_name << "\">" << pent->d_name << "</a>"; if (con.lookupAccessLog(relURL + pent->d_name) == "") myfile << " <-> <-><br>" << endl; else myfile << " " << con.lookupAccessLog(relURL + pent->d_name) << "<br>" << endl; } myfile << "</body></html>" << endl; closedir(pdir); ifstream inputfile2(makedir.c_str(), ios::in); string header = "HTTP/1.1 200 OK\r\n"; sstream.write (header.c_str(), header.size()); header = "Content-Type:text/html\r\n"; sstream.write (header.c_str(), header.size()); int filesize = FileSize (makedir.c_str()); header = "Content-Length:" + strconvert(filesize) + "\r\n"; sstream.write (header.c_str(), header.size()); // end with empty line sstream.write ("\r\n", 2); char c2; for (; filesize >0; filesize--) { c2 = inputfile2.get(); sstream.put (c2); //cout.put(c2); if (sstream.fail()) { // socket stream failed. Closed by client??? // nothing we can do, but stop break; } } sstream.flush (); inputfile2.close (); }