big svn cleanup
[anytun.git] / src / Sockets / tests / http_post.cpp
blob3b0b830281cc6ea11c0b93272f63ba9d1673a458
1 #include <HttpdSocket.h>
2 #include <SocketHandler.h>
3 #include <ListenSocket.h>
4 #include <StdoutLog.h>
5 #include <HttpPostSocket.h>
6 #include <HttpPutSocket.h>
7 #include <HttpdForm.h>
10 class sSocket : public HttpdSocket
12 public:
13 sSocket(ISocketHandler& h) : HttpdSocket(h) {
16 void Init()
18 if (GetParent() -> GetPort() == 443 || GetParent() -> GetPort() == 8443)
20 #ifdef HAVE_OPENSSL
21 EnableSSL();
22 #else
23 fprintf(stderr, "SSL not available\n");
24 #endif
28 void Exec()
30 std::string name;
31 std::string value;
32 GetForm() -> getfirst(name, value);
33 while (name.size())
35 fprintf(stderr, "%s: '%s'\n", name.c_str(), value.c_str());
36 GetForm() -> getnext(name, value);
38 CreateHeader();
39 GenerateDocument();
42 void CreateHeader()
44 SetStatus("200");
45 SetStatusText("OK");
46 fprintf(stderr, "Uri: '%s'\n", GetUri().c_str());
48 size_t x = 0;
49 for (size_t i = 0; i < GetUri().size(); i++)
50 if (GetUri()[i] == '.')
51 x = i;
52 std::string ext = GetUri().substr(x + 1);
53 if (ext == "gif" || ext == "jpg" || ext == "png")
54 AddResponseHeader("Content-type", "image/" + ext);
55 else
56 AddResponseHeader("Content-type", "text/" + ext);
58 AddResponseHeader("Connection", "close");
59 SendResponse();
62 void GenerateDocument()
64 Send("<html></html>");
65 SetCloseAndDelete();
68 #ifdef HAVE_OPENSSL
69 void InitSSLServer()
71 InitializeContext("httpd", "comb.pem", "", SSLv23_method());
73 #endif
78 int main(int argc, char *argv[])
80 std::string host = argc > 1 ? argv[1] : "www.alhem.net";
81 StdoutLog log;
82 SocketHandler h(&log);
83 ListenSocket<sSocket> l(h);
84 if (l.Bind(1028))
86 printf("Bind port 1028 failed\n");
87 return -1;
89 h.Add(&l);
90 ListenSocket<sSocket> l2(h);
91 if (l2.Bind(8443))
93 printf("Bind port 8443 failed\n");
94 return -1;
96 h.Add(&l2);
97 HttpPostSocket sock(h, "http://localhost:1028/postdata");
98 sock.AddField("name", "value");
99 sock.Open();
100 h.Add(&sock);
101 while (h.GetCount())
103 h.Select(1, 0);