Ejemplo n.º 1
0
void HTTPServer::onIncomingConnection ( SOCKET sock )
{

	//read data from socket
	char buffer[1024];
	int n;
	bzero ( buffer, sizeof ( buffer ) );

	n = read ( sock,buffer,1024 );
	if ( n < 0 ) {
		mLog ( "ERROR reading from socket", LOG_PERROR );
		exit ( 1 );
	}
	//mLog((string)"Read from buffer: " + buffer);

	MyString data ( buffer );
	//data now contains incoming data.

	string firstLine;
	firstLine = data.substr ( 0, data.find ( "\r\n" ) );

	//we should check if header is correct
	const string prefix ( "GET" );
	const string postfix ( "HTTP/1." );
#ifdef FULLDEBUG
	mLog ( ( "Data received: " + data ).c_str() );
#endif

	if ( firstLine.find ( prefix ) != 0 // doesn't start with prefix
	     || firstLine.find ( postfix ) != firstLine.length()-1 - postfix.length() // doesn't end with postfix
	     || firstLine.length() < 14 ) { // length is small
		// header is incorrect
		mLog ( "Bad request: " + firstLine );
		exit ( 1 );
	} else {
		// header is correct
		MyString req = firstLine.substr ( 4, firstLine.find ( postfix )-4 );
		req.trim();
#if defined(FULLDEBUG)
		mLog ( "request is:" + req );
		mLog ( "first line is:" + firstLine );
#endif

		onUrlRequested ( req, sock );
	}

	close ( sock );
	lock();
	openConnCount--;
	unlock();


}
Ejemplo n.º 2
0
void HTTPServer::errorReport ( int sock, string code, string title, string mesg )
{
	string temp;
	temp = ( "HTTP/1.0 " + code + " " + title + "\r\n" +
	         "\r\n" +
	         "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n" +
	         "<TITLE>" + code + " " + title + "</TITLE>\r\n" +
	         "</HEAD><BODY>\r\n" +
	         "<H1>" + title + "</H1>\r\n" + mesg + "<P>\r\n" +
	         "<HR><ADDRESS>FileServer 1.0 at " +
	         "127.0.0.1" +
	         " Port " + "8080" + "</ADDRESS>\r\n" +
	         "</BODY></HTML>\r\n" );
	mLog ( "Sending: " + temp );
	write ( sock, temp.c_str(), temp.length() );
	mLog ( code + " " + title );
}
Ejemplo n.º 3
0
Archivo: worker.c Proyecto: jxva/miaoox
static void AcceptClient() {
    char err[255];
    worker->connfd = netAccept(err, listenfd, &worker->cli_addr, &worker->cli_addr_len);
    if (worker->connfd < 0) {
        mLog(MSG_WARNING, "Accept client failed.", err);
        return;
    }
    evCreateIOEvent(worker->poll, worker->connfd, EV_READABLE, Protocol_Dispatch, NULL);
}
Ejemplo n.º 4
0
Archivo: worker.c Proyecto: jxva/miaoox
int InitWorker(Worker* w) {
    worker = w;
    worker->stop = 0;
    worker->max_event_num = MAX_EVENT_NUM;

    if (worker == NULL){
        mLog(MSG_WARNING, "Memmory allocate failed when creating Worker.");
        return -1;
    }
    if ((worker->poll = evCreateEventLoop(worker->max_event_num)) == NULL) {
        mLog(MSG_WARNING, "Memmory allocate failed when creating Event-loop.");
        return -1;
    }
    AcceptClient();
    InitWorkerSignals();

    while (!worker->stop) {
        evProcessEvents(worker->poll, EV_IO_EVENTS);
    }

    return 0;
}
Ejemplo n.º 5
0
/// Helper for converting floating point linear volume
/// to a logrithmic integer volume for dsound.
LONG SFXDSVoice::_linearToLogVolume( F32 linVolume )
{
   LONG logVolume;

   if ( linVolume <= 0.0f )
      logVolume = DSBVOLUME_MIN;
   else
   {
      logVolume = -2000.0 * mLog( 1.0f / linVolume );
      logVolume = mClamp( logVolume, DSBVOLUME_MIN, DSBVOLUME_MAX );
   }

   return logVolume;
}
Ejemplo n.º 6
0
int HTTPServer::getDirFiles ( const string& path, string* files )
{
	DIR *dp;
	struct dirent *dirp;
	vector<string> filesVect;
	vector<string> dirVect;

	if ( ( dp  = opendir ( path.c_str() ) ) == NULL ) {
		mLog ( "Error opening dir", LOG_PERROR );
		exit ( 1 );
	}

	while ( ( dirp = readdir ( dp ) ) != NULL ) {
		if ( dirp->d_type == DT_DIR )
			// is a directory
			dirVect.push_back ( string ( dirp->d_name ) );
		else
			//is file or link
			filesVect.push_back ( string ( dirp->d_name ) );
	}

	closedir ( dp );

	sort ( dirVect.begin(), dirVect.end() );
	sort ( filesVect.begin(), filesVect.end() );


	for ( int i=0; i<dirVect.size(); i++ ) { // could write for(string s : filesVect) , but this is faster
		*files += "<a href = \"" + path + dirVect[i] + "\">" + dirVect[i] + "</a>";
		*files += "<br>";
	}
	for ( int i=0; i<filesVect.size(); i++ ) { // could write for(string s : filesVect) , but this is faster
		*files += "<a href = \"" + path + filesVect[i] + "\">" + filesVect[i] + "</a>";
		*files += "<br>";
	}


	return filesVect.size() + dirVect.size();
}
Ejemplo n.º 7
0
		/// \brief Returns the database console log stream.
		ostream& getConsoleLog(void) { return mLog(); }
