NetServices Stack source

Dependents:   HelloWorld ServoInterfaceBoardExample1 4180_Lab4

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTTPText.cpp Source File

HTTPText.cpp

00001 
00002 /*
00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #include "HTTPText.h"
00025 
00026 
00027 
00028 #define MIN(a,b) ((a)<(b)?(a):(b))
00029 
00030 HTTPText::HTTPText(const string& encoding /*= "text/html"*/, int maxSize /*= DEFAULT_MAX_MEM_ALLOC*/) : HTTPData(), m_buf(), m_encoding(encoding), m_maxSize(maxSize)
00031 {
00032 
00033 }
00034 
00035 HTTPText::~HTTPText()
00036 {
00037 
00038 }
00039 
00040 const char* HTTPText::gets() const
00041 {
00042   return m_buf.c_str();
00043 }
00044 
00045 void HTTPText::puts(const char* str)
00046 {
00047   m_buf = string(str);
00048 }
00049 
00050 string& HTTPText::get()
00051 {
00052   return m_buf;
00053 }
00054 
00055 void HTTPText::set(const string& str)
00056 {
00057   m_buf = str;
00058 }
00059 
00060 void HTTPText::clear()
00061 {
00062   m_buf.clear();
00063 }
00064 
00065 int HTTPText::read(char* buf, int len)
00066 {
00067   len = MIN( len, m_buf.length() );
00068   memcpy( (void*)buf, (void*)m_buf.data(), len );
00069   m_buf.erase(0, len); //Erase these chars
00070   return len;
00071 }
00072 
00073 int HTTPText::write(const char* buf, int len)
00074 {
00075   len = MIN( m_maxSize - m_buf.length(), len ); //Do not go over m_maxSize
00076   m_buf.append( buf, len );
00077   return len;
00078 }
00079   
00080 string HTTPText::getDataType() //Internet media type for Content-Type header
00081 {
00082   return m_encoding;
00083 }
00084 
00085 void HTTPText::setDataType(const string& type) //Internet media type from Content-Type header
00086 {
00087   //Do not really care here
00088 }
00089 
00090 bool HTTPText::getIsChunked() //For Transfer-Encoding header
00091 {
00092   return false; //We don't want to chunk this
00093 }
00094 
00095 void HTTPText::setIsChunked(bool chunked) //From Transfer-Encoding header
00096 {
00097   //Nothing to do
00098 }
00099   
00100 int HTTPText::getDataLen() //For Content-Length header
00101 {
00102   return m_buf.length();
00103 }
00104 
00105 void HTTPText::setDataLen(int len) //From Content-Length header, or if the transfer is chunked, next chunk length
00106 {
00107   m_buf.reserve(MIN(m_buf.length() + len,m_maxSize));
00108 }