summaryrefslogtreecommitdiffstats
path: root/src/HTTPServer/HTTPMessage.h
blob: 4af5e471b1f0143de74cff82ce2bf9099b718244 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

// HTTPMessage.h

// Declares the cHTTPMessage class representing the common ancestor for HTTP request and response classes





#pragma once

#include "EnvelopeParser.h"





class cHTTPMessage
{
public:
	enum eStatus
	{
		HTTP_OK = 200,
		HTTP_BAD_REQUEST = 400,
	} ;

	enum eKind
	{
		mkRequest,
		mkResponse,
	} ;

	cHTTPMessage(eKind a_Kind);

	// Force a virtual destructor in all descendants
	virtual ~cHTTPMessage() {}

	/** Adds a header into the internal map of headers. Recognizes special headers: Content-Type and Content-Length */
	void AddHeader(const AString & a_Key, const AString & a_Value);

	void SetContentType  (const AString & a_ContentType) { m_ContentType   = a_ContentType; }
	void SetContentLength(size_t a_ContentLength)        { m_ContentLength = a_ContentLength; }

	const AString & GetContentType  (void) const { return m_ContentType; }
	size_t          GetContentLength(void) const { return m_ContentLength; }

protected:
	typedef std::map<AString, AString> cNameValueMap;

	eKind m_Kind;

	AStringMap m_Headers;

	/** Type of the content; parsed by AddHeader(), set directly by SetContentLength() */
	AString m_ContentType;

	/** Length of the content that is to be received.
	AString::npos when the object is created.
	Parsed by AddHeader() or set directly by SetContentLength() */
	size_t m_ContentLength;
} ;





class cHTTPResponse :
	public cHTTPMessage
{
	typedef cHTTPMessage super;

public:
	cHTTPResponse(void);

	/** Appends the response to the specified datastream - response line and headers.
	The body will be sent later directly through cConnection::Send()
	*/
	void AppendToData(AString & a_DataStream) const;
} ;