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 (); }
// NOTE: This algorithm is an XOR approach for encrypting a stream in place // and is very quick, however, it is not considered "strong encryption" and // therefore should not be used for encrypting highly sensitive information. void crypt_stream( iostream& io, const char* p_key, size_t key_length ) { unsigned char key[ c_max_key_size ]; unsigned char buf[ c_file_buf_size ]; memset( key, '\0', c_max_key_size ); io.seekg( 0, ios::end ); size_t length = ( size_t )io.tellg( ); io.seekg( 0, ios::beg ); memcpy( key, p_key, key_length ); unsigned char datkey = 0; for( size_t i = 0; i < key_length; i++ ) datkey += key[ i ]; size_t buflen = c_file_buf_size; for( size_t pos = 0; pos < length; pos += buflen ) { if( length - pos < c_file_buf_size ) buflen = length - pos; int dir = 1; int key_offset = 0; io.seekg( pos, ios::beg ); io.read( ( char* )buf, buflen ); unsigned char ch; for( size_t offset = 0; offset < buflen; offset++ ) { ch = buf[ offset ]; ch ^= datkey; buf[ offset ] = ch; datkey += ( key[ key_offset ] % 131 ); key[ key_offset ] ^= datkey; key_offset += dir; if( datkey % 31 == 0 ) dir *= -1; if( key_offset >= ( int )key_length ) key_offset = 0; if( key_offset < 0 ) key_offset = ( int )key_length - 1; } io.seekg( pos, ios::beg ); io.write( ( char* )buf, buflen ); } memset( buf, '\0', c_file_buf_size ); memset( key, '\0', c_max_key_size ); }
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 (); }