Home | History | Annotate | Download | only in src
      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_PARCOM_INTERNAL_H_
     19 #define HTTP_PARCOM_INTERNAL_H_
     20 
     21 #ifndef OSCL_REFCOUNTER_MEMFRAG_H_INCLUDED
     22 #include "oscl_refcounter_memfrag.h"
     23 #endif
     24 
     25 
     26 ///////////////////////////////////////////////////////////////////////////////////////
     27 const char HTTP_CHAR_CR      = 13;
     28 const char HTTP_CHAR_LF      = 10;
     29 const char HTTP_CHAR_NULL    = 0;
     30 const char HTTP_CHAR_COLON   = ':';
     31 const char HTTP_CHAR_DOLLAR  = '$';
     32 const char HTTP_CHAR_SPACE   = ' ';
     33 const char HTTP_CHAR_SLASH   = '/';
     34 const char HTTP_CHAR_SEMICOLON = ';';
     35 const char HTTP_CHAR_STAR    = '*';
     36 const char HTTP_CHAR_PLUS    = '+';
     37 const char HTTP_CHAR_MINUS   = '-';
     38 const char HTTP_CHAR_DOT     = '.';
     39 const char HTTP_CHAR_TAB     = '\t';
     40 const char HTTP_CHAR_COMMA   = ',';
     41 const char HTTP_CHAR_EQUAL   = '=';
     42 
     43 
     44 //An array of strings corresponding to HTTPMethod enum
     45 static const char* const HTTPMethodString[] =
     46 {
     47     "GET",      //METHOD_GET
     48     "HEAD",     //METHOD_HEAD
     49     "POST",     //METHOD_POST
     50     "DELETE",   //METHOD_DELETE
     51     "LINK",     //METHOD_LINK
     52     "UNLINK",   //METHOD_UNLINK
     53     "OPTIONS",  //METHOD_OPTIONS
     54     "PUT",      //METHOD_PUT
     55     "TRACE",    //METHOD_TRACE
     56     "CONNECT",  //METHOD_CONNECT
     57 };
     58 
     59 // The following structure is an extension of OsclMemoryFragment with a differentation between memory usage and memory capacity
     60 struct HTTPMemoryFragment
     61 {
     62     void *iPtr;
     63     uint32 iLen; // refer to the memory usage, initially (no memory use), len=0
     64     uint32 iCapacity;
     65 
     66     // constructor
     67     HTTPMemoryFragment()
     68     {
     69         clear();
     70     }
     71 
     72     // copy constructor
     73     HTTPMemoryFragment(const void *aPtr, const uint32 aCapacity) : iPtr((void *)aPtr), iLen(0), iCapacity(aCapacity)
     74     {
     75         ;
     76     }
     77 
     78     // copy constructor
     79     HTTPMemoryFragment(const OsclMemoryFragment &x)
     80     {
     81         iPtr = x.ptr;
     82         iLen = 0;
     83         iCapacity = x.len;
     84     }
     85 
     86     // operator "="
     87     HTTPMemoryFragment& operator=(const HTTPMemoryFragment& rhs)
     88     {
     89         iPtr = rhs.iPtr;
     90         iLen = rhs.iLen;
     91         iCapacity = rhs.iCapacity;
     92         return *this;
     93     }
     94 
     95     // operator "="
     96     HTTPMemoryFragment& operator=(const OsclMemoryFragment& rhs)
     97     {
     98         iPtr = rhs.ptr;
     99         iLen = 0;
    100         iCapacity = rhs.len;
    101         return *this;
    102     }
    103 
    104     // destructor
    105     ~HTTPMemoryFragment()
    106     {
    107         clear();
    108     }
    109 
    110     // clear
    111     void clear()
    112     {
    113         oscl_memset(this, 0, sizeof(HTTPMemoryFragment));
    114     }
    115 
    116     // bind
    117     void bind(const void *aPtr, const uint32 aCapacity)
    118     {
    119         iPtr = (void *)aPtr;
    120         iLen = 0;
    121         iCapacity = aCapacity;
    122     }
    123 
    124     void bind(const OsclMemoryFragment &x)
    125     {
    126         iPtr = x.ptr;
    127         iLen = 0;
    128         iCapacity = x.len;
    129     }
    130 
    131     // empty
    132     bool empty()
    133     {
    134         return ((uint8 *)iPtr == NULL || (iCapacity == 0));
    135     }
    136 
    137     // get functions
    138     void *getPtr()
    139     {
    140         return (void *)((uint8 *)iPtr + iLen);
    141     }
    142     uint32 getLen() const
    143     {
    144         return iLen;
    145     }
    146     uint32 getCapacity()
    147     {
    148         return iCapacity;
    149     }
    150     uint32 getAvailableSpace()
    151     {
    152         return iCapacity - iLen;
    153     }
    154 
    155     // update length
    156     bool update(const uint32 aLength)
    157     {
    158         if (isSpaceEnough(aLength))
    159         {
    160             iLen += aLength;
    161             return true;
    162         }
    163         return false;
    164     }
    165 
    166     bool update(const void *aPtr)
    167     {
    168         int32 aLen = (uint8*)aPtr - (uint8*)iPtr;
    169         if (aLen < 0 || (uint32)aLen > iCapacity) return false;
    170         iLen = aLen;
    171         return true;
    172     }
    173 
    174     // is remaining space enough for the new memory usage
    175     bool isSpaceEnough(uint32 aNewUsage)
    176     {
    177         return (iLen + aNewUsage <= iCapacity);
    178     }
    179 };
    180 
    181 // wrap OsclMemoryFragment and add more functionalities
    182 struct OsclMemoryFragWrapper
    183 {
    184     OsclMemoryFragment iMemFrag;
    185 
    186     // constructor
    187     OsclMemoryFragWrapper()
    188     {
    189         clear();
    190     }
    191 
    192     // copy constructers
    193     OsclMemoryFragWrapper(const OsclMemoryFragWrapper &rhs)
    194     {
    195         iMemFrag.ptr = rhs.iMemFrag.ptr;
    196         iMemFrag.len = rhs.iMemFrag.len;
    197     }
    198     OsclMemoryFragWrapper(const OsclMemoryFragment &rhs)
    199     {
    200         iMemFrag.ptr = rhs.ptr;
    201         iMemFrag.len = rhs.len;
    202     }
    203     OsclMemoryFragWrapper(const void *aPtr, const uint32 aLen)
    204     {
    205         bind(aPtr, aLen);
    206     }
    207 
    208     // assignment Operator
    209     OsclMemoryFragWrapper& operator= (const OsclMemoryFragment &x)
    210     {
    211         bind(x);
    212         return *this;
    213     }
    214 
    215     OsclMemoryFragWrapper& operator= (OsclMemoryFragWrapper &x)
    216     {
    217         bind((const OsclMemoryFragment &)(x.get()));
    218         return *this;
    219     }
    220 
    221     // clear
    222     void clear()
    223     {
    224         iMemFrag.ptr = NULL;
    225         iMemFrag.len = 0;
    226     }
    227 
    228     // empty
    229     bool empty()
    230     {
    231         return (((uint8*)iMemFrag.ptr == NULL) || (iMemFrag.len == 0));
    232     }
    233 
    234     // bind
    235     void bind(const void *aPtr, const uint32 aLen)
    236     {
    237         iMemFrag.ptr = (void *)aPtr;
    238         iMemFrag.len = aLen;
    239     }
    240     void bind(const OsclMemoryFragment &aFrag)
    241     {
    242         iMemFrag.ptr = aFrag.ptr;
    243         iMemFrag.len = aFrag.len;
    244     }
    245 
    246     // update
    247     void update(const uint32 aLen)
    248     {
    249         iMemFrag.len = aLen;
    250     }
    251     void update(const void *aPtr)
    252     {
    253         iMemFrag.ptr = (void *)aPtr;
    254     }
    255 
    256     // get
    257     OsclMemoryFragment &get()
    258     {
    259         return iMemFrag;
    260     }
    261     void *getPtr()
    262     {
    263         return iMemFrag.ptr;
    264     }
    265     uint32 getLen()
    266     {
    267         return iMemFrag.len;
    268     }
    269 };
    270 
    271 // This class is based on OsclRefCounterMemFrag, but with the following change,
    272 // memory fragment and its associated with reference counter can be updated any time
    273 class RefCounterMemoryFragment
    274 {
    275     public:
    276 
    277         // constructor
    278         RefCounterMemoryFragment(OsclMemoryFragment &m, OsclRefCounter *r) :
    279                 iMemfrag(m), iRefcnt(r)
    280         {
    281             if (iRefcnt) iRefcnt->addRef();
    282         }
    283 
    284         RefCounterMemoryFragment(const void *aPtr, const uint32 aLen, OsclRefCounter *r) :
    285                 iMemfrag(aPtr, aLen), iRefcnt(r)
    286         {
    287             if (iRefcnt) iRefcnt->addRef();
    288         }
    289 
    290         // Copy constructor.
    291         RefCounterMemoryFragment(const OsclRefCounterMemFrag &x) :
    292                 iMemfrag(((OsclRefCounterMemFrag &)x).getMemFrag()),
    293                 iRefcnt(((OsclRefCounterMemFrag &)x).getRefCounter())
    294         {
    295             if (iRefcnt) iRefcnt->addRef();
    296         }
    297 
    298         RefCounterMemoryFragment(const RefCounterMemoryFragment &x) :
    299                 iMemfrag(x.iMemfrag), iRefcnt(x.iRefcnt)
    300         {
    301             if (iRefcnt) iRefcnt->addRef();
    302         }
    303 
    304         // Default constructor.
    305         RefCounterMemoryFragment()
    306         {
    307             iMemfrag.clear();
    308             iRefcnt = 0;
    309         }
    310 
    311         // Assignment Operator
    312         RefCounterMemoryFragment& operator= (const RefCounterMemoryFragment &x)
    313         {
    314             if (this == &x) return *this; // protect against self-assignment
    315             bind((RefCounterMemoryFragment &)x);
    316             return *this;
    317         }
    318 
    319         // Destructor
    320         ~RefCounterMemoryFragment()
    321         {
    322             if (iRefcnt) iRefcnt->removeRef();
    323         }
    324 
    325         bool empty()
    326         {
    327             return (iMemfrag.empty() && iRefcnt == NULL);
    328         }
    329 
    330         // get functions
    331         // Returns a pointer to the contained reference counter object
    332         OsclRefCounter* getRefCounter()
    333         {
    334             return iRefcnt;
    335         }
    336         // Returns a reference to the contained memory fragment
    337         OsclMemoryFragment& getMemFrag()
    338         {
    339             return iMemfrag.get();
    340         }
    341         // Returns a pointer to the memory fragment data.
    342         OsclAny* getMemFragPtr()
    343         {
    344             return iMemfrag.get().ptr;
    345         }
    346         // Returns the size of the memory fragment data which equals its filled size.
    347         uint32 getMemFragSize()
    348         {
    349             return iMemfrag.get().len;
    350         }
    351         // Returns the reference counter's current count.
    352         uint32 getCount()
    353         {
    354             return (iRefcnt) ? iRefcnt->getCount() : 0;
    355         }
    356         void getRefCountMemFrag(OsclRefCounterMemFrag &aFrag)
    357         {
    358             aFrag = OsclRefCounterMemFrag(iMemfrag.get(), iRefcnt, iMemfrag.getLen());
    359             iRefcnt->addRef(); // have to add ref-count manually
    360         }
    361 
    362         // set functions
    363         void bind(OsclMemoryFragment &m, OsclRefCounter *r)
    364         {
    365             if (iRefcnt) iRefcnt->removeRef(); // remove ref for current memfrag
    366             iMemfrag = m;
    367             iRefcnt = r;
    368             if (iRefcnt) iRefcnt->addRef(); // add ref for new memfrag
    369         }
    370         void bind(OsclRefCounterMemFrag &x)
    371         {
    372             if (iRefcnt) iRefcnt->removeRef(); // remove ref for current memfrag
    373             // copy assigned object
    374             iMemfrag = x.getMemFrag();
    375             iRefcnt = x.getRefCounter();
    376             if (iRefcnt) iRefcnt->addRef(); // add ref for new memfrag
    377         }
    378         void bind(RefCounterMemoryFragment &x)
    379         {
    380             if (iRefcnt) iRefcnt->removeRef(); // remove ref for current memfrag
    381             // copy assigned object
    382             iMemfrag = x.iMemfrag;
    383             iRefcnt = x.iRefcnt;
    384             if (iRefcnt) iRefcnt->addRef(); // add ref for new memfrag
    385         }
    386 
    387         // update memory fragment with the same reference counter
    388         void update(OsclMemoryFragment &m)
    389         {
    390             iMemfrag.bind(m);
    391         }
    392         void update(void *aPtr)
    393         {
    394             iMemfrag.update(aPtr);
    395         }
    396         void update(uint32 aLen)
    397         {
    398             iMemfrag.update(aLen);
    399         }
    400 
    401     private:
    402         OsclMemoryFragWrapper iMemfrag;
    403         OsclRefCounter *iRefcnt;
    404 };
    405 
    406 
    407 #endif // HTTP_PARCOM_INTERNAL_H_
    408 
    409