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