void FileHandler::serve( Falcon::WOPI::Request* req ) { String sMimeType; // Get the document -- for now a very simple thing if ( ! m_client->options().findMimeType( m_sFile, sMimeType ) ) sMimeType = "unknown"; // Send the file Falcon::FileStat stats; if( ! Falcon::Sys::fal_stats( m_sFile, stats ) ) { m_client->replyError( 403 ); return; } FileStream fs; if( ! fs.open( m_sFile ) ) { m_client->log()->log( LOGLEVEL_WARN, "Can't open file "+ m_sFile ); m_client->replyError( 403 ); return; } m_client->log()->log( LOGLEVEL_INFO, "Sending file "+ m_sFile ); // ok we can serve the file String sReply = "HTTP/1.1 200 OK\r\n"; TimeStamp now; now.currentTime(); sReply += "Content-Type: " + sMimeType + "; charset=" + m_client->options().m_sTextEncoding + "\r\n"; sReply += "Date: " + now.toRFC2822() + "\r\n"; sReply += "Last-Modified: " + stats.m_mtime->toRFC2822() + "\r\n"; sReply += "\r\n"; // content length not strictly necessary now m_client->sendData( sReply ); char buffer[4096]; int len = fs.read( buffer, 4096 ); while( len > 0 ) { m_client->sendData( buffer, len ); len = fs.read( buffer, 4096 ); } if ( len < 0 ) { // error! m_client->log()->log( LOGLEVEL_WARN, "Error while reading file "+ m_sFile ); m_client->replyError( 403 ); } }
void FalhttpdClient::replyError( int errorID, const String& explain ) { String sErrorDesc = codeDesc( errorID ); String sReply; String sError; sReply.A("HTTP/1.1 ").N( errorID ).A( " " + sErrorDesc + "\r\n"); sError.N(errorID ).A( ": " + sErrorDesc ); // for now, we create the docuemnt here. // TODO Read the error document from a file. String sErrorDoc = "<html>\n" "<head>\r\n" "<title>Error " +sError + "</title>\n" "</head>\n" "<body>\n" "<h1>" + sError + "</h1>\n"; if( explain.size() != 0 ) { sErrorDoc += "<p>This abnormal condition has been encountered while receiving and processing Your request:</p>\n"; sErrorDoc += "<p><b>" + explain + "</b></p>\n"; } else { sErrorDoc += "<p>An error of type <b>" + sError + "</b> has been detected while parsing your request.</p>\n"; } sErrorDoc += getServerSignature(); sErrorDoc += "</body>\n</html>\n"; AutoCString content( sErrorDoc ); TimeStamp now; now.currentTime(); sReply += "Date: " + now.toRFC2822() + "\r\n"; sReply.A( "Content-Length: ").N( (int64) content.length() ).A("\r\n"); sReply += "Content-Type: text/html; charset=utf-8\r\n\r\n"; m_log->log( LOGLEVEL_INFO, "Sending ERROR reply to client " + m_sRemote + ": " + sError ); sendData( sReply ); sendData( content.c_str(), content.length() ); }