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
|
#include <HttpdSocket.h>
#include <SocketHandler.h>
#include <ListenSocket.h>
#include <StdoutLog.h>
#include <HttpPostSocket.h>
#include <HttpPutSocket.h>
#include <HttpdForm.h>
class sSocket : public HttpdSocket
{
public:
sSocket(ISocketHandler& h) : HttpdSocket(h) {
}
void Init()
{
if (GetParent() -> GetPort() == 443 || GetParent() -> GetPort() == 8443)
{
#ifdef HAVE_OPENSSL
EnableSSL();
#else
fprintf(stderr, "SSL not available\n");
#endif
}
}
void Exec()
{
std::string name;
std::string value;
GetForm() -> getfirst(name, value);
while (name.size())
{
fprintf(stderr, "%s: '%s'\n", name.c_str(), value.c_str());
GetForm() -> getnext(name, value);
}
CreateHeader();
GenerateDocument();
}
void CreateHeader()
{
SetStatus("200");
SetStatusText("OK");
fprintf(stderr, "Uri: '%s'\n", GetUri().c_str());
{
size_t x = 0;
for (size_t i = 0; i < GetUri().size(); i++)
if (GetUri()[i] == '.')
x = i;
std::string ext = GetUri().substr(x + 1);
if (ext == "gif" || ext == "jpg" || ext == "png")
AddResponseHeader("Content-type", "image/" + ext);
else
AddResponseHeader("Content-type", "text/" + ext);
}
AddResponseHeader("Connection", "close");
SendResponse();
}
void GenerateDocument()
{
Send("<html></html>");
SetCloseAndDelete();
}
#ifdef HAVE_OPENSSL
void InitSSLServer()
{
InitializeContext("httpd", "comb.pem", "", SSLv23_method());
}
#endif
};
int main(int argc, char *argv[])
{
std::string host = argc > 1 ? argv[1] : "www.alhem.net";
StdoutLog log;
SocketHandler h(&log);
ListenSocket<sSocket> l(h);
if (l.Bind(1028))
{
printf("Bind port 1028 failed\n");
return -1;
}
h.Add(&l);
ListenSocket<sSocket> l2(h);
if (l2.Bind(8443))
{
printf("Bind port 8443 failed\n");
return -1;
}
h.Add(&l2);
HttpPostSocket sock(h, "http://localhost:1028/postdata");
sock.AddField("name", "value");
sock.Open();
h.Add(&sock);
while (h.GetCount())
{
h.Select(1, 0);
}
}
|