241 lines
5.0 KiB
C++
241 lines
5.0 KiB
C++
#ifndef _PROTO_HTTPHEADER_HPP_
|
|
#define _PROTO_HTTPHEADER_HPP_
|
|
#ifndef _COMMON_BLOCK_HPP_
|
|
#include <common/block.hpp>
|
|
#endif
|
|
#ifndef _COMMON_STRING_HPP_
|
|
#include <common/string.hpp>
|
|
#endif
|
|
#ifndef _PROTO_SERIALIZEABLE_HPP_
|
|
#include <proto/serializeable.hpp>
|
|
#endif
|
|
|
|
class HTTPHeader : public Serializeable
|
|
{
|
|
public:
|
|
HTTPHeader();
|
|
HTTPHeader(Block<String> &headerLines);
|
|
HTTPHeader(const String &headerLines);
|
|
virtual ~HTTPHeader();
|
|
HTTPHeader &operator=(Block<String> &headerLines);
|
|
HTTPHeader &operator=(const String &headerLines);
|
|
|
|
const String &getDate(void)const;
|
|
void setDate(const String &date);
|
|
|
|
const String &getServer(void)const;
|
|
void setServer(const String &server);
|
|
|
|
const String &getLastModified(void)const;
|
|
void setLastModified(const String &lastModified);
|
|
|
|
const String &getETag(void)const;
|
|
void setETag(const String &eTag);
|
|
|
|
int getContentLength(void)const;
|
|
void setContentLength(int contentLength);
|
|
|
|
const String &getContentType(void)const;
|
|
void setContentType(const String &contentType);
|
|
|
|
const String &getAccept(void)const;
|
|
void setAccept(const String &accept);
|
|
|
|
String serialize(void);
|
|
private:
|
|
void parseHeader(Block<String> &headerLines);
|
|
void parseHeader(const String &headerLines);
|
|
void createHeaderLines(String strHeaders,Block<String> &headerLines);
|
|
|
|
String mDate;
|
|
String mServer;
|
|
String mLastModified;
|
|
String mETag;
|
|
String mAcceptRanges;
|
|
int mContentLength;
|
|
String mContentType;
|
|
String mAccept;
|
|
};
|
|
|
|
inline
|
|
HTTPHeader::HTTPHeader()
|
|
{
|
|
}
|
|
|
|
inline
|
|
HTTPHeader::HTTPHeader(Block<String> &headerLines)
|
|
{
|
|
parseHeader(headerLines);
|
|
}
|
|
|
|
inline
|
|
HTTPHeader::HTTPHeader(const String &headerLines)
|
|
{
|
|
parseHeader(headerLines);
|
|
}
|
|
|
|
inline
|
|
HTTPHeader::~HTTPHeader()
|
|
{
|
|
}
|
|
|
|
inline
|
|
HTTPHeader &HTTPHeader::operator=(Block<String> &headerLines)
|
|
{
|
|
parseHeader(headerLines);
|
|
return *this;
|
|
}
|
|
|
|
inline
|
|
HTTPHeader &HTTPHeader::operator=(const String &headerLines)
|
|
{
|
|
parseHeader(headerLines);
|
|
return *this;
|
|
}
|
|
|
|
inline
|
|
const String &HTTPHeader::getDate(void)const
|
|
{
|
|
return mDate;
|
|
}
|
|
|
|
inline
|
|
void HTTPHeader::setDate(const String &date)
|
|
{
|
|
mDate=date;
|
|
}
|
|
|
|
inline
|
|
const String &HTTPHeader::getServer(void)const
|
|
{
|
|
return mServer;
|
|
}
|
|
|
|
inline
|
|
void HTTPHeader::setServer(const String &server)
|
|
{
|
|
mServer=server;
|
|
}
|
|
|
|
inline
|
|
const String &HTTPHeader::getLastModified(void)const
|
|
{
|
|
return mLastModified;
|
|
}
|
|
|
|
inline
|
|
void HTTPHeader::setLastModified(const String &lastModified)
|
|
{
|
|
mLastModified=lastModified;
|
|
}
|
|
|
|
inline
|
|
const String &HTTPHeader::getETag(void)const
|
|
{
|
|
return mETag;
|
|
}
|
|
|
|
inline
|
|
void HTTPHeader::setETag(const String &eTag)
|
|
{
|
|
mETag=eTag;
|
|
}
|
|
|
|
inline
|
|
int HTTPHeader::getContentLength(void)const
|
|
{
|
|
return mContentLength;
|
|
}
|
|
|
|
inline
|
|
void HTTPHeader::setContentLength(int contentLength)
|
|
{
|
|
mContentLength=contentLength;
|
|
}
|
|
|
|
inline
|
|
const String &HTTPHeader::getContentType(void)const
|
|
{
|
|
return mContentType;
|
|
}
|
|
|
|
inline
|
|
void HTTPHeader::setContentType(const String &contentType)
|
|
{
|
|
mContentType=contentType;
|
|
}
|
|
|
|
inline
|
|
const String &HTTPHeader::getAccept(void)const
|
|
{
|
|
return mAccept;
|
|
}
|
|
|
|
inline
|
|
void HTTPHeader::setAccept(const String &accept)
|
|
{
|
|
mAccept=accept;
|
|
}
|
|
|
|
inline
|
|
void HTTPHeader::parseHeader(Block<String> &headerLines)
|
|
{
|
|
for(int index=0;index<headerLines.size();index++)
|
|
{
|
|
const String &headerLine=headerLines[index];
|
|
const String strToken=headerLine.betweenString(0,':');
|
|
if(strToken=="Date")mDate=headerLine.betweenString(' ',0);
|
|
else if(strToken=="Server")mServer=headerLine.betweenString(' ',0);
|
|
else if(strToken=="Last-Modified")mLastModified=headerLine.betweenString(' ',0);
|
|
else if(strToken=="ETag")mETag=headerLine.betweenString(' ',0);
|
|
else if(strToken=="Accept-Ranges")mAcceptRanges=headerLine.betweenString(' ',0);
|
|
else if(strToken=="Content-Length")mContentLength=headerLine.betweenString(' ',0).toInt();
|
|
else if(strToken=="Content-Type")mContentType=headerLine.betweenString(' ',0);
|
|
else if(strToken=="Accept")mAccept=headerLine.betweenString(' ',0);
|
|
}
|
|
}
|
|
|
|
inline
|
|
void HTTPHeader::parseHeader(const String &headerLines)
|
|
{
|
|
Block<String> blkHeaderLines;
|
|
createHeaderLines(headerLines,blkHeaderLines);
|
|
*this=blkHeaderLines;
|
|
}
|
|
|
|
inline
|
|
void HTTPHeader::createHeaderLines(String strHeaders,Block<String> &headerLines)
|
|
{
|
|
char *strPtr=0;
|
|
headerLines.remove();
|
|
if(strHeaders.isNull())return;
|
|
strPtr=strHeaders.str();
|
|
strPtr=::strtok(strPtr,"\n\0");
|
|
while(true)
|
|
{
|
|
if(!strPtr)break;
|
|
String str=strPtr;
|
|
str.removeTokens("\r\n");
|
|
headerLines.insert(&str);
|
|
strPtr=::strtok(0,"\n\0");
|
|
}
|
|
}
|
|
|
|
inline
|
|
String HTTPHeader::serialize(void)
|
|
{
|
|
String strHeader;
|
|
String crlf="\r\n";
|
|
|
|
if(!mDate.isNull())strHeader+=String("Date: ")+mDate+crlf;
|
|
if(!mServer.isNull())strHeader+=String("Server: ")+mServer+crlf;
|
|
if(!mLastModified.isNull())strHeader+=String("Last-Modified: ")+mLastModified+crlf;
|
|
if(!mETag.isNull())strHeader+=String("ETag: ")+mETag+crlf;
|
|
if(!mAcceptRanges.isNull())strHeader+=String("Accept-Ranges: ")+mAcceptRanges+crlf;
|
|
if(0!=mContentLength)strHeader+=String("Content-Length :")+String().fromInt(mContentLength)+crlf;
|
|
if(mContentType.isNull())strHeader+=String("Content-Type: ")+mContentType+crlf;
|
|
return strHeader;
|
|
}
|
|
#endif
|
|
|