diff options
Diffstat (limited to 'src/daemon/daemontest1.c')
-rw-r--r-- | src/daemon/daemontest1.c | 221 |
1 files changed, 221 insertions, 0 deletions
diff --git a/src/daemon/daemontest1.c b/src/daemon/daemontest1.c new file mode 100644 index 00000000..34313c78 --- /dev/null +++ b/src/daemon/daemontest1.c | |||
@@ -0,0 +1,221 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | (C) 2007 Christian Grothoff | ||
4 | |||
5 | libmicrohttpd is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | libmicrohttpd is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with libmicrohttpd; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file daemontest.c | ||
23 | * @brief Testcase for libmicrohttpd GET operations | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | |||
27 | #include "config.h" | ||
28 | #include "curl/curl.h" | ||
29 | #include "microhttpd.h" | ||
30 | #include <stdlib.h> | ||
31 | #include <unistd.h> | ||
32 | #include <string.h> | ||
33 | |||
34 | static int apc_all(void * cls, | ||
35 | const struct sockaddr * addr, | ||
36 | socklen_t addrlen) { | ||
37 | return MHD_YES; | ||
38 | } | ||
39 | |||
40 | struct CBC { | ||
41 | char * buf; | ||
42 | size_t pos; | ||
43 | size_t size; | ||
44 | }; | ||
45 | |||
46 | static size_t copyBuffer(void * ptr, | ||
47 | size_t size, | ||
48 | size_t nmemb, | ||
49 | void * ctx) { | ||
50 | struct CBC * cbc = ctx; | ||
51 | |||
52 | if (cbc->pos + size * nmemb > cbc->size) | ||
53 | return 0; /* overflow */ | ||
54 | memcpy(&cbc->buf[cbc->pos], | ||
55 | ptr, | ||
56 | size * nmemb); | ||
57 | cbc->pos += size * nmemb; | ||
58 | return size * nmemb; | ||
59 | } | ||
60 | |||
61 | static int ahc_echo(void * cls, | ||
62 | struct MHD_Session * session, | ||
63 | const char * url, | ||
64 | const char * method, | ||
65 | const char * upload_data, | ||
66 | unsigned int * upload_data_size) { | ||
67 | const char * me = cls; | ||
68 | struct MHD_Response * response; | ||
69 | int ret; | ||
70 | |||
71 | if (0 != strcmp(me, method)) | ||
72 | return MHD_NO; /* unexpected method */ | ||
73 | response = MHD_create_response_from_data(strlen(url), | ||
74 | (void*) url, | ||
75 | MHD_NO, | ||
76 | MHD_YES); | ||
77 | ret = MHD_queue_response(session, | ||
78 | MHD_HTTP_OK, | ||
79 | response); | ||
80 | MHD_destroy_response(response); | ||
81 | return ret; | ||
82 | } | ||
83 | |||
84 | |||
85 | static int testInternalGet() { | ||
86 | struct MHD_Daemon * d; | ||
87 | CURL * c; | ||
88 | char buf[2048]; | ||
89 | struct CBC cbc; | ||
90 | |||
91 | cbc.buf = buf; | ||
92 | cbc.size = 2048; | ||
93 | cbc.pos = 0; | ||
94 | d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_IPv4 | MHD_USE_DEBUG, | ||
95 | 1083, | ||
96 | &apc_all, | ||
97 | NULL, | ||
98 | &ahc_echo, | ||
99 | "GET"); | ||
100 | if (d == NULL) | ||
101 | return 1; | ||
102 | |||
103 | if (MHD_run(d) == MHD_NO) { | ||
104 | MHD_stop_daemon(d); | ||
105 | return 2; | ||
106 | } | ||
107 | |||
108 | c = curl_easy_init(); | ||
109 | curl_easy_setopt(c, | ||
110 | CURLOPT_URL, | ||
111 | "http://localhost:1083/hello_world"); | ||
112 | curl_easy_setopt(c, | ||
113 | CURLOPT_WRITEFUNCTION, | ||
114 | ©Buffer); | ||
115 | curl_easy_setopt(c, | ||
116 | CURLOPT_WRITEDATA, | ||
117 | &cbc); | ||
118 | curl_easy_setopt(c, | ||
119 | CURLOPT_FAILONERROR, | ||
120 | 1); | ||
121 | curl_easy_setopt(c, | ||
122 | CURLOPT_TIMEOUT, | ||
123 | 15L); | ||
124 | curl_easy_setopt(c, | ||
125 | CURLOPT_CONNECTTIMEOUT, | ||
126 | 15L); | ||
127 | // NOTE: use of CONNECTTIMEOUT without also | ||
128 | // setting NOSIGNAL results in really weird | ||
129 | // crashes on my system! | ||
130 | curl_easy_setopt(c, | ||
131 | CURLOPT_NOSIGNAL, | ||
132 | 1); | ||
133 | if (CURLE_OK != curl_easy_perform(c)) | ||
134 | return 3; | ||
135 | |||
136 | curl_easy_cleanup(c); | ||
137 | |||
138 | if (cbc.pos != strlen("hello_world")) | ||
139 | return 4; | ||
140 | |||
141 | if (0 != strncmp("hello_world", | ||
142 | cbc.buf, | ||
143 | strlen("hello_world"))) | ||
144 | return 5; | ||
145 | |||
146 | MHD_stop_daemon(d); | ||
147 | |||
148 | return 0; | ||
149 | } | ||
150 | |||
151 | static int testMultithreadedGet() { | ||
152 | struct MHD_Daemon * d; | ||
153 | CURL * c; | ||
154 | char buf[2048]; | ||
155 | struct CBC cbc; | ||
156 | |||
157 | cbc.buf = buf; | ||
158 | cbc.size = 2048; | ||
159 | cbc.pos = 0; | ||
160 | d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_IPv4 | MHD_USE_DEBUG, | ||
161 | 1084, | ||
162 | &apc_all, | ||
163 | NULL, | ||
164 | &ahc_echo, | ||
165 | "GET"); | ||
166 | if (d == NULL) | ||
167 | return 1; | ||
168 | |||
169 | if(MHD_run(d) == MHD_NO) | ||
170 | return 2; | ||
171 | |||
172 | |||
173 | c = curl_easy_init(); | ||
174 | curl_easy_setopt(c, | ||
175 | CURLOPT_URL, | ||
176 | "http://localhost:1084/hello_world"); | ||
177 | curl_easy_setopt(c, | ||
178 | CURLOPT_WRITEFUNCTION, | ||
179 | ©Buffer); | ||
180 | curl_easy_setopt(c, | ||
181 | CURLOPT_WRITEDATA, | ||
182 | &cbc); | ||
183 | curl_easy_setopt(c, | ||
184 | CURLOPT_FAILONERROR, | ||
185 | 1); | ||
186 | curl_easy_setopt(c, | ||
187 | CURLOPT_CONNECTTIMEOUT, | ||
188 | 15L); | ||
189 | // NOTE: use of CONNECTTIMEOUT without also | ||
190 | // setting NOSIGNAL results in really weird | ||
191 | // crashes on my system! | ||
192 | curl_easy_setopt(c, | ||
193 | CURLOPT_NOSIGNAL, | ||
194 | 1); | ||
195 | if (CURLE_OK != curl_easy_perform(c)) | ||
196 | return 3; | ||
197 | curl_easy_cleanup(c); | ||
198 | if (cbc.pos != strlen("hello_world")) | ||
199 | return 4; | ||
200 | |||
201 | if (0 != strncmp("hello_world", | ||
202 | cbc.buf, | ||
203 | strlen("hello_world"))) | ||
204 | return 5; | ||
205 | |||
206 | MHD_stop_daemon(d); | ||
207 | |||
208 | return 0; | ||
209 | } | ||
210 | |||
211 | int main(int argc, | ||
212 | char * const * argv) { | ||
213 | unsigned int errorCount = 0; | ||
214 | errorCount += testInternalGet(); | ||
215 | errorCount += testMultithreadedGet(); | ||
216 | if (errorCount != 0) | ||
217 | fprintf(stderr, | ||
218 | "Error (code: %u)\n", | ||
219 | errorCount); | ||
220 | return errorCount != 0; /* 0 == pass */ | ||
221 | } | ||