summaryrefslogtreecommitdiff
path: root/src/Sockets/tests/sloppy_http.cpp
blob: 9ea79c1a29edd202a0ce0c66251957f9867991d8 (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
#include <SocketHandler.h>
#include <TcpSocket.h>


class GetHttp : public TcpSocket
{
public:
	GetHttp(ISocketHandler& h, const char *request) : TcpSocket(h)
	, m_request(request) {}

	void OnConnect() {
		Send( m_request );
	}

	void OnRawData( const char *buf, size_t len ) {
		if (len > 0) {
			std::string tmp;
			tmp.resize( len );
			memcpy( &tmp[0], buf, len );
			m_response += tmp;
		}
	}

	const std::string& Response() {
		return m_response;
	}

private:
	std::string m_request;
	std::string m_response;
};


std::string get_http(const char *host, int port, const char *request)
{
	SocketHandler h;
	GetHttp sock(h, request);
	sock.Open( host, port );
	h.Add(&sock);
	while (h.GetCount()) {
		h.Select(1, 0);
	}
	return sock.Response();
}


int main(int argc, char *argv[])
{
	std::string zz = get_http("www.alhem.net", 80, "GET /index.html HTTP/1.0\r\n"
		"Host: www.alhem.net\r\n"
		"\r\n");
	printf("%s\n%d\n", zz.c_str(), zz.size());
}