PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
test.cpp File Reference
#include "happyhttp.h"
#include <cstdio>
#include <cstring>

Go to the source code of this file.

Functions

void OnBegin (const happyhttp::Response *r, void *userdata)
 
void OnData (const happyhttp::Response *r, void *userdata, const unsigned char *data, int n)
 
void OnComplete (const happyhttp::Response *r, void *userdata)
 
void Test1 ()
 
void Test2 ()
 
void Test3 ()
 
void Test4 ()
 
void Test5 ()
 
int main (int argc, char *argv[])
 

Variables

int count =0
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 157 of file test.cpp.

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}
const char * what() const
Definition happyhttp.h:129
void Test5()
Definition test.cpp:128

References Test5(), and happyhttp::Wobbly::what().

◆ OnBegin()

void OnBegin ( const happyhttp::Response r,
void *  userdata 
)

Definition at line 14 of file test.cpp.

15{
16 printf( "BEGIN (%d %s)\n", r->getstatus(), r->getreason() );
17 count = 0;
18}
int getstatus() const
const char * getreason() const
int count
Definition test.cpp:12

References count, happyhttp::Response::getreason(), and happyhttp::Response::getstatus().

Referenced by Test1(), Test2(), Test3(), Test4(), and Test5().

◆ OnComplete()

void OnComplete ( const happyhttp::Response r,
void *  userdata 
)

Definition at line 26 of file test.cpp.

27{
28 printf( "COMPLETE (%d bytes)\n", count );
29}

References count.

Referenced by Test1(), Test2(), Test3(), Test4(), and Test5().

◆ OnData()

void OnData ( const happyhttp::Response r,
void *  userdata,
const unsigned char *  data,
int  n 
)

Definition at line 20 of file test.cpp.

21{
22 fwrite( data,1,n, stdout );
23 count += n;
24}

References count.

Referenced by Test1(), Test2(), Test3(), Test4(), and Test5().

◆ Test1()

void Test1 ( )

Definition at line 33 of file test.cpp.

34{
35 puts("-----------------Test1------------------------" );
36 // simple simple GET
37 happyhttp::Connection conn( "scumways.com", 80 );
38 conn.setcallbacks( OnBegin, OnData, OnComplete, 0 );
39
40 conn.request( "GET", "/happyhttp/test.php", 0, 0,0 );
41
42 while( conn.outstanding() )
43 conn.pump();
44}
void OnData(const happyhttp::Response *r, void *userdata, const unsigned char *data, int n)
Definition test.cpp:20
void OnBegin(const happyhttp::Response *r, void *userdata)
Definition test.cpp:14
void OnComplete(const happyhttp::Response *r, void *userdata)
Definition test.cpp:26

References OnBegin(), OnComplete(), OnData(), happyhttp::Connection::outstanding(), happyhttp::Connection::pump(), happyhttp::Connection::request(), and happyhttp::Connection::setcallbacks().

◆ Test2()

void Test2 ( )

Definition at line 48 of file test.cpp.

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 );
64 conn.setcallbacks( OnBegin, OnData, OnComplete, 0 );
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}

References OnBegin(), OnComplete(), OnData(), happyhttp::Connection::outstanding(), happyhttp::Connection::pump(), happyhttp::Connection::request(), and happyhttp::Connection::setcallbacks().

◆ Test3()

void Test3 ( )

Definition at line 75 of file test.cpp.

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 );
84 conn.setcallbacks( OnBegin, OnData, OnComplete, 0 );
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}

References happyhttp::Connection::endheaders(), OnBegin(), OnComplete(), OnData(), happyhttp::Connection::outstanding(), happyhttp::Connection::pump(), happyhttp::Connection::putheader(), happyhttp::Connection::putrequest(), happyhttp::Connection::send(), and happyhttp::Connection::setcallbacks().

◆ Test4()

void Test4 ( )

Definition at line 99 of file test.cpp.

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 );
108 conn.setcallbacks( OnBegin, OnData, OnComplete, 0 );
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}

References happyhttp::Connection::endheaders(), OnBegin(), OnComplete(), OnData(), happyhttp::Connection::outstanding(), happyhttp::Connection::pump(), happyhttp::Connection::putheader(), happyhttp::Connection::putrequest(), happyhttp::Connection::send(), and happyhttp::Connection::setcallbacks().

◆ Test5()

void Test5 ( )

Definition at line 128 of file test.cpp.

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 );
137 conn.setcallbacks( OnBegin, OnData, OnComplete, 0 );
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}

References happyhttp::Connection::endheaders(), OnBegin(), OnComplete(), OnData(), happyhttp::Connection::outstanding(), happyhttp::Connection::pump(), happyhttp::Connection::putheader(), happyhttp::Connection::putrequest(), happyhttp::Connection::send(), and happyhttp::Connection::setcallbacks().

Referenced by main().

Variable Documentation

◆ count