PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
test.cpp
Go to the documentation of this file.
1// to compile, try:
2// g++ test.cpp happyhttp.cpp -o testhttp
3
4#include "happyhttp.h"
5#include <cstdio>
6#include <cstring>
7
8#ifdef WIN32
9#include <winsock2.h>
10#endif // WIN32
11
12int count=0;
13
14void OnBegin( const happyhttp::Response* r, void* userdata )
15{
16 printf( "BEGIN (%d %s)\n", r->getstatus(), r->getreason() );
17 count = 0;
18}
19
20void OnData( const happyhttp::Response* r, void* userdata, const unsigned char* data, int n )
21{
22 fwrite( data,1,n, stdout );
23 count += n;
24}
25
26void OnComplete( const happyhttp::Response* r, void* userdata )
27{
28 printf( "COMPLETE (%d bytes)\n", count );
29}
30
31
32
33void Test1()
34{
35 puts("-----------------Test1------------------------" );
36 // simple simple GET
37 happyhttp::Connection conn( "scumways.com", 80 );
39
40 conn.request( "GET", "/happyhttp/test.php", 0, 0,0 );
41
42 while( conn.outstanding() )
43 conn.pump();
44}
45
46
47
48void Test2()
49{
50 puts("-----------------Test2------------------------" );
51 // POST using high-level request interface
52
53 const char* headers[] =
54 {
55 "Connection", "close",
56 "Content-type", "application/x-www-form-urlencoded",
57 "Accept", "text/plain",
58 0
59 };
60
61 const char* body = "answer=42&name=Bubba";
62
63 happyhttp::Connection conn( "scumways.com", 80 );
65 conn.request( "POST",
66 "/happyhttp/test.php",
67 headers,
68 (const unsigned char*)body,
69 strlen(body) );
70
71 while( conn.outstanding() )
72 conn.pump();
73}
74
75void Test3()
76{
77 puts("-----------------Test3------------------------" );
78 // POST example using lower-level interface
79
80 const char* params = "answer=42&foo=bar";
81 int l = strlen(params);
82
83 happyhttp::Connection conn( "scumways.com", 80 );
85
86 conn.putrequest( "POST", "/happyhttp/test.php" );
87 conn.putheader( "Connection", "close" );
88 conn.putheader( "Content-Length", l );
89 conn.putheader( "Content-type", "application/x-www-form-urlencoded" );
90 conn.putheader( "Accept", "text/plain" );
91 conn.endheaders();
92 conn.send( (const unsigned char*)params, l );
93
94 while( conn.outstanding() )
95 conn.pump();
96}
97
98//This posts to local droopy server running on 8000; this fails.
99void Test4()
100{
101 puts("-----------------Test4------------------------" );
102 // POST example using lower-level interface
103
104 const char* params = "--||||\nContent-Disposition: form-data; name=\"upfile\"; filename=\"testfile.txt\" \nContent-Type: application/octet-stream \n\nfile contents1\nfile contents2\nfile contents3\nfile contents4\nfile contents5\nfile contents6\nfile contents7\nfile contents8\nfile contents9\nfile contents10\n--||||--";
105 int l = strlen(params);
106
107 happyhttp::Connection conn( "localhost", 8000 );
109
110 conn.putrequest( "POST", "/" );
111 conn.putheader( "Connection", "close" );
112 conn.putheader( "Content-Length", l );
113 conn.putheader( "Content-Type", "multipart/form-data; boundary=||||" );
114 conn.putheader( "Accept", "text/plain" );
115 conn.endheaders();
116 conn.send( (const unsigned char*)params, l );
117
118 while( conn.outstanding() )
119 {
120
121 conn.pump();
122 }
123}
124
125
126
127//This posts to posttestserver.com; this succeeds
128void Test5()
129{
130 puts("-----------------Test5------------------------" );
131 // POST example using lower-level interface
132
133 const char* params = "--||||\nContent-Disposition: form-data; name=\"upfile\"; filename=\"testfile.txt\" \nContent-Type: application/octet-stream \n\nfile contents1\nfile contents2\nfile contents3\nfile contents4\nfile contents5\nfile contents6\nfile contents7\nfile contents8\nfile contents9\nfile contents10\n--||||--";
134 int l = strlen(params);
135
136 happyhttp::Connection conn( "posttestserver.com", 80 );
138
139 conn.putrequest( "POST", "/post.php" );
140 conn.putheader( "Connection", "close" );
141 conn.putheader( "Content-Length", l );
142 conn.putheader( "Content-Type", "multipart/form-data; boundary=||||" );
143 conn.putheader( "Accept", "text/plain" );
144 conn.endheaders();
145 conn.send( (const unsigned char*)params, l );
146
147 while( conn.outstanding() )
148 {
149
150 conn.pump();
151 }
152}
153
154
155
156
157int main( int argc, char* argv[] )
158{
159#ifdef WIN32
160 WSAData wsaData;
161 int code = WSAStartup(MAKEWORD(1, 1), &wsaData);
162 if( code != 0 )
163 {
164 fprintf(stderr, "shite. %d\n",code);
165 return 0;
166 }
167#endif //WIN32
168 try
169 {
170 //Test1();
171 //Test2();
172 //Test3();
173 //Test4();
174 Test5();
175 }
176
177 catch( happyhttp::Wobbly& e )
178 {
179 fprintf(stderr, "Exception:\n%s\n", e.what() );
180 }
181
182#ifdef WIN32
183 WSACleanup();
184#endif // WIN32
185
186 return 0;
187}
188
189
190
void request(const char *method, const char *url, const char *headers[]=0, const unsigned char *body=0, int bodysize=0)
void putheader(const char *header, const char *value)
void setcallbacks(ResponseBegin_CB begincb, ResponseData_CB datacb, ResponseComplete_CB completecb, void *userdata)
void send(const unsigned char *buf, int numbytes)
bool outstanding() const
Definition happyhttp.h:179
void putrequest(const char *method, const char *url)
int getstatus() const
const char * getreason() const
const char * what() const
Definition happyhttp.h:129
void OnData(const happyhttp::Response *r, void *userdata, const unsigned char *data, int n)
Definition test.cpp:20
void Test4()
Definition test.cpp:99
int main(int argc, char *argv[])
Definition test.cpp:157
void Test3()
Definition test.cpp:75
void Test2()
Definition test.cpp:48
int count
Definition test.cpp:12
void OnBegin(const happyhttp::Response *r, void *userdata)
Definition test.cpp:14
void Test5()
Definition test.cpp:128
void Test1()
Definition test.cpp:33
void OnComplete(const happyhttp::Response *r, void *userdata)
Definition test.cpp:26