httpclient

Dependents:   Lab_6

Committer:
rr387
Date:
Thu Dec 30 15:21:34 2021 +0000
Revision:
0:a988a72f184b
retes

Who changed what in which revision?

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