2 /* Copyright (C) 2012 mbed.org, MIT License
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in all copies or
11 * substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #define MIN(x,y) (((x)<(y))?(x):(y))
32 HTTPText::HTTPText(char* str) : m_str(str), m_pos(0)
34 m_size = strlen(str) + 1;
37 HTTPText::HTTPText(char* str, size_t size) : m_str(str), m_size(size), m_pos(0)
43 /*virtual*/ int HTTPText::read(char* buf, size_t len, size_t* pReadLen)
45 *pReadLen = MIN(len, m_size - 1 - m_pos);
46 memcpy(buf, m_str + m_pos, *pReadLen);
51 /*virtual*/ int HTTPText::getDataType(char* type, size_t maxTypeLen) //Internet media type for Content-Type header
53 strncpy(type, "text/plain", maxTypeLen-1);
54 type[maxTypeLen-1] = '\0';
58 /*virtual*/ bool HTTPText::getIsChunked() //For Transfer-Encoding header
63 /*virtual*/ size_t HTTPText::getDataLen() //For Content-Length header
69 /*virtual*/ int HTTPText::write(const char* buf, size_t len)
71 size_t writeLen = MIN(len, m_size - 1 - m_pos);
72 memcpy(m_str + m_pos, buf, writeLen);
78 /*virtual*/ void HTTPText::setDataType(const char* type) //Internet media type from Content-Type header
83 /*virtual*/ void HTTPText::setIsChunked(bool chunked) //From Transfer-Encoding header
88 /*virtual*/ void HTTPText::setDataLen(size_t len) //From Content-Length header, or if the transfer is chunked, next chunk length