smf/smfservermodule/smfserver/transportmgr/smftransportmanagerutil.cpp
changeset 7 be09cf1f39dd
equal deleted inserted replaced
6:c39a6cfd1fb9 7:be09cf1f39dd
       
     1 /**
       
     2  * Copyright (c) 2010 Sasken Communication Technologies Ltd.
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the "Eclipse Public License v1.0" 
       
     6  * which accompanies  this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html"
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Chandradeep Gandhi, Sasken Communication Technologies Ltd - Initial contribution
       
    11  *
       
    12  * Contributors:
       
    13  * Manasij Roy, Nalina Hariharan
       
    14  * 
       
    15  * Description:
       
    16  * The Transport Manager Utility class provides the http/https transaction 
       
    17  * methods for the smf framework
       
    18  *
       
    19  */
       
    20 
       
    21 // Include files
       
    22 #include <QNetworkAccessManager>
       
    23 #include <QNetworkReply>
       
    24 #include <QSettings>
       
    25 #include <zlib.h>
       
    26 // For proxy settings on emulator only - REMOVE for device
       
    27 #include <QNetworkProxy>
       
    28 
       
    29 #include "smftransportmanagerutil.h"
       
    30 #include "smfpluginmanager.h"
       
    31 
       
    32 // Static Global constants
       
    33 static const char kSmfUserAgent[] = "SmfFrameWork";
       
    34 static const char kSmfCacheControl[] = "no-cache";
       
    35 static const char kSmfAcceptEncoding[] = "gzip";
       
    36 
       
    37 // Static data initialisation
       
    38 SmfTransportManagerUtil* SmfTransportManagerUtil::m_myInstance = NULL;
       
    39 
       
    40 /**
       
    41  * Method to get the instance of SmfTransportManagerUtil class
       
    42  * @return The instance of SmfTransportManager class
       
    43  */
       
    44 SmfTransportManagerUtil* SmfTransportManagerUtil::getInstance ( )
       
    45 	{
       
    46 	if(NULL == m_myInstance)
       
    47 		m_myInstance = new SmfTransportManagerUtil( );
       
    48 	return m_myInstance;
       
    49 	}
       
    50 
       
    51 /**
       
    52  * Constructor with default argument
       
    53  * @param aParent The parent object
       
    54  */
       
    55 SmfTransportManagerUtil::SmfTransportManagerUtil ( )
       
    56 		: m_networkAccessManager ( this )
       
    57 	{
       
    58 	// Storage of settings (Only data usage as of now)
       
    59 	m_settings = new QSettings("Sasken", "SmfTransportManager");
       
    60 
       
    61 	// Create the settings if no data is stored
       
    62 	if( !m_settings->contains("Sent Data") )
       
    63 		m_settings->setValue("Sent Data", 0);
       
    64 	if( !m_settings->contains("Received Data") )
       
    65 		m_settings->setValue("Received Data", 0);
       
    66 	}
       
    67 
       
    68 
       
    69 /**
       
    70  * Destructor
       
    71  */
       
    72 SmfTransportManagerUtil::~SmfTransportManagerUtil ( )
       
    73 	{
       
    74 	m_activeNetwReplyList.clear();
       
    75 	
       
    76 	if(m_settings)
       
    77 		delete m_settings;
       
    78 	
       
    79 	if(m_myInstance)
       
    80 		delete m_myInstance;
       
    81 	}
       
    82 
       
    83 
       
    84 /**
       
    85  * Method that does a http GET request. Returns the unique QNetworkReply
       
    86  * instance for this transaction to the plugin manager to trace the 
       
    87  * proper response.
       
    88  * @param aRequest The request formed by the plugin
       
    89  * @param aUrlList The list of accessible Urls for this plugin
       
    90  * @param aSOPCompliant [out] Output parameter indicating Same Origin 
       
    91  * Policy Compliance. Contains true if the request complies to the policy,
       
    92  * else false. If it is false, the network request will not performed
       
    93  * @return The unique QNetworkReply instance for this transaction
       
    94  */
       
    95 QNetworkReply* SmfTransportManagerUtil::get ( 
       
    96 		QNetworkRequest &aRequest,
       
    97 		const QList<QUrl> &aUrlList,
       
    98 		bool &aSOPCompliant )
       
    99 	{
       
   100 	bool found = false;
       
   101 	QString hostName = aRequest.url().host();
       
   102 	
       
   103 	// Same Origin Policy checking
       
   104 	// Check if the host name of the url in the request created by the 
       
   105 	// plugin is among the authorised url host name list
       
   106 	foreach(QUrl url, aUrlList)
       
   107 		{
       
   108 		if(0 == hostName.compare(url.host()))
       
   109 			{
       
   110 			found = true;
       
   111 			break;
       
   112 			}
       
   113 		}
       
   114 	
       
   115 	if(found)
       
   116 		{
       
   117 		aSOPCompliant = true;
       
   118 		
       
   119 		// Set header to accept gzip encoded data
       
   120 		aRequest.setRawHeader("Accept-encoding", kSmfAcceptEncoding);
       
   121 		
       
   122 		// Set the cache control
       
   123 		aRequest.setRawHeader("Cache-Control", kSmfCacheControl);
       
   124 
       
   125 		// Put the same user agent for all requests sent by Smf
       
   126 		aRequest.setRawHeader("User-Agent", kSmfUserAgent);
       
   127 		
       
   128 		QNetworkReply* reply = m_networkAccessManager.get(aRequest);
       
   129 	
       
   130 		connect(&m_networkAccessManager, SIGNAL(finished(QNetworkReply *)), 
       
   131 				this, SLOT(networkReplyFinished(QNetworkReply *)));
       
   132 		
       
   133 		connect(&m_networkAccessManager, SIGNAL(error(QNetworkReply::NetworkError)), 
       
   134 				this, SLOT(networkReplyError(QNetworkReply::NetworkError)));
       
   135 		
       
   136 		m_activeNetwReplyList.append(reply);
       
   137 		
       
   138 		return reply;
       
   139 		}
       
   140 	else
       
   141 		{
       
   142 		// Error :: SAME ORIGIN POLICY violation!!!
       
   143 		aSOPCompliant = false;
       
   144 		return NULL;
       
   145 		}
       
   146 	}
       
   147 
       
   148 
       
   149 /**
       
   150  * Method that does a http POST request. Returns the unique QNetworkReply
       
   151  * instance for this transaction to the plugin manager to trace the 
       
   152  * proper response.
       
   153  * @param aRequest The request formed by the plugin
       
   154  * @param aPostData The data to be posted via http POST request
       
   155  * @param aUrlList The list of accessible Urls for this plugin
       
   156  * @param aSOPCompliant [out] Output parameter indicating Same Origin 
       
   157  * Policy Compliance. Contains true if the request complies to the policy,
       
   158  * else false. If it is false, the network request will not performed
       
   159  * @return The unique QNetworkReply instance for this transaction
       
   160  */
       
   161 QNetworkReply* SmfTransportManagerUtil::post ( 
       
   162 		QNetworkRequest &aRequest, 
       
   163 		const QByteArray& aPostData,
       
   164 		const QList<QUrl> &aUrlList,
       
   165 		bool &aSOPCompliant )
       
   166 	{
       
   167 	bool found = false;
       
   168 	QString hostName = aRequest.url().host();
       
   169 	
       
   170 	// Same Origin Policy checking
       
   171 	// Check if the host name of the url in the request created by the 
       
   172 	// plugin is among the authorised url host name list
       
   173 	foreach(QUrl url, aUrlList)
       
   174 		{
       
   175 		if(0 == hostName.compare(url.host()))
       
   176 			{
       
   177 			found = true;
       
   178 			break;
       
   179 			}
       
   180 		}
       
   181 	
       
   182 	if(found)
       
   183 		{
       
   184 		aSOPCompliant = true;
       
   185 		
       
   186 		// Set the cache control
       
   187 		aRequest.setRawHeader("Cache-Control", kSmfCacheControl);
       
   188 		
       
   189 		// Put the same user agent for all requests sent by Smf
       
   190 		aRequest.setRawHeader("User-Agent", kSmfUserAgent);
       
   191 		
       
   192 		QNetworkReply* reply = m_networkAccessManager.post(aRequest, aPostData);
       
   193 	
       
   194 		connect(&m_networkAccessManager, SIGNAL(finished(QNetworkReply *)), 
       
   195 				this, SLOT(networkReplyFinished(QNetworkReply *)));
       
   196 		
       
   197 		connect(&m_networkAccessManager, SIGNAL(error(QNetworkReply::NetworkError)), 
       
   198 				this, SLOT(networkReplyError(QNetworkReply::NetworkError)));
       
   199 		
       
   200 		m_activeNetwReplyList.append(reply);
       
   201 		
       
   202 		return reply;
       
   203 		}
       
   204 	else
       
   205 		{
       
   206 		// Error :: SAME ORIGIN POLICY violation!!!
       
   207 		aSOPCompliant = false;
       
   208 		return NULL;
       
   209 		}
       
   210 	}
       
   211 
       
   212 
       
   213 /**
       
   214  * Method that does a http HEAD request. Returns the unique QNetworkReply
       
   215  * instance for this transaction to the plugin manager to trace the 
       
   216  * proper response.
       
   217  * @param aRequest The request formed by the plugin
       
   218  * @param aUrlList The list of accessible Urls for this plugin
       
   219  * @param aSOPCompliant [out] Output parameter indicating Same Origin 
       
   220  * Policy Compliance. Contains true if the request complies to the policy,
       
   221  * else false. If it is false, the network request will not performed
       
   222  * @return The unique QNetworkReply instance for this transaction
       
   223  */
       
   224 QNetworkReply* SmfTransportManagerUtil::head ( 
       
   225 		QNetworkRequest& aRequest,
       
   226 		const QList<QUrl> &aUrlList,
       
   227 		bool &aSOPCompliant )
       
   228 	{
       
   229 	bool found = false;
       
   230 	QString hostName = aRequest.url().host();
       
   231 
       
   232 	// Same Origin Policy checking
       
   233 	// Check if the host name of the url in the request created by the 
       
   234 	// plugin is among the authorised url host name list
       
   235 	foreach(QUrl url, aUrlList)
       
   236 		{
       
   237 		if(0 == hostName.compare(url.host()))
       
   238 			{
       
   239 			found = true;
       
   240 			break;
       
   241 			}
       
   242 		}
       
   243 	
       
   244 	if(found)
       
   245 		{
       
   246 		aSOPCompliant = true;
       
   247 		// Set the cache control
       
   248 		aRequest.setRawHeader("Cache-Control", kSmfCacheControl);
       
   249 		
       
   250 		// Put the same user agent for all requests sent by Smf
       
   251 		aRequest.setRawHeader("User-Agent", kSmfUserAgent);
       
   252 		
       
   253 		QNetworkReply* reply = m_networkAccessManager.head(aRequest);
       
   254 	
       
   255 		connect(&m_networkAccessManager, SIGNAL(finished(QNetworkReply *)), 
       
   256 				this, SLOT(networkReplyFinished(QNetworkReply *)));
       
   257 		
       
   258 		connect(&m_networkAccessManager, SIGNAL(error(QNetworkReply::NetworkError)), 
       
   259 				this, SLOT(networkReplyError(QNetworkReply::NetworkError)));
       
   260 		
       
   261 		m_activeNetwReplyList.append(reply);
       
   262 		
       
   263 		return reply;
       
   264 		}
       
   265 	else
       
   266 		{
       
   267 		// Error :: SAME ORIGIN POLICY violation!!!
       
   268 		aSOPCompliant = false;
       
   269 		return NULL;
       
   270 		}
       
   271 	}
       
   272 
       
   273 
       
   274 /**
       
   275  * Method that does a http PUT request. Returns the unique QNetworkReply
       
   276  * instance for this transaction to the plugin manager to trace the 
       
   277  * proper response.
       
   278  * @param aRequest The request formed by the plugin
       
   279  * @param aPostData The data to be posted via http PUT request
       
   280  * @param aUrlList The list of accessible Urls for this plugin
       
   281  * @param aSOPCompliant [out] Output parameter indicating Same Origin 
       
   282  * Policy Compliance. Contains true if the request complies to the policy,
       
   283  * else false. If it is false, the network request will not performed
       
   284  * @return The unique QNetworkReply instance for this transaction
       
   285  */
       
   286 QNetworkReply* SmfTransportManagerUtil::put ( 
       
   287 		QNetworkRequest &aRequest,
       
   288 		const QByteArray& aPostData,
       
   289 		const QList<QUrl> &aUrlList,
       
   290 		bool &aSOPCompliant )
       
   291 	{
       
   292 	bool found = false;
       
   293 	QString hostName = aRequest.url().host();
       
   294 
       
   295 	// Same Origin Policy checking
       
   296 	// Check if the host name of the url in the request created by the 
       
   297 	// plugin is among the authorised url host name list
       
   298 	foreach(QUrl url, aUrlList)
       
   299 		{
       
   300 		if(0 == hostName.compare(url.host()))
       
   301 			{
       
   302 			found = true;
       
   303 			break;
       
   304 			}
       
   305 		}
       
   306 	
       
   307 	if(found)
       
   308 		{
       
   309 		aSOPCompliant = true;
       
   310 		// Set the cache control
       
   311 		aRequest.setRawHeader("Cache-Control", kSmfCacheControl);
       
   312 		
       
   313 		// Put the same user agent for all requests sent by Smf
       
   314 		aRequest.setRawHeader("User-Agent", kSmfUserAgent);
       
   315 		
       
   316 		QNetworkReply* reply = m_networkAccessManager.put(aRequest, aPostData);
       
   317 	
       
   318 		connect(&m_networkAccessManager, SIGNAL(finished(QNetworkReply *)), 
       
   319 				this, SLOT(networkReplyFinished(QNetworkReply *)));
       
   320 		
       
   321 		connect(&m_networkAccessManager, SIGNAL(error(QNetworkReply::NetworkError)), 
       
   322 				this, SLOT(networkReplyError(QNetworkReply::NetworkError)));
       
   323 		
       
   324 		m_activeNetwReplyList.append(reply);
       
   325 		
       
   326 		return reply;
       
   327 		}
       
   328 	else
       
   329 		{
       
   330 		// Error :: SAME ORIGIN POLICY violation!!!
       
   331 		aSOPCompliant = false;
       
   332 		return NULL;
       
   333 		}
       
   334 	}
       
   335 
       
   336 
       
   337 /**
       
   338  * Method that does a http DELETE request. Returns the unique QNetworkReply
       
   339  * instance for this transaction to the plugin manager to trace the 
       
   340  * proper response.
       
   341  * @param aRequest The request formed by the plugin
       
   342  * @param aUrlList The list of accessible Urls for this plugin
       
   343  * @param aSOPCompliant [out] Output parameter indicating Same Origin 
       
   344  * Policy Compliance. Contains true if the request complies to the policy,
       
   345  * else false. If it is false, the network request will not performed
       
   346  * @return The unique QNetworkReply instance for this transaction
       
   347  */
       
   348 QNetworkReply* SmfTransportManagerUtil::deleteResource ( 
       
   349 		QNetworkRequest &aRequest,
       
   350 		const QList<QUrl> &aUrlList,
       
   351 		bool &aSOPCompliant )
       
   352 	{
       
   353 	bool found = false;
       
   354 	QString hostName = aRequest.url().host();
       
   355 
       
   356 	// Same Origin Policy checking
       
   357 	// Check if the host name of the url in the request created by the 
       
   358 	// plugin is among the authorised url host name list
       
   359 	foreach(QUrl url, aUrlList)
       
   360 		{
       
   361 		if(0 == hostName.compare(url.host()))
       
   362 			{
       
   363 			found = true;
       
   364 			break;
       
   365 			}
       
   366 		}
       
   367 	
       
   368 	if(found)
       
   369 		{
       
   370 		aSOPCompliant = true;
       
   371 		// Set the cache control
       
   372 		aRequest.setRawHeader("Cache-Control", kSmfCacheControl);
       
   373 		
       
   374 		// Put the same user agent for all requests sent by Smf
       
   375 		aRequest.setRawHeader("User-Agent", kSmfUserAgent);
       
   376 		
       
   377 		QNetworkReply* reply = m_networkAccessManager.deleteResource(aRequest);
       
   378 	
       
   379 		connect(&m_networkAccessManager, SIGNAL(finished(QNetworkReply *)), 
       
   380 				this, SLOT(networkReplyFinished(QNetworkReply *)));
       
   381 		
       
   382 		connect(&m_networkAccessManager, SIGNAL(error(QNetworkReply::NetworkError)), 
       
   383 				this, SLOT(networkReplyError(QNetworkReply::NetworkError)));
       
   384 		
       
   385 		m_activeNetwReplyList.append(reply);
       
   386 		
       
   387 		return reply;
       
   388 		}
       
   389 	else
       
   390 		{
       
   391 		// Error :: SAME ORIGIN POLICY violation!!!
       
   392 		aSOPCompliant = false;
       
   393 		return NULL;
       
   394 		}
       
   395 	}
       
   396 
       
   397 
       
   398 /**
       
   399  * Method that cancels the service request by cancelling the current 
       
   400  * http transaction.
       
   401  * @param aCancelReply The QNetworkReply instance whose transaction 
       
   402  * has to be cancelled
       
   403  * @return Returns true for success, else false 
       
   404  */
       
   405 bool SmfTransportManagerUtil::cancelRequest ( 
       
   406 		QNetworkReply *aCancelReply )
       
   407 	{
       
   408 	// Checks if this transaction is running
       
   409 	if( aCancelReply->isRunning() )
       
   410 		{
       
   411 		// Aborts the running transaction
       
   412 		aCancelReply->abort();
       
   413 		}
       
   414 	// Close the connection
       
   415 	aCancelReply->close();
       
   416 	
       
   417 	int index = m_activeNetwReplyList.indexOf(aCancelReply);
       
   418 	if( index >= 0 )
       
   419 		m_activeNetwReplyList.removeAt(index);
       
   420 	
       
   421 	return true;
       
   422 	}
       
   423 
       
   424 
       
   425 
       
   426 /*
       
   427  * Method that is called whenever a new network configuration is added to the system.
       
   428  * @param aResult SmfTransportResult
       
   429  */
       
   430 void SmfTransportManagerUtil::configurationAdded ( const SmfTransportResult &aResult )
       
   431 	{
       
   432 	// Inform PM and plugins that a new IAP ia added, and hence resend the request
       
   433 	SmfPluginManager::getInstance()->responseAvailable ( aResult, NULL, NULL );
       
   434 	}
       
   435 
       
   436 /*
       
   437  * Method that is called when the state of the network configuration changes.
       
   438  * @param aResult SmfTransportResult
       
   439  */
       
   440 void SmfTransportManagerUtil::configurationChanged ( const SmfTransportResult &aResult )
       
   441 	{
       
   442 	// Inform PM and plugins that the IAP is changed, and hence resend the request
       
   443 	SmfPluginManager::getInstance()->responseAvailable ( aResult, NULL, NULL );
       
   444 	}
       
   445 
       
   446 /*
       
   447  * Method that is called when a configuration is about to be removed from the system.
       
   448  * The removed configuration is invalid but retains name and identifier.
       
   449  * @param aResult SmfTransportResult
       
   450  */
       
   451 void SmfTransportManagerUtil::configurationRemoved ( const SmfTransportResult &aResult )
       
   452 	{
       
   453 	// Inform PM and plugins that the IAP is to be deleted, and 
       
   454 	// hence resend the request
       
   455 	SmfPluginManager::getInstance()->responseAvailable ( aResult, NULL, NULL );
       
   456 
       
   457 	}
       
   458 
       
   459 
       
   460 /**
       
   461  * Method to indicate the http transaction has finished.
       
   462  * @param aNetworkReply The QNetworkReply instance for which the http 
       
   463  * transaction was made
       
   464  */
       
   465 void SmfTransportManagerUtil::networkReplyFinished ( QNetworkReply *aNetworkReply )
       
   466 	{
       
   467 	// remove the QNetworkReply instance from the list of outstanding transactions
       
   468 	int index = m_activeNetwReplyList.indexOf(aNetworkReply);
       
   469 	if( index >= 0 )
       
   470 		m_activeNetwReplyList.removeAt(index);
       
   471 	
       
   472 	// indicate the result of transaction to the plugin manager
       
   473 	SmfTransportResult trResult;
       
   474 	convertErrorType(aNetworkReply->error(), trResult);
       
   475 	
       
   476 	// Store the data received
       
   477 	bool converted = false;
       
   478 	quint64 data = m_settings->value("Received Data").toULongLong(&converted);
       
   479 	if(converted)
       
   480 		{
       
   481 		data += aNetworkReply->readBufferSize();
       
   482 		m_settings->setValue("Received Data", data);
       
   483 		}
       
   484 
       
   485 	int error = 0;
       
   486 	QByteArray *arr = NULL;
       
   487 	
       
   488 	// Read the response from the device
       
   489 	QByteArray response = aNetworkReply->readAll();
       
   490 	
       
   491 	// Check if the http response header has the raw header "Content-Encoding:gzip"
       
   492 	// If so, inflate the gzip deflated data
       
   493 	QByteArray headerKey("Content-Encoding");
       
   494 	if(aNetworkReply->hasRawHeader(headerKey))
       
   495 		{
       
   496 		SmfPluginManager::getInstance()->server()->writeLog("Response is gzip encoded!!!");
       
   497 		if( "gzip" == QString(aNetworkReply->rawHeader(headerKey)))
       
   498 			arr = inflateResponse(response, error);
       
   499 		else
       
   500 			SmfPluginManager::getInstance()->server()->writeLog("Unsupported encoding format!!! - error");
       
   501 		}
       
   502 	else
       
   503 		SmfPluginManager::getInstance()->server()->writeLog("Response is not gzip encoded");
       
   504 	
       
   505 
       
   506 	SmfPluginManager::getInstance()->responseAvailable ( trResult, aNetworkReply, arr );
       
   507 	}
       
   508 
       
   509 
       
   510 /**
       
   511  * Method to deflate a gzipped network response. Once this method is called, 
       
   512  * QNetworkReply internal buffer for holding network response is emptied.
       
   513  * @param aResponse The QByteArray instance holding the gzip encoded data
       
   514  * @param aError Argument indicating error
       
   515  * @return a QByteArray* containing the deflated data. If deflating fails, 
       
   516  * the encoded data itself without deflation is returned.
       
   517  */
       
   518 QByteArray* SmfTransportManagerUtil::inflateResponse ( QByteArray &aResponse, int& aError )
       
   519 	{
       
   520 	Q_UNUSED(aError)
       
   521 	// Read the response from the device
       
   522 	SmfPluginManager::getInstance()->server()->writeLog("Encoded response content = "+QString(aResponse.toHex()));
       
   523 	SmfPluginManager::getInstance()->server()->writeLog("Encoded response content size = "+QString::number(aResponse.size(), 10));
       
   524 	
       
   525 	// Get the uncompressed size of the response (last 4 bytes of the compressed data as per GZIP format spec)
       
   526 	QByteArray sizeStr;
       
   527 	for(int count = 1 ; count <= 4 ; count++)
       
   528 		sizeStr.append(aResponse[aResponse.size()-count]);
       
   529 	SmfPluginManager::getInstance()->server()->writeLog("Size string as a string = "+QString(sizeStr.toHex()));
       
   530 	bool ok = false;
       
   531 	int uncomSize = sizeStr.toHex().toInt(&ok, 16);
       
   532 	SmfPluginManager::getInstance()->server()->writeLog("Size of uncompressed data = "+QString::number(uncomSize, 10));
       
   533 
       
   534 	// Initialize the data required for zlib method call
       
   535 	z_stream stream;
       
   536 	unsigned char *out = new unsigned char[uncomSize];
       
   537 	if(out)
       
   538 		SmfPluginManager::getInstance()->server()->writeLog("Memory allocated for output buffer");
       
   539 	else
       
   540 		{
       
   541 		//Error
       
   542 		SmfPluginManager::getInstance()->server()->writeLog("Memory not allocated for output buffer!!!");
       
   543 		return NULL;
       
   544 		}
       
   545 	stream.zalloc = Z_NULL;
       
   546 	stream.zfree = Z_NULL;
       
   547 	stream.opaque = Z_NULL;
       
   548 	stream.avail_in = 0;
       
   549 	stream.next_in = Z_NULL;
       
   550 	
       
   551 	int ret = inflateInit2(&stream, 16+MAX_WBITS);
       
   552 	SmfPluginManager::getInstance()->server()->writeLog("return value of inflateInit2() = "+QString::number(ret, 10));
       
   553 	if(Z_OK != ret)
       
   554 		{
       
   555 		SmfPluginManager::getInstance()->server()->writeLog("Error in inflateInit2, returning...");
       
   556 		delete[] out;
       
   557 		return NULL;
       
   558 		}
       
   559 
       
   560 	// Initialize the data required for inflate() method call
       
   561 	stream.avail_in = aResponse.size();
       
   562 	stream.next_in = (unsigned char *)aResponse.data();
       
   563 	stream.avail_out = uncomSize;
       
   564 	stream.next_out = out;
       
   565 	
       
   566 	ret = inflate(&stream, Z_NO_FLUSH);
       
   567 	SmfPluginManager::getInstance()->server()->writeLog("return value of inflate() = "+QString::number(ret, 10));
       
   568 	
       
   569 	switch (ret) 
       
   570 		{
       
   571 		// fall through
       
   572 		case Z_NEED_DICT:
       
   573 		case Z_DATA_ERROR:
       
   574 		case Z_MEM_ERROR:
       
   575 			{
       
   576 			(void)inflateEnd(&stream);
       
   577 			delete[] out;
       
   578 			return NULL;
       
   579 			}
       
   580 		}
       
   581 
       
   582 	SmfPluginManager::getInstance()->server()->writeLog("total bytes read so far = "+QString::number(stream.total_in, 10));
       
   583 	SmfPluginManager::getInstance()->server()->writeLog("total bytes output so far = "+QString::number(stream.total_out, 10));
       
   584 
       
   585 	// Write the inflated data to a QByteArray
       
   586 	QByteArray *uncompressedData = new QByteArray((char *)out);
       
   587 	delete[] out;
       
   588 	
       
   589 	// If there is some unwanted data at the end of uncompressed data, chop them
       
   590 	int chopLength = uncompressedData->size() - uncomSize;
       
   591 	SmfPluginManager::getInstance()->server()->writeLog("uncompressedData before chopping = "+QString(uncompressedData->toHex()));
       
   592 	SmfPluginManager::getInstance()->server()->writeLog("old size of uncompressed data = "+QString::number(uncompressedData->size(), 10));
       
   593 	uncompressedData->chop(chopLength);
       
   594 	SmfPluginManager::getInstance()->server()->writeLog("new size of uncompressed data = "+QString::number(uncompressedData->size(), 10));
       
   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 	
       
   605 	return uncompressedData;
       
   606 	}
       
   607 
       
   608 /**
       
   609  * Method called when the QNetworkReply detects an error in processing.
       
   610  * @param aError The QNetworkReply error code 
       
   611  */
       
   612 void SmfTransportManagerUtil::networkReplyError ( QNetworkReply::NetworkError aError )
       
   613 	{
       
   614 	// indicate the error to the plugin manager
       
   615 	SmfTransportResult trResult;
       
   616 	convertErrorType(aError, trResult);
       
   617 	
       
   618 	SmfPluginManager::getInstance()->responseAvailable ( trResult, NULL, NULL );
       
   619 	}
       
   620 
       
   621 
       
   622 /**
       
   623  * Method to convert QNetworkReply Error to the type SmfTransportResult 
       
   624  * QNetworkRequest received before executing the web query.
       
   625  * @param aError The QNetworkReply Error
       
   626  * @param aResult [out] The SmfTransportResult error
       
   627  */
       
   628 void SmfTransportManagerUtil::convertErrorType( const QNetworkReply::NetworkError &aError, 
       
   629 		SmfTransportResult &aResult )
       
   630 	{
       
   631 	switch(aError)
       
   632 		{
       
   633 		case QNetworkReply::NoError:
       
   634 			aResult = SmfTransportOpNoError;
       
   635 			break;
       
   636 			
       
   637 		case QNetworkReply::ConnectionRefusedError:
       
   638 			aResult = SmfTransportOpConnectionRefusedError;
       
   639 			break;
       
   640 			
       
   641 		case QNetworkReply::RemoteHostClosedError:
       
   642 			aResult = SmfTransportOpRemoteHostClosedError;
       
   643 			break;
       
   644 			
       
   645 		case QNetworkReply::HostNotFoundError:
       
   646 			aResult = SmfTransportOpHostNotFoundError;
       
   647 			break;
       
   648 			
       
   649 		case QNetworkReply::TimeoutError:
       
   650 			aResult = SmfTransportOpTimeoutError;
       
   651 			break;
       
   652 			
       
   653 		case QNetworkReply::OperationCanceledError:
       
   654 			aResult = SmfTransportOpOperationCanceledError;
       
   655 			break;
       
   656 			
       
   657 		case QNetworkReply::SslHandshakeFailedError:
       
   658 			aResult = SmfTransportOpSslHandshakeFailedError;
       
   659 			break;
       
   660 			
       
   661 		case QNetworkReply::ProxyConnectionRefusedError:
       
   662 			aResult = SmfTransportOpProxyConnectionRefusedError;
       
   663 			break;
       
   664 			
       
   665 		case QNetworkReply::ProxyConnectionClosedError:
       
   666 			aResult = SmfTransportOpProxyConnectionClosedError;
       
   667 			break;
       
   668 			
       
   669 		case QNetworkReply::ProxyNotFoundError:
       
   670 			aResult = SmfTransportOpProxyNotFoundError;
       
   671 			break;
       
   672 			
       
   673 		case QNetworkReply::ProxyTimeoutError:
       
   674 			aResult = SmfTransportOpProxyTimeoutError;
       
   675 			break;
       
   676 			
       
   677 		case QNetworkReply::ProxyAuthenticationRequiredError:
       
   678 			aResult = SmfTransportOpProxyAuthenticationRequiredError;
       
   679 			break;
       
   680 			
       
   681 		case QNetworkReply::ContentAccessDenied:
       
   682 			aResult = SmfTransportOpContentAccessDenied;
       
   683 			break;
       
   684 			
       
   685 		case QNetworkReply::ContentOperationNotPermittedError:
       
   686 			aResult = SmfTransportOpContentOperationNotPermittedError;
       
   687 			break;
       
   688 			
       
   689 		case QNetworkReply::ContentNotFoundError:
       
   690 			aResult = SmfTransportOpContentNotFoundError;
       
   691 			break;
       
   692 			
       
   693 		case QNetworkReply::AuthenticationRequiredError:
       
   694 			aResult = SmfTransportOpAuthenticationRequiredError;
       
   695 			break;
       
   696 			
       
   697 		case QNetworkReply::ContentReSendError:
       
   698 			aResult = SmfTransportOpContentReSendError;
       
   699 			break;
       
   700 			
       
   701 		case QNetworkReply::ProtocolUnknownError:
       
   702 			aResult = SmfTransportOpProtocolUnknownError;
       
   703 			break;
       
   704 
       
   705 		case QNetworkReply::ProtocolInvalidOperationError:
       
   706 			aResult = SmfTransportOpProtocolInvalidOperationError;
       
   707 			break;
       
   708 			
       
   709 		case QNetworkReply::UnknownNetworkError:
       
   710 			aResult = SmfTransportOpUnknownNetworkError;
       
   711 			break;
       
   712 			
       
   713 		case QNetworkReply::UnknownProxyError:
       
   714 			aResult = SmfTransportOpUnknownProxyError;
       
   715 			break;
       
   716 			
       
   717 		case QNetworkReply::UnknownContentError:
       
   718 			aResult = SmfTransportOpUnknownContentError;
       
   719 			break;
       
   720 			
       
   721 		case QNetworkReply::ProtocolFailure:
       
   722 			aResult = SmfTransportOpProtocolFailure;
       
   723 			break;
       
   724 			
       
   725 		default:
       
   726 			;
       
   727 		}
       
   728 	}
       
   729