📄 webserver.cpp
字号:
ec_cmd = new CECPacket(EC_OP_SERVER_REMOVE); } else if ( cmd == wxT("disconnect") ) { ec_cmd = new CECPacket(EC_OP_SERVER_DISCONNECT); } if ( ec_cmd && ip ) { ec_cmd->AddTag(CECTag(EC_TAG_SERVER, EC_IPv4_t(ip, port))); Send_Discard_V2_Request(ec_cmd); delete ec_cmd; }}void CWebServerBase::Send_Search_Cmd(wxString search, wxString extention, wxString type, EC_SEARCH_TYPE search_type, uint32 avail, uint32 min_size, uint32 max_size){ CECPacket search_req(EC_OP_SEARCH_START); search_req.AddTag(CEC_Search_Tag (search, search_type, type, extention, avail, min_size, max_size)); Send_Discard_V2_Request(&search_req);}bool CWebServerBase::Send_DownloadEd2k_Cmd(wxString link, uint8 cat){ CECPacket req(EC_OP_ADD_LINK); CECTag link_tag(EC_TAG_STRING, link); link_tag.AddTag(CECTag(EC_TAG_PARTFILE_CAT, cat)); req.AddTag(link_tag); const CECPacket *response = webInterface->SendRecvMsg_v2(&req); bool result = (response->GetOpCode() == EC_OP_FAILED); delete response; return result;}// We have to add gz-header and some other stuff// to standard zlib functions in order to use gzip in web pagesint CWebServerBase::GzipCompress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen, int level){ static const int gz_magic[2] = {0x1f, 0x8b}; // gzip magic header z_stream stream = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; stream.zalloc = (alloc_func)0; stream.zfree = (free_func)0; stream.opaque = (voidpf)0; uLong crc = crc32(0L, Z_NULL, 0); // init Zlib stream // NOTE windowBits is passed < 0 to suppress zlib header int err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY); if (err != Z_OK) { return err; } snprintf((char*)dest, *destLen, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1], Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, 255); // wire buffers stream.next_in = const_cast<Bytef*>(source); stream.avail_in = (uInt)sourceLen; stream.next_out = ((Bytef*) dest) + 10; stream.avail_out = *destLen - 18; // doit err = deflate(&stream, Z_FINISH); if (err != Z_STREAM_END) { deflateEnd(&stream); return err; } err = deflateEnd(&stream); crc = crc32(crc, (const Bytef *) source , sourceLen ); //CRC *(((Bytef*) dest)+10+stream.total_out) = (Bytef)(crc & 0xFF); *(((Bytef*) dest)+10+stream.total_out+1) = (Bytef)((crc>>8) & 0xFF); *(((Bytef*) dest)+10+stream.total_out+2) = (Bytef)((crc>>16) & 0xFF); *(((Bytef*) dest)+10+stream.total_out+3) = (Bytef)((crc>>24) & 0xFF); // Length *(((Bytef*) dest)+10+stream.total_out+4) = (Bytef)( sourceLen & 0xFF); *(((Bytef*) dest)+10+stream.total_out+5) = (Bytef)(( sourceLen >>8) & 0xFF); *(((Bytef*) dest)+10+stream.total_out+6) = (Bytef)(( sourceLen >>16) & 0xFF); *(((Bytef*) dest)+10+stream.total_out+7) = (Bytef)(( sourceLen >>24) & 0xFF); // return destLength *destLen = 10 + stream.total_out + 8; return err;}/* * Item container implementation */ServersInfo *ServerEntry::GetContainerInstance(){ return ServersInfo::m_This;}ServersInfo *ServersInfo::m_This = 0;ServersInfo::ServersInfo(CamulewebApp *webApp) : ItemsContainer<ServerEntry>(webApp){ m_This = this; }bool ServersInfo::ServersInfo::ReQuery(){ CECPacket srv_req(EC_OP_GET_SERVER_LIST); const CECPacket *srv_reply = m_webApp->SendRecvMsg_v2(&srv_req); if (!srv_reply) { return false; } // // query succeded - flush existing values and refill EraseAll(); for (int i = 0; i < srv_reply->GetTagCount(); ++i) { const CECTag *tag = srv_reply->GetTagByIndex(i); ServerEntry Entry; Entry.sServerName = _SpecialChars(tag->GetTagByNameSafe(EC_TAG_SERVER_NAME)->GetStringData()); Entry.sServerDescription = _SpecialChars(tag->GetTagByNameSafe(EC_TAG_SERVER_DESC)->GetStringData()); Entry.sServerIP = tag->GetIPv4Data().StringIP(false); Entry.nServerIP = tag->GetIPv4Data().IP(); Entry.nServerPort = tag->GetIPv4Data().m_port; Entry.nServerUsers = tag->GetTagByNameSafe(EC_TAG_SERVER_USERS)->GetInt(); Entry.nServerMaxUsers = tag->GetTagByNameSafe(EC_TAG_SERVER_USERS_MAX)->GetInt(); Entry.nServerFiles = tag->GetTagByNameSafe(EC_TAG_SERVER_FILES)->GetInt(); AddItem(Entry); } delete srv_reply; return true;}SharedFile::SharedFile(CEC_SharedFile_Tag *tag){ sFileName = _SpecialChars(tag->FileName()); lFileSize = tag->SizeFull(); sED2kLink = _SpecialChars(tag->FileEd2kLink()); nHash = tag->ID(); ProcessUpdate(tag);}void SharedFile::ProcessUpdate(CEC_SharedFile_Tag *tag){ nFileTransferred = tag->GetXferred(); nFileAllTimeTransferred = tag->GetAllXferred(); nFileRequests = tag->GetRequests(); nFileAllTimeRequests = tag->GetAllRequests(); nFileAccepts = tag->GetAccepts(); nFileAllTimeAccepts = tag->GetAllAccepts(); sFileHash = nHash.Encode(); nFilePriority = tag->Prio(); if ( nFilePriority >= 10 ) { bFileAutoPriority = true; nFilePriority -= 10; } else { bFileAutoPriority = false; }}SharedFileInfo *SharedFile::GetContainerInstance(){ return SharedFileInfo::m_This;}SharedFileInfo *SharedFileInfo::m_This = 0;SharedFileInfo::SharedFileInfo(CamulewebApp *webApp) : UpdatableItemsContainer<SharedFile, CEC_SharedFile_Tag, CMD4Hash>(webApp){ m_This = this;}bool SharedFileInfo::ReQuery(){ DoRequery(EC_OP_GET_SHARED_FILES, EC_TAG_KNOWNFILE); return true;}DownloadFile::DownloadFile(CEC_PartFile_Tag *tag){ nHash = tag->ID(); sFileName = _SpecialChars(tag->FileName()); lFileSize = tag->SizeFull(); sFileHash = nHash.Encode(); sED2kLink = _SpecialChars(tag->FileEd2kLink()); lFileCompleted = tag->SizeDone(); lFileTransferred = tag->SizeXfer(); lFileSpeed = tag->Speed(); fCompleted = (100.0*lFileCompleted) / lFileSize; wxtLastSeenComplete = wxDateTime( tag->LastSeenComplete() ); m_Encoder = PartFileEncoderData( (lFileSize + (PARTSIZE - 1)) / PARTSIZE, 10); ProcessUpdate(tag); }void DownloadFile::ProcessUpdate(CEC_PartFile_Tag *tag){ if (!tag) { return; } lFilePrio = tag->Prio(); if ( lFilePrio >= 10 ) { lFilePrio -= 10; bFileAutoPriority = true; } else { bFileAutoPriority = false; } nCat = tag->FileCat(); nFileStatus = tag->FileStatus(); lSourceCount = tag->SourceCount(); lNotCurrentSourceCount = tag->SourceNotCurrCount(); lTransferringSourceCount = tag->SourceXferCount(); lSourceCountA4AF = tag->SourceCountA4AF(); if ( lTransferringSourceCount > 0 ) { lFileCompleted = tag->SizeDone(); lFileTransferred = tag->SizeXfer(); lFileSpeed = tag->Speed(); fCompleted = (100.0*lFileCompleted) / lFileSize; } else { lFileSpeed = 0; } CECTag *gaptag = tag->GetTagByName(EC_TAG_PARTFILE_GAP_STATUS); CECTag *parttag = tag->GetTagByName(EC_TAG_PARTFILE_PART_STATUS); CECTag *reqtag = tag->GetTagByName(EC_TAG_PARTFILE_REQ_STATUS); if (gaptag && parttag && reqtag) { m_Encoder.Decode( (unsigned char *)gaptag->GetTagData(), gaptag->GetTagDataLen(), (unsigned char *)parttag->GetTagData(), parttag->GetTagDataLen()); const Gap_Struct *reqparts = (const Gap_Struct *)reqtag->GetTagData(); int reqcount = reqtag->GetTagDataLen() / sizeof(Gap_Struct); m_ReqParts.resize(reqcount); for (int i = 0; i < reqcount;i++) { m_ReqParts[i].start = ENDIAN_NTOHLL(reqparts[i].start); m_ReqParts[i].end = ENDIAN_NTOHLL(reqparts[i].end); } }}DownloadFileInfo *DownloadFile::GetContainerInstance(){ return DownloadFileInfo::m_This;}DownloadFileInfo *DownloadFileInfo::m_This = 0;DownloadFileInfo::DownloadFileInfo(CamulewebApp *webApp, CImageLib *imlib) : UpdatableItemsContainer<DownloadFile, CEC_PartFile_Tag, CMD4Hash>(webApp){ m_This = this; m_ImageLib = imlib;}void DownloadFileInfo::LoadImageParams(wxString &tpl, int width, int height){ m_Template = tpl; m_width = width; m_height = height;}void DownloadFileInfo::ItemInserted(DownloadFile *item){ item->m_Image = new CDynProgressImage(m_width, m_height, m_Template, item);#ifdef WITH_LIBPNG m_ImageLib->AddImage(item->m_Image, wxT("/") + item->m_Image->Name());#endif}void DownloadFileInfo::ItemDeleted(DownloadFile *item){#ifdef WITH_LIBPNG m_ImageLib->RemoveImage(wxT("/") + item->m_Image->Name());#endif delete item->m_Image;}bool DownloadFileInfo::ReQuery(){ DoRequery(EC_OP_GET_DLOAD_QUEUE, EC_TAG_PARTFILE); return true;}UploadFile::UploadFile(CEC_UpDownClient_Tag *tag){ nHash = tag->FileID(); sUserName = _SpecialChars(tag->ClientName()); nSpeed = tag->SpeedUp(); nTransferredUp = tag->XferUp(); nTransferredDown = tag->XferDown();}UploadsInfo *UploadFile::GetContainerInstance(){ return UploadsInfo::m_This;}UploadsInfo *UploadsInfo::m_This = 0;UploadsInfo::UploadsInfo(CamulewebApp *webApp) : ItemsContainer<UploadFile>(webApp){ m_This = this;}bool UploadsInfo::ReQuery(){ CECPacket up_req(EC_OP_GET_ULOAD_QUEUE); const CECPacket *up_reply = m_webApp->SendRecvMsg_v2(&up_req); if (!up_reply) { return false; } // // query succeded - flush existing values and refill EraseAll(); for(int i = 0; i < up_reply->GetTagCount(); i ++) { UploadFile curr((CEC_UpDownClient_Tag *)up_reply->GetTagByIndex(i)); AddItem(curr); } delete up_reply; return true;}SearchFile::SearchFile(CEC_SearchFile_Tag *tag){ nHash = tag->FileHash(); sHash = nHash.Encode(); sFileName = _SpecialChars(tag->FileName()); lFileSize = tag->SizeFull(); lSourceCount = tag->SourceCount(); bPresent = tag->AlreadyHave();}void SearchFile::ProcessUpdate(CEC_SearchFile_Tag *tag){ lSourceCount = tag->SourceCount();}SearchInfo *SearchFile::GetContainerInstance(){ return SearchInfo::m_This;}SearchInfo *SearchInfo::m_This = 0;SearchInfo::SearchInfo(CamulewebApp *webApp) : UpdatableItemsContainer<SearchFile, CEC_SearchFile_Tag, CMD4Hash>(webApp){ m_This = this;}bool SearchInfo::ReQuery(){ DoRequery(EC_OP_SEARCH_RESULTS, EC_TAG_SEARCHFILE); return true;}/*! * Image classes: * * CFileImage: simply represent local file * CDynProgressImage: dynamically generated from gap info */ CAnyImage::CAnyImage(int size){ m_size = 0; m_alloc_size = size; if ( m_alloc_size ) { m_data = new unsigned char[m_alloc_size]; } else { m_data = 0; }}CAnyImage::CAnyImage(int width, int height) : m_width(width), m_height(height){ m_size = 0; // allocate considering image header m_alloc_size = width * height * sizeof(uint32) + 0x100; if ( m_alloc_size ) { m_data = new unsigned char[m_alloc_size]; } else { m_data = 0; }}CAnyImage::~CAnyImage(){ if ( m_data ) { delete [] m_data; }}void CAnyImage::Realloc(int size){ if ( size == m_alloc_size ) { return; } // always grow, but shrink only x2 if ( (size > m_alloc_size) || (size < (m_alloc_size / 2)) ) { m_alloc_size = size; if ( m_data ) { delete [] m_data; } m_data = new unsigned char[m_alloc_size]; }}unsigned char *CAnyImage::RequestData(int &size){ size = m_size; return m_data;}void CAnyImage::SetHttpType(wxString ext){ m_Http = wxT("Content-Type: ") + ext + wxT("\r\n"); time_t t = time(NULL); char tmp[255]; strftime(tmp, 255, "%a, %d %b %Y %H:%M:%S GMT", gmtime(&t)); m_Http += wxT("Last-Modified: ") + wxString(char2unicode(tmp)) + wxT("\r\n"); m_Http += wxT("ETag: ") + MD5Sum(char2unicode(tmp)).GetHash() + wxT("\r\n");}CFileImage::CFileImage(const wxString& name) : CAnyImage(0){ m_size = 0; m_name = name; wxFFile fis(m_name); // FIXME: proper logging is needed if ( fis.IsOpened() ) { size_t file_size = fis.Length(); if ( file_size ) { Realloc(fis.Length()); m_size = fis.Read(m_data,file_size); } else { printf("CFileImage: file %s have zero length\n", (const char *)unicode2char(m_name)); } wxString ext = m_name.Right(3).MakeLower(); if ( ext == wxT("css") ) { SetHttpType(wxT("text/css")); } else { SetHttpType(wxT("image/") + ext); } } else { printf("CFileImage: failed to open %s\n", (const char *)unicode2char(m_name)); }}/*! * "Modifiers" for 3D look of progress bar. Those modifiers must be substracted from * image (with saturation), values, and not multiplied, as amule doesn for some reason. * */CImage3D_Modifiers::CImage3D_Modifiers(int width){ m_width = width; m_modifiers = new unsigned char[m_width]; for(int i = 0; i < m_width; i++) { // "70" - webserver uses fixed depth double f_curr_mod = 30 * (1 + cos( (2 * M_PI) * ( m_width - (((double)i)/m_width) ) ) ); m_modifiers[i] = (unsigned char)f_curr_mod; }}CImage3D_Modifiers::~CImage3D_Modifiers(){ delete [] m_modifiers;}CProgressImage::CProgressImage(int width, int height, wxString &tmpl, DownloadFile *file) : CAnyImage(width, height), m_template(tmpl){ m_file = file; m_gap_buf_size = m_gap_alloc_size = m_file->m_Encoder.m_gap_status.Size() / (2 * sizeof(uint64)); m_gap_buf = new Gap_Struct[m_gap_alloc_size]; m_ColorLine = new uint32[m_width];}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -