void LiveChatSessionManager::OnLiveChatManManagerTaskRun(TaskParam param) {
    RequestItem* item = (RequestItem *)param;
    if( item ) {
        RequestHandler(item);
        delete item;
    }
}
Пример #2
0
int CProxy::DoProxy(void)
{
	char line[MAX_LEN];
	int	iError, iLineLen;
	CString	relativefiletoRead;
	CString webroot;
	CString	filetoRead;
   int loop = 1;
   unsigned int nbytes = 255;
   char	*p1;
   char	content_type[25] = "";
   char	GET[25] = "GET";
   struct stat	statStruct;
   CString	strbuffer;
		char	msg[255] = "";
	char	searchstring[2046] = "";
	char	startswithstring[255] = "";
	char	errorcode[10] = "";
	int		searchflag = 0;
	int		startswithflag = 0;
	char	buffer[1024];

   int	totalSent = 0;
	CStdioFile file;
	FILE	*filep;
	char	*pszReferrer;
	char	*p2;
	char	tempURL2[1024] = "";
	char	tempURL3[256] = "";
	char	adminPass[255] = "";
	int		adminFlag = 0;
	int lengthtocopy = 0;
	char	*pp1 = 0;
	char	*pp2 = 0;


	memset(tempURL2, '\000', sizeof(tempURL2));
	memset(tempURL3, '\000', sizeof(tempURL3));
 
	memset(szRecvBuffer, 0, sizeof(szRecvBuffer));

	/* Read the first line of the request. */
	iError = recv(skAccept, szRecvBuffer, sizeof(szRecvBuffer)-1, 0);
	if (CheckSocketError(iError, "Couldn't read initial request")) {
		send_error( 400, "Bad Request", (char*) 0, "No request found." );
		goto ErrorExit;
	}


	memset(m_Referrer, '\000', sizeof(m_Referrer));

	strcpy(m_Referrer, "playlist.cgi");
	// Find end of first line
	pszReferrer = strstr(szRecvBuffer, "Referer: ");
	if (pszReferrer != 0) {
		p2 = strstr(pszReferrer, "&errorCode=");
		if (p2 == 0) {
			p2 = strstr(pszReferrer, "\r\n");
			if (p2 != 0) {
				memset(m_Referrer, '\000', sizeof(m_Referrer));
				strncpy(m_Referrer, pszReferrer + strlen("Referer: "), p2- (pszReferrer + strlen("Referer: ")));
			}
		}
		else {
			memset(m_Referrer, '\000', sizeof(m_Referrer));
			strncpy(m_Referrer, pszReferrer + strlen("Referer: "), p2- (pszReferrer + strlen("Referer: ")));
		}
	}
	m_pszEOL = strchr(szRecvBuffer, '\n');
	if (!m_pszEOL) {
		iLineLen = strlen(szRecvBuffer);

		if (iLineLen == 0) {
			socketErrorExit("Couldn't find end of line for first request");
			return 0;
		}

		m_pszEOL = szRecvBuffer + iLineLen;

	}

	m_pszEOL++;

	// Copy first line of recv() into line
	memset(line, 0, sizeof(line));

//	strncpy(line, szRecvBuffer, m_pszEOL - szRecvBuffer);
		strncpy(line, szRecvBuffer, sizeof(line)-1);

	// Calculate what's left in our buffer (excluding first line)
	m_iHeaderLen = iError - strlen(line);

    /* Parse it. */
    trim( line );

	if (strncmp(line, "GET ", 4)) {
		send_error( 400, "Only GET requests supported", (char*) 0, "Error" );
		goto ErrorExit;
	}
//    if ( sscanf( line, "%[^ ] %[^ ] %[^ ]", m_szMethod, m_szURL, m_szProtocol ) != 3 ) {

	pp1 = line + 4;
	pp2 = strchr(pp1, ' ');
	if (pp2 == 0) {
		send_error( 400, "Bad Request", (char*) 0, "Can't parse request." );
		goto ErrorExit;
	}

	memset(m_szURL, '\000', sizeof(m_szURL));
	if ((pp2-pp1) > sizeof(m_szURL) -1) {
		lengthtocopy = sizeof(m_szURL) - 1;
	}
	else {
		lengthtocopy = pp2-pp1;
	}
	strncpy(m_szURL, pp1, lengthtocopy);

	if (!strcmp(m_szURL, "/")) {
		strcat(m_szURL, "playlist.cgi");
	}
	strncpy(tempURL3, m_szURL, sizeof(tempURL3));
	if (!strncmp(m_szURL, "/?", 2)) {
		_snprintf(tempURL3, sizeof(tempURL3)-1, "/playlist.cgi%s", m_szURL+1);
		
	}
	if (!strncmp(m_szURL, "/&",2 )) {
		_snprintf(tempURL3, sizeof(tempURL3)-1, "/playlist.cgi%s", m_szURL+1);
	}
	strncpy(m_szURL, tempURL3, sizeof(m_szURL));

//	if (!strncmp(m_szURL, "/play.cgi", strlen("/play.cgi"))) {
//		PlayHandler(m_szURL);
//		goto NormalExit;
//	}
//	if (!strncmp(m_szURL, "/stop.cgi", strlen("/stop.cgi"))) {
//		StopHandler(m_szURL);
//		goto NormalExit;
//	}
	if (!strcmp(m_szURL, "/admin.cgi")) {
		sprintf(buffer, "HTTP/1.1 200 OK\nServer: WinampServer\nConnection: close\nContent-Type: text/html\r\n\r\n");
		iError = send(skAccept, buffer, strlen(buffer), 0);
		sprintf(buffer, "<html><form method=GET action=\"admin.cgi\">Enter Admin Password <input type=text name=pass><input type=Submit></form>");
		iError = send(skAccept, buffer, strlen(buffer), 0);
		goto NormalExit;
	}
	if (!strncmp(m_szURL, "/admin.cgi", strlen("/admin.cgi"))) {
		AdminHandler(m_szURL, &adminFlag);
	}
	if (!strncmp(m_szURL, "/request.cgi", strlen("/request.cgi"))) {
		RequestHandler(m_szURL);
		goto NormalExit;
	}
	
	p1 = strchr(m_szURL, '?');
	if (p1) {
		strncmp(tempURL2, m_szURL, p1-m_szURL);
	}
	else {
		p1 = strchr(m_szURL, '&');
		if (p1) {
			strncpy(tempURL2, m_szURL, p1-m_szURL);
		}
		else {
			strcpy(tempURL2, m_szURL);
		}
	}


	if ((!strncmp(m_szURL, "/playlist.cgi", strlen("/playlist.cgi"))) || (!strncmp(m_szURL, "/admin.cgi", strlen("/admin.cgi"))) || (!strcmp(tempURL2, "/"))) {
		memset(m_Current, '\000', sizeof(m_Current));
		PlaylistHandler(m_szURL, searchstring, startswithstring, m_Current, errorcode, &adminFlag);
		strcpy(content_type, "text/html");
		if (strlen(searchstring) == 0) {
			searchflag = 0;
		}
		else {
			searchflag = 1;
		}
		if (strlen(startswithstring) == 0) {
			startswithflag = 0;
		}
		else {
			startswithflag = 1;
		}
		relativefiletoRead = g_Playlistfile;
	}
	else {
		relativefiletoRead = m_szURL;
	}

	p1 = stristr(m_szURL, ".");
	if (p1) {
		p1++;
		if (strcmp(p1, "jpg") == 0) {
			strcpy(content_type, "image/jpeg");
		}
		if (strcmp(p1, "jpeg") == 0) {
			strcpy(content_type, "image/jpeg");
		}
		if (strcmp(p1, "gif") == 0) {
			strcpy(content_type, "image/gif");
		}
		if (strcmp(p1, "png") == 0) {
			strcpy(content_type, "image/png");
		}
		if (strcmp(p1, "css") == 0) {
			strcpy(content_type, "text/css");
		}
		if (strcmp(p1, "wav") == 0) {
			strcpy(content_type, "audio/wav");
		}
		if (strcmp(p1, "avi") == 0) {
			strcpy(content_type, "video/x-msvideo");
		}
		if (strcmp(p1, "mov") == 0) {
			strcpy(content_type, "video/quicktime");
		}
		if (strcmp(p1, "mpg") == 0) {
			strcpy(content_type, "video/mpeg");
		}
		if (strcmp(p1, "mpeg") == 0) {
			strcpy(content_type, "video/mpeg");
		}
		if (strcmp(p1, "vrml") == 0) {
			strcpy(content_type, "model/vrml");
		}
		if (strcmp(p1, "mp3") == 0) {
			strcpy(content_type, "audio/mpeg");
		}
		if (strcmp(p1, "pls") == 0) {
			strcpy(content_type, "audio/x-scpls");
		}
		if (strcmp(p1, "html") == 0) {
			strcpy(content_type, "text/html");
		}
	}

	if (relativefiletoRead.Left(2) == "/.") {
		sprintf(msg, "Invalid File %s - relative paths not supported", LPCSTR(relativefiletoRead));
		send_error( 400, msg, (char*) 0, "" );
		goto ErrorExit;
	}
	
	relativefiletoRead.Replace("/", "\\");
	webroot = g_WebRoot;
	if (g_WebRoot[strlen(g_WebRoot)] != '\\') {
		filetoRead = webroot + "\\" + relativefiletoRead;
	}
	else {
		filetoRead = webroot + relativefiletoRead;
	}

	filetoRead.Replace("\\\\", "\\");
	
	if (stat(filetoRead.GetBuffer(filetoRead.GetLength()), &statStruct) != 0) {
		sprintf(msg, "Invalid File %s", LPCSTR(relativefiletoRead));
		send_error( 400, msg, (char*) 0, "" );
		goto ErrorExit;
  	}

	
	
	if (!strcmp(content_type, "text/html")) {
		sprintf(buffer, "HTTP/1.1 200 OK\nServer: WinampServer\nConnection: close\nContent-Type: %s\r\n\r\n",content_type);
	}
	else {
		sprintf(buffer, "HTTP/1.1 200 OK\nServer: WinampServer\nContent-Length: %d\nConnection: close\nContent-Type: %s\r\n\r\n", statStruct.st_size,content_type);
	}
	iError = send(skAccept, buffer, strlen(buffer), 0);

	/* Read in input: */

	char	buf[8096];
	memset(buf, '\000', sizeof(buf));


	if (!strcmp(content_type, "text/html")) {
		filep = fopen(LPCSTR(filetoRead), "r");
		if (filep == NULL) {
			send_error( 400, "Invalid File", (char*) 0, "" );
			goto ErrorExit;
		}
		strbuffer = "";
		while (fgets(buf, sizeof(buf), filep) != NULL) {
			strbuffer = buf;
			if (!SubstituteTags(&strbuffer,skAccept, searchflag, searchstring, startswithflag, startswithstring, errorcode, adminFlag)) {
				iError = send(skAccept, strbuffer.GetBuffer(strbuffer.GetLength()), strbuffer.GetLength(), 0);
				iError = send(skAccept, "\r\n", strlen("\r\n"), 0);
				strbuffer = "";
			}
			memset(buf, '\000', sizeof(buf));
		}
		fclose(filep);
   }
   else {
	   FILE	*filep;
	   filep = fopen(LPCSTR(filetoRead), "rb");

		if (filep == 0) {
			send_error( 400, "Invalid File", (char*) 0, "" );
			goto ErrorExit;
		}
		int ret = 0;
		while (!feof(filep)) {
			memset(buf, '\000', sizeof(buf));
			ret = fread(buf, 1, sizeof(buf), filep);
			iError = send(skAccept, buf, ret, 0);
			totalSent = totalSent + iError;
		}
		fclose(filep);

   }
	closesocket(skClient);
	return 1;

NormalExit:
	closesocket(skClient);
	return 1;

ErrorExit:
	closesocket(skClient);
	return 0;
}