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