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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
/**
* ======================== legal notice ======================
*
* File: HTTPMessage.cc
* Created: 3. Juli 2012, 17
* Author: <a href="mailto:geronimo013@gmx.de">Geronimo</a>
* Project: libnetworking: classes for tcp/ip sockets and http-protocol handling
*
* CMP - compound media player
*
* is a client/server mediaplayer intended to play any media from any workstation
* without the need to export or mount shares. cmps is an easy to use backend
* with a (ready to use) HTML-interface. Additionally the backend supports
* authentication via HTTP-digest authorization.
* cmpc is a client with vdr-like osd-menues.
*
* Copyright (c) 2012 Reinhard Mantey, some rights reserved!
* published under Creative Commons by-sa
* For details see http://creativecommons.org/licenses/by-sa/3.0/
*
* The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp
*
* --------------------------------------------------------------
*/
#include <HTTPMessage.h>
#include <Authorization.h>
#include <stdio.h>
#include <stddef.h>
#include <util.h>
static void freeHeaderCallback(void *elem)
{
free(elem);
}
cHTTPMessage::cHTTPMessage(HTTPProtocol Proto)
: protocol(Proto)
, timeStamp(0)
, header(freeHeaderCallback)
, auth(NULL)
, contentSize(0)
, contentType(NULL)
{
}
cHTTPMessage::~cHTTPMessage()
{
if (auth) delete auth;
FREE(contentType);
}
char *cHTTPMessage::FormatTime(time_t timeStamp)
{
struct tm *t = gmtime(&timeStamp);
char buf[128];
strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S GMT", t);
return strdup(buf);
}
void cHTTPMessage::SetHeader(const char *Name, const char *Value)
{
header.put(Name, Value ? strdup(Value) : NULL);
}
const char *cHTTPMessage::GetHeader(const char* Name) const
{
return (const char *) header.get(Name);
}
size_t cHTTPMessage::WritePrefix(char *Buffer, size_t BufSize)
///< writes message prefix to existing buffer. Prefix is the message without
///< the content body, so internal and external content will be processed
///< separately.
///< IMPORTANT: content size and content type has to be set before this call!
///< or this information needs to be added by custom call.
{
cManagedMap::const_iterator it = Headers().begin();
size_t n = WriteFirstLine(Buffer, BufSize);
strcpy(Buffer + n, "Date: ");
n += 6;
n += WriteTime(Buffer + n, BufSize - n);
while (it != Headers().end()) {
n += snprintf(Buffer + n, BufSize - n, "%s: %s\r\n", it->first.c_str(), (const char *) it->second);
++it;
}
if (auth) n += auth->Write(Buffer + n, BufSize - n);
if (ContentSize() > 0) {
n += snprintf(Buffer + n, BufSize - n, "Content-Type: %s\r\n", ContentType());
n += snprintf(Buffer + n, BufSize - n, "Content-Length: %ld\r\n", ContentSize());
if (n < BufSize - 3) {
Buffer[n++] = '\n'; // this is the separator between header and content!
Buffer[n] = 0;
}
}
return n;
}
int cHTTPMessage::WriteTime(char *Buffer, size_t BufSize)
{
if (!timeStamp) timeStamp = time(NULL);
struct tm *t = gmtime(&timeStamp);
return strftime(Buffer, BufSize, "%a, %d %b %Y %H:%M:%S GMT\r\n", t);
}
void cHTTPMessage::Reset()
{
header.clear();
contentSize = 0;
}
size_t cHTTPMessage::ContentSize() const
{
return contentSize;
}
void cHTTPMessage::SetContentType(const char* ContentType)
{
FREE(contentType);
contentType = ContentType ? strdup(ContentType) : NULL;
}
void cHTTPMessage::SetAuthorization(cAuthorization* Authorization)
{
if (auth) {
delete auth;
auth = NULL;
}
auth = Authorization;
}
const char *cHTTPMessage::ProtocolString(void) {
switch (protocol) {
case HTTP_1_0: return "HTTP/1.0";
case HTTP_1_1: return "HTTP/1.1";
default: return "Unknown/Unsupported";
}
}
void cHTTPMessage::Dump(void )
{
cManagedMap::const_iterator it = Headers().begin();
char buf[1024];
WriteFirstLine(buf, sizeof(buf));
printf("=========== Dump HTTP message ==============\n");
printf("Protocol: %s\n", ProtocolString());
printf("first line: %s\n", buf);
printf("header entries:\n");
while (it != Headers().end()) {
printf("\t[%s] ==> >|%s|<\n", it->first.c_str(), (const char *) it->second);
++it;
}
if (auth) auth->Dump();
if (ContentSize()) {
printf("---------- content (%ld Bytes of type %s) -------------\n"
, ContentSize(), ContentType());
}
else printf(">>> NO Content!\n");
printf("========== End Dump HTTP message ===========\n");
}
|