⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webserver.cpp

📁 电驴的MAC源代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	} else if ( m_scale_up != 1 ) {		y_axis_max /= m_scale_up;	}	// X---	if ( y_axis_max > 999 ) {		m_digits[y_axis_max / 1000]->Apply(m_row_ptrs, img_delta, img_delta);	}	// -X--	if ( y_axis_max > 99 ) {		m_digits[(y_axis_max % 1000) / 100]->Apply(m_row_ptrs,			2*img_delta+m_num_font_w_size, img_delta);	}	// --X-	if ( y_axis_max > 9 ) {		m_digits[(y_axis_max % 100) / 10]->Apply(m_row_ptrs, 			3*img_delta+2*m_num_font_w_size, img_delta);	}	// ---X	m_digits[y_axis_max % 10]->Apply(m_row_ptrs, 4*img_delta+3*m_num_font_w_size, img_delta);	int prev_data = m_data->GetFirst();	if ( m_scale_down != 1 ) {		prev_data /= m_scale_down;	} else if ( m_scale_up != 1 ) {		prev_data *= m_scale_up;	}	if ( m_scale1024 ) {		if ( prev_data > 1024) {			prev_data /= 1024;		} else {			prev_data = 1;		}	}	for(int j = m_left_margin + 1, curr_data = m_data->GetNext(); j < m_width; j++, curr_data = m_data->GetNext()) {		if ( m_scale_down != 1 ) {			curr_data /= m_scale_down;		} else if ( m_scale_up != 1 ) {			curr_data *= m_scale_up;		}		if ( m_scale1024 ) {			if ( curr_data > 1024) {				curr_data /= 1024;			} else {				curr_data = 1;			}		}		//		// draw between curr_data and prev_data		//		int min_y, max_y;		if ( prev_data > curr_data ) {			min_y = curr_data; max_y = prev_data;		} else {			min_y = prev_data; max_y = curr_data;		}		for(int k = min_y; k <= max_y; k++) {			int i = m_y_axis_size - k;			png_bytep u_row = m_row_ptrs[i];			set_rgb_color_val(u_row+3*j, graph_color, 0);		}		prev_data = curr_data;	}}unsigned char *CDynStatisticImage::RequestData(int &size){	DrawImage();		return CDynPngImage::RequestData(size);}wxString CDynStatisticImage::GetHTML(){	return wxEmptyString;}//// Imprint numbers on generated png's////                                                 0     1     2     3     4     5     6     7     8     9const int CNumImageMask::m_num_to_7_decode[] = {0x77, 0x24, 0x5d, 0x6d, 0x2e, 0x5d, 0x7a, 0x25, 0x7f, 0x2f};CNumImageMask::CNumImageMask(int number, int width, int height){	m_v_segsize = height / 2;	m_h_segsize = width;	m_height = height;	m_width = width;		m_row_mask_ptrs = new png_bytep[m_height];	for(int i = 0; i < m_height; i++) {		m_row_mask_ptrs[i] = new png_byte[3*m_width];		memset(m_row_mask_ptrs[i], 0x00, 3*m_width);	}		int seg_status = m_num_to_7_decode[number];	for(int i = 0; i < 7; i++) {		if ( seg_status & (1 << i) ) {			DrawSegment(i);		}	}}CNumImageMask::~CNumImageMask(){	for(int i = 0; i < m_height; i++) {		delete [] m_row_mask_ptrs[i];	}	delete [] m_row_mask_ptrs;}void CNumImageMask::Apply(png_bytep *image, int offx, int offy){	offx *= 3;	for(int i = 0; i < m_height; i++) {		png_bytep img_row = image[offy + i];		png_bytep num_row = m_row_mask_ptrs[i];		for(int j = 0; j < m_width*3; j++) {			img_row[offx + j] |= num_row[j];		}	}}void CNumImageMask::DrawHorzLine(int off){	png_bytep m_row = m_row_mask_ptrs[off*(m_v_segsize-1)];	for(int i = 0; i < m_h_segsize; i++) {		m_row[i*3] = m_row[i*3+1] = m_row[i*3+2] = 0xff;	}}void CNumImageMask::DrawVertLine(int offx, int offy){	for(int i = 0; i < m_v_segsize; i++) {		png_bytep m_row = m_row_mask_ptrs[offy*(m_v_segsize-1)+i];		int x_idx = offx*(m_h_segsize-1)*3;		m_row[x_idx] = m_row[x_idx+1] = m_row[x_idx+2] = 0xff;	}}/* * Segment id decoding defined as following *  *   ---- 0 ---- *   |         | *   1         2 *   |___ 3 ___| *   |         | *   4         5 *   |___ 6 ___| */void CNumImageMask::DrawSegment(int id){	switch(id) {		case 0:			DrawHorzLine(0);			break;		case 1:			DrawVertLine(0, 0);			break;		case 2:			DrawVertLine(1, 0);			break;		case 3:			DrawHorzLine(1);			break;		case 4:			DrawVertLine(0, 1);			break;		case 5:			DrawVertLine(1, 1);			break;		case 6:			DrawHorzLine(2);			break;		default:			wxASSERT(0);			break;	}}#endifCImageLib::CImageLib(wxString image_dir) : m_image_dir(image_dir){}CImageLib::~CImageLib(){}void CImageLib::AddImage(CAnyImage *img, const wxString &name){	CAnyImage *prev = m_image_map[name];	if ( prev ) {		delete prev;	}	m_image_map[name] = img;}void CImageLib::RemoveImage(const wxString &name){	CAnyImage *prev = m_image_map[name];	if ( prev ) {		m_image_map.erase(name);	}}CAnyImage *CImageLib::GetImage(wxString &name){	CAnyImage *img = m_image_map[name];	if ( img ) {		return img;	}	wxFileName filename(m_image_dir + name);	CFileImage *fimg = new CFileImage(filename.GetFullPath());	if ( fimg->OpenOk() ) {		m_image_map[name] = fimg;		return fimg;	} else {		delete fimg;		return 0;	}}/*  * Script-based webserver */CScriptWebServer::CScriptWebServer(CamulewebApp *webApp, const wxString& templateDir)	: CWebServerBase(webApp, templateDir), m_wwwroot(templateDir){	wxString img_tmpl(wxT("<img src=%s height=20 width=%d>"));	m_DownloadFileInfo.LoadImageParams(img_tmpl, 200, 20);	if ( ::wxFileExists(wxFileName(m_wwwroot, wxT("index.html")).GetFullPath()) ) {		m_index = wxT("index.html");	} else if ( ::wxFileExists(wxFileName(m_wwwroot, wxT("index.php")).GetFullPath()) ) {		m_index = wxT("index.php");	} else {		webInterface->Show(_("Index file not found: ") + 			wxFileName(m_wwwroot, wxT("index.{html,php}")).GetFullPath() + wxT("\n"));	}}CScriptWebServer::~CScriptWebServer(){}char *CScriptWebServer::GetErrorPage(const char *message, long &size){	char *buf = new char [1024];	snprintf(buf, 1024,		"<html><title> Error -%s </title></html>", message);	size = strlen(buf);		return buf;}char *CScriptWebServer::Get_404_Page(long &size){		char *buf = new char [1024];		strcpy(buf, "<html><title> Error - requested page not found </title></html>");				size = strlen(buf);				return buf;}char *CScriptWebServer::ProcessHtmlRequest(const char *filename, long &size){	FILE *f = fopen(filename, "r");	if ( !f ) {		return Get_404_Page(size);	}	if ( fseek(f, 0, SEEK_END) != 0 ) {		return GetErrorPage("fseek failed", size);	}	size = ftell(f);	char *buf = new char [size+1];	rewind(f);	fread(buf, 1, size, f);	buf[size] = 0;	fclose(f);		return buf;}char *CScriptWebServer::ProcessPhpRequest(const char *filename, CSession *sess, long &size){	FILE *f = fopen(filename, "r");	if ( !f ) {		return Get_404_Page(size);	}	CWriteStrBuffer buffer;	CPhpFilter(this, sess, filename, &buffer);		size = buffer.Length();	char *buf = new char [size+1];	buffer.CopyAll(buf);		fclose(f);		return buf;}CSession *CScriptWebServer::CheckLoggedin(ThreadData &Data){	time_t curr_time = time(0);	CSession *session = 0;	if ( Data.SessionID && m_sessions.count(Data.SessionID) ) {		session = &m_sessions[Data.SessionID];		// session times out in 2 hours		if ( (curr_time - session->m_last_access) > 7200 ) {			Print(_("Session expired - requesting login\n"));			m_sessions.erase(Data.SessionID);			session = 0;		} else {			if ( session->m_loggedin ) {				Print(_("Session ok, logged in\n"));			} else {				Print(_("Session ok, not logged in\n"));			}			session->m_last_access = curr_time;		}	} else {		Print(_("No session opened - will request login\n"));	}	if ( !session ) {		while ( !Data.SessionID || m_sessions.count(Data.SessionID) ) {			Data.SessionID = rand();		}		session = &m_sessions[Data.SessionID];		session->m_last_access = curr_time;		session->m_loggedin = false;		Print(_("Session created - requesting login\n"));	}	Data.parsedURL.ConvertParams(session->m_get_vars);	return session;}void CScriptWebServer::ProcessURL(ThreadData Data){	wxMutexLocker lock(m_mutexChildren);	long httpOutLen;	char *httpOut = 0;		// Strip out any path-component to prevent information leakage.	wxString filename = wxFileName(Data.parsedURL.File()).GetFullName();	Print(_("Processing request [original]: ") + filename + wxT("\n"));	if ( filename.Length() == 0 ) {		filename = m_index;	}	CSession *session = CheckLoggedin(Data);	session->m_vars["login_error"] = "";	if ( !session->m_loggedin ) {		filename = wxT("login.php");				wxString PwStr(Data.parsedURL.Param(wxT("pass")));		if (webInterface->m_AdminPass.IsEmpty() and webInterface->m_GuestPass.IsEmpty()) {			session->m_vars["login_error"] = "No password specified, login will not be allowed.";		} else if ( PwStr.Length() ) {			Print(_("Checking password\n"));			session->m_loggedin = false;						CMD4Hash PwHash;			if (not PwHash.Decode(MD5Sum(PwStr).GetHash())) {				Print(_("Password hash invalid\n"));				session->m_vars["login_error"] = "Invalid password hash, please report on http://forum.amule.org";			} else if ( PwHash == webInterface->m_AdminPass ) {				session->m_loggedin = true;				// m_vars is map<string, string> - so _() will not work here !				session->m_vars["guest_login"] = "0";			} else if ( PwHash == webInterface->m_GuestPass ) {				session->m_loggedin = true;				session->m_vars["guest_login"] = "1";			} else {				session->m_vars["login_error"] = "Password incorrect, please try again.";			}						if ( session->m_loggedin ) {				filename = m_index;				Print(_("Password ok\n"));			} else {				Print(_("Password bad\n"));			}		} else {			Print(_("You did not enter any password. Blank password is not allowed.\n"));		}	} else {		//		// if logged in, but requesting login page again,		// means logout command		//		if ( filename == wxT("login.php") ) {			Print(_("Logout requested\n"));			session->m_loggedin = false;		}	}	Print(_("Processing request [redirected]: ") + filename + wxT("\n"));		session->m_vars["auto_refresh"] = (const char *)unicode2char(		wxString::Format(wxT("%d"), webInterface->m_PageRefresh));	session->m_vars["content_type"] = "text/html";		wxString req_file(wxFileName(m_wwwroot, filename).GetFullPath());	if ( req_file.Find(wxT(".html")) != -1 ) {		httpOut = ProcessHtmlRequest(unicode2char(req_file), httpOutLen);	} else if ( req_file.Find(wxT(".php")) != -1 ) {		httpOut = ProcessPhpRequest(unicode2char(req_file), session, httpOutLen);	} else if ( req_file.Find(wxT(".css")) != -1 ) {		session->m_vars["content_type"] = "text/css";		httpOut = ProcessHtmlRequest(unicode2char(req_file), httpOutLen);	} else if ( req_file.Find(wxT(".js")) != -1 ) {		session->m_vars["content_type"] = "text/javascript";		httpOut = ProcessHtmlRequest(unicode2char(req_file), httpOutLen);	} else {		httpOut = GetErrorPage("aMuleweb doesn't handle the requested file type ", httpOutLen);	}		bool isUseGzip = webInterface->m_UseGzip;	if (isUseGzip)	{		bool bOk = false;		uLongf destLen = httpOutLen + 1024;		char *gzipOut = new char[destLen];		if( GzipCompress((Bytef*)gzipOut, &destLen, 		   (const Bytef*)httpOut, httpOutLen, Z_DEFAULT_COMPRESSION) == Z_OK) {			bOk = true;		}		if ( bOk ) {			delete [] httpOut;			httpOut = gzipOut;			httpOutLen = destLen;		} else {			isUseGzip = false;			delete[] gzipOut;		}	}			if ( httpOut ) {		Data.pSocket->SendHttpHeaders(session->m_vars["content_type"].c_str(), isUseGzip, httpOutLen, Data.SessionID);		Data.pSocket->SendData(httpOut, httpOutLen);		delete [] httpOut;	}}CNoTemplateWebServer::CNoTemplateWebServer(CamulewebApp *webApp) : CScriptWebServer(webApp, wxEmptyString){}CNoTemplateWebServer::~CNoTemplateWebServer(){}void CNoTemplateWebServer::ProcessURL(ThreadData Data){	/*	 * Since template has not been found, I suspect that installation is broken. Falling back	 * into hardcoded page as last resort.	 */	const char *httpOut = ""	"<html>"		"<head>"			"<title>aMuleWeb error page</title>"			"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"		"</head>"		"<body>"			"<p>You are seeing this page instead of aMuleWeb login page because a valid template has not been found.</p>"			"<p>This probably means that there's a problem with your aMule installation </p>"			"<ul>"				"<li>Before installing new versions, please ensure that you uninstalled older versions of aMule.</li>"				"<li>If you are installing by compiling from source, check configuration and run &quot;make&quot; and &quot;make install&quot; again </li>"				"<li>If you are installing by using a precompiled package, you may need to contact the package maintainer </li>"			"</ul>"			"<p>For more information please visit</p>"			"<p><a href=\"http://www.amule.org\">aMule main site</a> or <a href=\"http://forum.amule.org\">aMule forums</a></p>"		"</body>"	"</html>";	long httpOutLen = strlen(httpOut);	Data.pSocket->SendHttpHeaders("text/html", false, httpOutLen, 0);	Data.pSocket->SendData(httpOut, httpOutLen);}// File_checked_for_headers

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -