Home | History | Annotate | Download | only in include
      1 /* ------------------------------------------------------------------
      2  * Copyright (C) 1998-2009 PacketVideo
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
     13  * express or implied.
     14  * See the License for the specific language governing permissions
     15  * and limitations under the License.
     16  * -------------------------------------------------------------------
     17  */
     18 #ifndef HTTP_PARSER_EXTERNAL_H_
     19 #define HTTP_PARSER_EXTERNAL_H_
     20 
     21 #include "oscl_refcounter_memfrag.h"
     22 #include "oscl_vector.h"
     23 
     24 // content info exposed to user
     25 struct HTTPContentInfo
     26 {
     27     uint32 iContentLength;      // for "Content-Length"
     28     uint32 iContentRangeLeft;   // for "Content-Range"
     29     uint32 iContentRangeRight;
     30 
     31     // constructor
     32     HTTPContentInfo()
     33     {
     34         clear();
     35     }
     36     void clear()
     37     {
     38         oscl_memset(this, 0, sizeof(HTTPContentInfo));
     39     }
     40 };
     41 
     42 ///////////////////////////////////////////////////////////////////////////////////////
     43 // The following structure is an array of ref-counted memory fragments
     44 typedef Oscl_TAlloc<OsclRefCounterMemFrag, OsclMemAllocator> HTTPEntityUnit_Alloc;
     45 
     46 class HTTPEntityUnit
     47 {
     48     public:
     49         Oscl_Vector<OsclRefCounterMemFrag, HTTPEntityUnit_Alloc> iFragments;
     50 
     51     public:
     52         // default constructor
     53         HTTPEntityUnit()
     54         {
     55             clear();
     56         }
     57 
     58         // copy constructor
     59         HTTPEntityUnit(const HTTPEntityUnit &x) : iFragments(x.iFragments)
     60         {
     61             ;
     62         }
     63 
     64         // destructor
     65         ~HTTPEntityUnit()
     66         {
     67             clear();
     68         }
     69 
     70         // operator "="
     71         HTTPEntityUnit& operator=(const HTTPEntityUnit &x)
     72         {
     73             iFragments = x.iFragments;
     74             return *this;
     75         }
     76 
     77         // get number of fragments
     78         uint32 getNumFragments()
     79         {
     80             return iFragments.size();
     81         }
     82 
     83         // get memory fragment
     84         bool getMemFrag(uint32 index, OsclRefCounterMemFrag& memfrag)
     85         {
     86             if (index >= iFragments.size()) return false;
     87 
     88             memfrag = OsclRefCounterMemFrag(iFragments[index]);
     89             return true;
     90         }
     91 
     92         // add a memory fragment
     93         bool addMemFrag(const OsclRefCounterMemFrag& memfrag)
     94         {
     95             int err = 0;
     96             OSCL_TRY(err, iFragments.push_back(memfrag);)
     97             return (err == 0);
     98         }
     99 
    100         // get total size of all the available memory fragments
    101         uint32 getTotalFragSize()
    102         {
    103             uint32 total_size = 0, i;
    104             for (i = 0; i < iFragments.size(); i++)
    105             {
    106                 total_size += (iFragments[i].getMemFrag()).len;
    107             }
    108             return total_size;
    109         }
    110 
    111         // clear memory fragments
    112         void clear()
    113         {
    114             iFragments.clear();
    115         }
    116 
    117         // empty
    118         bool empty()
    119         {
    120             return iFragments.empty();
    121         }
    122 };
    123 
    124 ///////////////////////////////////////////////////////////////////////////////////////
    125 // The following structure is a ref-counted array of ref-counted memory fragments.
    126 class RefCountHTTPEntityUnit
    127 {
    128         HTTPEntityUnit iEntityUnit;
    129         OsclRefCounter *iRefcnt;
    130 
    131     public:
    132         // default constructor
    133         RefCountHTTPEntityUnit() : iRefcnt(NULL)
    134         {
    135             ;
    136         }
    137 
    138         // another constructor
    139         RefCountHTTPEntityUnit(HTTPEntityUnit &aEntityUnit, OsclRefCounter *aRefcnt) :
    140                 iEntityUnit(aEntityUnit), iRefcnt(aRefcnt)
    141         {
    142             if (iRefcnt) iRefcnt->addRef();
    143         }
    144 
    145         // copy constructor
    146         RefCountHTTPEntityUnit(const RefCountHTTPEntityUnit &rhs) :
    147                 iEntityUnit(rhs.iEntityUnit), iRefcnt(rhs.iRefcnt)
    148         {
    149             if (iRefcnt) iRefcnt->addRef();
    150         }
    151 
    152         // destructor
    153         ~RefCountHTTPEntityUnit()
    154         {
    155             if (iRefcnt) iRefcnt->removeRef();
    156         }
    157 
    158         // operator "="
    159         RefCountHTTPEntityUnit& operator=(const RefCountHTTPEntityUnit& rhs)
    160         {
    161             if (this == &rhs)
    162             {
    163                 return *this;    // protect against self-assignment
    164             }
    165 
    166             bind(rhs.iEntityUnit, rhs.iRefcnt);
    167             return *this;
    168         }
    169 
    170         // bind
    171         void bind(const HTTPEntityUnit &aEntityUnit, OsclRefCounter *aRefcnt)
    172         {
    173             // remove ref for current entity unit
    174             if (iRefcnt) iRefcnt->removeRef();
    175 
    176             // copy assigned object
    177             iEntityUnit = aEntityUnit;
    178             iRefcnt = aRefcnt;
    179 
    180             // add ref for new entity unit
    181             if (iRefcnt) iRefcnt->addRef();
    182         }
    183 
    184         // Returns a pointer to the contained reference counter object
    185         OsclRefCounter* getRefCounter() const
    186         {
    187             return iRefcnt;
    188         }
    189 
    190         // return the entity unit
    191         HTTPEntityUnit& getEntityUnit()
    192         {
    193             return iEntityUnit;
    194         }
    195 
    196         // empty
    197         bool empty()
    198         {
    199             return (iEntityUnit.empty() && iRefcnt == NULL);
    200         }
    201         // clear
    202         void clear()
    203         {
    204             iEntityUnit.clear();
    205             iRefcnt = NULL;
    206         }
    207 
    208 };
    209 
    210 #endif // HTTP_PARSER_EXTERNAL_H_
    211 
    212