summaryrefslogtreecommitdiff
path: root/src/Sockets/tests/https.cpp
blob: 45b36ff80fc23fd19fbc42b5bbeb177c2d67ad04 (plain) (blame)
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
#include <HTTPSocket.h>
#include <SocketHandler.h>
#include <ListenSocket.h>
#include <Exception.h>


class tSocket : public HTTPSocket
{
public:
	tSocket(ISocketHandler& h) : HTTPSocket(h)
	, m_first(false)
	, m_sz(0) {
		EnableSSL();
	}

	tSocket(ISocketHandler& h, const std::string& host) : HTTPSocket(h), m_host(host)
	, m_first(false)
	, m_sz(0) {
		EnableSSL();
		Open(host, 4443);
	}

	void InitSSLServer() {
		InitializeContext("", "comb.pem", "", SSLv23_method());
	}

	void OnSSLAccept() {
		printf("OnSSLAccept()\n");
		HTTPSocket::OnSSLAccept();
	}

	void OnSSLConnect() {
		printf("OnSSLConnect()\n");
		HTTPSocket::OnSSLConnect();
	}

	void OnAccept() {
printf("OnAccept\n");
	}

	void OnConnect() {
printf("OnConnect\n");
		Send("GET /index.html HTTP/1.0\r\n");
		Send("Host: " + m_host + "\r\n");
		Send("\r\n");
	}

	void OnFirst() {
	}

	void OnHeader(const std::string& key, const std::string& value) {
		fprintf(stderr, "%s: %s\n", key.c_str(), value.c_str());
	}

	void OnHeaderComplete() {
		fprintf(stderr, "\n");
	}

	void OnData(const char *buf, size_t sz) {
		if (1||!m_first)
		{
			std::string str = buf;
			str.resize( sz );
			printf("%s", str.c_str());
			m_first = true;
		}
		m_sz += sz;
	}

	void OnDelete() {
		fprintf(stderr, "Content length: %d\n", m_sz);
	}

private:
	std::string m_host;
	bool m_first;
	size_t m_sz;
};


int main(int argc, char *argv[])
{
	try
	{
		SocketHandler h;
		if (argc > 1 && !strcmp(argv[1], "-server"))
		{
			ListenSocket<tSocket> l(h);
			l.Bind(4443);
			h.Add(&l);
			while (h.GetCount())
			{
				h.Select(1, 0);
			}
		}
		else
		{
			std::string host = argc > 1 ? argv[1] : "www.alhem.net";
			tSocket sock(h, host);
			h.Add(&sock);
			while (h.GetCount())
			{
				h.Select(1, 0);
			}
		}
	}
	catch (const Exception& e)
	{
		printf("%s\n", e.ToString().c_str());
	}
}