EthernetNetIf Compatibility.

Dependents:   XBeeWiFi_SPI_example

Fork of NetServicesSource by Donatien Garnier

Committer:
donatien
Date:
Fri Jun 11 16:05:15 2010 +0000
Revision:
0:632c9925f013

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:632c9925f013 1
donatien 0:632c9925f013 2 /*
donatien 0:632c9925f013 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
donatien 0:632c9925f013 4
donatien 0:632c9925f013 5 Permission is hereby granted, free of charge, to any person obtaining a copy
donatien 0:632c9925f013 6 of this software and associated documentation files (the "Software"), to deal
donatien 0:632c9925f013 7 in the Software without restriction, including without limitation the rights
donatien 0:632c9925f013 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
donatien 0:632c9925f013 9 copies of the Software, and to permit persons to whom the Software is
donatien 0:632c9925f013 10 furnished to do so, subject to the following conditions:
donatien 0:632c9925f013 11
donatien 0:632c9925f013 12 The above copyright notice and this permission notice shall be included in
donatien 0:632c9925f013 13 all copies or substantial portions of the Software.
donatien 0:632c9925f013 14
donatien 0:632c9925f013 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
donatien 0:632c9925f013 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
donatien 0:632c9925f013 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
donatien 0:632c9925f013 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
donatien 0:632c9925f013 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
donatien 0:632c9925f013 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
donatien 0:632c9925f013 21 THE SOFTWARE.
donatien 0:632c9925f013 22 */
donatien 0:632c9925f013 23
donatien 0:632c9925f013 24 #include "HTTPMap.h"
donatien 0:632c9925f013 25 #include "../../util/url.h"
donatien 0:632c9925f013 26
donatien 0:632c9925f013 27 //#define __DEBUG
donatien 0:632c9925f013 28 #include "dbg/dbg.h"
donatien 0:632c9925f013 29
donatien 0:632c9925f013 30 HTTPMap::HTTPMap(const string& keyValueSep /*= "="*/, const string& pairSep /*= "&"*/) :
donatien 0:632c9925f013 31 HTTPData(), Dictionary(), m_buf(), m_len(0), m_chunked(false), m_keyValueSep(keyValueSep), m_pairSep(pairSep)
donatien 0:632c9925f013 32 {
donatien 0:632c9925f013 33
donatien 0:632c9925f013 34 }
donatien 0:632c9925f013 35
donatien 0:632c9925f013 36 HTTPMap::~HTTPMap()
donatien 0:632c9925f013 37 {
donatien 0:632c9925f013 38
donatien 0:632c9925f013 39 }
donatien 0:632c9925f013 40
donatien 0:632c9925f013 41 void HTTPMap::clear()
donatien 0:632c9925f013 42 {
donatien 0:632c9925f013 43 m_buf.clear();
donatien 0:632c9925f013 44 }
donatien 0:632c9925f013 45
donatien 0:632c9925f013 46 int HTTPMap::read(char* buf, int len)
donatien 0:632c9925f013 47 {
donatien 0:632c9925f013 48 memcpy( (void*)buf, (void*)m_buf.data(), len );
donatien 0:632c9925f013 49 m_buf.erase(0, len); //Erase these chars
donatien 0:632c9925f013 50 return len;
donatien 0:632c9925f013 51 }
donatien 0:632c9925f013 52
donatien 0:632c9925f013 53 int HTTPMap::write(const char* buf, int len)
donatien 0:632c9925f013 54 {
donatien 0:632c9925f013 55 m_buf.append( buf, len );
donatien 0:632c9925f013 56 if( ( m_chunked && !len ) ||
donatien 0:632c9925f013 57 ( !m_chunked && ( m_buf.length() >= m_len ) ) ) //Last chunk or EOF
donatien 0:632c9925f013 58 {
donatien 0:632c9925f013 59 parseString();
donatien 0:632c9925f013 60 }
donatien 0:632c9925f013 61 return len;
donatien 0:632c9925f013 62 }
donatien 0:632c9925f013 63
donatien 0:632c9925f013 64 string HTTPMap::getDataType() //Internet media type for Content-Type header
donatien 0:632c9925f013 65 {
donatien 0:632c9925f013 66 return "application/x-www-form-urlencoded";
donatien 0:632c9925f013 67 }
donatien 0:632c9925f013 68
donatien 0:632c9925f013 69 void HTTPMap::setDataType(const string& type) //Internet media type from Content-Type header
donatien 0:632c9925f013 70 {
donatien 0:632c9925f013 71 //Do not really care here
donatien 0:632c9925f013 72 }
donatien 0:632c9925f013 73
donatien 0:632c9925f013 74 int HTTPMap::getDataLen() //For Content-Length header
donatien 0:632c9925f013 75 {
donatien 0:632c9925f013 76 //This will be called before any read occurs, so let's generate our string object
donatien 0:632c9925f013 77 //Generate string
donatien 0:632c9925f013 78 generateString();
donatien 0:632c9925f013 79 return m_len;
donatien 0:632c9925f013 80 }
donatien 0:632c9925f013 81
donatien 0:632c9925f013 82 bool HTTPMap::getIsChunked() //For Transfer-Encoding header
donatien 0:632c9925f013 83 {
donatien 0:632c9925f013 84 return false; //We don't want to chunk this
donatien 0:632c9925f013 85 }
donatien 0:632c9925f013 86
donatien 0:632c9925f013 87 void HTTPMap::setIsChunked(bool chunked) //From Transfer-Encoding header
donatien 0:632c9925f013 88 {
donatien 0:632c9925f013 89 m_chunked = chunked;
donatien 0:632c9925f013 90 }
donatien 0:632c9925f013 91
donatien 0:632c9925f013 92 void HTTPMap::setDataLen(int len) //From Content-Length header, or if the transfer is chunked, next chunk length
donatien 0:632c9925f013 93 {
donatien 0:632c9925f013 94 m_len += len; //Useful so that we can parse string when last byte is written if not chunked
donatien 0:632c9925f013 95 m_buf.reserve( m_len );
donatien 0:632c9925f013 96 }
donatien 0:632c9925f013 97
donatien 0:632c9925f013 98 void HTTPMap::generateString()
donatien 0:632c9925f013 99 {
donatien 0:632c9925f013 100 Dictionary::iterator it;
donatien 0:632c9925f013 101 bool first = true;
donatien 0:632c9925f013 102 while( !Dictionary::empty() )
donatien 0:632c9925f013 103 {
donatien 0:632c9925f013 104 if(!first)
donatien 0:632c9925f013 105 {
donatien 0:632c9925f013 106 m_buf.append( m_pairSep );
donatien 0:632c9925f013 107 }
donatien 0:632c9925f013 108 else
donatien 0:632c9925f013 109 {
donatien 0:632c9925f013 110 first = false;
donatien 0:632c9925f013 111 }
donatien 0:632c9925f013 112 it = Dictionary::begin();
donatien 0:632c9925f013 113 m_buf.append( Url::encode( (*it).first ) );
donatien 0:632c9925f013 114 m_buf.append( m_keyValueSep );
donatien 0:632c9925f013 115 m_buf.append( Url::encode( (*it).second ) );
donatien 0:632c9925f013 116 Dictionary::erase(it); //Free memory as we process data
donatien 0:632c9925f013 117 }
donatien 0:632c9925f013 118 m_len = m_buf.length();
donatien 0:632c9925f013 119 DBG("\r\nData [len %d]:\r\n%s\r\n", m_len, m_buf.c_str());
donatien 0:632c9925f013 120 }
donatien 0:632c9925f013 121
donatien 0:632c9925f013 122 void HTTPMap::parseString()
donatien 0:632c9925f013 123 {
donatien 0:632c9925f013 124 string key;
donatien 0:632c9925f013 125 string value;
donatien 0:632c9925f013 126
donatien 0:632c9925f013 127 int pos = 0;
donatien 0:632c9925f013 128
donatien 0:632c9925f013 129 int key_pos;
donatien 0:632c9925f013 130 int key_len;
donatien 0:632c9925f013 131
donatien 0:632c9925f013 132 int value_pos;
donatien 0:632c9925f013 133 int value_len;
donatien 0:632c9925f013 134
donatien 0:632c9925f013 135 bool eof = false;
donatien 0:632c9925f013 136 while(!eof)
donatien 0:632c9925f013 137 {
donatien 0:632c9925f013 138 key_pos = pos;
donatien 0:632c9925f013 139 value_pos = m_buf.find(m_keyValueSep, pos);
donatien 0:632c9925f013 140 if(value_pos < 0)
donatien 0:632c9925f013 141 {
donatien 0:632c9925f013 142 //Parse error, break
donatien 0:632c9925f013 143 break;
donatien 0:632c9925f013 144 }
donatien 0:632c9925f013 145
donatien 0:632c9925f013 146 key_len = value_pos - key_pos;
donatien 0:632c9925f013 147
donatien 0:632c9925f013 148 value_pos+= m_keyValueSep.length();
donatien 0:632c9925f013 149
donatien 0:632c9925f013 150 pos = m_buf.find( m_pairSep, value_pos );
donatien 0:632c9925f013 151 if(pos < 0) //Not found
donatien 0:632c9925f013 152 {
donatien 0:632c9925f013 153 pos = m_buf.length();
donatien 0:632c9925f013 154 eof = true;
donatien 0:632c9925f013 155 }
donatien 0:632c9925f013 156
donatien 0:632c9925f013 157 value_len = pos - value_pos;
donatien 0:632c9925f013 158
donatien 0:632c9925f013 159 pos += m_pairSep.length();
donatien 0:632c9925f013 160
donatien 0:632c9925f013 161 //Append elenent
donatien 0:632c9925f013 162 Dictionary::operator[]( Url::decode( m_buf.substr(key_pos, key_len) ) ) = Url::decode( m_buf.substr(value_pos, value_len) );
donatien 0:632c9925f013 163 }
donatien 0:632c9925f013 164
donatien 0:632c9925f013 165 m_buf.clear(); //Free data
donatien 0:632c9925f013 166
donatien 0:632c9925f013 167 }