void ThumbnailLoader::run() { ThumbnailRequestHandler requestHandler( this ); { QMutexLocker locker( &m_initMutex ); m_requestHandler = &requestHandler; connect( &requestHandler, SIGNAL(thumbnailLoaded(QImage)), this , SLOT (thumbnailLoaded(QImage)) ); m_initCondition.wakeAll(); } exec(); QMutexLocker locker( &m_initMutex ); disconnect( &requestHandler ); m_requestHandler = 0; m_initCondition.wakeAll(); }
void DebugServer::runningServer() { int socketfd, newSocketfd, portNr; socklen_t clientLength; sockaddressIn_t serverAddress, clientAddress; socketfd = socket(AF_INET, SOCK_STREAM, 0); if (socketfd < 0 ) { cout << "Error opening socket" << endl; } bzero((char*) &serverAddress, sizeof(serverAddress)); portNr = PORT_TO_PI; serverAddress.sin_family = AF_INET; serverAddress.sin_addr.s_addr = INADDR_ANY; serverAddress.sin_port = htons(portNr); if (bind(socketfd, reinterpret_cast<socketAddress_t*>(&serverAddress), sizeof(serverAddress)) < 0 ) { cout << "ERROR on binding" << endl; } listen(socketfd,5); clientLength = sizeof(clientAddress); newSocketfd = accept(socketfd, reinterpret_cast<socketAddress_t*>(&clientAddress), &clientLength); if (newSocketfd < 0) { cout << "ERROR on accept" << endl; } requestHandler(newSocketfd); close(newSocketfd); close(socketfd); }
RequestResponse ArtistRequest::invoke(const QStringList &arguments, const QString &who, const RequestInvocationContext &context) { const QString &id = arguments.join(QChar::Space); InformationResourceRepository &repository = context.informationResourceRepository(); IdGenerator &idGenerator = context.idGenerator(); IInformationResource *resource = repository.get(id); if (resource) { IIterator<Artist> *iterator = resource->iterator<Artist>(); if (iterator) { Artist *artist = iterator->next(resource, repository, idGenerator); if (artist) { return RequestResponse("templates/Artist.qml", artist); } return RequestResponse("templates/NoMoreData.qml", nullptr); } } else { RequestHandler requestHandler(repository, idGenerator); Artist *artist = requestHandler .get<Artist>("artist.getInfo" , as::artist = arguments.join(QChar::Space) , as::username = who ); if (artist) { return RequestResponse("templates/Artist.qml", artist); } NotFound *notFound = new NotFound(); notFound->setTypeName("Artist"); return RequestResponse("templates/NotFound.qml", notFound); } return RequestResponse(); }
void HttpServer::readCb(bufferevent *bev, void *ctx) { size_t length = evbuffer_get_length(bufferevent_get_input(bev)); char *data = (char *) malloc(sizeof(char) * length); evbuffer_remove(bufferevent_get_input(bev), data, length); HttpRequestParser requestParser; HttpRequest request; HttpResponse response; requestParser.reset(); requestParser.parse(request, data, length); HttpRequestHandler requestHandler(rootDir_); requestHandler.handleRequest(&request, &response); evbuffer_add(bufferevent_get_output(bev), response.toString().c_str(), response.toString().length()); free(data); }
QList<Artist *> TagArtistIterator::fetchMore(Tag *resource, InformationResourceRepository &informationResourceRepository, IdGenerator &idGenerator, int page) { RequestHandler requestHandler(informationResourceRepository, idGenerator); ArtistsEnvelope *envelope = requestHandler .get<ArtistsEnvelope>("tag.getTopArtists" , as::tag = resource->name() , as::limit = 10 , as::page = page ); if (envelope) { return envelope->artists(); } return QList<Artist *>(); }
void CopySenderServer::handle(QString data){ //QFutureWatcher<void> *watcher = new QFutureWatcher<void>; //connect(watcher, SIGNAL(), QFuture <QStringList>future = QtConcurrent::run(this, &CopySenderServer::splitRequests, data); QStringList protocolRequests = future.result(); if(protocolRequests.isEmpty()) return;//no requests found ///////////HANDLE ALL THE REQUESTS THAT IS SENT THROUGH/////////////// /////////////E.g. It could be multiple requests/////////////////// for(int i = 0; i < protocolRequests.length(); i++){ requestHandler(protocolRequests.at(i)); } }
OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest, void *callbackParam) { OCEntityHandlerResult ehResult = OC_EH_OK; OCEntityHandlerResponse response = { 0, 0, OC_EH_ERROR, 0, 0, { },{ 0 }, false }; OCBaseResourceT *resource = (OCBaseResourceT*) callbackParam; OCRepPayload* payload = NULL; // Check the request type if(entityHandlerRequest && (flag & OC_REQUEST_FLAG)) { OCRepPayloadDestroy(payload); ehResult = requestHandler(entityHandlerRequest, resource, &payload); } if(ehResult == OC_EH_ERROR) { OIC_LOG(ERROR, TAG, "ERROR getting request handler and setting paylaod"); return ehResult; } // Send the response ehResult = responseHandler(&response, entityHandlerRequest, payload, ehResult); if(ehResult == OC_EH_OK) { OIC_LOG(DEBUG, TAG, "Response sent successfully"); } else { OIC_LOG(ERROR, TAG, "ERROR Sending the response"); } if (entityHandlerRequest && (flag & OC_OBSERVE_FLAG)) { OIC_LOG(DEBUG, TAG, "Observer flag"); ehResult = observerHandler(entityHandlerRequest, resource); } OCRepPayloadDestroy(payload); return ehResult; }
void ProtocolHandler::handle(QString data){ if(slaveSocket == 0){ return; } QFuture <QStringList>future = QtConcurrent::run(this, &ProtocolHandler::splitRequests, data); QStringList protocolRequests = future.result(); if(protocolRequests.isEmpty()) return;//no requests found ///////////HANDLE ALL THE REQUESTS THAT IS SENT THROUGH/////////////// /////////////E.g. It could be multiple requests/////////////////// for(int i = 0; i < protocolRequests.length(); i++){ requestHandler(protocolRequests.at(i)); } }
void QgsServer::handleRequest( QgsServerRequest &request, QgsServerResponse &response, const QgsProject *project ) { Qgis::MessageLevel logLevel = QgsServerLogger::instance()->logLevel(); QTime time; //used for measuring request time if loglevel < 1 qApp->processEvents(); if ( logLevel == Qgis::Info ) { time.start(); } // Pass the filters to the requestHandler, this is needed for the following reasons: // Allow server request to call sendResponse plugin hook if enabled QgsFilterResponseDecorator responseDecorator( sServerInterface->filters(), response ); //Request handler QgsRequestHandler requestHandler( request, response ); try { // TODO: split parse input into plain parse and processing from specific services requestHandler.parseInput(); } catch ( QgsMapServiceException &e ) { QgsMessageLog::logMessage( "Parse input exception: " + e.message(), QStringLiteral( "Server" ), Qgis::Critical ); requestHandler.setServiceException( e ); } // Set the request handler into the interface for plugins to manipulate it sServerInterface->setRequestHandler( &requestHandler ); // Call requestReady() method (if enabled) responseDecorator.start(); // Plugins may have set exceptions if ( !requestHandler.exceptionRaised() ) { try { const QgsServerParameters params = request.serverParameters(); printRequestParameters( params.toMap(), logLevel ); //Config file path if ( ! project ) { QString configFilePath = configPath( *sConfigFilePath, params.map() ); // load the project if needed and not empty project = mConfigCache->project( configFilePath ); if ( ! project ) { throw QgsServerException( QStringLiteral( "Project file error" ) ); } sServerInterface->setConfigFilePath( configFilePath ); } else { sServerInterface->setConfigFilePath( project->fileName() ); } if ( ! params.fileName().isEmpty() ) { const QString value = QString( "attachment; filename=\"%1\"" ).arg( params.fileName() ); requestHandler.setResponseHeader( QStringLiteral( "Content-Disposition" ), value ); } // Lookup for service QgsService *service = sServiceRegistry->getService( params.service(), params.version() ); if ( service ) { service->executeRequest( request, responseDecorator, project ); } else { throw QgsOgcServiceException( QStringLiteral( "Service configuration error" ), QStringLiteral( "Service unknown or unsupported" ) ); } } catch ( QgsServerException &ex ) { responseDecorator.write( ex ); } catch ( QgsException &ex ) { // Internal server error response.sendError( 500, ex.what() ); } } // Terminate the response responseDecorator.finish(); // We are done using requestHandler in plugins, make sure we don't access // to a deleted request handler from Python bindings sServerInterface->clearRequestHandler(); if ( logLevel == Qgis::Info ) { QgsMessageLog::logMessage( "Request finished in " + QString::number( time.elapsed() ) + " ms", QStringLiteral( "Server" ), Qgis::Info ); } }
void createServer( void ( *requestHandler )( int connection ) ) { struct sockaddr_in server, client; int addressSize = sizeof( struct sockaddr_in ); printf( "Initializing ...\n" ); int serverSocket = socket( AF_INET , SOCK_STREAM , 0 ); if ( serverSocket == -1 ) { printf( "Could not create socket\n" ); return; } printf( "Socket Created\n" ); int port = 5390; server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons( port ); if ( bind( serverSocket, ( struct sockaddr * ) &server , sizeof( server ) ) < 0 ) { // Quick and Dirty if ( errno == 98 ) { port = 5391; server.sin_port = htons( port ); if ( bind( serverSocket, ( struct sockaddr * ) &server , sizeof( server ) ) < 0 ) { printf( "Could not bind %d\n", errno ); return; } } } printf( "Server Bound. %d\n", port ); listen( serverSocket, 3 ); // ... // while ( 1 ) { int connection = accept( serverSocket, (struct sockaddr *) &client, (socklen_t*) &addressSize ); if ( connection < 0 ) { printf( "Accept Failed" ); return; } printf( "\t---> Connection Recieved\n" ); requestHandler( connection ); } }
void QgsServer::handleRequest( QgsServerRequest &request, QgsServerResponse &response ) { QgsMessageLog::MessageLevel logLevel = QgsServerLogger::instance()->logLevel(); QTime time; //used for measuring request time if loglevel < 1 QgsProject::instance()->removeAllMapLayers(); qApp->processEvents(); if ( logLevel == QgsMessageLog::INFO ) { time.start(); } // Pass the filters to the requestHandler, this is needed for the following reasons: // Allow server request to call sendResponse plugin hook if enabled QgsFilterResponseDecorator responseDecorator( sServerInterface->filters(), response ); //Request handler QgsRequestHandler requestHandler( request, response ); try { // TODO: split parse input into plain parse and processing from specific services requestHandler.parseInput(); } catch ( QgsMapServiceException &e ) { QgsMessageLog::logMessage( "Parse input exception: " + e.message(), QStringLiteral( "Server" ), QgsMessageLog::CRITICAL ); requestHandler.setServiceException( e ); } // Set the request handler into the interface for plugins to manipulate it sServerInterface->setRequestHandler( &requestHandler ); // Call requestReady() method (if enabled) responseDecorator.start(); // Plugins may have set exceptions if ( !requestHandler.exceptionRaised() ) { try { QMap<QString, QString> parameterMap = request.parameters(); printRequestParameters( parameterMap, logLevel ); //Config file path QString configFilePath = configPath( *sConfigFilePath, parameterMap ); // load the project if needed and not empty const QgsProject *project = mConfigCache->project( configFilePath ); if ( ! project ) { throw QgsServerException( QStringLiteral( "Project file error" ) ); } sServerInterface->setConfigFilePath( configFilePath ); //Service parameter QString serviceString = parameterMap.value( QStringLiteral( "SERVICE" ) ); if ( serviceString.isEmpty() ) { // SERVICE not mandatory for WMS 1.3.0 GetMap & GetFeatureInfo QString requestString = parameterMap.value( QStringLiteral( "REQUEST" ) ); if ( requestString == QLatin1String( "GetMap" ) || requestString == QLatin1String( "GetFeatureInfo" ) ) { serviceString = QStringLiteral( "WMS" ); } } QString versionString = parameterMap.value( QStringLiteral( "VERSION" ) ); //possibility for client to suggest a download filename QString outputFileName = parameterMap.value( QStringLiteral( "FILE_NAME" ) ); if ( !outputFileName.isEmpty() ) { requestHandler.setResponseHeader( QStringLiteral( "Content-Disposition" ), "attachment; filename=\"" + outputFileName + "\"" ); } // Lookup for service QgsService *service = sServiceRegistry.getService( serviceString, versionString ); if ( service ) { service->executeRequest( request, responseDecorator, project ); } else { throw QgsOgcServiceException( QStringLiteral( "Service configuration error" ), QStringLiteral( "Service unknown or unsupported" ) ) ; } } catch ( QgsServerException &ex ) { responseDecorator.write( ex ); } catch ( QgsException &ex ) { // Internal server error response.sendError( 500, ex.what() ); } } // Terminate the response responseDecorator.finish(); // We are done using requestHandler in plugins, make sure we don't access // to a deleted request handler from Python bindings sServerInterface->clearRequestHandler(); if ( logLevel == QgsMessageLog::INFO ) { QgsMessageLog::logMessage( "Request finished in " + QString::number( time.elapsed() ) + " ms", QStringLiteral( "Server" ), QgsMessageLog::INFO ); } }
int main (int argc, char *argv[]) { /* Verify correct command-line call */ if (argc != 2) { perror("Correct usage: ./webserv port-number.\n"); exit(-1); } /* Get the port from the command line */ int port = atoi(argv[1]); /* Create the socket */ int sd = socket(AF_INET, SOCK_STREAM, 0); if (sd == -1) { perror("The socket() syscall failed.\n"); exit(-1); } /* Set socket as SO_REUSEADDR */ int sock_opt_val = 1; if (setsockopt (sd, SOL_SOCKET, SO_REUSEADDR, (char *) &sock_opt_val, sizeof(sock_opt_val)) < 0) { perror ("(servConn): Failed to set SO_REUSEADDR on INET socket"); exit (-1); } /* Set up the socket/port information */ struct sockaddr_in server; bzero(&server, sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons(port); server.sin_addr.s_addr = htonl(INADDR_ANY); /* Bind the socket to the port */ int ret = bind(sd, (struct sockaddr *) &server, sizeof(struct sockaddr)); if (ret == -1) { perror("The bind() call failed.\n"); exit(-1); } /* Listen to the socket with a backlog of 5 */ listen(sd, 5); /* Receive and handle new connections */ struct sockaddr_in client; int len; int cd; for ( ; ; ) { len = sizeof(client); cd = accept(sd, (struct sockaddr *) &client, &len); if (cd == -1) { perror ("(servConn): accept() error"); exit (-1); } if (fork() == 0) { close(sd); char buffer[1024]; int n = read(cd, buffer, 1024); if (n == 0) { exit(0); } requestHandler(buffer, cd); exit(0); } close(cd); } }