RobT's fork of HTTPClient library

Fork of HTTPClient_ToBeRemoved by Donatien Garnier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTTPMap.cpp Source File

HTTPMap.cpp

00001 /* HTTPMap.cpp */
00002 /*
00003 Copyright (C) 2012 ARM Limited.
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a copy of
00006 this software and associated documentation files (the "Software"), to deal in
00007 the Software without restriction, including without limitation the rights to
00008 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
00009 of the Software, and to permit persons to whom the Software is furnished to do
00010 so, subject to the following conditions:
00011 
00012 The above copyright notice and this permission notice shall be included in all
00013 copies or substantial portions of the Software.
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00021 SOFTWARE.
00022 */
00023 
00024 #include "core/fwk.h"
00025 
00026 #include "HTTPMap.h"
00027 
00028 #include <cstring>
00029 
00030 #include <cctype>
00031 
00032 HTTPMap::HTTPMap() : m_pos(0), m_count(0)
00033 {
00034 
00035 }
00036 
00037 void HTTPMap::put(const char* key, const char* value)
00038 {
00039   if(m_count >= HTTPMAP_TABLE_SIZE)
00040   {
00041     return;
00042   }
00043   m_keys[m_count] = key;
00044   m_values[m_count] = value;
00045   m_count++;
00046 }
00047 
00048 void HTTPMap::clear()
00049 {
00050   m_count = 0;
00051   m_pos = 0;
00052 }
00053 
00054 
00055 /*virtual*/ int HTTPMap::read(char* buf, size_t len, size_t* pReadLen)
00056 {
00057   if(m_pos >= m_count)
00058   {
00059     *pReadLen = 0;
00060     m_pos = 0;
00061     return OK;
00062   }
00063 
00064   //URL encode
00065   char* out = buf;
00066   const char* in = m_keys[m_pos];
00067   if( (m_pos != 0) && (out - buf < len - 1) )
00068   {
00069     *out='&';
00070     out++;
00071   }
00072 
00073   while( (*in != '\0') && (out - buf < len - 3) )
00074   {
00075     if (std::isalnum(*in) || *in == '-' || *in == '_' || *in == '.' || *in == '~')
00076     {
00077       *out = *in;
00078       out++;
00079     }
00080     else if( *in == ' ' )
00081     {
00082       *out='+';
00083       out++;
00084     }
00085     else
00086     {
00087       char hex[] = "0123456789abcdef";
00088       *out='%';
00089       out++;
00090       *out=hex[(*in>>4)&0xf];
00091       out++;
00092       *out=hex[(*in)&0xf];
00093       out++;
00094     }
00095     in++;
00096   }
00097 
00098   if( out - buf < len - 1 )
00099   {
00100     *out='=';
00101     out++;
00102   }
00103 
00104   in = m_values[m_pos];
00105   while( (*in != '\0') && (out - buf < len - 3) )
00106   {
00107     if (std::isalnum(*in) || *in == '-' || *in == '_' || *in == '.' || *in == '~')
00108     {
00109       *out = *in;
00110       out++;
00111     }
00112     else if( *in == ' ' )
00113     {
00114       *out='+';
00115       out++;
00116     }
00117     else
00118     {
00119       char hex[] = "0123456789abcdef";
00120       *out='%';
00121       out++;
00122       *out=hex[(*in>>4)&0xf];
00123       out++;
00124       *out=hex[(*in)&0xf];
00125       out++;
00126     }
00127     in++;
00128   }
00129 
00130   *pReadLen = out - buf;
00131 
00132   m_pos++;
00133   return OK;
00134 }
00135 
00136 /*virtual*/ int HTTPMap::getDataType(char* type, size_t maxTypeLen) //Internet media type for Content-Type header
00137 {
00138   strncpy(type, "application/x-www-form-urlencoded", maxTypeLen-1);
00139   type[maxTypeLen-1] = '\0';
00140   return OK;
00141 }
00142 
00143 /*virtual*/ bool HTTPMap::getIsChunked() //For Transfer-Encoding header
00144 {
00145   return false; ////Data is computed one key/value pair at a time
00146 }
00147 
00148 /*virtual*/ size_t HTTPMap::getDataLen() //For Content-Length header
00149 {
00150   size_t count = 0;
00151   for(size_t i = 0; i< m_count; i++)
00152   {
00153     //URL encode
00154     const char* in = m_keys[i];
00155     if( i != 0 )
00156     {
00157       count++;
00158     }
00159 
00160     while( (*in != '\0') )
00161     {
00162       if (std::isalnum(*in) || *in == '-' || *in == '_' || *in == '.' || *in == '~')
00163       {
00164         count++;
00165       }
00166       else if( *in == ' ' )
00167       {
00168         count++;
00169       }
00170       else
00171       {
00172         count+=3;
00173       }
00174       in++;
00175     }
00176 
00177     count ++;
00178 
00179     in = m_values[i];
00180     while( (*in != '\0') )
00181     {
00182       if (std::isalnum(*in) || *in == '-' || *in == '_' || *in == '.' || *in == '~')
00183       {
00184         count++;
00185       }
00186       else if( *in == ' ' )
00187       {
00188         count++;
00189       }
00190       else
00191       {
00192         count+=3;
00193       }
00194       in++;
00195     }
00196   }
00197   return count;
00198 }