Ejemplo n.º 1
0
void QgsGetRequestHandler::sendServiceException( const QgsMapServiceException& ex ) const
{
  //create Exception DOM document
  QDomDocument exceptionDoc;
  QDomElement serviceExceptionReportElem = exceptionDoc.createElement( "ServiceExceptionReport" );
  serviceExceptionReportElem.setAttribute( "version", "1.3.0" );
  serviceExceptionReportElem.setAttribute( "xmlns", "http://www.opengis.net/ogc" );
  exceptionDoc.appendChild( serviceExceptionReportElem );
  QDomElement serviceExceptionElem = exceptionDoc.createElement( "ServiceException" );
  serviceExceptionElem.setAttribute( "code", ex.code() );
  QDomText messageText = exceptionDoc.createTextNode( ex.message() );
  serviceExceptionElem.appendChild( messageText );
  serviceExceptionReportElem.appendChild( serviceExceptionElem );

  QByteArray ba = exceptionDoc.toByteArray();
  sendHttpResponse( &ba, "text/xml" );
}
Ejemplo n.º 2
0
void QgsGetRequestHandler::sendGetMapResponse( const QString& service, QImage* img ) const
{
  if ( img )
  {
    if ( mFormat != "PNG" && mFormat != "JPG" )
    {
      sendServiceException( QgsMapServiceException( "InvalidFormat", "Output format '" + mFormat + "' is not supported in the GetMap request" ) );
      return;
    }

    //store the image in a QByteArray and send it directly
    QByteArray ba;
    QBuffer buffer( &ba );
    buffer.open( QIODevice::WriteOnly );
    img->save( &buffer, mFormat.toLocal8Bit().data(), -1 );

    sendHttpResponse( &ba, formatToMimeType( mFormat ) );
  }
}
void *handleHttpRequest(void *acceptedConn){

  httpState = 200;
  int sock = *(int*)acceptedConn;
  char buffer[maxBuffer];
  
  /* Empties the buffer memory */
  bzero(buffer,maxBuffer);
  
  int i;
  do {
  /*********************************************************************/
  /* STEP : Accept a HTTP request */  
	acceptAndParseHttpRequest(sock);
  
  /*********************************************************************/
  /* STEP : Get requested file from the server */
	if ( httpState == 200 ) {  
		getRequestedFile(sock);
	}
  /*********************************************************************/
  /* STEP : Create and send the HTTP response */
	if ( httpState == 200 ) {  
		sendHttpResponse(sock);
	} 
	
	/* else display the error */
	if (httpState == 400 || httpState == 404 ) {
		errorDisplay(sock);
	}
  } while((!strcmp(connectionStatus,"Connection: keep-alive"))&&(!strcmp(httpVersion,"1.1")));
	
	free(acceptedConn);

	/* enable terminating option on the pthreads  */
   i = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
   i = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);

	/* Should get canceled while we sleep */
   sleep(300);        
   
   return NULL;
}
void* handleConnection(void* aCon)
{
    connection con = *( (connection*) aCon );

    printf( "\nConnection with client %d established.", con );

    char* buffer;
    if ( readHttpRequest( con, &buffer ) != 0 ) {
        //printf( "\nError reading message." );
        sendHttpResponse( con, 500, ERROR_500 );
        end_contact( con );
        return 0;
    }

    struct Request req = parseRequest( buffer );

    printf( "\nReceived request from client:" );
    printRequestData( &req );

    if ( req.invalid != 0 ) {
        printf( "\nInvalid request: %s", buffer );
        sendHttpResponse( con, 400, ERROR_400 );
        freeRequest( &req );
        end_contact( con );
        return 0;
    }

    free( buffer );

    checkCommandValid( &req );

    if ( req.invalid != 0 ) {
        printf( "\nInvalid command: %s", req.command );
        sendHttpResponse( con, 405, ERROR_405 );
        freeRequest( &req );
        end_contact( con );
        return 0;
    }

    checkPathValid( &req );

    if ( req.invalid != 0 ) {
        printf( "\nInvalid path requested: %s", req.path );
        sendHttpResponse( con, 403, ERROR_403 );
        freeRequest( &req );
        end_contact( con );
        return 0;
    }

    getFullPath( &req );

    if ( req.invalid != 0 ) {
        printf( "\nUnknown file: %s", req.path );
        sendHttpResponse( con, 404, ERROR_404 );
        freeRequest( &req );
        end_contact( con );
        return 0;
    }

    FILE * fd = fopen( req.path, "r" );

    if( !fd )
    {
        printf( "\nCould not open file: %s", req.path );
        sendHttpResponse( con, 403, ERROR_403 );
        freeRequest( &req );
        end_contact( con );
        return 0;
    }

    if( sendHttpResponse( con, 200, STATUS_200 ) );
    {
        int num_bytes = 0;

        char * readbuf = malloc( 1024 );
        while( !feof( fd ) )
        {
            num_bytes = fread( readbuf, 1, 1, fd );
            if( num_bytes < 0 )
                break;

            if( write( con, readbuf, num_bytes ) < 0 )
                break;

        }

        write( con, "\r\n\r\n", 4 );
        fclose( fd );
        free( readbuf );
    }

    printf( "\nClosing Connection %d", con);
    end_contact( con );

    return 0;
}
Ejemplo n.º 5
0
void QgsGetRequestHandler::sendGetPrintResponse( QByteArray* ba ) const
{
  sendHttpResponse( ba, formatToMimeType( mFormat ) );
}
Ejemplo n.º 6
0
void QgsGetRequestHandler::sendGetFeatureInfoResponse( const QDomDocument& infoDoc, const QString& infoFormat ) const
{
  QByteArray ba;
  QgsMSDebugMsg( "Info format is:" + infoFormat );

  if ( infoFormat == "text/xml" )
  {
    ba = infoDoc.toByteArray();
  }
  else if ( infoFormat == "text/plain" || infoFormat == "text/html" )
  {
    //create string
    QString featureInfoString;

    if ( infoFormat == "text/plain" )
    {
      featureInfoString.append( "GetFeatureInfo results\n" );
      featureInfoString.append( "\n" );
    }
    else if ( infoFormat == "text/html" )
    {
      featureInfoString.append( "<HEAD>\n" );
      featureInfoString.append( "<TITLE> GetFeatureInfo results </TITLE>\n" );
      featureInfoString.append( "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">\n" );
      featureInfoString.append( "</HEAD>\n" );
      featureInfoString.append( "<BODY>\n" );
    }

    QDomNodeList layerList = infoDoc.elementsByTagName( "Layer" );

    //layer loop
    for ( int i = 0; i < layerList.size(); ++i )
    {
      QDomElement layerElem = layerList.at( i ).toElement();
      if ( infoFormat == "text/plain" )
      {
        featureInfoString.append( "Layer '" + layerElem.attribute( "name" ) + "'\n" );
      }
      else if ( infoFormat == "text/html" )
      {
        featureInfoString.append( "<TABLE border=1 width=100%>\n" );
        featureInfoString.append( "<TR><TH width=25%>Layer</TH><TD>" + layerElem.attribute( "name" ) + "</TD></TR>\n" );
        featureInfoString.append( "</BR>" );
      }

      //feature loop (for vector layers)
      QDomNodeList featureNodeList = layerElem.elementsByTagName( "Feature" );
      QDomElement currentFeatureElement;

      if ( featureNodeList.size() < 1 ) //raster layer?
      {
        QDomNodeList attributeNodeList = layerElem.elementsByTagName( "Attribute" );
        for ( int j = 0; j < attributeNodeList.size(); ++j )
        {
          QDomElement attributeElement = attributeNodeList.at( j ).toElement();
          if ( infoFormat == "text/plain" )
          {
            featureInfoString.append( attributeElement.attribute( "name" ) + " = '" +
                                      attributeElement.attribute( "value" ) + "'\n" );
          }
          else if ( infoFormat == "text/html" )
          {
            featureInfoString.append( "<TR><TH>" + attributeElement.attribute( "name" ) + "</TH><TD>" +
                                      attributeElement.attribute( "value" ) + "</TD></TR>\n" );
          }
        }
      }
      else //vector layer
      {
        for ( int j = 0; j < featureNodeList.size(); ++j )
        {
          QDomElement featureElement = featureNodeList.at( j ).toElement();
          if ( infoFormat == "text/plain" )
          {
            featureInfoString.append( "Feature " + featureElement.attribute( "id" ) + "\n" );
          }
          else if ( infoFormat == "text/html" )
          {
            featureInfoString.append( "<TABLE border=1 width=100%>\n" );
            featureInfoString.append( "<TR><TH>Feature</TH><TD>" + featureElement.attribute( "id" ) + "</TD></TR>\n" );
          }
          //attribute loop
          QDomNodeList attributeNodeList = featureElement.elementsByTagName( "Attribute" );
          for ( int k = 0; k < attributeNodeList.size(); ++k )
          {
            QDomElement attributeElement = attributeNodeList.at( k ).toElement();
            if ( infoFormat == "text/plain" )
            {
              featureInfoString.append( attributeElement.attribute( "name" ) + " = '" +
                                        attributeElement.attribute( "value" ) + "'\n" );
            }
            else if ( infoFormat == "text/html" )
            {
              featureInfoString.append( "<TR><TH>" + attributeElement.attribute( "name" ) + "</TH><TD>" + attributeElement.attribute( "value" ) + "</TD></TR>\n" );
            }
          }

          if ( infoFormat == "text/html" )
          {
            featureInfoString.append( "</TABLE>\n</BR>\n" );
          }
        }
      }
      if ( infoFormat == "text/plain" )
      {
        featureInfoString.append( "\n" );
      }
      else if ( infoFormat == "text/html" )
      {
        featureInfoString.append( "</TABLE>\n<BR></BR>\n" );

      }
    }
    if ( infoFormat == "text/html" )
    {
      featureInfoString.append( "</BODY>\n" );
    }
    ba = featureInfoString.toUtf8();
  }
  else //unsupported format, send exception
  {
    //todo: send service exception
  }

  sendHttpResponse( &ba, infoFormat );
}
Ejemplo n.º 7
0
void QgsGetRequestHandler::sendGetStyleResponse( const QDomDocument& doc ) const
{
  QByteArray ba = doc.toByteArray();
  sendHttpResponse( &ba, "text/xml" );
}
Ejemplo n.º 8
0
void QgsHttpRequestHandler::sendGetCapabilitiesResponse( const QDomDocument& doc ) const
{
  QByteArray ba = doc.toByteArray();
  sendHttpResponse( &ba, "text/xml" );
}