Simple IoT Board用のHTTP GETサンプルです。

Dependencies:   SimpleIoTBoardLib mbed

Committer:
jksoft
Date:
Sun Nov 15 13:29:46 2015 +0000
Revision:
0:0ae1235453b0
??

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:0ae1235453b0 1 /* HTTPText.cpp */
jksoft 0:0ae1235453b0 2 /* Copyright (C) 2012 mbed.org, MIT License
jksoft 0:0ae1235453b0 3 *
jksoft 0:0ae1235453b0 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
jksoft 0:0ae1235453b0 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
jksoft 0:0ae1235453b0 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
jksoft 0:0ae1235453b0 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
jksoft 0:0ae1235453b0 8 * furnished to do so, subject to the following conditions:
jksoft 0:0ae1235453b0 9 *
jksoft 0:0ae1235453b0 10 * The above copyright notice and this permission notice shall be included in all copies or
jksoft 0:0ae1235453b0 11 * substantial portions of the Software.
jksoft 0:0ae1235453b0 12 *
jksoft 0:0ae1235453b0 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
jksoft 0:0ae1235453b0 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
jksoft 0:0ae1235453b0 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
jksoft 0:0ae1235453b0 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jksoft 0:0ae1235453b0 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
jksoft 0:0ae1235453b0 18 */
jksoft 0:0ae1235453b0 19
jksoft 0:0ae1235453b0 20 #include "HTTPText.h"
jksoft 0:0ae1235453b0 21
jksoft 0:0ae1235453b0 22 #include <cstring>
jksoft 0:0ae1235453b0 23
jksoft 0:0ae1235453b0 24 #define OK 0
jksoft 0:0ae1235453b0 25
jksoft 0:0ae1235453b0 26 using std::memcpy;
jksoft 0:0ae1235453b0 27 using std::strncpy;
jksoft 0:0ae1235453b0 28 using std::strlen;
jksoft 0:0ae1235453b0 29
jksoft 0:0ae1235453b0 30 #define MIN(x,y) (((x)<(y))?(x):(y))
jksoft 0:0ae1235453b0 31
jksoft 0:0ae1235453b0 32 HTTPText::HTTPText(char* str) : m_str(str), m_pos(0)
jksoft 0:0ae1235453b0 33 {
jksoft 0:0ae1235453b0 34 m_size = strlen(str) + 1;
jksoft 0:0ae1235453b0 35 }
jksoft 0:0ae1235453b0 36
jksoft 0:0ae1235453b0 37 HTTPText::HTTPText(char* str, size_t size) : m_str(str), m_size(size), m_pos(0)
jksoft 0:0ae1235453b0 38 {
jksoft 0:0ae1235453b0 39
jksoft 0:0ae1235453b0 40 }
jksoft 0:0ae1235453b0 41
jksoft 0:0ae1235453b0 42 //IHTTPDataIn
jksoft 0:0ae1235453b0 43 /*virtual*/ void HTTPText::readReset()
jksoft 0:0ae1235453b0 44 {
jksoft 0:0ae1235453b0 45 m_pos = 0;
jksoft 0:0ae1235453b0 46 }
jksoft 0:0ae1235453b0 47
jksoft 0:0ae1235453b0 48 /*virtual*/ int HTTPText::read(char* buf, size_t len, size_t* pReadLen)
jksoft 0:0ae1235453b0 49 {
jksoft 0:0ae1235453b0 50 *pReadLen = MIN(len, m_size - 1 - m_pos);
jksoft 0:0ae1235453b0 51 memcpy(buf, m_str + m_pos, *pReadLen);
jksoft 0:0ae1235453b0 52 m_pos += *pReadLen;
jksoft 0:0ae1235453b0 53 return OK;
jksoft 0:0ae1235453b0 54 }
jksoft 0:0ae1235453b0 55
jksoft 0:0ae1235453b0 56 /*virtual*/ int HTTPText::getDataType(char* type, size_t maxTypeLen) //Internet media type for Content-Type header
jksoft 0:0ae1235453b0 57 {
jksoft 0:0ae1235453b0 58 #if 1
jksoft 0:0ae1235453b0 59 strncpy(type, "application/json", maxTypeLen-1);
jksoft 0:0ae1235453b0 60 #else
jksoft 0:0ae1235453b0 61 strncpy(type, "text/plain", maxTypeLen-1);
jksoft 0:0ae1235453b0 62 #endif
jksoft 0:0ae1235453b0 63 type[maxTypeLen-1] = '\0';
jksoft 0:0ae1235453b0 64 return OK;
jksoft 0:0ae1235453b0 65 }
jksoft 0:0ae1235453b0 66
jksoft 0:0ae1235453b0 67 /*virtual*/ bool HTTPText::getIsChunked() //For Transfer-Encoding header
jksoft 0:0ae1235453b0 68 {
jksoft 0:0ae1235453b0 69 return false;
jksoft 0:0ae1235453b0 70 }
jksoft 0:0ae1235453b0 71
jksoft 0:0ae1235453b0 72 /*virtual*/ size_t HTTPText::getDataLen() //For Content-Length header
jksoft 0:0ae1235453b0 73 {
jksoft 0:0ae1235453b0 74 return m_size - 1;
jksoft 0:0ae1235453b0 75 }
jksoft 0:0ae1235453b0 76
jksoft 0:0ae1235453b0 77 //IHTTPDataOut
jksoft 0:0ae1235453b0 78 /*virtual*/ void HTTPText::writeReset()
jksoft 0:0ae1235453b0 79 {
jksoft 0:0ae1235453b0 80 m_pos = 0;
jksoft 0:0ae1235453b0 81 }
jksoft 0:0ae1235453b0 82
jksoft 0:0ae1235453b0 83 /*virtual*/ int HTTPText::write(const char* buf, size_t len)
jksoft 0:0ae1235453b0 84 {
jksoft 0:0ae1235453b0 85 size_t writeLen = MIN(len, m_size - 1 - m_pos);
jksoft 0:0ae1235453b0 86 memcpy(m_str + m_pos, buf, writeLen);
jksoft 0:0ae1235453b0 87 m_pos += writeLen;
jksoft 0:0ae1235453b0 88 m_str[m_pos] = '\0';
jksoft 0:0ae1235453b0 89 return OK;
jksoft 0:0ae1235453b0 90 }
jksoft 0:0ae1235453b0 91
jksoft 0:0ae1235453b0 92 /*virtual*/ void HTTPText::setDataType(const char* type) //Internet media type from Content-Type header
jksoft 0:0ae1235453b0 93 {
jksoft 0:0ae1235453b0 94
jksoft 0:0ae1235453b0 95 }
jksoft 0:0ae1235453b0 96
jksoft 0:0ae1235453b0 97 /*virtual*/ void HTTPText::setIsChunked(bool chunked) //From Transfer-Encoding header
jksoft 0:0ae1235453b0 98 {
jksoft 0:0ae1235453b0 99
jksoft 0:0ae1235453b0 100 }
jksoft 0:0ae1235453b0 101
jksoft 0:0ae1235453b0 102 /*virtual*/ void HTTPText::setDataLen(size_t len) //From Content-Length header, or if the transfer is chunked, next chunk length
jksoft 0:0ae1235453b0 103 {
jksoft 0:0ae1235453b0 104
jksoft 0:0ae1235453b0 105 }
jksoft 0:0ae1235453b0 106
jksoft 0:0ae1235453b0 107
jksoft 0:0ae1235453b0 108