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
166
167
168
169
170
171
|
/**
* ======================== legal notice ======================
*
* File: HTTPRequest.cc
* Created: 3. Juli 2012, 17:54
* 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 <HTTPRequest.h>
#include <HTTPResponse.h>
#include <Authorization.h>
#include <Principal.h>
#include <Logging.h>
#include <util.h>
#include <stdio.h>
cHTTPRequest::cHTTPRequest(const char *MessageBuf)
: method(INVALID)
, url(NULL)
{
ParseMessage(MessageBuf);
}
cHTTPRequest::cHTTPRequest(cHTTPRequest::HTTPRequestMethod Method, const char* Url)
: method(Method)
, url(Url)
{
}
cHTTPRequest::cHTTPRequest(const cHTTPResponse &res, const char *Url)
: method(cHTTPRequest::GET)
, url(Url)
{
SetAuthorization(new cAuthorization(*res.Authorization()));
Authorization()->SetUri(Url);
}
cHTTPRequest::~cHTTPRequest()
{
}
const char *requestMethod2String(cHTTPRequest::HTTPRequestMethod Method)
{
switch (Method) {
case cHTTPRequest::GET: return "GET";
case cHTTPRequest::POST: return "POST";
default: return "unknown/unsupported";
}
}
void cHTTPRequest::SetURL(const char *Url)
{
url.ParseURL(Url);
}
const char *cHTTPRequest::ClientHost(void ) const
{
return (const char *) Headers().get("Host");
}
const char *cHTTPRequest::UserAgent() const
{
return (const char *) Headers().get("User-Agent");
}
void cHTTPRequest::SetUser(const char *UserID, const char *Password)
{
char *uri = Url().ToString();
this->Authorization()->CalculateResponse(uri, UserID, Password);
free(uri);
}
static const char *fetchLine(char *Buf, int BufSize, const char *Source)
{
const char *end = strchr(Source, '\r');
int len = 0;
if (!end) end = strchr(Source, '\n');
if (!end) end = Source + strlen(Source);
len = end - Source;
if (len > BufSize) len = BufSize - 1;
strncpy(Buf, Source, len);
Buf[len] = 0;
end = Source + len;
return end;
}
void cHTTPRequest::ParseMessage(const char *RequestBuf)
{
char scratch[512];
const char *start = RequestBuf, *end = strchr(start, ' ');
const char *name;
char *value;
if (strncasecmp("GET", start, end - start)) SetMethod(GET);
else if (strncasecmp("POST", start, end - start)) SetMethod(POST);
start = end;
while (*start == ' ') ++start;
end = strchr(start, ' ');
// printf("request-URI %*.*s\n", (int)(end - start), (int)(end - start), start);
if ((end - start) >= (int)sizeof(scratch)) {
esyslog("URI exhausted buffer - abort request!");
return;
}
else {
strncpy(scratch, start, end - start);
scratch[end - start] = 0;
SetURL(scratch);
}
start = end;
while (*start == ' ') ++start;
end = strchr(start, '\r');
if (!end) end = strchr(start, '\n');
// printf("http-protocol %*.*s\n", (int)(end - start), (int)(end - start), start);
if (!strncmp("HTTP/1.1", start, end - start)) SetProtocol(HTTP_1_1);
else SetProtocol(HTTP_1_0);
for (start = end; start && *start; start = end) {
if (*start == '\r') ++start;
if (*start == '\n') ++start;
if (!*start) break;
end = fetchLine(scratch, sizeof(scratch), start);
// printf("a line from request (%03d) [%s]\n", strlen(scratch), scratch);
name = scratch;
value = strchr((char *)name, ':');
if (value) {
if (value) *value++ = 0;
if (*value == ' ') *value++ = 0;
SetHeader(name, value);
}
else {
// printf("possibly end of header ...\n");
continue;
}
// printf("\nresult - name [%s] => |>%s<|\n", name, value);
}
}
size_t cHTTPRequest::WriteFirstLine(char* Buffer, size_t BufSize)
{
size_t n = snprintf(Buffer, BufSize, "%s ", requestMethod2String(Method()));
int tmp = url.WriteBuf(Buffer + n, BufSize - n);
if (tmp > 0) n += tmp;
n += snprintf(Buffer + n, BufSize - n, " %s\n", ProtocolString());
return n;
}
|