applayerprotocols/httpexamples/request_headers.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 forming request headers. Each request header handled by HTTP is listed along with RFC refereces to
       
    19 // show the allowed syntax.
       
    20 //
       
    21 
       
    22 // Throughout:  iStrTb is a handle to the HTTP string table;
       
    23 //  i.e.   RStringPool iStrTb;
       
    24 //
       
    25 // Strings are represented as handles to HTTP strings;
       
    26 //  i.e.   RStringF str;
       
    27 //
       
    28 // String table lookup (i.e. getting the string handle from some descriptor) is done as so:
       
    29 //
       
    30 //  RStringF str = iStrTb.OpenFStringL(_L8("my string"));
       
    31 //
       
    32 // Strings which are 'very well known' (e.g. those in the HTTP RFC) and therefore certainly expected to be in ROM
       
    33 // can be converted to HTTP string handles using enumerations:
       
    34 //
       
    35 //  RStringF str = iStrTb.String(HTTP::EAccept,RHTTPSession::GetTable()); // guaranteed to give the same handle as:
       
    36 //  RHttpString str = iStrTb.OpenFStringL(_L8("KHttpAccept"));
       
    37 
       
    38 
       
    39 
       
    40 // RFC2616, section 14.1
       
    41 // Accept         = "Accept" ":"
       
    42 //                    #( media-range [ accept-params ] )
       
    43 //   media-range    = ( "*/*"
       
    44 //                    | ( type "/" "*" )
       
    45 //                    | ( type "/" subtype )
       
    46 //                    ) *( ";" parameter )
       
    47 //   accept-params  = ";" "q" "=" qvalue *( accept-extension )
       
    48 //   accept-extension = ";" token [ "=" ( token | quoted-string ) ]
       
    49 //
       
    50 void AcceptExample(RHTTPRequest aReq)
       
    51 	{
       
    52 	RHTTPHeaders hdr = aReq.GetHeaderCollection();
       
    53 
       
    54 	// Setting a single media range with no parameter, e.g. Accept: text/html
       
    55 	RStringF textHtml = iStrTb.OpenFStringL(_L8("text/html"));
       
    56 	THTTPHdrVal accVal(textHtml);
       
    57 	hdr.SetFieldL(iStrTb.String(HTTP::EAccept,RHTTPSession::GetTable()), accVal);
       
    58 	textHtml.Close();
       
    59 
       
    60 	// Setting several media ranges with no parameters, e.g. Accept: text/html; text/vnd.wap.wml
       
    61 	RStringF textHtml = iStrTb.OpenFStringL(_L8("text/html"));
       
    62 	RStringF textWml  = iStrTb.OpenFStringL(_L8("text/vnd.wap.wml"));
       
    63 	THTTPHdrVal accVal(textHtml);
       
    64 	hdr.SetFieldL(iStrTb.String(HTTP::EAccept,RHTTPSession::GetTable()), accVal);
       
    65 	accVal.SetStrtextWml);
       
    66 	hdr.SetFieldL(iStrTb.String(HTTP::EAccept,RHTTPSession::GetTable()), accVal);
       
    67 	textHtml.Close();
       
    68 
       
    69 	// Setting a media range with a 'q' parameter,  Accept: text/html; q=0.8
       
    70 	RStringF textHtml  = iStrTb.OpenFStringL(_L8("text/html"));
       
    71 	THTTPHdrVal accVal(textHtml);
       
    72 	THTTPHdrVal q(THTTPHdrVal::TQConv(0.8));
       
    73 	hdr.SetFieldL(iStrTb.String(HTTP::EAccept,RHTTPSession::GetTable()), accVal, iStrTb.String(HTTP::EQ,RHTTPSession::GetTable()), q);
       
    74 	textHtml.Close();
       
    75 
       
    76 	// Using an accept extension,  Accept: text/html; extended=value
       
    77 	RStringF textHtml  = iStrTb.OpenFStringL(_L8("text/html"));
       
    78 	RStringF extended  = iStrTb.OpenFStringL(_L8("extended"));
       
    79 	RStringF extendVal = iStrTb.OpenFStringL(_L8("value"));
       
    80 	THTTPHdrVal accVal(textHtml);
       
    81 	THTTPHdrVal extVal(extendVal);
       
    82 	hdr.SetFieldL(iStrTb.String(HTTP::EAccept,RHTTPSession::GetTable()), accVal, extended, extVal);
       
    83 	textHtml.Close();
       
    84 	extended.Close();
       
    85 	extendVal.Close();
       
    86 	}
       
    87 
       
    88 
       
    89 // RFC2616, section 14.8;  RFC2617 
       
    90 // Authorization  = "Authorization" ":" credentials
       
    91 // credentials = auth-scheme #auth-param
       
    92 // credentials = "Basic" basic-credentials
       
    93 // basic-credentials = base64-user-pass
       
    94 // base64-user-pass  = <base64 [4] encoding of user-pass, except not limited to 76 char/line>
       
    95 // user-pass   = userid ":" password
       
    96 // userid      = *<TEXT excluding ":">
       
    97 // password    = *TEXT
       
    98 void AuthorizationBasicExample(RHTTPRequest aReq)
       
    99 	{
       
   100 	RHTTPHeaders hdr = aReq.GetHeaderCollection();
       
   101 
       
   102 	// Setting an authorization credential, e.g. Authorization: Basic c3ltYmlhbjpmMXN5bmNtbA==
       
   103 	RStringF basicCred = iStrTb.OpenFStringL(_L8("c3ltYmlhbjpmMXN5bmNtbA=="));
       
   104 	THTTPHdrVal authVal(iStrTb.String(HTTP::EBasic,RHTTPSession::GetTable()));
       
   105 	hdr.SetFieldL(iStrTb.String(HTTP::EAuthorization,RHTTPSession::GetTable()), authVal); // sets part 1 'Basic'
       
   106 	authVal.SetStrbasicCred);
       
   107 	hdr.SetFieldL(iStrTb.String(HTTP::EAuthorization,RHTTPSession::GetTable()), authVal); // sets part 2 'c3ltYmlhbjpmMXN5bmNtbA'
       
   108 	basicCred.Close();
       
   109 	}
       
   110 
       
   111 
       
   112 // RFC2616, section 14.13
       
   113 // Content-Length    = "Content-Length" ":" 1*DIGIT
       
   114 void ContentLengthExample(RHTTPRequest aReq)
       
   115 	{
       
   116 	RHTTPHeaders hdr = aReq.GetHeaderCollection();
       
   117 
       
   118 	// Setting Content-Length header, e.g. Content-Length: 12345
       
   119 	THTTPHdrVal lenVal(12345);
       
   120 	hdr.SetFieldL(iStrTb.String(HTTP::EContentLength, lenVal);
       
   121 	}
       
   122 
       
   123 
       
   124 // RFC2616, section 14.23
       
   125 // Host = "Host" ":" host [ ":" port ] ; Section 3.2.2
       
   126 void HostExample(RHTTPRequest aReq)
       
   127 	{
       
   128 	RHTTPHeaders hdr = aReq.GetHeaderCollection();
       
   129 
       
   130 	// Setting host header, e.g. Host: www.symbian.com
       
   131 	RStringF domain = iStrTb.OpenFStringL(_L8("www.symbian.com"));
       
   132 	THTTPHdrVal domVal(domain);
       
   133 	hdr.SetFieldL(iStrTb.String(HTTP::EHost, domVal);
       
   134 	domain.Close();
       
   135 
       
   136 	// Setting a host header with a specific port number, e.g. Host: www.symbian.com:80
       
   137 	RStringF domain = iStrTb.OpenFStringL(_L8("www.symbian.com"));
       
   138 	THTTPHdrVal domVal(domain);
       
   139 	THTTPHdrVal portParam(80);
       
   140 	// A string constant HTTP::EPort will be used to identify the parameter
       
   141 	hdr.SetFieldL(iStrTb.String(HTTP::EHost, domVal, EHTTPPort, portParam);
       
   142 	domain.Close();
       
   143 	}