Symbian
Symbian Developer Library

SYMBIAN OS V9.4

Feedback

[Index] [Previous] [Next]


How to encode and decode WSP headers

[Top]


WSP Header encoding

The Wireless Session Protocol (WSP) offers services that are most suited for browsing applications. It provides functionality equivalent to HTTP 1.1 which incorporates features such as long-lived sessions, a common facility for data push, capability negotiation and session suspend/resume. For more details on WSP services, refer to WAP WSP.

Reading headers in a WSP buffer

TWspField object holds the name and value pair of the WSP header field. TWspHeaderSegmenter pulls the header / value pair out of the WSP buffer (into TWspField). Call TWspHeaderSegmenter::NextL() to iterate through the WSP buffer. It calls TWspPrimitiveDecoder.

CWspHeaderEncoder allows you to encode the values and parameters of the header field. It creates one header at a time from the name / value pairs.

Follow the steps below to encode the WSP header:

  1. Create a header encoder object.

    CWspHeaderEncoder* primEncoder = CWspHeaderEncoder::NewLC();//creates a pointer to CWspHeaderEncoder object
  2. Start a new encoded header to encode the parameter value.

    primEncoder->StartHeaderL(0x27); // start a new encoded header 
  3. Call CWspHeaderEncoder::StartValueLengthL() to calculate the length of encodings that are added subsequently. The value calculated, is stored as part of the encoded string, as specified in the WSP standard.

    primEncoder->StartValueLengthL();
  4. Encode the header field and add it to the encoded field.

    Note: The appropriate WSP method is used for encoding the header field of a data type such as integer, date, text string and so on.

    primEncoder->AddIntegerL(0x7F);
    primEncoder->AddUintVarL(0xff);
    primEncoder->AddLongIntL(999999);
    _LIT8(KString, "WSP Encode: String");
    primEncoder->AddTextStringL(KString);
    TDateTime time(2006,EMarch,20,06,36,30,000000); //create a date time object
    primEncoder->AddDateL(time); // add 
    TUInt intVal=489;
    primEncoder->AddLTokenL(intVal);
    _LIT8(KTokenText, "WSP Encode: Token Text");
    primEncoder->AddLTokenTextL(KTokenText);
  5. Call CWspHeaderEncoder::EndValuesLengthL() at the time of header construction when ValueLength can be calculated.

  6. Assuming that the length of the header encodings has been calculated and added to the encoder field, call CWspHeaderEncoder::EndValuesLengthL().

    primEncoder->EndValueLengthL();
  7. Call CWspHeaderEncoder::EndHeaderL(), to complete the header encoding. This completes and returns the encoded header field's 8 bit buffer. EndHeaderL() panics if EndValueLengthL() is not called after CWspHeaderEncoder::StartValueLengthL().

    HBufC8* buf = primEncoder->EndHeaderL();

    this returns a pointer to the buffer containing the encoded field.

WSP Header primitive encoding

TWspPrimitiveEncoder allows encoding of header values. This encodes the input using various WSP encoding methods as follows: TWspPrimitiveEncoder is used to convert data into binary strings.

ShortInt() encodes the TUint8 parameter value.

TUint8 input;
input=0x7F; // MAX input value
TUInt8 uInt8 = TWspPrimitiveEncoder::ShortInt(input);// encode short integer 

If the input value is greater that 127, it returns 0.

LongIntL() encodes the TUint32 parameter value.

TUint32 input;
input=0x10000;
HBufC8  buf = TWspPrimitiveEncoder::LongIntL(input);// encode long int

TextStringL() encodes the string value.

_LIT8(KString1,"X-Header-1.foo");
HBufC8  buf = TWspPrimitiveEncoder::TextStringL(KString1);// encode field name

UintVarL() encodes the TUint32 parameter value.

TUint32 input;
input=0x3FFF;
HBufC8  buf = TWspPrimitiveEncoder::UintVarL(input); //encode UintVar

DateL() encodes the date value.

TDateTime date(1970,EJanuary,0,0,0,0,0);
HBufC8  buf = TWspPrimitiveEncoder::DateL(date);//encode the date value

WSP Header primitive decoding

TWspPrimitiveDecoder allows decoding the primitive values of the header. This allows interpreting the encoded header.

Call String(), Val7Bit(), LongInt(), Integer(), UIntVar(), LengthVal(), Date() as appropriate to get the required byte to which the buffer is currently pointing. Before calling these functions, check if the data type that is passed to the functions is one of the type supported by TWspHeaderType. To get the type of the variable, call VarType(), which returns either ELengthVal, EQuotedString, EString or E7BitVal of TWspHeaderType as appropriate. If no data type is set to the data it points, the function returns ENotSet.

TPtrC8 aBuf;
TWspPrimitiveDecoder dec(aBuf);
TInt length;
TInt int = dec.LengthVal(length); // get length in bytes of data 

Decode the string value of the header using String().

TPtrC8 aBuf;
TWspPrimitiveDecoder dec(aBuf);
TPtrC8 str;
TInt int = dec.String(str); //get the string that the buffer is pointing to

Decode the integer value of the header using Integer().

TPtrC8 aBuf;
TWspPrimitiveDecoder dec(aBuf);
TUint32 val;
TInt int = dec.Integer(val); // get the integer that the buffer is pointing to

Decode the integer value of the header using UIntVar().

TPtrC8 aBuf;
TWspPrimitiveDecoder dec(aBuf);
TUint32 val;
TUint32 uInt =dec.UintVar(val);  //get the TUint32 value of the header

[Top]


See also

InetProtUtils Overview