big svn cleanup
[anytun.git] / src / Sockets / tests / sloppy_http.cpp
blob9ea79c1a29edd202a0ce0c66251957f9867991d8
1 #include <SocketHandler.h>
2 #include <TcpSocket.h>
5 class GetHttp : public TcpSocket
7 public:
8 GetHttp(ISocketHandler& h, const char *request) : TcpSocket(h)
9 , m_request(request) {}
11 void OnConnect() {
12 Send( m_request );
15 void OnRawData( const char *buf, size_t len ) {
16 if (len > 0) {
17 std::string tmp;
18 tmp.resize( len );
19 memcpy( &tmp[0], buf, len );
20 m_response += tmp;
24 const std::string& Response() {
25 return m_response;
28 private:
29 std::string m_request;
30 std::string m_response;
34 std::string get_http(const char *host, int port, const char *request)
36 SocketHandler h;
37 GetHttp sock(h, request);
38 sock.Open( host, port );
39 h.Add(&sock);
40 while (h.GetCount()) {
41 h.Select(1, 0);
43 return sock.Response();
47 int main(int argc, char *argv[])
49 std::string zz = get_http("www.alhem.net", 80, "GET /index.html HTTP/1.0\r\n"
50 "Host: www.alhem.net\r\n"
51 "\r\n");
52 printf("%s\n%d\n", zz.c_str(), zz.size());