applayerprotocols/httpservice/src/chttpclienttransaction.cpp
changeset 0 b16258d2340f
child 28 dd1f62411ef8
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "chttpclienttransaction.h"
       
    17 #include "chttpclienttransactionimpl.h"
       
    18 #include "httpclientutils.h"
       
    19 #include "cheaders.h"
       
    20 #include "cheaderfield.h"
       
    21 #include "mhttpdatareceiver.h"
       
    22 #include "mhttpdatasender.h"
       
    23 
       
    24 EXPORT_C THttpContentSinkOp::THttpContentSinkOp()
       
    25 : iDataReceiver(NULL)
       
    26     {
       
    27     
       
    28     }
       
    29 
       
    30 EXPORT_C TBool THttpContentSinkOp::GetData(TPtrC8& aData)
       
    31     {
       
    32     return iDataReceiver->GetData(aData);
       
    33     }
       
    34 
       
    35 EXPORT_C void THttpContentSinkOp::Release()
       
    36     {
       
    37     iDataReceiver->Release();
       
    38     }
       
    39 
       
    40 EXPORT_C void THttpContentSinkOp::Store() // Store into a file if a sink is provided.
       
    41     {
       
    42     iDataReceiver->Store();
       
    43     }
       
    44 
       
    45 THttpContentSinkOp::THttpContentSinkOp(MHttpDataReceiver& aReceiver)
       
    46 : iDataReceiver(&aReceiver)
       
    47     {    
       
    48     }
       
    49 // --------------------------------------------------------------------------------------------
       
    50 
       
    51 EXPORT_C THttpContentSourceOp::THttpContentSourceOp()
       
    52 : iDataSender(NULL)
       
    53     {
       
    54     
       
    55     }
       
    56 
       
    57 EXPORT_C void THttpContentSourceOp::Notify(const TDesC8& aData, TBool aLast /* = EFalse */)
       
    58     {    
       
    59     iDataSender->Notify(aData, aLast);
       
    60     }    
       
    61 
       
    62 THttpContentSourceOp::THttpContentSourceOp(MHttpDataSender* aSender)
       
    63 : iDataSender(aSender)
       
    64     {
       
    65     }
       
    66 
       
    67 // --------------------------------------------------------------------------------------------
       
    68 
       
    69 /**
       
    70  * Create the HTTP transaction by specifiying the method [GET, POST etc..] and a URI
       
    71  * 
       
    72  * @param aClient - CHttpService instance
       
    73  * @param aMethod - HTTP method. [GET, POST etc...]
       
    74  * @param aUri - URI
       
    75  * 
       
    76  * @return KErrNoMemory incase of failure otherwise KErrNone for success
       
    77  * 
       
    78  */
       
    79 
       
    80 EXPORT_C TInt CHttpClientTransaction::Create(CHttpService& aClient, const TDesC8& aMethod, const TDesC8& aUri)
       
    81 	{
       
    82 	TRAPD(err, iHttpClientTrans = CHttpClientTransactionImpl::NewL(aClient, *this, aMethod, aUri));
       
    83 	return err;
       
    84 	}
       
    85 
       
    86 /**
       
    87  * Destructor
       
    88  */
       
    89 EXPORT_C CHttpClientTransaction::~CHttpClientTransaction()
       
    90 	{
       
    91 	delete	iHttpClientTrans;
       
    92 	}
       
    93 
       
    94 /**
       
    95  * Returns HTTP response status code
       
    96  * 
       
    97  */
       
    98 EXPORT_C TInt CHttpClientTransaction::StatusCode() const
       
    99 	{
       
   100 	return iHttpClientTrans->Transaction().Response().StatusCode();	
       
   101 	}
       
   102 	
       
   103 /**
       
   104  * Returns HTTP response status text
       
   105  * 
       
   106  */
       
   107 EXPORT_C const TDesC8& CHttpClientTransaction::StatusText() const
       
   108 	{
       
   109 	return iHttpClientTrans->Transaction().Response().StatusText().DesC();
       
   110 	}
       
   111 
       
   112 /**
       
   113  * Disable the pipelining for “this” transaction. The transaction will be send on a 
       
   114  * free TCP connection and no other transactions will be send via that TCP connection 
       
   115  * till the response for “this” transaction is received
       
   116  */
       
   117 EXPORT_C void CHttpClientTransaction::DisablePipelining()
       
   118 	{
       
   119 	RStringPool sp = iHttpClientTrans->StringPool();
       
   120 	RStringF property = sp.StringF(HTTP::EHttpPipelining, RHTTPSession::GetTable());
       
   121 	THTTPHdrVal disablePipelining = sp.StringF(HTTP::EDisablePipelining, RHTTPSession::GetTable());
       
   122 	iHttpClientTrans->Transaction().PropertySet().SetProperty(property, disablePipelining);	
       
   123 	}
       
   124 
       
   125 /**
       
   126  * Disable automatic retry on a TCP connection failure. On TCP connection failure the error 
       
   127  * will be notified via OnError function
       
   128  * 
       
   129  */
       
   130 EXPORT_C void CHttpClientTransaction::SetNoRetry()
       
   131 	{
       
   132 	RStringPool sp = iHttpClientTrans->StringPool();
       
   133 	RStringF property = sp.StringF(HTTP::ENotifyOnDisconnect, RHTTPSession::GetTable());
       
   134 	THTTPHdrVal disconnectNotify = sp.StringF(HTTP::EEnableDisconnectNotification, RHTTPSession::GetTable());
       
   135 	iHttpClientTrans->Transaction().PropertySet().SetProperty(property, disconnectNotify);	
       
   136 	}
       
   137 
       
   138 /**
       
   139  * Set the dynamic HTTP content source.
       
   140  */
       
   141 EXPORT_C TInt CHttpClientTransaction::SetContentSource(MHttpContentSource& aSource)
       
   142 	{
       
   143 	return iHttpClientTrans->SetDynamicContentSource(&aSource);
       
   144 	}
       
   145 	
       
   146 /**
       
   147  * Set the static HTTP content source.
       
   148  * 
       
   149  * @param aFile - An opened file
       
   150  */
       
   151 EXPORT_C TInt CHttpClientTransaction::SetContentSource(RFile& aFile)
       
   152 	{
       
   153 	return iHttpClientTrans->SetStaticContentSource(aFile);
       
   154 	}
       
   155 
       
   156 /**
       
   157  * Set the dynamic HTTP content sink
       
   158  * 
       
   159  */
       
   160 EXPORT_C TInt CHttpClientTransaction::SetContentSink(MHttpContentSink& aSink)
       
   161 	{
       
   162 	return iHttpClientTrans->SetDynamicContentSink(&aSink);
       
   163 	}
       
   164 
       
   165 /**
       
   166  * Set the static HTTP content sink
       
   167  * 
       
   168  * @param aFile - An opened file
       
   169  */
       
   170 EXPORT_C TInt CHttpClientTransaction::SetContentSink(RFile& aFile)
       
   171 	{
       
   172 	return iHttpClientTrans->SetStaticContentSink(aFile);
       
   173 	}	
       
   174 /**
       
   175  * Set the HTTP request timeout. If the HTTP request didn't send in the specified 
       
   176  * time OnError will be called with an error code
       
   177  * 
       
   178  * @param aValue - Request timeout value
       
   179  */
       
   180 EXPORT_C void CHttpClientTransaction::SetRequestTimeout(TInt aValue)
       
   181 	{
       
   182 	RStringPool sp = iHttpClientTrans->StringPool();
       
   183 	RStringF property = sp.StringF(HTTP::ESendTimeOutValue, RHTTPSession::GetTable());
       
   184 	THTTPHdrVal requestTimeout(aValue);
       
   185 	iHttpClientTrans->Transaction().PropertySet().SetProperty(property, requestTimeout);			
       
   186 	}
       
   187 
       
   188 /**
       
   189  * Set the HTTP response timeout. If the HTTP response didn't arrive in the specified 
       
   190  * time OnError will be called with an error code
       
   191  */
       
   192 EXPORT_C void CHttpClientTransaction::SetResponseTimeout(TInt aValue)
       
   193 	{
       
   194 	RStringPool sp = iHttpClientTrans->StringPool();
       
   195 	RStringF property = sp.StringF(HTTP::EReceiveTimeOutValue, RHTTPSession::GetTable());
       
   196 	THTTPHdrVal responseTimeout(aValue);
       
   197 	iHttpClientTrans->Transaction().PropertySet().SetProperty(property, responseTimeout);			
       
   198 	}
       
   199 
       
   200 EXPORT_C TInt CHttpClientTransaction::AddRequestHeader(TInt aHeaderId, const THttpHeaderValueVariant& aHeaderValue)
       
   201 	{
       
   202     RHTTPHeaders requestHeaders = iHttpClientTrans->Transaction().Request().GetHeaderCollection();;
       
   203     RStringPool sp = iHttpClientTrans->StringPool();
       
   204     THTTPHdrVal val = HttpClientUtils::CopyHttpHdrVal(aHeaderValue, sp);
       
   205     if(val.Type() == THTTPHdrVal::KNoType)
       
   206         return KErrNoMemory;
       
   207     TInt err = requestHeaders.SetField(sp.StringF(aHeaderId, RHTTPSession::GetTable()), val);
       
   208     HttpClientUtils::CloseString(val);
       
   209     return err;	
       
   210 	}
       
   211 
       
   212 EXPORT_C TInt CHttpClientTransaction::AddRequestHeader(const TDesC8& aHeaderName, const THttpHeaderValueVariant& aHeaderValue)
       
   213     {
       
   214     RHTTPHeaders requestHeaders = iHttpClientTrans->Transaction().Request().GetHeaderCollection();;
       
   215     RStringPool sp = iHttpClientTrans->StringPool();
       
   216     THTTPHdrVal val = HttpClientUtils::CopyHttpHdrVal(aHeaderValue, sp);
       
   217     if(val.Type() == THTTPHdrVal::KNoType)
       
   218         return KErrNoMemory;
       
   219     RStringF str;
       
   220     TInt err = HttpClientUtils::OpenStringF(aHeaderName, sp, str); 
       
   221     if(err != KErrNone)
       
   222         {
       
   223         HttpClientUtils::CloseString(val);
       
   224         return err;
       
   225         }
       
   226     err = requestHeaders.SetField(str, val);
       
   227     HttpClientUtils::CloseString(val);
       
   228     str.Close();
       
   229     return err;    
       
   230     }
       
   231 
       
   232 EXPORT_C TInt CHttpClientTransaction::AddRequestHeader(TInt aHeaderId, const THttpHeaderValueVariant& aHeaderValue, TInt aParamId, const THttpHeaderValueVariant& aParamValue)
       
   233 	{
       
   234     RHTTPHeaders requestHeaders = iHttpClientTrans->Transaction().Request().GetHeaderCollection();;
       
   235     RStringPool sp = iHttpClientTrans->Transaction().Session().StringPool();
       
   236     THTTPHdrVal val = HttpClientUtils::CopyHttpHdrVal(aHeaderValue, sp);
       
   237     if(val.Type() == THTTPHdrVal::KNoType)
       
   238         return KErrNoMemory;
       
   239 
       
   240     THTTPHdrVal val2 = HttpClientUtils::CopyHttpHdrVal(aParamValue, sp);
       
   241     if(val2.Type() == THTTPHdrVal::KNoType)
       
   242         {
       
   243         HttpClientUtils::CloseString(val);
       
   244         return KErrNoMemory;
       
   245         }
       
   246     TInt err = requestHeaders.SetField(sp.StringF(aHeaderId, RHTTPSession::GetTable()), val,
       
   247                                         sp.StringF(aParamId, RHTTPSession::GetTable()), val2);
       
   248     HttpClientUtils::CloseString(val);
       
   249     HttpClientUtils::CloseString(val2);
       
   250     return err;
       
   251 	}
       
   252 
       
   253 EXPORT_C TInt CHttpClientTransaction::AddRequestHeader(const TDesC8& aHeaderName, const THttpHeaderValueVariant& aHeaderValue, const TDesC8& aParamName, const THttpHeaderValueVariant& aParamValue)
       
   254     {
       
   255     RHTTPHeaders requestHeaders = iHttpClientTrans->Transaction().Request().GetHeaderCollection();;
       
   256     RStringPool sp = iHttpClientTrans->Transaction().Session().StringPool();
       
   257     THTTPHdrVal val = HttpClientUtils::CopyHttpHdrVal(aHeaderValue, sp);
       
   258     if(val.Type() == THTTPHdrVal::KNoType)
       
   259         return KErrNoMemory;
       
   260 
       
   261     THTTPHdrVal val2 = HttpClientUtils::CopyHttpHdrVal(aParamValue, sp);
       
   262     if(val2.Type() == THTTPHdrVal::KNoType)
       
   263         {
       
   264         HttpClientUtils::CloseString(val);
       
   265         return KErrNoMemory;
       
   266         }
       
   267     RStringF str;
       
   268     TInt err = HttpClientUtils::OpenStringF(aHeaderName, sp, str); 
       
   269     if(err != KErrNone)
       
   270         {
       
   271         HttpClientUtils::CloseString(val);
       
   272         HttpClientUtils::CloseString(val2);
       
   273         return err;
       
   274         }
       
   275 
       
   276     RStringF str2;
       
   277     err = HttpClientUtils::OpenStringF(aParamName, sp, str2); 
       
   278     if(err != KErrNone)
       
   279         {
       
   280         HttpClientUtils::CloseString(val);
       
   281         HttpClientUtils::CloseString(val2);
       
   282         str.Close();
       
   283         return err;
       
   284         }
       
   285     err = requestHeaders.SetField(str, val, str2, val2);
       
   286     HttpClientUtils::CloseString(val);
       
   287     HttpClientUtils::CloseString(val2);
       
   288     str.Close();
       
   289     str2.Close();
       
   290     return err;  
       
   291     }
       
   292 
       
   293 EXPORT_C TInt CHttpClientTransaction::AddCustomRequestHeader(const TDesC8& aHeaderName, const TDesC8& aHeaderValue)
       
   294 	{
       
   295     _LIT8(KFieldSeparator, "\n");
       
   296     RHTTPHeaders requestHeaders = iHttpClientTrans->Transaction().Request().GetHeaderCollection();;   
       
   297     RStringPool sp = iHttpClientTrans->StringPool();
       
   298     RStringF str;
       
   299     if(HttpClientUtils::OpenStringF(aHeaderName, sp, str) != KErrNone)
       
   300         {
       
   301         return KErrNoMemory;
       
   302         }
       
   303     TInt err = requestHeaders.SetRawField(str, aHeaderValue, KFieldSeparator);
       
   304     str.Close();
       
   305     return err;
       
   306 	}
       
   307 
       
   308 
       
   309 EXPORT_C THttpHeaderIterator CHttpClientTransaction::ResponseHeaders() const
       
   310 	{
       
   311 	CHeaders* responseHeaders = iHttpClientTrans->Transaction().Response().GetHeaderCollection().iImplementation;
       
   312     return THttpHeaderIterator(responseHeaders);
       
   313 	}
       
   314 	
       
   315 EXPORT_C THttpHeaderElementIterator CHttpClientTransaction::ResponseHeader(TInt aHeaderId)
       
   316 	{
       
   317 	RStringPool sp = iHttpClientTrans->StringPool();
       
   318 	RHTTPHeaders responseHeaders = iHttpClientTrans->Transaction().Response().GetHeaderCollection();
       
   319 	CHeaderField* field = responseHeaders.iImplementation->LookupField(sp.StringF(aHeaderId, RHTTPSession::GetTable()));
       
   320 	if(field == NULL)
       
   321 	    {
       
   322 	    return THttpHeaderElementIterator(NULL);
       
   323 	    }
       
   324 	return THttpHeaderElementIterator(&field->iElementArray);
       
   325 	}
       
   326 	
       
   327 EXPORT_C THttpHeaderElementIterator CHttpClientTransaction::ResponseHeader(const TDesC8& aHeaderName)
       
   328 	{
       
   329     RStringPool sp = iHttpClientTrans->StringPool();
       
   330 	RHTTPHeaders responseHeaders = iHttpClientTrans->Transaction().Response().GetHeaderCollection();
       
   331 	CHeaderField* field = NULL;
       
   332 	RStringF str;
       
   333 	if(HttpClientUtils::OpenStringF(aHeaderName, sp, str) == KErrNone)
       
   334 	    {
       
   335 	    field = responseHeaders.iImplementation->LookupField(str);
       
   336 	    str.Close();
       
   337 	    }
       
   338 	if(field == NULL)
       
   339 	    {
       
   340 	    return THttpHeaderElementIterator(NULL);
       
   341 	    }
       
   342 	return THttpHeaderElementIterator(&field->iElementArray);
       
   343 	}
       
   344 
       
   345 EXPORT_C TInt CHttpClientTransaction::SendRequest()
       
   346 	{
       
   347     RStringF name = iHttpClientTrans->StringPool().StringF(HTTP::EContentLength, RHTTPSession::GetTable());
       
   348     RHTTPHeaders requestHeaders = iHttpClientTrans->Transaction().Request().GetHeaderCollection();
       
   349     THTTPHdrVal value;
       
   350     if(requestHeaders.GetField(name, 0, value) == KErrNone)
       
   351         {
       
   352         iHttpClientTrans->SetDynamicContentSourceLength(value.Int());
       
   353         }
       
   354 	
       
   355 	return iHttpClientTrans->Transaction().Submit();
       
   356 	}
       
   357 	
       
   358 EXPORT_C void CHttpClientTransaction::CancelRequest()
       
   359 	{
       
   360 	iHttpClientTrans->Transaction().Cancel();
       
   361 	}
       
   362 
       
   363 /**
       
   364 virtual functions
       
   365 */
       
   366 EXPORT_C void CHttpClientTransaction::OnTransferProgress(TInt /* aTotal */, TInt /* aTransffered */)
       
   367 	{
       
   368 	
       
   369 	}
       
   370 	
       
   371 EXPORT_C TBool CHttpClientTransaction::OnRedirection()
       
   372 	{
       
   373 	return ETrue;
       
   374 	}
       
   375 
       
   376 EXPORT_C void CHttpClientTransaction::OnWarning(TInt /* aWarning */)
       
   377     {
       
   378     
       
   379     }
       
   380 
       
   381 EXPORT_C CHttpClientTransaction::CHttpClientTransaction()
       
   382 : iHttpClientTrans(NULL)
       
   383 	{
       
   384 	}