applayerprotocols/httpexamples/header_examples.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2001-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 <http.h>
       
    17 
       
    18 // eg.s of Header usage.
       
    19 //
       
    20 
       
    21 // Throughout:  iStrTb is a handle to the HTTP string table;
       
    22 //  i.e.   RStringPool iStrTb;
       
    23 //
       
    24 // Strings are represented as handles to HTTP strings;
       
    25 //  i.e.   RStringF str;
       
    26 //
       
    27 // String table lookup (i.e. getting the string handle from some descriptor) is done as so:
       
    28 //
       
    29 //  RStringF str = iStrTb.OpenFStringL(_L8("my string"));
       
    30 //
       
    31 // Strings which are 'very well known' (e.g. those in the HTTP RFC) and therefore certainly expected to be in ROM
       
    32 // can be converted to HTTP string handles using enumerations:
       
    33 //
       
    34 //  RStringF str = iStrTb.String(HTTP::EAccept,RHTTPSession::GetTable()); // guaranteed to give the same handle as:
       
    35 //  RHttpString str = iStrTb.OpenFStringL(_L8("KHttpAccept"));
       
    36 
       
    37 
       
    38 
       
    39 // 1. Client API creation of simple headers
       
    40 //	  
       
    41 //    Accept: text/html
       
    42 //	  Expires: Mon, 15 Jan 2001 10:30:25 GMT
       
    43 //	  Location: http://www.symbian.com/
       
    44 //	  Max-Forwards: 3
       
    45 //
       
    46 void Example1(RHTTPRequest aReq)
       
    47 	{
       
    48 	RHTTPHeaders hdr = aReq.GetHeaderCollection();
       
    49 
       
    50 	RStringF textHtml = iStrTb.OpenFStringL(_L8("text/html"));
       
    51 	THTTPHdrVal accVal(textHtml);
       
    52 	hdr.SetFieldL(iStrTb.String(HTTP::EAccept,RHTTPSession::GetTable()), accVal);
       
    53 	textHtml.Close();
       
    54 
       
    55 	TDateTime dateVal(2001, EJanuary, 14, 10, 30, 25, 0);
       
    56 	THTTPHdrVal expVal(dateVal);
       
    57 	hdr.SetFieldL(iStrTb.String(HTTP::EExpires,RHTTPSession::GetTable()), expVal);
       
    58 
       
    59 	RStringF symbUrl = iStrTb.OpenFStringL(_L8("http://www.symbian.com/"));
       
    60 	THTTPHdrVal locVal(symbUrl);
       
    61 	hdr.SetFieldL(iStrTb.String(HTTP::ELocation,RHTTPSession::GetTable()), locVal);
       
    62 	symbUrl.Close();
       
    63 
       
    64 	THTTPHdrVal maxFwdsVal(3);
       
    65 	hdr.SetFieldL(iStrTb.String(HTTP::EMaxForwards,RHTTPSession::GetTable()), maxFwdsVal);
       
    66 	}
       
    67 
       
    68 
       
    69 // 2. Client API creation of headers with several parts
       
    70 //
       
    71 //	  Accept-Charset: us-ascii, utf-8
       
    72 //	  Accept-Language: en-gb. fr
       
    73 //
       
    74 void Example2(RHTTPRequest aReq)
       
    75 	{
       
    76 	RHTTPHeaders hdr = aReq.GetHeaderCollection();
       
    77 
       
    78 	RStringF usAscii = iStrTb.OpenFStringL(_L8("us-ascii"));
       
    79 	RStringF utf8    = iStrTb.OpenFStringL(_L8("utf-8"));
       
    80 	THTTPHdrVal accChSetVal(usAscii);
       
    81 	hdr.SetFieldL(iStrTb.String(HTTP::EAcceptCharset,RHTTPSession::GetTable()), accChSetVal); // sets the first part
       
    82 	accChSetVal.SetStrutf8);
       
    83 	hdr.SetFieldL(iStrTb.String(HTTP::EAcceptCharset,RHTTPSession::GetTable()), accChSetVal); // adds an additional part
       
    84 	usAscii.Close();
       
    85 	utf8.Close();
       
    86 
       
    87 	RStringF enGb = iStrTb.OpenFStringL(_L8("en-gb"));
       
    88 	RStringF fr   = iStrTb.OpenFStringL(_L8("fr"));
       
    89 	THTTPHdrVal accLangVal(enGb);
       
    90 	hdr.SetFieldL(iStrTb.String(HTTP::EAcceptLanguage,RHTTPSession::GetTable()), accLangVal);
       
    91 	accLangVal.SetStrfr);
       
    92 	hdr.SetFieldL(iStrTb.String(HTTP::EAcceptLanguage,RHTTPSession::GetTable()), accLangVal);
       
    93 	enGb.Close();
       
    94 	fr.Close();
       
    95 	}
       
    96 
       
    97 
       
    98 // 3. Client API creation of headers with parameters
       
    99 //
       
   100 //	  Accept: text/html; q=0.8, text/plain; q=0.2, */*
       
   101 //
       
   102 void Example3(RHTTPRequest aReq)
       
   103 	{
       
   104 	RHTTPHeaders hdr = aReq.GetHeaderCollection();
       
   105 
       
   106 	RStringF textHtml  = iStrTb.OpenFStringL(_L8("text/html"));
       
   107 	RStringF textPlain = iStrTb.OpenFStringL(_L8("text/plain"));
       
   108 	RStringF anyAny	  = iStrTb.OpenFStringL(_L8("*/*"));
       
   109 	THTTPHdrVal accVal(textHtml);
       
   110 	THTTPHdrVal q(THTTPHdrVal::TQConv(0.8));
       
   111 	hdr.SetFieldL(iStrTb.String(HTTP::EAccept,RHTTPSession::GetTable()), accVal, iStrTb.String(HTTP::EQ,RHTTPSession::GetTable()), q);
       
   112 	accVal.SetStrtextPlain);
       
   113 	q.SetInt(THTTPHdrVal::TQConv(0.2));
       
   114 	hdr.SetFieldL(iStrTb.String(HTTP::EAccept,RHTTPSession::GetTable()), accVal, iStrTb.String(HTTP::EQ,RHTTPSession::GetTable()), q);
       
   115 	accVal.SetStranyAny);
       
   116 	hdr.SetFieldL(iStrTb.String(HTTP::EAccept,RHTTPSession::GetTable()), accVal);
       
   117 	textHtml.Close();
       
   118 	textPlain.Close();
       
   119 	anyAny.Close();
       
   120 	}