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