big svn cleanup
[anytun.git] / src / Sockets / tests / httpd.cpp
blobe1106a0b6be70dc865250e81b28976236642e24b
1 #include <HttpdSocket.h>
2 #include <SocketHandler.h>
3 #include <ListenSocket.h>
4 #include <StdoutLog.h>
7 class sSocket : public HttpdSocket
9 public:
10 sSocket(ISocketHandler& h) : HttpdSocket(h) {
13 void Init()
15 if (GetParent() -> GetPort() == 443 || GetParent() -> GetPort() == 8443)
17 #ifdef HAVE_OPENSSL
18 EnableSSL();
19 #else
20 fprintf(stderr, "SSL not available\n");
21 #endif
25 void Exec()
27 CreateHeader();
28 GenerateDocument();
31 void CreateHeader()
33 SetStatus("200");
34 SetStatusText("OK");
35 fprintf(stderr, "Uri: '%s'\n", GetUri().c_str());
37 size_t x = 0;
38 for (size_t i = 0; i < GetUri().size(); i++)
39 if (GetUri()[i] == '.')
40 x = i;
41 std::string ext = GetUri().substr(x + 1);
42 if (ext == "gif" || ext == "jpg" || ext == "png")
43 AddResponseHeader("Content-type", "image/" + ext);
44 else
45 AddResponseHeader("Content-type", "text/" + ext);
47 AddResponseHeader("Connection", "close");
48 SendResponse();
51 void GenerateDocument()
53 std::string fn = GetUri().substr(1);
54 FILE *fil = fopen(fn.c_str(), "rb");
55 if (fil)
57 char slask[1000];
58 int n = fread(slask,1,1000,fil);
59 while (n > 0)
61 SendBuf(slask, n);
62 n = fread(slask,1,1000,fil);
64 fclose(fil);
66 else
68 SetStatus("404");
69 SetStatusText("Not Found");
71 SetCloseAndDelete();
74 #ifdef HAVE_OPENSSL
75 void InitSSLServer()
77 InitializeContext("httpd", "comb.pem", "", SSLv23_method());
79 #endif
84 int main(int argc, char *argv[])
86 std::string host = argc > 1 ? argv[1] : "www.alhem.net";
87 StdoutLog log;
88 SocketHandler h(&log);
89 ListenSocket<sSocket> l(h);
90 if (l.Bind(1028))
92 printf("Bind port 1028 failed\n");
93 return -1;
95 h.Add(&l);
96 ListenSocket<sSocket> l2(h);
97 if (l2.Bind(8443))
99 printf("Bind port 8443 failed\n");
100 return -1;
102 h.Add(&l2);
103 while (h.GetCount())
105 h.Select(1, 0);