smf/smfservermodule/smfserver/transportmgr/smftransportmanagerutil.cpp
changeset 14 a469c0e6e7fb
parent 7 be09cf1f39dd
child 18 013a02bf2bb0
equal deleted inserted replaced
13:b5d63d5fc252 14:a469c0e6e7fb
    17  * methods for the smf framework
    17  * methods for the smf framework
    18  *
    18  *
    19  */
    19  */
    20 
    20 
    21 // Include files
    21 // Include files
       
    22 #define EMULATORTESTING
       
    23 
    22 #include <QNetworkAccessManager>
    24 #include <QNetworkAccessManager>
    23 #include <QNetworkReply>
    25 #include <QNetworkReply>
    24 #include <QSettings>
    26 #include <QSettings>
    25 #include <zlib.h>
    27 #include <zlib.h>
       
    28 #ifdef EMULATORTESTING
    26 // For proxy settings on emulator only - REMOVE for device
    29 // For proxy settings on emulator only - REMOVE for device
    27 #include <QNetworkProxy>
    30 #include <QNetworkProxy>
       
    31 #endif
    28 
    32 
    29 #include "smftransportmanagerutil.h"
    33 #include "smftransportmanagerutil.h"
    30 #include "smfpluginmanager.h"
    34 #include "smfpluginmanager.h"
    31 
    35 
    32 // Static Global constants
    36 // Static Global constants
    61 	// Create the settings if no data is stored
    65 	// Create the settings if no data is stored
    62 	if( !m_settings->contains("Sent Data") )
    66 	if( !m_settings->contains("Sent Data") )
    63 		m_settings->setValue("Sent Data", 0);
    67 		m_settings->setValue("Sent Data", 0);
    64 	if( !m_settings->contains("Received Data") )
    68 	if( !m_settings->contains("Received Data") )
    65 		m_settings->setValue("Received Data", 0);
    69 		m_settings->setValue("Received Data", 0);
       
    70 	
       
    71 #ifdef EMULATORTESTING
       
    72 	qDebug()<<"Using PROXY SETTINGS!!!, change for device settings";
       
    73 	
       
    74 	// Reading the keys, CSM Stubbed - START
       
    75 	QFile file("c:\\data\\DoNotShare.txt");
       
    76 	if (!file.open(QIODevice::ReadOnly))
       
    77 		{
       
    78 		qDebug()<<"File to read the windows username and password could not be opened, returning!!!";
       
    79 		return;
       
    80 		}
       
    81 	
       
    82 	QByteArray arr = file.readAll();
       
    83 	QList<QByteArray> list = arr.split(' ');
       
    84 	file.close();
       
    85 	
       
    86 	QString username(list[0]);
       
    87 	QString password(list[1]);
       
    88 
       
    89     // For proxy settings on emulator only - REMOVE for device
       
    90     QString httpProxy = "10.1.0.224";
       
    91     QString httpPort = "3148";
       
    92     
       
    93 	QString httpUser(username);
       
    94 	QString httpPass(password);
       
    95 
       
    96     //==Classes used from Network Module==
       
    97     QNetworkProxy proxy;
       
    98 
       
    99     proxy.setType(QNetworkProxy::HttpProxy);
       
   100     proxy.setHostName(httpProxy);
       
   101     proxy.setPort(httpPort.toInt());
       
   102     proxy.setUser(httpUser);
       
   103     proxy.setPassword(httpPass);
       
   104 		
       
   105     QNetworkProxy::setApplicationProxy(proxy);
       
   106 #endif
    66 	}
   107 	}
    67 
   108 
    68 
   109 
    69 /**
   110 /**
    70  * Destructor
   111  * Destructor
    95 QNetworkReply* SmfTransportManagerUtil::get ( 
   136 QNetworkReply* SmfTransportManagerUtil::get ( 
    96 		QNetworkRequest &aRequest,
   137 		QNetworkRequest &aRequest,
    97 		const QList<QUrl> &aUrlList,
   138 		const QList<QUrl> &aUrlList,
    98 		bool &aSOPCompliant )
   139 		bool &aSOPCompliant )
    99 	{
   140 	{
       
   141 	qDebug()<<"Inside SmfTransportManagerUtil::get()";;
   100 	bool found = false;
   142 	bool found = false;
   101 	QString hostName = aRequest.url().host();
   143 	QString hostName = aRequest.url().host();
   102 	
   144 	
   103 	// Same Origin Policy checking
   145 	// Same Origin Policy checking
   104 	// Check if the host name of the url in the request created by the 
   146 	// Check if the host name of the url in the request created by the 
   113 		}
   155 		}
   114 	
   156 	
   115 	if(found)
   157 	if(found)
   116 		{
   158 		{
   117 		aSOPCompliant = true;
   159 		aSOPCompliant = true;
   118 		
   160 
   119 		// Set header to accept gzip encoded data
   161 		// Set header to accept gzip encoded data
   120 		aRequest.setRawHeader("Accept-encoding", kSmfAcceptEncoding);
   162 		aRequest.setRawHeader("Accept-encoding", kSmfAcceptEncoding);
   121 		
   163 		
   122 		// Set the cache control
   164 		// Set the cache control
   123 		aRequest.setRawHeader("Cache-Control", kSmfCacheControl);
   165 		aRequest.setRawHeader("Cache-Control", kSmfCacheControl);
   124 
   166 
   125 		// Put the same user agent for all requests sent by Smf
   167 		// Put the same user agent for all requests sent by Smf
   126 		aRequest.setRawHeader("User-Agent", kSmfUserAgent);
   168 		aRequest.setRawHeader("User-Agent", kSmfUserAgent);
   127 		
   169 		
   128 		QNetworkReply* reply = m_networkAccessManager.get(aRequest);
   170 		QNetworkReply* reply = m_networkAccessManager.get(aRequest);
       
   171 		qDebug()<<"QNetworkReply of get() = "<<(long)reply;
   129 	
   172 	
   130 		connect(&m_networkAccessManager, SIGNAL(finished(QNetworkReply *)), 
   173 		connect(&m_networkAccessManager, SIGNAL(finished(QNetworkReply *)), 
   131 				this, SLOT(networkReplyFinished(QNetworkReply *)));
   174 				this, SLOT(networkReplyFinished(QNetworkReply *)));
   132 		
   175 		
   133 		connect(&m_networkAccessManager, SIGNAL(error(QNetworkReply::NetworkError)), 
   176 		connect(&m_networkAccessManager, SIGNAL(error(QNetworkReply::NetworkError)), 
   139 		}
   182 		}
   140 	else
   183 	else
   141 		{
   184 		{
   142 		// Error :: SAME ORIGIN POLICY violation!!!
   185 		// Error :: SAME ORIGIN POLICY violation!!!
   143 		aSOPCompliant = false;
   186 		aSOPCompliant = false;
       
   187 		qDebug()<<"SOP violation";
   144 		return NULL;
   188 		return NULL;
   145 		}
   189 		}
   146 	}
   190 	}
   147 
   191 
   148 
   192 
   162 		QNetworkRequest &aRequest, 
   206 		QNetworkRequest &aRequest, 
   163 		const QByteArray& aPostData,
   207 		const QByteArray& aPostData,
   164 		const QList<QUrl> &aUrlList,
   208 		const QList<QUrl> &aUrlList,
   165 		bool &aSOPCompliant )
   209 		bool &aSOPCompliant )
   166 	{
   210 	{
       
   211 	qDebug()<<"Inside SmfTransportManagerUtil::post()";
   167 	bool found = false;
   212 	bool found = false;
   168 	QString hostName = aRequest.url().host();
   213 	QString hostName = aRequest.url().host();
   169 	
   214 	
   170 	// Same Origin Policy checking
   215 	// Same Origin Policy checking
   171 	// Check if the host name of the url in the request created by the 
   216 	// Check if the host name of the url in the request created by the 
   180 		}
   225 		}
   181 	
   226 	
   182 	if(found)
   227 	if(found)
   183 		{
   228 		{
   184 		aSOPCompliant = true;
   229 		aSOPCompliant = true;
       
   230 
       
   231 		// Set header to accept gzip encoded data
       
   232 		aRequest.setRawHeader("Accept-encoding", kSmfAcceptEncoding);
   185 		
   233 		
   186 		// Set the cache control
   234 		// Set the cache control
   187 		aRequest.setRawHeader("Cache-Control", kSmfCacheControl);
   235 		aRequest.setRawHeader("Cache-Control", kSmfCacheControl);
   188 		
   236 		
   189 		// Put the same user agent for all requests sent by Smf
   237 		// Put the same user agent for all requests sent by Smf
   190 		aRequest.setRawHeader("User-Agent", kSmfUserAgent);
   238 		aRequest.setRawHeader("User-Agent", kSmfUserAgent);
   191 		
   239 		
       
   240 #ifdef DETAILEDDEBUGGING
       
   241 		qDebug()<<"post data size is = "<<aPostData.size();
       
   242 		qDebug()<<"post data is = "<<QString(aPostData);
       
   243 #endif
       
   244 		
   192 		QNetworkReply* reply = m_networkAccessManager.post(aRequest, aPostData);
   245 		QNetworkReply* reply = m_networkAccessManager.post(aRequest, aPostData);
       
   246 		qDebug()<<"QNetworkReply of post() = "<<(long)reply;
   193 	
   247 	
   194 		connect(&m_networkAccessManager, SIGNAL(finished(QNetworkReply *)), 
   248 		connect(&m_networkAccessManager, SIGNAL(finished(QNetworkReply *)), 
   195 				this, SLOT(networkReplyFinished(QNetworkReply *)));
   249 				this, SLOT(networkReplyFinished(QNetworkReply *)));
   196 		
   250 		
   197 		connect(&m_networkAccessManager, SIGNAL(error(QNetworkReply::NetworkError)), 
   251 		connect(&m_networkAccessManager, SIGNAL(error(QNetworkReply::NetworkError)), 
   203 		}
   257 		}
   204 	else
   258 	else
   205 		{
   259 		{
   206 		// Error :: SAME ORIGIN POLICY violation!!!
   260 		// Error :: SAME ORIGIN POLICY violation!!!
   207 		aSOPCompliant = false;
   261 		aSOPCompliant = false;
       
   262 		qDebug()<<"SOP violation";
   208 		return NULL;
   263 		return NULL;
   209 		}
   264 		}
   210 	}
   265 	}
   211 
   266 
   212 
   267 
   224 QNetworkReply* SmfTransportManagerUtil::head ( 
   279 QNetworkReply* SmfTransportManagerUtil::head ( 
   225 		QNetworkRequest& aRequest,
   280 		QNetworkRequest& aRequest,
   226 		const QList<QUrl> &aUrlList,
   281 		const QList<QUrl> &aUrlList,
   227 		bool &aSOPCompliant )
   282 		bool &aSOPCompliant )
   228 	{
   283 	{
       
   284 	qDebug()<<"Inside SmfTransportManagerUtil::head()";
   229 	bool found = false;
   285 	bool found = false;
   230 	QString hostName = aRequest.url().host();
   286 	QString hostName = aRequest.url().host();
   231 
   287 
   232 	// Same Origin Policy checking
   288 	// Same Origin Policy checking
   233 	// Check if the host name of the url in the request created by the 
   289 	// Check if the host name of the url in the request created by the 
   242 		}
   298 		}
   243 	
   299 	
   244 	if(found)
   300 	if(found)
   245 		{
   301 		{
   246 		aSOPCompliant = true;
   302 		aSOPCompliant = true;
       
   303 		
       
   304 		// Set header to accept gzip encoded data
       
   305 		aRequest.setRawHeader("Accept-encoding", kSmfAcceptEncoding);
       
   306 		
   247 		// Set the cache control
   307 		// Set the cache control
   248 		aRequest.setRawHeader("Cache-Control", kSmfCacheControl);
   308 		aRequest.setRawHeader("Cache-Control", kSmfCacheControl);
   249 		
   309 		
   250 		// Put the same user agent for all requests sent by Smf
   310 		// Put the same user agent for all requests sent by Smf
   251 		aRequest.setRawHeader("User-Agent", kSmfUserAgent);
   311 		aRequest.setRawHeader("User-Agent", kSmfUserAgent);
   252 		
   312 		
   253 		QNetworkReply* reply = m_networkAccessManager.head(aRequest);
   313 		QNetworkReply* reply = m_networkAccessManager.head(aRequest);
       
   314 		qDebug()<<"QNetworkReply of head() = "<<(long)reply;
   254 	
   315 	
   255 		connect(&m_networkAccessManager, SIGNAL(finished(QNetworkReply *)), 
   316 		connect(&m_networkAccessManager, SIGNAL(finished(QNetworkReply *)), 
   256 				this, SLOT(networkReplyFinished(QNetworkReply *)));
   317 				this, SLOT(networkReplyFinished(QNetworkReply *)));
   257 		
   318 		
   258 		connect(&m_networkAccessManager, SIGNAL(error(QNetworkReply::NetworkError)), 
   319 		connect(&m_networkAccessManager, SIGNAL(error(QNetworkReply::NetworkError)), 
   264 		}
   325 		}
   265 	else
   326 	else
   266 		{
   327 		{
   267 		// Error :: SAME ORIGIN POLICY violation!!!
   328 		// Error :: SAME ORIGIN POLICY violation!!!
   268 		aSOPCompliant = false;
   329 		aSOPCompliant = false;
       
   330 		qDebug()<<"SOP violation";
   269 		return NULL;
   331 		return NULL;
   270 		}
   332 		}
   271 	}
   333 	}
   272 
   334 
   273 
   335 
   287 		QNetworkRequest &aRequest,
   349 		QNetworkRequest &aRequest,
   288 		const QByteArray& aPostData,
   350 		const QByteArray& aPostData,
   289 		const QList<QUrl> &aUrlList,
   351 		const QList<QUrl> &aUrlList,
   290 		bool &aSOPCompliant )
   352 		bool &aSOPCompliant )
   291 	{
   353 	{
       
   354 	qDebug()<<"Inside SmfTransportManagerUtil::put()";
   292 	bool found = false;
   355 	bool found = false;
   293 	QString hostName = aRequest.url().host();
   356 	QString hostName = aRequest.url().host();
   294 
   357 
   295 	// Same Origin Policy checking
   358 	// Same Origin Policy checking
   296 	// Check if the host name of the url in the request created by the 
   359 	// Check if the host name of the url in the request created by the 
   305 		}
   368 		}
   306 	
   369 	
   307 	if(found)
   370 	if(found)
   308 		{
   371 		{
   309 		aSOPCompliant = true;
   372 		aSOPCompliant = true;
       
   373 		
       
   374 		// Set header to accept gzip encoded data
       
   375 		aRequest.setRawHeader("Accept-encoding", kSmfAcceptEncoding);
       
   376 		
   310 		// Set the cache control
   377 		// Set the cache control
   311 		aRequest.setRawHeader("Cache-Control", kSmfCacheControl);
   378 		aRequest.setRawHeader("Cache-Control", kSmfCacheControl);
   312 		
   379 		
   313 		// Put the same user agent for all requests sent by Smf
   380 		// Put the same user agent for all requests sent by Smf
   314 		aRequest.setRawHeader("User-Agent", kSmfUserAgent);
   381 		aRequest.setRawHeader("User-Agent", kSmfUserAgent);
   315 		
   382 		
   316 		QNetworkReply* reply = m_networkAccessManager.put(aRequest, aPostData);
   383 		QNetworkReply* reply = m_networkAccessManager.put(aRequest, aPostData);
       
   384 		qDebug()<<"QNetworkReply of put() = "<<(long)reply;
   317 	
   385 	
   318 		connect(&m_networkAccessManager, SIGNAL(finished(QNetworkReply *)), 
   386 		connect(&m_networkAccessManager, SIGNAL(finished(QNetworkReply *)), 
   319 				this, SLOT(networkReplyFinished(QNetworkReply *)));
   387 				this, SLOT(networkReplyFinished(QNetworkReply *)));
   320 		
   388 		
   321 		connect(&m_networkAccessManager, SIGNAL(error(QNetworkReply::NetworkError)), 
   389 		connect(&m_networkAccessManager, SIGNAL(error(QNetworkReply::NetworkError)), 
   327 		}
   395 		}
   328 	else
   396 	else
   329 		{
   397 		{
   330 		// Error :: SAME ORIGIN POLICY violation!!!
   398 		// Error :: SAME ORIGIN POLICY violation!!!
   331 		aSOPCompliant = false;
   399 		aSOPCompliant = false;
       
   400 		qDebug()<<"SOP violation";
   332 		return NULL;
   401 		return NULL;
   333 		}
   402 		}
   334 	}
   403 	}
   335 
   404 
   336 
   405 
   348 QNetworkReply* SmfTransportManagerUtil::deleteResource ( 
   417 QNetworkReply* SmfTransportManagerUtil::deleteResource ( 
   349 		QNetworkRequest &aRequest,
   418 		QNetworkRequest &aRequest,
   350 		const QList<QUrl> &aUrlList,
   419 		const QList<QUrl> &aUrlList,
   351 		bool &aSOPCompliant )
   420 		bool &aSOPCompliant )
   352 	{
   421 	{
       
   422 	qDebug()<<"Inside SmfTransportManagerUtil::deleteResource()";
   353 	bool found = false;
   423 	bool found = false;
   354 	QString hostName = aRequest.url().host();
   424 	QString hostName = aRequest.url().host();
   355 
   425 
   356 	// Same Origin Policy checking
   426 	// Same Origin Policy checking
   357 	// Check if the host name of the url in the request created by the 
   427 	// Check if the host name of the url in the request created by the 
   366 		}
   436 		}
   367 	
   437 	
   368 	if(found)
   438 	if(found)
   369 		{
   439 		{
   370 		aSOPCompliant = true;
   440 		aSOPCompliant = true;
       
   441 		
       
   442 		// Set header to accept gzip encoded data
       
   443 		aRequest.setRawHeader("Accept-encoding", kSmfAcceptEncoding);
       
   444 		
   371 		// Set the cache control
   445 		// Set the cache control
   372 		aRequest.setRawHeader("Cache-Control", kSmfCacheControl);
   446 		aRequest.setRawHeader("Cache-Control", kSmfCacheControl);
   373 		
   447 		
   374 		// Put the same user agent for all requests sent by Smf
   448 		// Put the same user agent for all requests sent by Smf
   375 		aRequest.setRawHeader("User-Agent", kSmfUserAgent);
   449 		aRequest.setRawHeader("User-Agent", kSmfUserAgent);
   376 		
   450 		
   377 		QNetworkReply* reply = m_networkAccessManager.deleteResource(aRequest);
   451 		QNetworkReply* reply = m_networkAccessManager.deleteResource(aRequest);
       
   452 		qDebug()<<"QNetworkReply of deleteResource() = "<<(long)reply;
   378 	
   453 	
   379 		connect(&m_networkAccessManager, SIGNAL(finished(QNetworkReply *)), 
   454 		connect(&m_networkAccessManager, SIGNAL(finished(QNetworkReply *)), 
   380 				this, SLOT(networkReplyFinished(QNetworkReply *)));
   455 				this, SLOT(networkReplyFinished(QNetworkReply *)));
   381 		
   456 		
   382 		connect(&m_networkAccessManager, SIGNAL(error(QNetworkReply::NetworkError)), 
   457 		connect(&m_networkAccessManager, SIGNAL(error(QNetworkReply::NetworkError)), 
   388 		}
   463 		}
   389 	else
   464 	else
   390 		{
   465 		{
   391 		// Error :: SAME ORIGIN POLICY violation!!!
   466 		// Error :: SAME ORIGIN POLICY violation!!!
   392 		aSOPCompliant = false;
   467 		aSOPCompliant = false;
       
   468 		qDebug()<<"SOP violation";
   393 		return NULL;
   469 		return NULL;
   394 		}
   470 		}
   395 	}
   471 	}
   396 
   472 
   397 
   473 
   403  * @return Returns true for success, else false 
   479  * @return Returns true for success, else false 
   404  */
   480  */
   405 bool SmfTransportManagerUtil::cancelRequest ( 
   481 bool SmfTransportManagerUtil::cancelRequest ( 
   406 		QNetworkReply *aCancelReply )
   482 		QNetworkReply *aCancelReply )
   407 	{
   483 	{
       
   484 	qDebug()<<"Inside SmfTransportManagerUtil::cancelRequest()";
       
   485 	
   408 	// Checks if this transaction is running
   486 	// Checks if this transaction is running
   409 	if( aCancelReply->isRunning() )
   487 	if( aCancelReply->isRunning() )
   410 		{
   488 		{
   411 		// Aborts the running transaction
   489 		// Aborts the running transaction
   412 		aCancelReply->abort();
   490 		aCancelReply->abort();
   462  * @param aNetworkReply The QNetworkReply instance for which the http 
   540  * @param aNetworkReply The QNetworkReply instance for which the http 
   463  * transaction was made
   541  * transaction was made
   464  */
   542  */
   465 void SmfTransportManagerUtil::networkReplyFinished ( QNetworkReply *aNetworkReply )
   543 void SmfTransportManagerUtil::networkReplyFinished ( QNetworkReply *aNetworkReply )
   466 	{
   544 	{
       
   545 	qDebug()<<"Inside SmfTransportManagerUtil::networkReplyFinished()";
       
   546 	qDebug()<<"QNetworkReply = "<<(long)aNetworkReply;
       
   547 	qDebug()<<"QNetworkReply error code = "<<aNetworkReply->error();
       
   548 	
   467 	// remove the QNetworkReply instance from the list of outstanding transactions
   549 	// remove the QNetworkReply instance from the list of outstanding transactions
   468 	int index = m_activeNetwReplyList.indexOf(aNetworkReply);
   550 	int index = m_activeNetwReplyList.indexOf(aNetworkReply);
   469 	if( index >= 0 )
   551 	if( index >= 0 )
   470 		m_activeNetwReplyList.removeAt(index);
   552 		m_activeNetwReplyList.removeAt(index);
   471 	
   553 	
   472 	// indicate the result of transaction to the plugin manager
   554 	// indicate the result of transaction to the plugin manager
   473 	SmfTransportResult trResult;
   555 	SmfTransportResult trResult;
   474 	convertErrorType(aNetworkReply->error(), trResult);
   556 	convertErrorType(aNetworkReply->error(), trResult);
   475 	
   557 	
   476 	// Store the data received
   558 	// Read the response from the device
       
   559 	QByteArray response = aNetworkReply->readAll();
       
   560 	qDebug()<<"Response size is = "<<response.size();
       
   561 #ifdef DETAILEDDEBUGGING
       
   562 	qDebug()<<"Response is = "<<QString(response);
       
   563 #endif
       
   564 
       
   565 	// Store the number of bytes of data received
   477 	bool converted = false;
   566 	bool converted = false;
   478 	quint64 data = m_settings->value("Received Data").toULongLong(&converted);
   567 	quint64 data = m_settings->value("Received Data").toULongLong(&converted);
   479 	if(converted)
   568 	if(converted)
   480 		{
   569 		{
   481 		data += aNetworkReply->readBufferSize();
   570 		data += response.size();
   482 		m_settings->setValue("Received Data", data);
   571 		m_settings->setValue("Received Data", data);
   483 		}
   572 		}
   484 
   573 
   485 	int error = 0;
   574 	SmfError error = SmfUnknownError;
   486 	QByteArray *arr = NULL;
   575 	QByteArray *arr = NULL;
   487 	
   576 	
   488 	// Read the response from the device
   577 #ifdef DETAILEDDEBUGGING
   489 	QByteArray response = aNetworkReply->readAll();
   578 	foreach(QByteArray arr, aNetworkReply->rawHeaderList())
       
   579 			qDebug()<<QString(arr)<<" = "<<QString(aNetworkReply->rawHeader(arr));
       
   580 #endif
   490 	
   581 	
   491 	// Check if the http response header has the raw header "Content-Encoding:gzip"
   582 	// Check if the http response header has the raw header "Content-Encoding:gzip"
   492 	// If so, inflate the gzip deflated data
   583 	bool gzipEncoded = false;
   493 	QByteArray headerKey("Content-Encoding");
   584 	QByteArray headerKey("Content-Encoding");
   494 	if(aNetworkReply->hasRawHeader(headerKey))
   585 	if(aNetworkReply->hasRawHeader(headerKey))
   495 		{
   586 		{
   496 		SmfPluginManager::getInstance()->server()->writeLog("Response is gzip encoded!!!");
   587 		qDebug()<<"Content-Encoding:"<<QString(aNetworkReply->rawHeader(headerKey));
       
   588 		
       
   589 		// If http response header has the raw header "Content-Encoding:gzip"
   497 		if( "gzip" == QString(aNetworkReply->rawHeader(headerKey)))
   590 		if( "gzip" == QString(aNetworkReply->rawHeader(headerKey)))
       
   591 			{
       
   592 			QByteArray firstByte(1, response[0]);
       
   593 			QByteArray secondByte(1, response[1]);
       
   594 			
       
   595 #ifdef DETAILEDDEBUGGING
       
   596 			qDebug()<<"first byte : "<<QString(firstByte.toHex());
       
   597 			qDebug()<<"second byte : "<<QString(secondByte.toHex());
       
   598 #endif
       
   599 			
       
   600 			// And the header says the response is a valid gzip data. 
       
   601 			// If so, inflate the gzip deflated data
       
   602 			if((QString("1f") == QString(firstByte.toHex())) && (QString("8b") == QString(secondByte.toHex())) )
       
   603 				{
       
   604 				gzipEncoded = true;
       
   605 				qDebug()<<"Response is gzip encoded!!!";
   498 			arr = inflateResponse(response, error);
   606 			arr = inflateResponse(response, error);
       
   607 				if(!arr)
       
   608 					trResult = SmfTransportOpGzipError;
       
   609 			}
   499 		else
   610 		else
   500 			SmfPluginManager::getInstance()->server()->writeLog("Unsupported encoding format!!! - error");
   611 			{
   501 		}
   612 				qDebug()<<"Header says gzip encoded, but response is not a valid data, so ignoring the header!!!";
       
   613 				arr = new QByteArray(response);
       
   614 				}
       
   615 			}
       
   616 		else
       
   617 			{
       
   618 			qDebug()<<"Unsupported encoding format!!! - error";
       
   619 			trResult = SmfTransportOpUnsupportedContentEncodingFormat;
       
   620 			}
       
   621 		}
       
   622 	
   502 	else
   623 	else
   503 		SmfPluginManager::getInstance()->server()->writeLog("Response is not gzip encoded");
   624 		{
   504 	
   625 		qDebug()<<"Response is not gzip encoded";
       
   626 		arr = new QByteArray(response);
       
   627 		}
       
   628 	
       
   629 	// Write the uncompressed data to a file
       
   630 #ifdef DETAILEDDEBUGGING
       
   631 	if(arr)
       
   632 		{
       
   633 		QFile file1("c:\\data\\networkResponse.txt");
       
   634 		if (!file1.open(QIODevice::WriteOnly))
       
   635 			qDebug()<<"File to write the network response could not be opened";
       
   636 		file1.write(*arr);
       
   637 		file1.close();
       
   638 		}
       
   639 #endif
   505 
   640 
   506 	SmfPluginManager::getInstance()->responseAvailable ( trResult, aNetworkReply, arr );
   641 	SmfPluginManager::getInstance()->responseAvailable ( trResult, aNetworkReply, arr );
   507 	}
   642 	}
   508 
   643 
   509 
   644 
   510 /**
   645 /**
   511  * Method to deflate a gzipped network response. Once this method is called, 
   646  * Method to inflate a gzipped network response.
   512  * QNetworkReply internal buffer for holding network response is emptied.
   647  * @param aResponse The QByteArray holding the gzip encoded data
   513  * @param aResponse The QByteArray instance holding the gzip encoded data
       
   514  * @param aError Argument indicating error
   648  * @param aError Argument indicating error
   515  * @return a QByteArray* containing the deflated data. If deflating fails, 
   649  * @return a QByteArray* containing the inflated data. If inflation fails, 
   516  * the encoded data itself without deflation is returned.
   650  * NULL is returned
   517  */
   651  */
   518 QByteArray* SmfTransportManagerUtil::inflateResponse ( QByteArray &aResponse, int& aError )
   652 QByteArray* SmfTransportManagerUtil::inflateResponse ( QByteArray &aResponse, SmfError& aError )
   519 	{
   653 	{
   520 	Q_UNUSED(aError)
   654 	qDebug()<<"Inside SmfTransportManagerUtil::inflateResponse()";
       
   655 	
   521 	// Read the response from the device
   656 	// Read the response from the device
   522 	SmfPluginManager::getInstance()->server()->writeLog("Encoded response content = "+QString(aResponse.toHex()));
   657 	qDebug()<<"Encoded response content size = "<<aResponse.size();
   523 	SmfPluginManager::getInstance()->server()->writeLog("Encoded response content size = "+QString::number(aResponse.size(), 10));
   658 	
       
   659 	if(aResponse.size() <= 0)
       
   660 		{
       
   661 		aError = SmfTMUnknownContentError;
       
   662 		return NULL;
       
   663 		}
   524 	
   664 	
   525 	// Get the uncompressed size of the response (last 4 bytes of the compressed data as per GZIP format spec)
   665 	// Get the uncompressed size of the response (last 4 bytes of the compressed data as per GZIP format spec)
   526 	QByteArray sizeStr;
   666 	QByteArray sizeStr;
   527 	for(int count = 1 ; count <= 4 ; count++)
   667 	for(int count = 1 ; count <= 4 ; count++)
   528 		sizeStr.append(aResponse[aResponse.size()-count]);
   668 		sizeStr.append(aResponse[aResponse.size()-count]);
   529 	SmfPluginManager::getInstance()->server()->writeLog("Size string as a string = "+QString(sizeStr.toHex()));
   669 	qDebug()<<"Size string as a string = "<<QString(sizeStr.toHex());
   530 	bool ok = false;
   670 	bool ok = false;
   531 	int uncomSize = sizeStr.toHex().toInt(&ok, 16);
   671 	int uncomSize = sizeStr.toHex().toInt(&ok, 16);
   532 	SmfPluginManager::getInstance()->server()->writeLog("Size of uncompressed data = "+QString::number(uncomSize, 10));
   672 	qDebug()<<"Size of uncompressed data = "<<uncomSize;
   533 
   673 
   534 	// Initialize the data required for zlib method call
   674 	// Initialize the data required for zlib method call
   535 	z_stream stream;
   675 	z_stream stream;
   536 	unsigned char *out = new unsigned char[uncomSize];
   676 	unsigned char *out = new unsigned char[uncomSize];
   537 	if(out)
   677 	if(out)
   538 		SmfPluginManager::getInstance()->server()->writeLog("Memory allocated for output buffer");
   678 		qDebug()<<"Memory allocated for output buffer";
   539 	else
   679 	else
   540 		{
   680 		{
   541 		//Error
   681 		//Error
   542 		SmfPluginManager::getInstance()->server()->writeLog("Memory not allocated for output buffer!!!");
   682 		qDebug()<<"Memory not allocated for output buffer!!!";
       
   683 		aError = SmfMemoryAllocationFailure;
   543 		return NULL;
   684 		return NULL;
   544 		}
   685 		}
   545 	stream.zalloc = Z_NULL;
   686 	stream.zalloc = Z_NULL;
   546 	stream.zfree = Z_NULL;
   687 	stream.zfree = Z_NULL;
   547 	stream.opaque = Z_NULL;
   688 	stream.opaque = Z_NULL;
   548 	stream.avail_in = 0;
   689 	stream.avail_in = 0;
   549 	stream.next_in = Z_NULL;
   690 	stream.next_in = Z_NULL;
   550 	
   691 	
   551 	int ret = inflateInit2(&stream, 16+MAX_WBITS);
   692 	int ret = inflateInit2(&stream, 16+MAX_WBITS);
   552 	SmfPluginManager::getInstance()->server()->writeLog("return value of inflateInit2() = "+QString::number(ret, 10));
   693 	qDebug()<<"return value of inflateInit2() = "<<ret;
   553 	if(Z_OK != ret)
   694 	if(Z_OK != ret)
   554 		{
   695 		{
   555 		SmfPluginManager::getInstance()->server()->writeLog("Error in inflateInit2, returning...");
   696 		qDebug()<<"Error in inflateInit2, returning...";
   556 		delete[] out;
   697 		delete[] out;
       
   698 		if(Z_STREAM_ERROR == ret)
       
   699 			aError = SmfTMGzipStreamError;
       
   700 		else if(Z_MEM_ERROR == ret)
       
   701 			aError = SmfTMGzipMemoryError;
       
   702 		else if(Z_DATA_ERROR == ret)
       
   703 			aError = SmfTMGzipDataError;
       
   704 		else
       
   705 			aError = SmfUnknownError;
   557 		return NULL;
   706 		return NULL;
   558 		}
   707 		}
   559 
   708 
   560 	// Initialize the data required for inflate() method call
   709 	// Initialize the data required for inflate() method call
   561 	stream.avail_in = aResponse.size();
   710 	stream.avail_in = aResponse.size();
   562 	stream.next_in = (unsigned char *)aResponse.data();
   711 	stream.next_in = (unsigned char *)aResponse.data();
   563 	stream.avail_out = uncomSize;
   712 	stream.avail_out = uncomSize;
   564 	stream.next_out = out;
   713 	stream.next_out = out;
   565 	
   714 	
   566 	ret = inflate(&stream, Z_NO_FLUSH);
   715 	ret = inflate(&stream, Z_NO_FLUSH);
   567 	SmfPluginManager::getInstance()->server()->writeLog("return value of inflate() = "+QString::number(ret, 10));
   716 	qDebug()<<"return value of inflate() = "<<ret;
   568 	
   717 	
   569 	switch (ret) 
   718 	switch (ret) 
   570 		{
   719 		{
   571 		// fall through
   720 		// fall through
   572 		case Z_NEED_DICT:
   721 		case Z_NEED_DICT:
   573 		case Z_DATA_ERROR:
   722 		case Z_DATA_ERROR:
   574 		case Z_MEM_ERROR:
   723 		case Z_MEM_ERROR:
   575 			{
   724 			{
   576 			(void)inflateEnd(&stream);
   725 			(void)inflateEnd(&stream);
   577 			delete[] out;
   726 			delete[] out;
       
   727 			aError = SmfTMGzipDataError;
   578 			return NULL;
   728 			return NULL;
   579 			}
   729 			}
   580 		}
   730 		}
   581 
   731 
   582 	SmfPluginManager::getInstance()->server()->writeLog("total bytes read so far = "+QString::number(stream.total_in, 10));
   732 	qDebug()<<"total bytes read so far = "<<stream.total_in;
   583 	SmfPluginManager::getInstance()->server()->writeLog("total bytes output so far = "+QString::number(stream.total_out, 10));
   733 	qDebug()<<"total bytes output so far = "<<stream.total_out;
   584 
   734 
   585 	// Write the inflated data to a QByteArray
   735 	// Write the inflated data to a QByteArray
   586 	QByteArray *uncompressedData = new QByteArray((char *)out);
   736 	QByteArray *uncompressedData = new QByteArray((char *)out, stream.total_out);
   587 	delete[] out;
   737 	delete[] out;
   588 	
   738 	
   589 	// If there is some unwanted data at the end of uncompressed data, chop them
   739 	// If there is some unwanted data at the end of uncompressed data, chop them
   590 	int chopLength = uncompressedData->size() - uncomSize;
   740 	int chopLength = uncompressedData->size() - uncomSize;
   591 	SmfPluginManager::getInstance()->server()->writeLog("uncompressedData before chopping = "+QString(uncompressedData->toHex()));
   741 	qDebug()<<"old size of uncompressed data = "<<uncompressedData->size();
   592 	SmfPluginManager::getInstance()->server()->writeLog("old size of uncompressed data = "+QString::number(uncompressedData->size(), 10));
       
   593 	uncompressedData->chop(chopLength);
   742 	uncompressedData->chop(chopLength);
   594 	SmfPluginManager::getInstance()->server()->writeLog("new size of uncompressed data = "+QString::number(uncompressedData->size(), 10));
   743 	qDebug()<<"new size of uncompressed data = "<<uncompressedData->size();
   595 	SmfPluginManager::getInstance()->server()->writeLog("uncompressedData after chopping = "+QString(uncompressedData->toHex()));
       
   596 
       
   597 	// Write the uncompressed data to a file
       
   598 	QFile file1("c:\\data\\uncompressedflickrresponse.txt");
       
   599 	if (!file1.open(QIODevice::WriteOnly))
       
   600 		SmfPluginManager::getInstance()->server()->writeLog("File to write the uncompressed response could not be opened");
       
   601 	file1.write(uncompressedData->data());
       
   602 	file1.close();
       
   603 	SmfPluginManager::getInstance()->server()->writeLog("uncompressed data written to c:\\data\\uncompressedflickrresponse.txt");
       
   604 	
   744 	
   605 	return uncompressedData;
   745 	return uncompressedData;
   606 	}
   746 	}
       
   747 
       
   748 
       
   749 /**
       
   750  * Method to deflate a network request to gzip format.
       
   751  * @param aResponse The QByteArray that has to be gzipped
       
   752  * @param aError Argument indicating error
       
   753  * @return a QByteArray* containing the deflated data. If deflation fails, 
       
   754  * NULL is returned
       
   755  */
       
   756 QByteArray* SmfTransportManagerUtil::deflateRequest( QByteArray &aResponse, SmfError& aError )
       
   757 	{
       
   758 	qDebug()<<"Inside SmfTransportManagerUtil::deflateRequest()";
       
   759 	
       
   760 	if(aResponse.size() <= 0)
       
   761 		{
       
   762 		aError = SmfTMUnknownContentError;
       
   763 		return NULL;
       
   764 		}
       
   765 	
       
   766 	// Initialize the data required for zlib initialize method call
       
   767 	z_stream stream;
       
   768 	stream.zalloc = Z_NULL;
       
   769 	stream.zfree = Z_NULL;
       
   770 	stream.opaque = Z_NULL;
       
   771 	stream.next_in = (unsigned char *)aResponse.data();
       
   772 	stream.avail_in = aResponse.size();
       
   773 	stream.total_out = 0;
       
   774 
       
   775 	int level = Z_DEFAULT_COMPRESSION;
       
   776 	int method = Z_DEFLATED;
       
   777 	int windowBits = 16+MAX_WBITS;
       
   778 	int mem_level = 8;
       
   779 	int strategy = Z_DEFAULT_STRATEGY;
       
   780 	
       
   781 	// Call deflateInit2 for gzip compression initialization
       
   782 	int initError = deflateInit2(&stream, level, method, windowBits, mem_level, strategy);
       
   783 	qDebug()<<"Return value of deflateInit2() = "<<initError;
       
   784 	qDebug()<<"Error msg if any = "<<QString(zError(initError));
       
   785 	
       
   786 	if(Z_OK != initError)
       
   787 		{
       
   788 		qDebug()<<"deflateInit2() failed, returning error = "<<QString(zError(initError));
       
   789 		
       
   790 		if(Z_STREAM_ERROR == initError)
       
   791 			aError = SmfTMGzipStreamError;
       
   792 		else if(Z_MEM_ERROR == initError)
       
   793 			aError = SmfTMGzipMemoryError;
       
   794 		else if(Z_DATA_ERROR == initError)
       
   795 			aError = SmfTMGzipDataError;
       
   796 		else
       
   797 			aError = SmfUnknownError;
       
   798 		return NULL;
       
   799 		}
       
   800 
       
   801 	// Initialize the data required for inflate() method call
       
   802 	int out_size = (int)((aResponse.size()*1.01)+17);
       
   803 	unsigned char *out = new unsigned char[out_size];
       
   804 	if(out)
       
   805 		{
       
   806 		qDebug()<<"Memory allocated for output buffer with size = "<<out_size;
       
   807 		memset(out, 0, out_size);
       
   808 		}
       
   809 	else
       
   810 		{
       
   811 		//Error
       
   812 		qDebug()<<"Memory not allocated for output buffer!!!";
       
   813 		
       
   814 		deflateEnd(&stream);
       
   815 		aError = SmfTMGzipDataError;
       
   816 		return NULL;
       
   817 		}
       
   818 
       
   819 	stream.next_out = out;
       
   820 	stream.avail_out = out_size;
       
   821 	
       
   822 #ifdef DETAILEDDEBUGGING
       
   823 	qDebug()<<"Before calling deflate()";
       
   824 	qDebug()<<"next_in = "<<(long)stream.next_in;
       
   825 	qDebug()<<"avail_in = "<<stream.avail_in;
       
   826 	qDebug()<<"next_out = "<<(long)stream.next_out;
       
   827 	qDebug()<<"avail_out = "<<stream.avail_out;
       
   828 	qDebug()<<"total_in = "<<stream.total_in;
       
   829 	qDebug()<<"total_out = "<<stream.total_out;
       
   830 #endif
       
   831 	
       
   832 	int deflateStatus;
       
   833 	do
       
   834 		{
       
   835 		stream.next_out = out + stream.total_out;
       
   836 		stream.avail_out = out_size - stream.total_out;
       
   837 		
       
   838 		deflateStatus = deflate(&stream, Z_FINISH);
       
   839 		qDebug()<<"Return value of deflate() is = "<<deflateStatus;
       
   840 		qDebug()<<"Error msg if any = "<<QString(zError(deflateStatus));
       
   841 		
       
   842 #ifdef DETAILEDDEBUGGING
       
   843 		qDebug()<<"avail_in = "<<stream.avail_in;
       
   844 		qDebug()<<"avail_out = "<<stream.avail_out;
       
   845 		qDebug()<<"total_in = "<<stream.total_in;
       
   846 		qDebug()<<"total_out = "<<stream.total_out;
       
   847 #endif
       
   848 		
       
   849 		}while(Z_OK == deflateStatus);
       
   850 	
       
   851      if (Z_STREAM_END != deflateStatus)  
       
   852      {  
       
   853          QString errorMsg;  
       
   854          switch (deflateStatus)  
       
   855          {  
       
   856              case Z_ERRNO:  
       
   857                  errorMsg.append("Error occured while reading file.");
       
   858                  aError = SmfUnknownError;
       
   859                  break;  
       
   860              case Z_STREAM_ERROR:  
       
   861                  errorMsg.append("The stream state was inconsistent (e.g., next_in or next_out was NULL).");
       
   862                  aError = SmfTMGzipStreamError;
       
   863                  break;  
       
   864              case Z_DATA_ERROR:  
       
   865                  errorMsg.append("The deflate data was invalid or incomplete.");
       
   866                  aError = SmfTMGzipDataError;
       
   867                  break;  
       
   868              case Z_MEM_ERROR:  
       
   869                  errorMsg.append("Memory could not be allocated for processing.");
       
   870                  aError = SmfTMGzipMemoryError;
       
   871                  break;  
       
   872              case Z_BUF_ERROR:  
       
   873                  errorMsg.append("Ran out of output buffer for writing compressed bytes.");
       
   874                  aError = SmfTMGzipMemoryError;
       
   875                  break;  
       
   876              case Z_VERSION_ERROR:  
       
   877                  errorMsg.append("The version of zlib.h and the version of the library linked do not match.");
       
   878                  aError = SmfUnknownError;
       
   879                  break;  
       
   880              default:  
       
   881                  errorMsg.append("Unknown error code.");
       
   882                  aError = SmfUnknownError;
       
   883          }  
       
   884    
       
   885          qDebug()<<"Error string for deflate() is = "<<errorMsg;
       
   886          
       
   887          // Free data structures that were dynamically created for the stream.  
       
   888          deflateEnd(&stream);  
       
   889    
       
   890          return NULL;  
       
   891      } 
       
   892 			
       
   893      int retVal = deflateEnd(&stream);
       
   894 	qDebug()<<"Return value of deflateEnd() = "<<retVal;
       
   895 	qDebug()<<"Error msg if any = "<<QString(zError(retVal));
       
   896 
       
   897 #ifdef DETAILEDDEBUGGING
       
   898 	/*QByteArray byteArray;
       
   899 	for (int i=0; i<stream.total_out;i++)
       
   900 		{
       
   901 		QString str(out[i]);
       
   902 		byteArray.clear();
       
   903 		byteArray.append(str.toAscii());
       
   904 		qDebug()<<QString("out[%1] = '%2'").arg(i).arg(QString(byteArray.toHex()));
       
   905 		}*/
       
   906 #endif
       
   907 	
       
   908 	// Write the inflated data to a QByteArray
       
   909 	QByteArray *compressedData = new QByteArray((char*)out, stream.total_out);
       
   910 	delete[] out;
       
   911 	qDebug()<<"Number of bytes of compressed data = "<<compressedData->size();
       
   912 	
       
   913 #ifdef DETAILEDDEBUGGING
       
   914 	qDebug()<<"compressed data = "<<QString(compressedData->toHex());
       
   915 #endif
       
   916 	
       
   917 	return compressedData;
       
   918 	}
       
   919 
       
   920 
   607 
   921 
   608 /**
   922 /**
   609  * Method called when the QNetworkReply detects an error in processing.
   923  * Method called when the QNetworkReply detects an error in processing.
   610  * @param aError The QNetworkReply error code 
   924  * @param aError The QNetworkReply error code 
   611  */
   925  */
   612 void SmfTransportManagerUtil::networkReplyError ( QNetworkReply::NetworkError aError )
   926 void SmfTransportManagerUtil::networkReplyError ( QNetworkReply::NetworkError aError )
   613 	{
   927 	{
       
   928 	qDebug()<<"Inside SmfTransportManagerUtil::networkReplyError()";
       
   929 	
   614 	// indicate the error to the plugin manager
   930 	// indicate the error to the plugin manager
   615 	SmfTransportResult trResult;
   931 	SmfTransportResult trResult;
   616 	convertErrorType(aError, trResult);
   932 	convertErrorType(aError, trResult);
   617 	
   933 	
   618 	SmfPluginManager::getInstance()->responseAvailable ( trResult, NULL, NULL );
   934 	SmfPluginManager::getInstance()->responseAvailable ( trResult, NULL, NULL );