-
Notifications
You must be signed in to change notification settings - Fork 35
/
test.cpp
129 lines (97 loc) · 2.42 KB
/
test.cpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "happyhttp.h"
#include <cstdio>
#include <cstring>
#ifdef WIN32
#include <winsock2.h>
#endif // WIN32
int count=0;
void OnBegin( const happyhttp::Response* r, void* userdata )
{
printf( "BEGIN (%d %s)\n", r->getstatus(), r->getreason() );
count = 0;
}
void OnData( const happyhttp::Response* r, void* userdata, const unsigned char* data, int n )
{
fwrite( data,1,n, stdout );
count += n;
}
void OnComplete( const happyhttp::Response* r, void* userdata )
{
printf( "COMPLETE (%d bytes)\n", count );
}
void Test1()
{
puts("-----------------Test1------------------------" );
// simple simple GET
happyhttp::Connection conn( "www.scumways.com", 80 );
conn.setcallbacks( OnBegin, OnData, OnComplete, 0 );
conn.request( "GET", "/happyhttp/test.php", 0, 0,0 );
while( conn.outstanding() )
conn.pump();
}
void Test2()
{
puts("-----------------Test2------------------------" );
// POST using high-level request interface
const char* headers[] =
{
"Connection", "close",
"Content-type", "application/x-www-form-urlencoded",
"Accept", "text/plain",
0
};
const char* body = "answer=42&name=Bubba";
happyhttp::Connection conn( "www.scumways.com", 80 );
conn.setcallbacks( OnBegin, OnData, OnComplete, 0 );
conn.request( "POST",
"/happyhttp/test.php",
headers,
(const unsigned char*)body,
strlen(body) );
while( conn.outstanding() )
conn.pump();
}
void Test3()
{
puts("-----------------Test3------------------------" );
// POST example using lower-level interface
const char* params = "answer=42&foo=bar";
int l = strlen(params);
happyhttp::Connection conn( "www.scumways.com", 80 );
conn.setcallbacks( OnBegin, OnData, OnComplete, 0 );
conn.putrequest( "POST", "/happyhttp/test.php" );
conn.putheader( "Connection", "close" );
conn.putheader( "Content-Length", l );
conn.putheader( "Content-type", "application/x-www-form-urlencoded" );
conn.putheader( "Accept", "text/plain" );
conn.endheaders();
conn.send( (const unsigned char*)params, l );
while( conn.outstanding() )
conn.pump();
}
int main( int argc, char* argv[] )
{
#ifdef WIN32
WSAData wsaData;
int code = WSAStartup(MAKEWORD(1, 1), &wsaData);
if( code != 0 )
{
fprintf(stderr, "shite. %d\n",code);
return 0;
}
#endif //WIN32
try
{
Test1();
Test2();
Test3();
}
catch( happyhttp::Wobbly& e )
{
fprintf(stderr, "Exception:\n%s\n", e.what() );
}
#ifdef WIN32
WSACleanup();
#endif // WIN32
return 0;
}