Ejemplo n.º 8
0
void HTTPServer::onUrlRequested ( MyString req, SOCKET sock )
{
	if ( req.find ( ".." ) !=-1 ||
	     req.find ( "/.ht" ) !=-1 || req.endsWith ( "~" ) ) {
		// evil hacker trying to read non-wwwhome or secret file
		errorReport ( sock, "403", "Forbidden",
		              "You don't have permission to access the requested URL." );
	} else {
		MyString path = req;
		MyFile f ( path );
		if ( f.isDirectory() && !path.endsWith ( "/" ) ) {
			// redirect browser if referring to directory without final '/'
			path += "/";
		}

		if ( f.isDirectory() ) {
#if defined(FULLDEBUG) || defined(DEBUG)
			mLog ( "Is a directory: " + path );
#endif
			// if directory, implicitly add 'index.html'
			string header;
			header =  ( string ) "HTTP/1.1 200 OK\r\n"
			          + "Content-Type: text/html\r\n";

			string length = "Content-Length: ";

			string html_header = "<html><body>";
			// out all files here
			string files;
			getDirFiles ( path, &files );

			string html_footer = "</body></html>\r\n\r\n";
			string data = html_header + files + html_footer;


			//count content-length.
			stringstream sstm;
			sstm << data.length();
			length += sstm.str() + "\r\n\r\n";

			data = header + length + html_header + files + html_footer;

			int n = write ( sock, data.c_str(), data.length() +1 );
			if ( n < 0 ) {
				mLog ( "ERROR writing to socket" );
				exit ( 1 );
			}
#ifdef FULLDEBUG
			mLog ( "Wrote: " + data );
#endif

		} else {
			try {
				// send files

				MyString temp;
				temp = ( string ) "HTTP/1.0 200 OK\r\n";
				temp += 		"Content-Type: " + guessContentType ( path ) + "\r\n";


				string data;
				parseFile ( sock, path, &data ); // send raw file

				//count content-length.
				string length = "Content-Length: ";
				stringstream sstm;
				sstm << data.length();
				length += sstm.str() + "\r\n\r\n";

				temp += length + data;

				int n = write ( sock, temp.c_str(), temp.length() );
				if ( n < 0 ) {
					mLog ( "ERROR writing to socket" );
					exit ( 1 );
				}
#if defined(DEBUG) || defined(FULLDEBUG)
				mLog ( "200 OK" );
#endif
			} catch ( ... ) {
				// file not found
				errorReport ( sock, "404", "Not Found",
				              "The requested URL was not found on this server." );
			}//try-catch
		}//else

	}//else
}
Ejemplo n.º 9
0
void GuiVehicleControl::onMouseMove(const GuiEvent &evt)
{
	// Contribute mouse position to MoveManager::*Speed variables
	// Dead zone (mMouseDeadZone), with the rest contributing
	// mContributePitch, mContributeYaw, and mContriuteRoll.
	// (Mouse position normalized to 0.0->1.0)

	  // Calculate useful vectors (squared)
	Point2I realPos = globalToLocalCoord(evt.mousePoint);
	Point2F normalizedPos((F32)realPos.x / (F32)mBounds.extent.x,
                         (F32)realPos.y / (F32)mBounds.extent.y);
	Point2F centerPos(normalizedPos.x - 0.5, normalizedPos.y - 0.5);
   
	if (mCurveMode == 1) {
		normalizedPos.x = mCurveCoeff * (F32)(normalizedPos.x*normalizedPos.x);
		normalizedPos.y = mCurveCoeff * (F32)(normalizedPos.y*normalizedPos.y);
	} else if (mCurveMode == 2) {
		normalizedPos.x = mCurveCoeff * mExp((F32)mLog((F32)normalizedPos.x));
		normalizedPos.y = mCurveCoeff * mExp((F32)mLog((F32)normalizedPos.y));
	} else {
		normalizedPos *= mCurveCoeff;
	}
   
	//Con::printf("real =[%d,%d], normal=[%f,%f], center=[%f,%f]", realPos.x, realPos.y, normalizedPos.x, normalizedPos.y, centerPos.x, centerPos.y);

	// Send directly to *Speed variables
	// TODO: factor in sensitivity?
	// centerPos is used, expressed in the range -0.5,0.5

	// Pitch goes from up -> down,but needs to be converted to 0-1 range
	MoveManager::mPitchSpeed = centerPos.y * 2;
   	
	// Calculate YAW - must be in dead zone
	F32 absX = mFabs(centerPos.x);
	if (absX < mMouseDeadZone) {
		// Calculate in range (0.0-1.0) in dead zone
		MoveManager::mYawSpeed = absX * (1.0 / mMouseDeadZone);
		if (centerPos.x < 0.0f) MoveManager::mYawSpeed = -MoveManager::mYawSpeed;
	} else {
		MoveManager::mYawSpeed = 0;
	}
   	
	// Calculate ROLL
	if (absX > mMouseDeadZone)
	{
		// Negate dead zone and determine percentage outside dead zone
		// (0.5-mMouseDeadZone)
		MoveManager::mRollSpeed = (absX - mMouseDeadZone) * ( 1.0 / (0.5-mMouseDeadZone) );
		if (centerPos.x < 0.0f) MoveManager::mRollSpeed = -MoveManager::mRollSpeed;
	} else {
		MoveManager::mRollSpeed = 0;
	}

	// Calculate X and Y axis
	if (!mUseRightAxis) {
		MoveManager::mXAxis_L = centerPos.x;
		MoveManager::mYAxis_L = centerPos.y;
	} else {
		MoveManager::mXAxis_R = centerPos.x;
		MoveManager::mYAxis_R = centerPos.y;
	}