Mac addr added ICRS

Dependencies:   mbed

Fork of Email2Screen by Oliver Mattos

Committer:
Hello1024
Date:
Mon Nov 21 18:25:34 2011 +0000
Revision:
0:1619a6b826d7

        

Who changed what in which revision?

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