Adding, Removing and Accessing Header Field Parts

Header field parts are added by repeated invocation of the RHTTPHeaders::SetFieldL() method. On the first invocation, when no field of that name exists in the header, a new field is created with a single part. On subsequent invocations, additional parts are added to the existing header field.

This behaviour is consistent with HTTP/1.1 in which:

Accept: text/*, text/html

is preferable to

Accept: text/* text/html

Although the two are semantically equivalent, the API will not output the second variant under any circumstance.

When parts are added to a header field, they are given an index number. The first part to be added has index 0, the second is index 1, and so on. This index is necessary when identifying parts for retrieval (using RHTTPHeaders::GetField()).

To remove a single header field part, RHTTPHeaders::RemoveFieldPart() is used, specifying a part index. To remove a header field entirely, RHTTPHeaders::RemoveField() is used.

Part values are accessed using RHTTPHeaders::GetField(). The client must supply a part index and an uninitialized THTTPHdrVal, which will be filled by the method. This example, from HTTPEXAMPLECLIENT, retrieves all response headers and prints their values:

void CHttpEventHandler::DumpRespHeadersL(RHTTPTransaction& aTrans)
    {
    RHTTPResponse resp = aTrans.Response();
    RStringPool strP = aTrans.Session().StringPool();
    RHTTPHeaders hdr = resp.GetHeaderCollection();
    THTTPHdrFieldIter it = hdr.Fields();

    TBuf<KMaxHeaderNameLen>  fieldName16;
    TBuf<KMaxHeaderValueLen> fieldVal16;

    while (it.AtEnd() == EFalse)
        {
        RStringTokenF fieldName = it();
        RStringF fieldNameStr = strP.StringF(fieldName);
        THTTPHdrVal fieldVal;
        if (hdr.GetField(fieldNameStr,0,fieldVal) == KErrNone)
            {
            const TDesC8& fieldNameDesC = fieldNameStr.DesC();
            fieldName16.Copy(fieldNameDesC.Left(KMaxHeaderNameLen));
            switch (fieldVal.Type())
                {
            case THTTPHdrVal::KTIntVal:
                iUtils.Test().Printf(_L("%S: %d\n"), &fieldName16, fieldVal.Int());
                break;
            case THTTPHdrVal::KStrFVal:
                {
                RStringF fieldValStr = strP.StringF(fieldVal.StrF());
                const TDesC8& fieldValDesC = fieldValStr.DesC();
                fieldVal16.Copy(fieldValDesC.Left(KMaxHeaderValueLen));
                iUtils.Test().Printf(_L("%S: %S\n"), &fieldName16, &fieldVal16);
                fieldValStr.Close();
                }
                break;
            case THTTPHdrVal::KStrVal:
                {
                RString fieldValStr = strP.String(fieldVal.Str());
                const TDesC8& fieldValDesC = fieldValStr.DesC();
                fieldVal16.Copy(fieldValDesC.Left(KMaxHeaderValueLen));
                iUtils.Test().Printf(_L("%S: %S\n"), &fieldName16, &fieldVal16);
                fieldValStr.Close();
                }
                break;
            case THTTPHdrVal::KDateVal:
                {
                TDateTime date = fieldVal.DateTime();
                TBuf<40> dateTimeString;
                TTime t(date);
                t.FormatL(dateTimeString,KDateFormat);
                iUtils.Test().Printf(_L("%S: %S\n"), &fieldName16, &dateTimeString);
                } 
                break;
            default:
                iUtils.Test().Printf(_L("%S: <unrecognised value type>\n"), &fieldName16);
                break;
                }
            ...
            }
        // Advance the iterator to get the next field
        ++it;
        }
     }

Note that in this example, only the first part of each header field is displayed.