Esempio n. 1
0
// dump request
void dump_request( HttpRequest& req, HttpUrl& url, CSocket& s )
{
	s.printf( "<br><br><table border width=650>" );
	s.printf( "<tr><td>%s</td><td>%s</td></tr>", req.method(), url.file() );
	for( HttpRequest::TagsIterator it=req.tags_begin();
		it != req.tags_end(); it++ )
		{
			s.printf( "<tr><td><b>%s</b></td>", (*it).first.c_str() );		 
			if( !(*it).second.empty() ) s.printf( "<td>%s</td></tr>", (char*)(*it).second );
		}
	s.printf("</table>" );
	// informatiile din url
	s.printf( "<table border width=650>" );
	for( HttpUrl::QueryIterator itr=url.query_begin();
		itr != url.query_end(); itr++ )
		{
			s.printf( "<tr><td><b>%s</b></td>", (*itr).first.c_str() );		 
			if( !(*itr).second.empty() ) s.printf( "<td>%s</td></tr>", (char*)(*itr).second );
		}
	s.printf("</table>" );
}
Esempio n. 2
0
/**

  This is the main function from the HTTP 
  server. Every new connection from a client
  is handled here. Here the request from the
  client is handled.
  */
void HTTPServer( GN::ClientList&, GN::CSocket & s )
{
	try
	{
		// buffer pentru cererea HTTP
		char buffer[ MAX_REQUEST_RECEIVE_BUFFER+2 ];

		HttpRequest	req;		// Clasa pentru procesare cerere HTTP
		HttpUrl url;			// Clasa pentru procesare URL
		HttpResponse r;			// Clasa pentru raspunsul HTTP
		
		// receptioneaza cerere
		int ret = s.receive( buffer, MAX_REQUEST_RECEIVE_BUFFER );
		buffer[ret]=0;

		// proceseaza cererea primita
		req.parse( buffer, ret );
		check_for_errors( s, req );

		// parse url
		url.parse( req.url(), WWW_FOLDER );
				
		// Verifica daca exista fisierul
		FILE * file = fopen( url.file(), "r");
		if( !file  )
		{
			r.redirect( STD_404_PAGE );
			r.send(s);

			GSTD::CError error( 0, "File not found");
			throw error;
		}
		fclose(file);
		
		// Trimite raspunsul ok - fisierul exista si continutul va fi trimis
		r.set_status( HTTP_OK );
		
		r.set("Server", "GCWS 1.0");
		r.set("Connection","close");
	
		// incarca DLL
		
		// seteaza tipul continutului
		get_content_type( url.ext(), buffer );
		r.set("Content-type", buffer );
		
		// trimite raspunsul cu toate campurile setate corespunzator
		r.send(s);

		// send file - one buffer at time
		file = fopen( url.file(), "rb" );
		int nb;
		do
		{
			nb = fread( buffer, 1, MAX_REQUEST_RECEIVE_BUFFER, file );
			if( nb ) s.send( buffer, nb );
		}while( nb );
		fclose( file );

		// if( url.is_set("debug") ) dump_request( req, url, s );

	}
	catch( GSTD::CError error )
	{
		throw error;
	}
}